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/dom/CSSSelectorWatch.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/StyleEngine.h" |
| 9 #include "core/frame/FrameView.h" |
| 10 #include "core/html/HTMLElement.h" |
| 11 #include "core/testing/DummyPageHolder.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 class CSSSelectorWatchTest : public ::testing::Test { |
| 17 protected: |
| 18 void SetUp() override; |
| 19 |
| 20 Document& document() { return m_dummyPageHolder->document(); } |
| 21 StyleEngine& styleEngine() { return document().styleEngine(); } |
| 22 |
| 23 static const HashSet<String> addedSelectors(const CSSSelectorWatch& watch) {
return watch.m_addedSelectors; } |
| 24 static const HashSet<String> removedSelectors(const CSSSelectorWatch& watch)
{ return watch.m_removedSelectors; } |
| 25 static void clearAddedRemoved(CSSSelectorWatch&); |
| 26 |
| 27 private: |
| 28 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 29 }; |
| 30 |
| 31 void CSSSelectorWatchTest::SetUp() |
| 32 { |
| 33 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 34 } |
| 35 |
| 36 void CSSSelectorWatchTest::clearAddedRemoved(CSSSelectorWatch& watch) |
| 37 { |
| 38 watch.m_addedSelectors.clear(); |
| 39 watch.m_removedSelectors.clear(); |
| 40 } |
| 41 |
| 42 TEST_F(CSSSelectorWatchTest, RecalcOnDocumentChange) |
| 43 { |
| 44 document().body()->setInnerHTML( |
| 45 "<div>" |
| 46 " <span id='x' class='a'></span>" |
| 47 " <span id='y' class='b'><span></span></span>" |
| 48 " <span id='z'><span></span></span>" |
| 49 "</div>", ASSERT_NO_EXCEPTION); |
| 50 |
| 51 CSSSelectorWatch& watch = CSSSelectorWatch::from(document()); |
| 52 |
| 53 Vector<String> selectors; |
| 54 selectors.append(".a"); |
| 55 watch.watchCSSSelectors(selectors); |
| 56 |
| 57 document().view()->updateAllLifecyclePhases(); |
| 58 |
| 59 selectors.clear(); |
| 60 selectors.append(".b"); |
| 61 selectors.append(".c"); |
| 62 selectors.append("#nomatch"); |
| 63 watch.watchCSSSelectors(selectors); |
| 64 |
| 65 document().view()->updateAllLifecyclePhases(); |
| 66 |
| 67 Element* x = document().getElementById("x"); |
| 68 Element* y = document().getElementById("y"); |
| 69 Element* z = document().getElementById("z"); |
| 70 ASSERT_TRUE(x); |
| 71 ASSERT_TRUE(y); |
| 72 ASSERT_TRUE(z); |
| 73 |
| 74 x->removeAttribute(HTMLNames::classAttr); |
| 75 y->removeAttribute(HTMLNames::classAttr); |
| 76 z->setAttribute(HTMLNames::classAttr, "c"); |
| 77 |
| 78 clearAddedRemoved(watch); |
| 79 |
| 80 unsigned beforeCount = styleEngine().styleForElementCount(); |
| 81 document().view()->updateAllLifecyclePhases(); |
| 82 unsigned afterCount = styleEngine().styleForElementCount(); |
| 83 |
| 84 EXPECT_EQ(2u, afterCount - beforeCount); |
| 85 |
| 86 EXPECT_EQ(1u, addedSelectors(watch).size()); |
| 87 EXPECT_TRUE(addedSelectors(watch).contains(".c")); |
| 88 |
| 89 EXPECT_EQ(1u, removedSelectors(watch).size()); |
| 90 EXPECT_TRUE(removedSelectors(watch).contains(".b")); |
| 91 } |
| 92 |
| 93 } // namespace blink |
OLD | NEW |