summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/css_selector_watch_test.cc
blob: fdeba84858786ccec0b67f2fcf50eca27edf0650 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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"));
}

class CSSSelectorWatchCQTest : public CSSSelectorWatchTest,
                               private ScopedCSSContainerQueriesForTest {
 protected:
  CSSSelectorWatchCQTest() : ScopedCSSContainerQueriesForTest(true) {}
};

TEST_F(CSSSelectorWatchCQTest, ContainerQueryDisplayNone) {
  CSSSelectorWatch& watch = CSSSelectorWatch::From(GetDocument());

  GetDocument().body()->setInnerHTML(R"HTML(
    <style>
      .c #container {
        container-name: c1;
        container-type: inline-size;
      }
      .c #inner { display: none; }
      @container c1 (min-width: 200px) {
        .c #inner { display: inline }
      }
    </style>
    <div id="container">
      <span id="inner"></span>
    </div>
  )HTML");

  Vector<String> selectors;
  selectors.push_back("#inner");
  watch.WatchCSSSelectors(selectors);
  UpdateAllLifecyclePhasesForTest();

  EXPECT_EQ(1u, AddedSelectors(watch).size());
  EXPECT_TRUE(AddedSelectors(watch).Contains("#inner"));
  EXPECT_EQ(0u, RemovedSelectors(watch).size());

  // Setting the class 'c' on body will make #inner display:none, but also make
  // #container a container 'c1' which is flipping the span back to
  // display:inline.
  ClearAddedRemoved(watch);
  GetDocument().body()->setAttribute(html_names::kClassAttr, "c");
  UpdateAllLifecyclePhasesForTest();

  // Element::UpdateCallbackSelectors() will both remove and add #inner in the
  // two passes. First without the CQ matching, and then in an interleaved style
  // and layout pass. The accounting in CSSSelectorWatch::UpdateSelectorMatches
  // will make sure we up with a zero balance.
  EXPECT_EQ(0u, AddedSelectors(watch).size());
  EXPECT_EQ(0u, RemovedSelectors(watch).size());
}

}  // namespace blink