| 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 13 matching lines...) Expand all Loading... |
| 24 #include "core/testing/DummyPageHolder.h" | 24 #include "core/testing/DummyPageHolder.h" |
| 25 #include "platform/heap/Handle.h" | 25 #include "platform/heap/Handle.h" |
| 26 #include "platform/heap/Heap.h" | 26 #include "platform/heap/Heap.h" |
| 27 #include "platform/network/ResourceRequest.h" | 27 #include "platform/network/ResourceRequest.h" |
| 28 #include "platform/testing/URLTestHelpers.h" | 28 #include "platform/testing/URLTestHelpers.h" |
| 29 #include "platform/testing/UnitTestHelpers.h" | 29 #include "platform/testing/UnitTestHelpers.h" |
| 30 #include "platform/weborigin/KURL.h" | 30 #include "platform/weborigin/KURL.h" |
| 31 #include "public/platform/Platform.h" | 31 #include "public/platform/Platform.h" |
| 32 #include "public/platform/WebURLResponse.h" | 32 #include "public/platform/WebURLResponse.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 #include "wtf/PtrUtil.h" |
| 34 #include "wtf/RefPtr.h" | 35 #include "wtf/RefPtr.h" |
| 35 #include "wtf/text/WTFString.h" | 36 #include "wtf/text/WTFString.h" |
| 37 #include <memory> |
| 36 | 38 |
| 37 namespace blink { | 39 namespace blink { |
| 38 | 40 |
| 39 class Document; | 41 class Document; |
| 40 | 42 |
| 41 namespace { | 43 namespace { |
| 42 | 44 |
| 43 class CSSStyleSheetResourceTest : public ::testing::Test { | 45 class CSSStyleSheetResourceTest : public ::testing::Test { |
| 44 protected: | 46 protected: |
| 45 CSSStyleSheetResourceTest() | 47 CSSStyleSheetResourceTest() |
| 46 { | 48 { |
| 47 m_originalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create
()); | 49 m_originalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create
()); |
| 48 m_page = DummyPageHolder::create(); | 50 m_page = DummyPageHolder::create(); |
| 49 document()->setURL(KURL(KURL(), "https://localhost/")); | 51 document()->setURL(KURL(KURL(), "https://localhost/")); |
| 50 } | 52 } |
| 51 | 53 |
| 52 ~CSSStyleSheetResourceTest() override | 54 ~CSSStyleSheetResourceTest() override |
| 53 { | 55 { |
| 54 replaceMemoryCacheForTesting(m_originalMemoryCache.release()); | 56 replaceMemoryCacheForTesting(m_originalMemoryCache.release()); |
| 55 } | 57 } |
| 56 | 58 |
| 57 Document* document() { return &m_page->document(); } | 59 Document* document() { return &m_page->document(); } |
| 58 | 60 |
| 59 Persistent<MemoryCache> m_originalMemoryCache; | 61 Persistent<MemoryCache> m_originalMemoryCache; |
| 60 OwnPtr<DummyPageHolder> m_page; | 62 std::unique_ptr<DummyPageHolder> m_page; |
| 61 }; | 63 }; |
| 62 | 64 |
| 63 TEST_F(CSSStyleSheetResourceTest, PruneCanCauseEviction) | 65 TEST_F(CSSStyleSheetResourceTest, PruneCanCauseEviction) |
| 64 { | 66 { |
| 65 { | 67 { |
| 66 // We need this scope to remove all references. | 68 // We need this scope to remove all references. |
| 67 KURL imageURL(KURL(), "https://localhost/image"); | 69 KURL imageURL(KURL(), "https://localhost/image"); |
| 68 KURL cssURL(KURL(), "https://localhost/style.css"); | 70 KURL cssURL(KURL(), "https://localhost/style.css"); |
| 69 | 71 |
| 70 // We need to disable loading because we manually give a response to | 72 // We need to disable loading because we manually give a response to |
| 71 // the image resource. | 73 // the image resource. |
| 72 document()->fetcher()->setAutoLoadImages(false); | 74 document()->fetcher()->setAutoLoadImages(false); |
| 73 | 75 |
| 74 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTes
t(ResourceRequest(cssURL), "utf-8"); | 76 CSSStyleSheetResource* cssResource = CSSStyleSheetResource::createForTes
t(ResourceRequest(cssURL), "utf-8"); |
| 75 memoryCache()->add(cssResource); | 77 memoryCache()->add(cssResource); |
| 76 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n
ullAtom, String()), nullptr); | 78 cssResource->responseReceived(ResourceResponse(cssURL, "style/css", 0, n
ullAtom, String()), nullptr); |
| 77 cssResource->finish(); | 79 cssResource->finish(); |
| 78 | 80 |
| 79 StyleSheetContents* contents = StyleSheetContents::create(CSSParserConte
xt(HTMLStandardMode, nullptr)); | 81 StyleSheetContents* contents = StyleSheetContents::create(CSSParserConte
xt(HTMLStandardMode, nullptr)); |
| 80 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); | 82 CSSStyleSheet* sheet = CSSStyleSheet::create(contents, document()); |
| 81 EXPECT_TRUE(sheet); | 83 EXPECT_TRUE(sheet); |
| 82 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create( | 84 CSSCrossfadeValue* crossfade = CSSCrossfadeValue::create( |
| 83 CSSImageValue::create(String("image"), imageURL), | 85 CSSImageValue::create(String("image"), imageURL), |
| 84 CSSImageValue::create(String("image"), imageURL), | 86 CSSImageValue::create(String("image"), imageURL), |
| 85 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number))
; | 87 CSSPrimitiveValue::create(1.0, CSSPrimitiveValue::UnitType::Number))
; |
| 86 Vector<OwnPtr<CSSParserSelector>> selectors; | 88 Vector<std::unique_ptr<CSSParserSelector>> selectors; |
| 87 selectors.append(adoptPtr(new CSSParserSelector())); | 89 selectors.append(wrapUnique(new CSSParserSelector())); |
| 88 selectors[0]->setMatch(CSSSelector::Id); | 90 selectors[0]->setMatch(CSSSelector::Id); |
| 89 selectors[0]->setValue("foo"); | 91 selectors[0]->setValue("foo"); |
| 90 CSSProperty property(CSSPropertyBackground, *crossfade); | 92 CSSProperty property(CSSPropertyBackground, *crossfade); |
| 91 contents->parserAppendRule( | 93 contents->parserAppendRule( |
| 92 StyleRule::create(CSSSelectorList::adoptSelectorVector(selectors), I
mmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); | 94 StyleRule::create(CSSSelectorList::adoptSelectorVector(selectors), I
mmutableStylePropertySet::create(&property, 1, HTMLStandardMode))); |
| 93 | 95 |
| 94 crossfade->loadSubimages(document()); | 96 crossfade->loadSubimages(document()); |
| 95 Resource* imageResource = memoryCache()->resourceForURL(imageURL, Memory
Cache::defaultCacheIdentifier()); | 97 Resource* imageResource = memoryCache()->resourceForURL(imageURL, Memory
Cache::defaultCacheIdentifier()); |
| 96 ASSERT_TRUE(imageResource); | 98 ASSERT_TRUE(imageResource); |
| 97 ResourceResponse imageResponse; | 99 ResourceResponse imageResponse; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 // The underlying |contents| for the stylesheet resource must have a | 149 // The underlying |contents| for the stylesheet resource must have a |
| 148 // matching reference status. | 150 // matching reference status. |
| 149 EXPECT_TRUE(memoryCache()->contains(imageResource)); | 151 EXPECT_TRUE(memoryCache()->contains(imageResource)); |
| 150 EXPECT_FALSE(memoryCache()->contains(cssResource)); | 152 EXPECT_FALSE(memoryCache()->contains(cssResource)); |
| 151 EXPECT_FALSE(contents->isReferencedFromResource()); | 153 EXPECT_FALSE(contents->isReferencedFromResource()); |
| 152 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext)); | 154 EXPECT_FALSE(cssResource->restoreParsedStyleSheet(parserContext)); |
| 153 } | 155 } |
| 154 | 156 |
| 155 } // namespace | 157 } // namespace |
| 156 } // namespace blink | 158 } // namespace blink |
| OLD | NEW |