Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(187)

Side by Side Diff: third_party/WebKit/Source/core/css/CSSStyleSheetResourceTest.cpp

Issue 2013013002: Add a StyleResourceClient in CSSStyleSheetResourceTest to keep Resource alive Base URL: https://chromium.googlesource.com/chromium/src.git@MemoryCache_test0
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/fetch/CSSStyleSheetResource.h" 5 #include "core/fetch/CSSStyleSheetResource.h"
6 6
7 #include "core/css/CSSCrossfadeValue.h" 7 #include "core/css/CSSCrossfadeValue.h"
8 #include "core/css/CSSImageValue.h" 8 #include "core/css/CSSImageValue.h"
9 #include "core/css/CSSPrimitiveValue.h" 9 #include "core/css/CSSPrimitiveValue.h"
10 #include "core/css/CSSProperty.h" 10 #include "core/css/CSSProperty.h"
11 #include "core/css/CSSSelectorList.h" 11 #include "core/css/CSSSelectorList.h"
12 #include "core/css/CSSStyleSheet.h" 12 #include "core/css/CSSStyleSheet.h"
13 #include "core/css/StylePropertySet.h" 13 #include "core/css/StylePropertySet.h"
14 #include "core/css/StyleRule.h" 14 #include "core/css/StyleRule.h"
15 #include "core/css/StyleSheetContents.h" 15 #include "core/css/StyleSheetContents.h"
16 #include "core/css/parser/CSSParserMode.h" 16 #include "core/css/parser/CSSParserMode.h"
17 #include "core/css/parser/CSSParserSelector.h" 17 #include "core/css/parser/CSSParserSelector.h"
18 #include "core/dom/Document.h" 18 #include "core/dom/Document.h"
19 #include "core/fetch/FetchContext.h" 19 #include "core/fetch/FetchContext.h"
20 #include "core/fetch/FetchInitiatorTypeNames.h" 20 #include "core/fetch/FetchInitiatorTypeNames.h"
21 #include "core/fetch/FetchRequest.h" 21 #include "core/fetch/FetchRequest.h"
22 #include "core/fetch/MemoryCache.h" 22 #include "core/fetch/MemoryCache.h"
23 #include "core/fetch/ResourceFetcher.h" 23 #include "core/fetch/ResourceFetcher.h"
24 #include "core/fetch/StyleSheetResourceClient.h"
24 #include "core/testing/DummyPageHolder.h" 25 #include "core/testing/DummyPageHolder.h"
25 #include "platform/heap/Handle.h" 26 #include "platform/heap/Handle.h"
26 #include "platform/heap/Heap.h" 27 #include "platform/heap/Heap.h"
27 #include "platform/network/ResourceRequest.h" 28 #include "platform/network/ResourceRequest.h"
28 #include "platform/testing/URLTestHelpers.h" 29 #include "platform/testing/URLTestHelpers.h"
29 #include "platform/testing/UnitTestHelpers.h" 30 #include "platform/testing/UnitTestHelpers.h"
30 #include "platform/weborigin/KURL.h" 31 #include "platform/weborigin/KURL.h"
31 #include "public/platform/Platform.h" 32 #include "public/platform/Platform.h"
32 #include "public/platform/WebURLResponse.h" 33 #include "public/platform/WebURLResponse.h"
33 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
34 #include "wtf/RefPtr.h" 35 #include "wtf/RefPtr.h"
35 #include "wtf/text/WTFString.h" 36 #include "wtf/text/WTFString.h"
36 37
37 namespace blink { 38 namespace blink {
38 39
39 class Document; 40 class Document;
40 41
41 namespace { 42 namespace {
42 43
44 class MockStyleSheetResourceClient : public StyleSheetResourceClient {
45 public:
46 String debugName() const override { return "MockStyleSheetResourceClient"; }
47 };
48
43 class CSSStyleSheetResourceTest : public ::testing::Test { 49 class CSSStyleSheetResourceTest : public ::testing::Test {
44 protected: 50 protected:
45 CSSStyleSheetResourceTest() 51 CSSStyleSheetResourceTest()
46 { 52 {
47 m_originalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create ()); 53 m_originalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create ());
48 m_page = DummyPageHolder::create(); 54 m_page = DummyPageHolder::create();
49 document()->setURL(KURL(KURL(), "https://localhost/")); 55 document()->setURL(KURL(KURL(), "https://localhost/"));
50 } 56 }
51 57
52 ~CSSStyleSheetResourceTest() override 58 ~CSSStyleSheetResourceTest() override
(...skipping 12 matching lines...) Expand all
65 { 71 {
66 // We need this scope to remove all references. 72 // We need this scope to remove all references.
67 KURL imageURL(KURL(), "https://localhost/image"); 73 KURL imageURL(KURL(), "https://localhost/image");
68 KURL cssURL(KURL(), "https://localhost/style.css"); 74 KURL cssURL(KURL(), "https://localhost/style.css");
69 75
70 // We need to disable loading because we manually give a response to 76 // We need to disable loading because we manually give a response to
71 // the image resource. 77 // the image resource.
72 document()->fetcher()->setAutoLoadImages(false); 78 document()->fetcher()->setAutoLoadImages(false);
73 79
74 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTes t(ResourceRequest(cssURL), "utf-8"); 80 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTes t(ResourceRequest(cssURL), "utf-8");
81 MockStyleSheetResourceClient client;
82 cssResource->addClient(&client);
75 memoryCache()->add(cssResource); 83 memoryCache()->add(cssResource);
76 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n ullAtom, String()), nullptr); 84 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n ullAtom, String()), nullptr);
77 cssResource->finish(); 85 cssResource->finish();
78 86
79 StyleSheetContents* contents = StyleSheetContents::create(CSSParserConte xt(HTMLStandardMode, nullptr)); 87 StyleSheetContents* contents = StyleSheetContents::create(CSSParserConte xt(HTMLStandardMode, nullptr));
80 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); 88 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document());
81 EXPECT_TRUE(sheet); 89 EXPECT_TRUE(sheet);
82 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create( 90 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create(
83 CSSImageValue::create(String("image"), imageURL), 91 CSSImageValue::create(String("image"), imageURL),
84 CSSImageValue::create(String("image"), imageURL), 92 CSSImageValue::create(String("image"), imageURL),
(...skipping 20 matching lines...) Expand all
105 memoryCache()->update(cssResource, cssResource->size(), cssResource->siz e(), false); 113 memoryCache()->update(cssResource, cssResource->size(), cssResource->siz e(), false);
106 memoryCache()->update(imageResource, imageResource->size(), imageResourc e->size(), false); 114 memoryCache()->update(imageResource, imageResource->size(), imageResourc e->size(), false);
107 if (!memoryCache()->isInSameLRUListForTest(cssResource, imageResource)) { 115 if (!memoryCache()->isInSameLRUListForTest(cssResource, imageResource)) {
108 // We assume that the LRU list is determined by |size / accessCount| . 116 // We assume that the LRU list is determined by |size / accessCount| .
109 for (size_t i = 0; i < cssResource->size() + 1; ++i) 117 for (size_t i = 0; i < cssResource->size() + 1; ++i)
110 memoryCache()->update(cssResource, cssResource->size(), cssResou rce->size(), true); 118 memoryCache()->update(cssResource, cssResource->size(), cssResou rce->size(), true);
111 for (size_t i = 0; i < imageResource->size() + 1; ++i) 119 for (size_t i = 0; i < imageResource->size() + 1; ++i)
112 memoryCache()->update(imageResource, imageResource->size(), imag eResource->size(), true); 120 memoryCache()->update(imageResource, imageResource->size(), imag eResource->size(), true);
113 } 121 }
114 ASSERT_TRUE(memoryCache()->isInSameLRUListForTest(cssResource, imageReso urce)); 122 ASSERT_TRUE(memoryCache()->isInSameLRUListForTest(cssResource, imageReso urce));
123 cssResource->removeClient(&client);
115 } 124 }
116 ThreadHeap::collectAllGarbage(); 125 ThreadHeap::collectAllGarbage();
117 // This operation should not lead to crash! 126 // This operation should not lead to crash!
118 memoryCache()->pruneAll(); 127 memoryCache()->pruneAll();
119 } 128 }
120 129
121 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached) 130 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached)
122 { 131 {
123 const char url[] = "https://localhost/style.css"; 132 const char url[] = "https://localhost/style.css";
124 KURL imageURL(KURL(), url); 133 KURL imageURL(KURL(), url);
(...skipping 22 matching lines...) Expand all
147 // The underlying |contents| for the stylesheet resource must have a 156 // The underlying |contents| for the stylesheet resource must have a
148 // matching reference status. 157 // matching reference status.
149 EXPECT_TRUE(memoryCache()->contains(imageResource)); 158 EXPECT_TRUE(memoryCache()->contains(imageResource));
150 EXPECT_FALSE(memoryCache()->contains(cssResource)); 159 EXPECT_FALSE(memoryCache()->contains(cssResource));
151 EXPECT_FALSE(contents->isReferencedFromResource()); 160 EXPECT_FALSE(contents->isReferencedFromResource());
152 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext)); 161 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext));
153 } 162 }
154 163
155 } // namespace 164 } // namespace
156 } // namespace blink 165 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698