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/css/ActiveStyleSheets.h" | 5 #include "core/css/ActiveStyleSheets.h" |
6 | 6 |
7 #include "core/css/CSSStyleSheet.h" | 7 #include "core/css/CSSStyleSheet.h" |
8 #include "core/css/MediaQueryEvaluator.h" | 8 #include "core/css/MediaQueryEvaluator.h" |
9 #include "core/css/StyleSheetContents.h" | 9 #include "core/css/StyleSheetContents.h" |
10 #include "core/css/parser/CSSParserMode.h" | 10 #include "core/css/parser/CSSParserMode.h" |
| 11 #include "core/dom/shadow/ShadowRoot.h" |
| 12 #include "core/dom/shadow/ShadowRootInit.h" |
| 13 #include "core/frame/FrameView.h" |
| 14 #include "core/html/HTMLElement.h" |
| 15 #include "core/testing/DummyPageHolder.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
12 | 17 |
13 namespace blink { | 18 namespace blink { |
14 | 19 |
15 class ActiveStyleSheetsTest : public ::testing::Test { | 20 class ActiveStyleSheetsTest : public ::testing::Test { |
16 public: | 21 protected: |
17 static CSSStyleSheet* createSheet() { | 22 static CSSStyleSheet* createSheet(const String& cssText = String()) { |
18 StyleSheetContents* contents = | 23 StyleSheetContents* contents = |
19 StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)); | 24 StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)); |
| 25 contents->parseString(cssText); |
20 contents->ensureRuleSet(MediaQueryEvaluator(), | 26 contents->ensureRuleSet(MediaQueryEvaluator(), |
21 RuleHasDocumentSecurityOrigin); | 27 RuleHasDocumentSecurityOrigin); |
22 return CSSStyleSheet::create(contents); | 28 return CSSStyleSheet::create(contents); |
23 } | 29 } |
24 }; | 30 }; |
25 | 31 |
| 32 class ApplyRulesetsTest : public ActiveStyleSheetsTest { |
| 33 protected: |
| 34 void SetUp() override { |
| 35 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 36 } |
| 37 |
| 38 Document& document() { return m_dummyPageHolder->document(); } |
| 39 StyleEngine& styleEngine() { return document().styleEngine(); } |
| 40 ShadowRoot& attachShadow(Element& host); |
| 41 |
| 42 private: |
| 43 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 44 }; |
| 45 |
| 46 ShadowRoot& ApplyRulesetsTest::attachShadow(Element& host) { |
| 47 ShadowRootInit init; |
| 48 init.setMode("open"); |
| 49 ShadowRoot* shadowRoot = host.attachShadow( |
| 50 ScriptState::forMainWorld(document().frame()), init, ASSERT_NO_EXCEPTION); |
| 51 EXPECT_TRUE(shadowRoot); |
| 52 return *shadowRoot; |
| 53 } |
| 54 |
26 TEST_F(ActiveStyleSheetsTest, CompareActiveStyleSheets_NoChange) { | 55 TEST_F(ActiveStyleSheetsTest, CompareActiveStyleSheets_NoChange) { |
27 ActiveStyleSheetVector oldSheets; | 56 ActiveStyleSheetVector oldSheets; |
28 ActiveStyleSheetVector newSheets; | 57 ActiveStyleSheetVector newSheets; |
29 HeapVector<Member<RuleSet>> changedRuleSets; | 58 HeapVector<Member<RuleSet>> changedRuleSets; |
30 | 59 |
31 EXPECT_EQ(NoActiveSheetsChanged, | 60 EXPECT_EQ(NoActiveSheetsChanged, |
32 compareActiveStyleSheets(oldSheets, newSheets, changedRuleSets)); | 61 compareActiveStyleSheets(oldSheets, newSheets, changedRuleSets)); |
33 EXPECT_EQ(0u, changedRuleSets.size()); | 62 EXPECT_EQ(0u, changedRuleSets.size()); |
34 | 63 |
35 CSSStyleSheet* sheet1 = createSheet(); | 64 CSSStyleSheet* sheet1 = createSheet(); |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 293 |
265 newSheets.append(std::make_pair(sheet1, &sheet1->contents()->ruleSet())); | 294 newSheets.append(std::make_pair(sheet1, &sheet1->contents()->ruleSet())); |
266 newSheets.append(std::make_pair(sheet3, &sheet3->contents()->ruleSet())); | 295 newSheets.append(std::make_pair(sheet3, &sheet3->contents()->ruleSet())); |
267 | 296 |
268 EXPECT_EQ(ActiveSheetsChanged, | 297 EXPECT_EQ(ActiveSheetsChanged, |
269 compareActiveStyleSheets(oldSheets, newSheets, changedRuleSets)); | 298 compareActiveStyleSheets(oldSheets, newSheets, changedRuleSets)); |
270 ASSERT_EQ(1u, changedRuleSets.size()); | 299 ASSERT_EQ(1u, changedRuleSets.size()); |
271 EXPECT_EQ(&sheet3->contents()->ruleSet(), changedRuleSets[0]); | 300 EXPECT_EQ(&sheet3->contents()->ruleSet(), changedRuleSets[0]); |
272 } | 301 } |
273 | 302 |
| 303 TEST_F(ApplyRulesetsTest, AddUniversalRuleToDocument) { |
| 304 document().view()->updateAllLifecyclePhases(); |
| 305 |
| 306 CSSStyleSheet* sheet = createSheet("body * { color:red }"); |
| 307 |
| 308 ActiveStyleSheetVector newStyleSheets; |
| 309 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 310 |
| 311 applyRuleSetChanges(styleEngine(), document(), ActiveStyleSheetVector(), |
| 312 newStyleSheets); |
| 313 |
| 314 EXPECT_EQ(SubtreeStyleChange, document().getStyleChangeType()); |
| 315 } |
| 316 |
| 317 TEST_F(ApplyRulesetsTest, AddUniversalRuleToShadowTree) { |
| 318 document().body()->setInnerHTML("<div id=host></div>", ASSERT_NO_EXCEPTION); |
| 319 Element* host = document().getElementById("host"); |
| 320 ASSERT_TRUE(host); |
| 321 |
| 322 ShadowRoot& shadowRoot = attachShadow(*host); |
| 323 document().view()->updateAllLifecyclePhases(); |
| 324 |
| 325 CSSStyleSheet* sheet = createSheet("body * { color:red }"); |
| 326 |
| 327 ActiveStyleSheetVector newStyleSheets; |
| 328 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 329 |
| 330 applyRuleSetChanges(styleEngine(), shadowRoot, ActiveStyleSheetVector(), |
| 331 newStyleSheets); |
| 332 |
| 333 EXPECT_FALSE(document().needsStyleRecalc()); |
| 334 EXPECT_EQ(SubtreeStyleChange, host->getStyleChangeType()); |
| 335 } |
| 336 |
| 337 TEST_F(ApplyRulesetsTest, AddShadowV0BoundaryCrossingRuleToDocument) { |
| 338 document().view()->updateAllLifecyclePhases(); |
| 339 |
| 340 CSSStyleSheet* sheet = createSheet(".a /deep/ .b { color:red }"); |
| 341 |
| 342 ActiveStyleSheetVector newStyleSheets; |
| 343 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 344 |
| 345 applyRuleSetChanges(styleEngine(), document(), ActiveStyleSheetVector(), |
| 346 newStyleSheets); |
| 347 |
| 348 EXPECT_EQ(SubtreeStyleChange, document().getStyleChangeType()); |
| 349 } |
| 350 |
| 351 TEST_F(ApplyRulesetsTest, AddShadowV0BoundaryCrossingRuleToShadowTree) { |
| 352 document().body()->setInnerHTML("<div id=host></div>", ASSERT_NO_EXCEPTION); |
| 353 Element* host = document().getElementById("host"); |
| 354 ASSERT_TRUE(host); |
| 355 |
| 356 ShadowRoot& shadowRoot = attachShadow(*host); |
| 357 document().view()->updateAllLifecyclePhases(); |
| 358 |
| 359 CSSStyleSheet* sheet = createSheet(".a /deep/ .b { color:red }"); |
| 360 |
| 361 ActiveStyleSheetVector newStyleSheets; |
| 362 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 363 |
| 364 applyRuleSetChanges(styleEngine(), shadowRoot, ActiveStyleSheetVector(), |
| 365 newStyleSheets); |
| 366 |
| 367 EXPECT_FALSE(document().needsStyleRecalc()); |
| 368 EXPECT_EQ(SubtreeStyleChange, host->getStyleChangeType()); |
| 369 } |
| 370 |
| 371 TEST_F(ApplyRulesetsTest, AddFontFaceRuleToDocument) { |
| 372 document().view()->updateAllLifecyclePhases(); |
| 373 |
| 374 CSSStyleSheet* sheet = |
| 375 createSheet("@font-face { font-family: ahum; src: url(ahum.ttf) }"); |
| 376 |
| 377 ActiveStyleSheetVector newStyleSheets; |
| 378 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 379 |
| 380 applyRuleSetChanges(styleEngine(), document(), ActiveStyleSheetVector(), |
| 381 newStyleSheets); |
| 382 |
| 383 EXPECT_EQ(SubtreeStyleChange, document().getStyleChangeType()); |
| 384 } |
| 385 |
| 386 TEST_F(ApplyRulesetsTest, AddFontFaceRuleToShadowTree) { |
| 387 document().body()->setInnerHTML("<div id=host></div>", ASSERT_NO_EXCEPTION); |
| 388 Element* host = document().getElementById("host"); |
| 389 ASSERT_TRUE(host); |
| 390 |
| 391 ShadowRoot& shadowRoot = attachShadow(*host); |
| 392 document().view()->updateAllLifecyclePhases(); |
| 393 |
| 394 CSSStyleSheet* sheet = |
| 395 createSheet("@font-face { font-family: ahum; src: url(ahum.ttf) }"); |
| 396 |
| 397 ActiveStyleSheetVector newStyleSheets; |
| 398 newStyleSheets.append(std::make_pair(sheet, &sheet->contents()->ruleSet())); |
| 399 |
| 400 applyRuleSetChanges(styleEngine(), shadowRoot, ActiveStyleSheetVector(), |
| 401 newStyleSheets); |
| 402 |
| 403 EXPECT_FALSE(document().needsLayoutTreeUpdate()); |
| 404 } |
| 405 |
274 } // namespace blink | 406 } // namespace blink |
OLD | NEW |