| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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/dom/TreeScopeStyleSheetCollection.h" | |
| 6 | |
| 7 #include "core/css/CSSStyleSheet.h" | |
| 8 #include "core/css/StyleSheetContents.h" | |
| 9 #include "core/css/parser/CSSParserMode.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 class TreeScopeStyleSheetCollectionTest : public testing::Test { | |
| 15 protected: | |
| 16 using SheetVector = HeapVector<Member<CSSStyleSheet>>; | |
| 17 using ContentsVector = HeapVector<Member<StyleSheetContents>>; | |
| 18 | |
| 19 enum UpdateType { | |
| 20 Reconstruct = TreeScopeStyleSheetCollection::Reconstruct, | |
| 21 Reset = TreeScopeStyleSheetCollection::Reset, | |
| 22 Additive = TreeScopeStyleSheetCollection::Additive | |
| 23 }; | |
| 24 | |
| 25 static CSSStyleSheet* createSheet() | |
| 26 { | |
| 27 return CSSStyleSheet::create(StyleSheetContents::create(CSSParserContext
(HTMLStandardMode, nullptr))); | |
| 28 } | |
| 29 | |
| 30 static void compareStyleSheets(const SheetVector& oldStyleSheets, const Shee
tVector& newStyleSheets, const ContentsVector& expAddedSheets, UpdateType testUp
dateType) | |
| 31 { | |
| 32 ContentsVector addedSheets; | |
| 33 UpdateType updateType = static_cast<UpdateType>(TreeScopeStyleSheetColle
ction::compareStyleSheets(oldStyleSheets, newStyleSheets, addedSheets)); | |
| 34 EXPECT_EQ(testUpdateType, updateType); | |
| 35 EXPECT_EQ(expAddedSheets.size(), addedSheets.size()); | |
| 36 if (expAddedSheets.size() == addedSheets.size()) { | |
| 37 for (unsigned i = 0; i < addedSheets.size(); i++) | |
| 38 EXPECT_EQ(expAddedSheets.at(i), addedSheets.at(i)); | |
| 39 } | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsAppend) | |
| 44 { | |
| 45 CSSStyleSheet* sheet1 = createSheet(); | |
| 46 CSSStyleSheet* sheet2 = createSheet(); | |
| 47 | |
| 48 ContentsVector added; | |
| 49 SheetVector previous; | |
| 50 SheetVector current; | |
| 51 | |
| 52 previous.append(sheet1); | |
| 53 | |
| 54 current.append(sheet1); | |
| 55 current.append(sheet2); | |
| 56 | |
| 57 added.append(sheet2->contents()); | |
| 58 | |
| 59 compareStyleSheets(previous, current, added, Additive); | |
| 60 } | |
| 61 | |
| 62 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsPrepend) | |
| 63 { | |
| 64 CSSStyleSheet* sheet1 = createSheet(); | |
| 65 CSSStyleSheet* sheet2 = createSheet(); | |
| 66 | |
| 67 ContentsVector added; | |
| 68 SheetVector previous; | |
| 69 SheetVector current; | |
| 70 | |
| 71 previous.append(sheet2); | |
| 72 | |
| 73 current.append(sheet1); | |
| 74 current.append(sheet2); | |
| 75 | |
| 76 added.append(sheet1->contents()); | |
| 77 | |
| 78 compareStyleSheets(previous, current, added, Reconstruct); | |
| 79 } | |
| 80 | |
| 81 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsert) | |
| 82 { | |
| 83 CSSStyleSheet* sheet1 = createSheet(); | |
| 84 CSSStyleSheet* sheet2 = createSheet(); | |
| 85 CSSStyleSheet* sheet3 = createSheet(); | |
| 86 | |
| 87 ContentsVector added; | |
| 88 SheetVector previous; | |
| 89 SheetVector current; | |
| 90 | |
| 91 previous.append(sheet1); | |
| 92 previous.append(sheet3); | |
| 93 | |
| 94 current.append(sheet1); | |
| 95 current.append(sheet2); | |
| 96 current.append(sheet3); | |
| 97 | |
| 98 added.append(sheet2->contents()); | |
| 99 | |
| 100 compareStyleSheets(previous, current, added, Reconstruct); | |
| 101 } | |
| 102 | |
| 103 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsRemove) | |
| 104 { | |
| 105 CSSStyleSheet* sheet1 = createSheet(); | |
| 106 CSSStyleSheet* sheet2 = createSheet(); | |
| 107 CSSStyleSheet* sheet3 = createSheet(); | |
| 108 | |
| 109 ContentsVector added; | |
| 110 SheetVector previous; | |
| 111 SheetVector current; | |
| 112 | |
| 113 previous.append(sheet1); | |
| 114 previous.append(sheet2); | |
| 115 previous.append(sheet3); | |
| 116 | |
| 117 current.append(sheet1); | |
| 118 current.append(sheet3); | |
| 119 | |
| 120 added.append(sheet2->contents()); | |
| 121 | |
| 122 // This is really the same as Insert. TreeScopeStyleSheetCollection::compare
StyleSheets | |
| 123 // will assert if you pass an array that is longer as the first parameter. | |
| 124 compareStyleSheets(current, previous, added, Reconstruct); | |
| 125 } | |
| 126 | |
| 127 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsertRemove) | |
| 128 { | |
| 129 CSSStyleSheet* sheet1 = createSheet(); | |
| 130 CSSStyleSheet* sheet2 = createSheet(); | |
| 131 CSSStyleSheet* sheet3 = createSheet(); | |
| 132 | |
| 133 ContentsVector added; | |
| 134 SheetVector previous; | |
| 135 SheetVector current; | |
| 136 | |
| 137 previous.append(sheet1); | |
| 138 previous.append(sheet2); | |
| 139 | |
| 140 current.append(sheet2); | |
| 141 current.append(sheet3); | |
| 142 | |
| 143 // TODO(rune@opera.com): This is clearly wrong. We add sheet3 and remove she
et1 and | |
| 144 // compareStyleSheets returns sheet2 and sheet3 as added (crbug/475858). | |
| 145 added.append(sheet2->contents()); | |
| 146 added.append(sheet3->contents()); | |
| 147 | |
| 148 compareStyleSheets(previous, current, added, Reconstruct); | |
| 149 } | |
| 150 | |
| 151 } // namespace blink | |
| OLD | NEW |