summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/css_selector_watch_test.cc
blob: 46e58f5f8105fe349d73477c80b5e1e8a1c6b9ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/css/css_selector_watch.h"

#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/css/style_engine.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"

namespace blink {

class CSSSelectorWatchTest : public PageTestBase {
 protected:
  StyleEngine& GetStyleEngine() { return GetDocument().GetStyleEngine(); }

  static const HashSet<String> AddedSelectors(const CSSSelectorWatch& watch) {
    return watch.added_selectors_;
  }
  static const HashSet<String> RemovedSelectors(const CSSSelectorWatch& watch) {
    return watch.removed_selectors_;
  }
  static void ClearAddedRemoved(CSSSelectorWatch&);
};

void CSSSelectorWatchTest::ClearAddedRemoved(CSSSelectorWatch& watch) {
  watch.added_selectors_.clear();
  watch.removed_selectors_.clear();
}

TEST_F(CSSSelectorWatchTest, RecalcOnDocumentChange) {
  GetDocument().body()->setInnerHTML(R"HTML(
    <div>
      <span id='x' class='a'></span>
      <span id='y' class='b'><span></span></span>
      <span id='z'><span></span></span>
    </div>
  )HTML");

  CSSSelectorWatch& watch = CSSSelectorWatch::From(GetDocument());

  Vector<String> selectors;
  selectors.push_back(".a");
  watch.WatchCSSSelectors(selectors);

  UpdateAllLifecyclePhasesForTest();

  selectors.clear();
  selectors.push_back(".b");
  selectors.push_back(".c");
  selectors.push_back("#nomatch");
  watch.WatchCSSSelectors(selectors);

  UpdateAllLifecyclePhasesForTest();

  Element* x = GetDocument().getElementById("x");
  Element* y = GetDocument().getElementById("y");
  Element* z = GetDocument().getElementById("z");
  ASSERT_TRUE(x);
  ASSERT_TRUE(y);
  ASSERT_TRUE(z);

  x->removeAttribute(html_names::kClassAttr);
  y->removeAttribute(html_names::kClassAttr);
  z->setAttribute(html_names::kClassAttr, "c");

  ClearAddedRemoved(watch);

  unsigned before_count = GetStyleEngine().StyleForElementCount();
  UpdateAllLifecyclePhasesForTest();
  unsigned after_count = GetStyleEngine().StyleForElementCount();

  EXPECT_EQ(2u, after_count - before_count);

  EXPECT_EQ(1u, AddedSelectors(watch).size());
  EXPECT_TRUE(AddedSelectors(watch).Contains(".c"));

  EXPECT_EQ(1u, RemovedSelectors(watch).size());
  EXPECT_TRUE(RemovedSelectors(watch).Contains(".b"));
}

}  // namespace blink