gfx/tests/gtest/gfxWordCacheTest.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "gtest/gtest.h"
michael@0 7
michael@0 8 #include "nsCOMPtr.h"
michael@0 9 #include "nsTArray.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsDependentString.h"
michael@0 12
michael@0 13 #include "prinrval.h"
michael@0 14
michael@0 15 #include "gfxContext.h"
michael@0 16 #include "gfxFont.h"
michael@0 17 #include "gfxPlatform.h"
michael@0 18
michael@0 19 #include "gfxFontTest.h"
michael@0 20 #include "mozilla/Attributes.h"
michael@0 21
michael@0 22 class FrameTextRunCache;
michael@0 23
michael@0 24 static FrameTextRunCache *gTextRuns = nullptr;
michael@0 25
michael@0 26 /*
michael@0 27 * Cache textruns and expire them after 3*10 seconds of no use.
michael@0 28 */
michael@0 29 class FrameTextRunCache MOZ_FINAL : public nsExpirationTracker<gfxTextRun,3> {
michael@0 30 public:
michael@0 31 enum { TIMEOUT_SECONDS = 10 };
michael@0 32 FrameTextRunCache()
michael@0 33 : nsExpirationTracker<gfxTextRun,3>(TIMEOUT_SECONDS*1000) {}
michael@0 34 ~FrameTextRunCache() {
michael@0 35 AgeAllGenerations();
michael@0 36 }
michael@0 37
michael@0 38 void RemoveFromCache(gfxTextRun* aTextRun) {
michael@0 39 if (aTextRun->GetExpirationState()->IsTracked()) {
michael@0 40 RemoveObject(aTextRun);
michael@0 41 }
michael@0 42 }
michael@0 43
michael@0 44 // This gets called when the timeout has expired on a gfxTextRun
michael@0 45 virtual void NotifyExpired(gfxTextRun* aTextRun) {
michael@0 46 RemoveFromCache(aTextRun);
michael@0 47 delete aTextRun;
michael@0 48 }
michael@0 49 };
michael@0 50
michael@0 51 static gfxTextRun *
michael@0 52 MakeTextRun(const char16_t *aText, uint32_t aLength,
michael@0 53 gfxFontGroup *aFontGroup, const gfxFontGroup::Parameters* aParams,
michael@0 54 uint32_t aFlags)
michael@0 55 {
michael@0 56 nsAutoPtr<gfxTextRun> textRun;
michael@0 57 if (aLength == 0) {
michael@0 58 abort();
michael@0 59 //textRun = aFontGroup->MakeEmptyTextRun(aParams, aFlags);
michael@0 60 } else if (aLength == 1 && aText[0] == ' ') {
michael@0 61 abort();
michael@0 62 //textRun = aFontGroup->MakeSpaceTextRun(aParams, aFlags);
michael@0 63 } else {
michael@0 64 textRun = aFontGroup->MakeTextRun(aText, aLength, aParams, aFlags);
michael@0 65 }
michael@0 66 if (!textRun)
michael@0 67 return nullptr;
michael@0 68 nsresult rv = gTextRuns->AddObject(textRun);
michael@0 69 if (NS_FAILED(rv)) {
michael@0 70 gTextRuns->RemoveFromCache(textRun);
michael@0 71 return nullptr;
michael@0 72 }
michael@0 73 return textRun.forget();
michael@0 74 }
michael@0 75
michael@0 76 static already_AddRefed<gfxContext>
michael@0 77 MakeContext ()
michael@0 78 {
michael@0 79 const int size = 200;
michael@0 80
michael@0 81 nsRefPtr<gfxASurface> surface;
michael@0 82
michael@0 83 surface = gfxPlatform::GetPlatform()->
michael@0 84 CreateOffscreenSurface(IntSize(size, size),
michael@0 85 gfxASurface::ContentFromFormat(gfxImageFormat::RGB24));
michael@0 86 nsRefPtr<gfxContext> ctx = new gfxContext(surface);
michael@0 87 return ctx.forget();
michael@0 88 }
michael@0 89
michael@0 90 TEST(Gfx, WordCache) {
michael@0 91 gTextRuns = new FrameTextRunCache();
michael@0 92
michael@0 93 nsRefPtr<gfxContext> ctx = MakeContext();
michael@0 94 {
michael@0 95 gfxFontStyle style (mozilla::gfx::FontStyle::NORMAL,
michael@0 96 139,
michael@0 97 10.0,
michael@0 98 0,
michael@0 99 NS_NewPermanentAtom(NS_LITERAL_STRING("en")),
michael@0 100 0.0,
michael@0 101 false, false,
michael@0 102 NS_LITERAL_STRING(""));
michael@0 103
michael@0 104 nsRefPtr<gfxFontGroup> fontGroup =
michael@0 105 gfxPlatform::GetPlatform()->CreateFontGroup(NS_LITERAL_STRING("Geneva, MS Sans Serif, Helvetica,serif"), &style, nullptr);
michael@0 106
michael@0 107 gfxTextRunFactory::Parameters params = {
michael@0 108 ctx, nullptr, nullptr, nullptr, 0, 60
michael@0 109 };
michael@0 110
michael@0 111 uint32_t flags = gfxTextRunFactory::TEXT_IS_PERSISTENT;
michael@0 112
michael@0 113 // First load an Arabic word into the cache
michael@0 114 const char cString[] = "\xd8\xaa\xd9\x85";
michael@0 115 nsDependentCString cStr(cString);
michael@0 116 NS_ConvertUTF8toUTF16 str(cStr);
michael@0 117 gfxTextRun *tr = MakeTextRun(str.get(), str.Length(), fontGroup, &params, flags);
michael@0 118 tr->GetAdvanceWidth(0, str.Length(), nullptr);
michael@0 119
michael@0 120 // Now try to trigger an assertion with a word cache bug. The first
michael@0 121 // word is in the cache so it gets added to the new textrun directly.
michael@0 122 // The second word is not in the cache
michael@0 123 const char cString2[] = "\xd8\xaa\xd9\x85\n\xd8\xaa\xd8\x85 ";
michael@0 124 nsDependentCString cStr2(cString2);
michael@0 125 NS_ConvertUTF8toUTF16 str2(cStr2);
michael@0 126 gfxTextRun *tr2 = MakeTextRun(str2.get(), str2.Length(), fontGroup, &params, flags);
michael@0 127 tr2->GetAdvanceWidth(0, str2.Length(), nullptr);
michael@0 128 }
michael@0 129
michael@0 130 delete gTextRuns;
michael@0 131 gTextRuns = nullptr;
michael@0 132
michael@0 133 }

mercurial