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 return CSSStyleSheet::create(StyleSheetContents::create( | |
27 CSSParserContext(HTMLStandardMode, nullptr))); | |
28 } | |
29 | |
30 static void compareStyleSheets(const SheetVector& oldStyleSheets, | |
31 const SheetVector& newStyleSheets, | |
32 const ContentsVector& expAddedSheets, | |
33 UpdateType testUpdateType) { | |
34 ContentsVector addedSheets; | |
35 UpdateType updateType = static_cast<UpdateType>( | |
36 TreeScopeStyleSheetCollection::compareStyleSheets( | |
37 oldStyleSheets, newStyleSheets, addedSheets)); | |
38 EXPECT_EQ(testUpdateType, updateType); | |
39 EXPECT_EQ(expAddedSheets.size(), addedSheets.size()); | |
40 if (expAddedSheets.size() == addedSheets.size()) { | |
41 for (unsigned i = 0; i < addedSheets.size(); i++) | |
42 EXPECT_EQ(expAddedSheets.at(i), addedSheets.at(i)); | |
43 } | |
44 } | |
45 }; | |
46 | |
47 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsAppend) { | |
48 CSSStyleSheet* sheet1 = createSheet(); | |
49 CSSStyleSheet* sheet2 = createSheet(); | |
50 | |
51 ContentsVector added; | |
52 SheetVector previous; | |
53 SheetVector current; | |
54 | |
55 previous.append(sheet1); | |
56 | |
57 current.append(sheet1); | |
58 current.append(sheet2); | |
59 | |
60 added.append(sheet2->contents()); | |
61 | |
62 compareStyleSheets(previous, current, added, Additive); | |
63 } | |
64 | |
65 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsPrepend) { | |
66 CSSStyleSheet* sheet1 = createSheet(); | |
67 CSSStyleSheet* sheet2 = createSheet(); | |
68 | |
69 ContentsVector added; | |
70 SheetVector previous; | |
71 SheetVector current; | |
72 | |
73 previous.append(sheet2); | |
74 | |
75 current.append(sheet1); | |
76 current.append(sheet2); | |
77 | |
78 added.append(sheet1->contents()); | |
79 | |
80 compareStyleSheets(previous, current, added, Reconstruct); | |
81 } | |
82 | |
83 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsert) { | |
84 CSSStyleSheet* sheet1 = createSheet(); | |
85 CSSStyleSheet* sheet2 = createSheet(); | |
86 CSSStyleSheet* sheet3 = createSheet(); | |
87 | |
88 ContentsVector added; | |
89 SheetVector previous; | |
90 SheetVector current; | |
91 | |
92 previous.append(sheet1); | |
93 previous.append(sheet3); | |
94 | |
95 current.append(sheet1); | |
96 current.append(sheet2); | |
97 current.append(sheet3); | |
98 | |
99 added.append(sheet2->contents()); | |
100 | |
101 compareStyleSheets(previous, current, added, Reconstruct); | |
102 } | |
103 | |
104 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsRemove) { | |
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. | |
123 // TreeScopeStyleSheetCollection::compareStyleSheets will assert if you pass | |
124 // an array that is longer as the first parameter. | |
125 compareStyleSheets(current, previous, added, Reconstruct); | |
126 } | |
127 | |
128 TEST_F(TreeScopeStyleSheetCollectionTest, CompareStyleSheetsInsertRemove) { | |
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 | |
144 // sheet1 and compareStyleSheets returns sheet2 and sheet3 as added | |
145 // (crbug/475858). | |
146 added.append(sheet2->contents()); | |
147 added.append(sheet3->contents()); | |
148 | |
149 compareStyleSheets(previous, current, added, Reconstruct); | |
150 } | |
151 | |
152 } // namespace blink | |
OLD | NEW |