OLD | NEW |
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" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 ~CSSStyleSheetResourceTest() override { | 53 ~CSSStyleSheetResourceTest() override { |
54 replaceMemoryCacheForTesting(m_originalMemoryCache.release()); | 54 replaceMemoryCacheForTesting(m_originalMemoryCache.release()); |
55 } | 55 } |
56 | 56 |
57 Document& document() { return m_page->document(); } | 57 Document& document() { return m_page->document(); } |
58 | 58 |
59 Persistent<MemoryCache> m_originalMemoryCache; | 59 Persistent<MemoryCache> m_originalMemoryCache; |
60 std::unique_ptr<DummyPageHolder> m_page; | 60 std::unique_ptr<DummyPageHolder> m_page; |
61 }; | 61 }; |
62 | 62 |
63 TEST_F(CSSStyleSheetResourceTest, PruneCanCauseEviction) { | |
64 { | |
65 // We need this scope to remove all references. | |
66 KURL imageURL(KURL(), "https://localhost/image"); | |
67 KURL cssURL(KURL(), "https://localhost/style.css"); | |
68 | |
69 // We need to disable loading because we manually give a response to | |
70 // the image resource. | |
71 document().fetcher()->setAutoLoadImages(false); | |
72 | |
73 CSSStyleSheetResource* cssResource = | |
74 CSSStyleSheetResource::createForTest(ResourceRequest(cssURL), "utf-8"); | |
75 memoryCache()->add(cssResource); | |
76 cssResource->responseReceived( | |
77 ResourceResponse(cssURL, "style/css", 0, nullAtom, String()), nullptr); | |
78 cssResource->finish(); | |
79 | |
80 StyleSheetContents* contents = | |
81 StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)); | |
82 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); | |
83 EXPECT_TRUE(sheet); | |
84 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create( | |
85 CSSImageValue::create(String("image"), imageURL), | |
86 CSSImageValue::create(String("image"), imageURL), | |
87 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number)); | |
88 Vector<std::unique_ptr<CSSParserSelector>> selectors; | |
89 selectors.append(makeUnique<CSSParserSelector>()); | |
90 selectors[0]->setMatch(CSSSelector::Id); | |
91 selectors[0]->setValue("foo"); | |
92 CSSProperty property(CSSPropertyBackground, *crossfade); | |
93 contents->parserAppendRule(StyleRule::create( | |
94 CSSSelectorList::adoptSelectorVector(selectors), | |
95 ImmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); | |
96 | |
97 crossfade->loadSubimages(document()); | |
98 Resource* imageResource = memoryCache()->resourceForURL( | |
99 imageURL, MemoryCache::defaultCacheIdentifier()); | |
100 ASSERT_TRUE(imageResource); | |
101 ResourceResponse imageResponse; | |
102 imageResponse.setURL(imageURL); | |
103 imageResponse.setHTTPHeaderField("cache-control", "no-store"); | |
104 imageResource->responseReceived(imageResponse, nullptr); | |
105 | |
106 contents->checkLoaded(); | |
107 cssResource->saveParsedStyleSheet(contents); | |
108 | |
109 memoryCache()->update(cssResource, cssResource->size(), cssResource->size(), | |
110 false); | |
111 memoryCache()->update(imageResource, imageResource->size(), | |
112 imageResource->size(), false); | |
113 if (!memoryCache()->isInSameLRUListForTest(cssResource, imageResource)) { | |
114 // We assume that the LRU list is determined by |size / accessCount|. | |
115 for (size_t i = 0; i < cssResource->size() + 1; ++i) | |
116 memoryCache()->update(cssResource, cssResource->size(), | |
117 cssResource->size(), true); | |
118 for (size_t i = 0; i < imageResource->size() + 1; ++i) | |
119 memoryCache()->update(imageResource, imageResource->size(), | |
120 imageResource->size(), true); | |
121 } | |
122 ASSERT_TRUE( | |
123 memoryCache()->isInSameLRUListForTest(cssResource, imageResource)); | |
124 } | |
125 ThreadState::current()->collectAllGarbage(); | |
126 // This operation should not lead to crash! | |
127 memoryCache()->pruneAll(); | |
128 } | |
129 | |
130 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached) { | 63 TEST_F(CSSStyleSheetResourceTest, DuplicateResourceNotCached) { |
131 const char url[] = "https://localhost/style.css"; | 64 const char url[] = "https://localhost/style.css"; |
132 KURL imageURL(KURL(), url); | 65 KURL imageURL(KURL(), url); |
133 KURL cssURL(KURL(), url); | 66 KURL cssURL(KURL(), url); |
134 | 67 |
135 // Emulate using <img> to do async stylesheet preloads. | 68 // Emulate using <img> to do async stylesheet preloads. |
136 | 69 |
137 Resource* imageResource = ImageResource::create(ResourceRequest(imageURL)); | 70 Resource* imageResource = ImageResource::create(ResourceRequest(imageURL)); |
138 ASSERT_TRUE(imageResource); | 71 ASSERT_TRUE(imageResource); |
139 memoryCache()->add(imageResource); | 72 memoryCache()->add(imageResource); |
(...skipping 17 matching lines...) Expand all Loading... |
157 // The underlying |contents| for the stylesheet resource must have a | 90 // The underlying |contents| for the stylesheet resource must have a |
158 // matching reference status. | 91 // matching reference status. |
159 EXPECT_TRUE(memoryCache()->contains(imageResource)); | 92 EXPECT_TRUE(memoryCache()->contains(imageResource)); |
160 EXPECT_FALSE(memoryCache()->contains(cssResource)); | 93 EXPECT_FALSE(memoryCache()->contains(cssResource)); |
161 EXPECT_FALSE(contents->isReferencedFromResource()); | 94 EXPECT_FALSE(contents->isReferencedFromResource()); |
162 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext)); | 95 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext)); |
163 } | 96 } |
164 | 97 |
165 } // namespace | 98 } // namespace |
166 } // namespace blink | 99 } // namespace blink |
OLD | NEW |