OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/fetch/CSSStyleSheetResource.h" |
| 6 |
| 7 #include "core/css/CSSCrossfadeValue.h" |
| 8 #include "core/css/CSSImageValue.h" |
| 9 #include "core/css/CSSPrimitiveValue.h" |
| 10 #include "core/css/CSSProperty.h" |
| 11 #include "core/css/CSSSelectorList.h" |
| 12 #include "core/css/CSSStyleSheet.h" |
| 13 #include "core/css/StylePropertySet.h" |
| 14 #include "core/css/StyleRule.h" |
| 15 #include "core/css/StyleSheetContents.h" |
| 16 #include "core/css/parser/CSSParserMode.h" |
| 17 #include "core/css/parser/CSSParserSelector.h" |
| 18 #include "core/dom/Document.h" |
| 19 #include "core/fetch/FetchContext.h" |
| 20 #include "core/fetch/FetchInitiatorTypeNames.h" |
| 21 #include "core/fetch/FetchRequest.h" |
| 22 #include "core/fetch/MemoryCache.h" |
| 23 #include "core/fetch/ResourceFetcher.h" |
| 24 #include "core/testing/DummyPageHolder.h" |
| 25 #include "platform/heap/Handle.h" |
| 26 #include "platform/network/ResourceRequest.h" |
| 27 #include "platform/testing/URLTestHelpers.h" |
| 28 #include "platform/testing/UnitTestHelpers.h" |
| 29 #include "platform/weborigin/KURL.h" |
| 30 #include "public/platform/Platform.h" |
| 31 #include "public/platform/WebURLResponse.h" |
| 32 #include "public/platform/WebUnitTestSupport.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "wtf/RefPtr.h" |
| 35 #include "wtf/text/WTFString.h" |
| 36 |
| 37 namespace blink { |
| 38 |
| 39 class Document; |
| 40 |
| 41 namespace { |
| 42 |
| 43 class CSSStyleSheetResourceTest : public ::testing::Test { |
| 44 protected: |
| 45 CSSStyleSheetResourceTest() |
| 46 { |
| 47 m_originalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create
()); |
| 48 m_page = DummyPageHolder::create(); |
| 49 document()->setURL(KURL(KURL(), "https://localhost/")); |
| 50 } |
| 51 |
| 52 ~CSSStyleSheetResourceTest() override |
| 53 { |
| 54 replaceMemoryCacheForTesting(m_originalMemoryCache.release()); |
| 55 } |
| 56 |
| 57 Document* document() { return &m_page->document(); } |
| 58 |
| 59 Persistent<MemoryCache> m_originalMemoryCache; |
| 60 OwnPtr<DummyPageHolder> m_page; |
| 61 }; |
| 62 |
| 63 TEST_F(CSSStyleSheetResourceTest, PruneCanCauseEviction) |
| 64 { |
| 65 { |
| 66 // We need this scope to remove all references. |
| 67 KURL imageURL(KURL(), "https://localhost/image"); |
| 68 KURL cssURL(KURL(), "https://localhost/style.css"); |
| 69 |
| 70 // We need to disable loading because we manually give a response to |
| 71 // the image resource. |
| 72 document()->fetcher()->setAutoLoadImages(false); |
| 73 |
| 74 ResourcePtr<CSSStyleSheetResource> cssResource = CSSStyleSheetResource::
createForTest(ResourceRequest(cssURL), "utf-8"); |
| 75 memoryCache()->add(cssResource.get()); |
| 76 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n
ullAtom, String()), nullptr); |
| 77 cssResource->finish(); |
| 78 |
| 79 RefPtrWillBeRawPtr<StyleSheetContents> contents = StyleSheetContents::cr
eate(CSSParserContext(HTMLStandardMode, nullptr)); |
| 80 RefPtrWillBeRawPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(contents
, document()); |
| 81 EXPECT_TRUE(sheet); |
| 82 RefPtrWillBeRawPtr<CSSCrossfadeValue> crossfade = CSSCrossfadeValue::cre
ate( |
| 83 CSSImageValue::create(String("image"), imageURL), |
| 84 CSSImageValue::create(String("image"), imageURL), |
| 85 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number))
; |
| 86 Vector<OwnPtr<CSSParserSelector>> selectors; |
| 87 selectors.append(adoptPtr(new CSSParserSelector())); |
| 88 selectors[0]->setMatch(CSSSelector::Id); |
| 89 selectors[0]->setValue("foo"); |
| 90 CSSProperty property(CSSPropertyBackground, crossfade); |
| 91 contents->parserAppendRule( |
| 92 StyleRule::create(CSSSelectorList::adoptSelectorVector(selectors), I
mmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); |
| 93 |
| 94 crossfade->loadSubimages(document()); |
| 95 ResourcePtr<Resource> imageResource = memoryCache()->resourceForURL(imag
eURL, MemoryCache::defaultCacheIdentifier()); |
| 96 ASSERT_TRUE(imageResource); |
| 97 ResourceResponse imageResponse; |
| 98 imageResponse.setURL(imageURL); |
| 99 imageResponse.setHTTPHeaderField("cache-control", "no-store"); |
| 100 imageResource->responseReceived(imageResponse, nullptr); |
| 101 |
| 102 contents->checkLoaded(); |
| 103 cssResource->saveParsedStyleSheet(contents); |
| 104 |
| 105 memoryCache()->update(cssResource.get(), cssResource->size(), cssResourc
e->size(), false); |
| 106 memoryCache()->update(imageResource.get(), imageResource->size(), imageR
esource->size(), false); |
| 107 if (!memoryCache()->isInSameLRUListForTest(cssResource.get(), imageResou
rce.get())) { |
| 108 // We assume that the LRU list is determined by |size / accessCount|
. |
| 109 for (size_t i = 0; i < cssResource->size() + 1; ++i) |
| 110 memoryCache()->update(cssResource.get(), cssResource->size(), cs
sResource->size(), true); |
| 111 for (size_t i = 0; i < imageResource->size() + 1; ++i) |
| 112 memoryCache()->update(imageResource.get(), imageResource->size()
, imageResource->size(), true); |
| 113 } |
| 114 ASSERT_TRUE(memoryCache()->isInSameLRUListForTest(cssResource.get(), ima
geResource.get())); |
| 115 } |
| 116 Heap::collectAllGarbage(); |
| 117 // This operation should not lead to crash! |
| 118 memoryCache()->pruneAll(); |
| 119 } |
| 120 |
| 121 } // namespace |
| 122 } // namespace blink |
OLD | NEW |