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/custom/CustomElementUpgradeSorter.h" |
| 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "core/HTMLNames.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/Element.h" |
| 11 #include "core/dom/shadow/ShadowRoot.h" |
| 12 #include "core/dom/shadow/ShadowRootInit.h" |
| 13 #include "core/html/HTMLDocument.h" |
| 14 #include "core/testing/DummyPageHolder.h" |
| 15 #include "platform/heap/Handle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "wtf/OwnPtr.h" |
| 18 #include "wtf/text/AtomicString.h" |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 class CustomElementUpgradeSorterTest : public ::testing::Test { |
| 23 public: |
| 24 Element* createElementWithId(const char* localName, const char* id) |
| 25 { |
| 26 NonThrowableExceptionState noExceptions; |
| 27 Element* element = |
| 28 document()->createElement(localName, AtomicString(), noExceptions); |
| 29 element->setAttribute(HTMLNames::idAttr, id); |
| 30 return element; |
| 31 } |
| 32 |
| 33 Document* document() { return &m_page->document(); } |
| 34 |
| 35 ScriptState* scriptState() |
| 36 { |
| 37 return ScriptState::forMainWorld(&m_page->frame()); |
| 38 } |
| 39 |
| 40 ShadowRoot* attachShadowTo(Element* element) |
| 41 { |
| 42 NonThrowableExceptionState noExceptions; |
| 43 ShadowRootInit shadowRootInit; |
| 44 return |
| 45 element->attachShadow(scriptState(), shadowRootInit, noExceptions); |
| 46 } |
| 47 |
| 48 protected: |
| 49 void SetUp() override |
| 50 { |
| 51 m_page = DummyPageHolder::create(IntSize(1, 1)); |
| 52 } |
| 53 |
| 54 void TearDown() override |
| 55 { |
| 56 m_page = nullptr; |
| 57 } |
| 58 |
| 59 private: |
| 60 OwnPtr<DummyPageHolder> m_page; |
| 61 }; |
| 62 |
| 63 TEST_F(CustomElementUpgradeSorterTest, inOtherDocument_notInSet) |
| 64 { |
| 65 NonThrowableExceptionState noExceptions; |
| 66 Element* element = |
| 67 document()->createElement("a-a", AtomicString(), noExceptions); |
| 68 |
| 69 Document* otherDocument = HTMLDocument::create(); |
| 70 otherDocument->appendChild(element); |
| 71 EXPECT_EQ(otherDocument, element->ownerDocument()) |
| 72 << "sanity: another document should have adopted an element on append"; |
| 73 |
| 74 CustomElementUpgradeSorter sorter; |
| 75 sorter.add(element); |
| 76 |
| 77 HeapVector<Member<Element>> elements; |
| 78 sorter.sorted(&elements, document()); |
| 79 EXPECT_EQ(0u, elements.size()) |
| 80 << "the adopted-away candidate should not have been included"; |
| 81 } |
| 82 |
| 83 TEST_F(CustomElementUpgradeSorterTest, oneCandidate) |
| 84 { |
| 85 NonThrowableExceptionState noExceptions; |
| 86 Element* element = |
| 87 document()->createElement("a-a", AtomicString(), noExceptions); |
| 88 document()->documentElement()->appendChild(element); |
| 89 |
| 90 CustomElementUpgradeSorter sorter; |
| 91 sorter.add(element); |
| 92 |
| 93 HeapVector<Member<Element>> elements; |
| 94 sorter.sorted(&elements, document()); |
| 95 EXPECT_EQ(1u, elements.size()) |
| 96 << "exactly one candidate should be in the result set"; |
| 97 EXPECT_TRUE(elements.contains(element)) |
| 98 << "the candidate should be the element that was added"; |
| 99 } |
| 100 |
| 101 TEST_F(CustomElementUpgradeSorterTest, candidatesInDocumentOrder) |
| 102 { |
| 103 Element* a = createElementWithId("a-a", "a"); |
| 104 Element* b = createElementWithId("a-a", "b"); |
| 105 Element* c = createElementWithId("a-a", "c"); |
| 106 |
| 107 document()->documentElement()->appendChild(a); |
| 108 a->appendChild(b); |
| 109 document()->documentElement()->appendChild(c); |
| 110 |
| 111 CustomElementUpgradeSorter sorter; |
| 112 sorter.add(b); |
| 113 sorter.add(a); |
| 114 sorter.add(c); |
| 115 |
| 116 HeapVector<Member<Element>> elements; |
| 117 sorter.sorted(&elements, document()); |
| 118 EXPECT_EQ(3u, elements.size()); |
| 119 EXPECT_EQ(a, elements[0].get()); |
| 120 EXPECT_EQ(b, elements[1].get()); |
| 121 EXPECT_EQ(c, elements[2].get()); |
| 122 } |
| 123 |
| 124 TEST_F(CustomElementUpgradeSorterTest, sorter_ancestorInSet) |
| 125 { |
| 126 // A* |
| 127 // + B |
| 128 // + C* |
| 129 Element* a = createElementWithId("a-a", "a"); |
| 130 Element* b = createElementWithId("a-a", "b"); |
| 131 Element* c = createElementWithId("a-a", "c"); |
| 132 |
| 133 document()->documentElement()->appendChild(a); |
| 134 a->appendChild(b); |
| 135 b->appendChild(c); |
| 136 |
| 137 CustomElementUpgradeSorter sort; |
| 138 sort.add(c); |
| 139 sort.add(a); |
| 140 |
| 141 HeapVector<Member<Element>> elements; |
| 142 sort.sorted(&elements, document()); |
| 143 EXPECT_EQ(2u, elements.size()); |
| 144 EXPECT_EQ(a, elements[0].get()); |
| 145 EXPECT_EQ(c, elements[1].get()); |
| 146 } |
| 147 |
| 148 TEST_F(CustomElementUpgradeSorterTest, sorter_deepShallow) |
| 149 { |
| 150 // A |
| 151 // + B* |
| 152 // C* |
| 153 Element* a = createElementWithId("a-a", "a"); |
| 154 Element* b = createElementWithId("a-a", "b"); |
| 155 Element* c = createElementWithId("a-a", "c"); |
| 156 |
| 157 document()->documentElement()->appendChild(a); |
| 158 a->appendChild(b); |
| 159 document()->documentElement()->appendChild(c); |
| 160 |
| 161 CustomElementUpgradeSorter sort; |
| 162 sort.add(b); |
| 163 sort.add(c); |
| 164 |
| 165 HeapVector<Member<Element>> elements; |
| 166 sort.sorted(&elements, document()); |
| 167 EXPECT_EQ(2u, elements.size()); |
| 168 EXPECT_EQ(b, elements[0].get()); |
| 169 EXPECT_EQ(c, elements[1].get()); |
| 170 } |
| 171 |
| 172 TEST_F(CustomElementUpgradeSorterTest, sorter_shallowDeep) |
| 173 { |
| 174 // A* |
| 175 // B |
| 176 // + C* |
| 177 Element* a = createElementWithId("a-a", "a"); |
| 178 Element* b = createElementWithId("a-a", "b"); |
| 179 Element* c = createElementWithId("a-a", "c"); |
| 180 |
| 181 document()->documentElement()->appendChild(a); |
| 182 document()->documentElement()->appendChild(b); |
| 183 b->appendChild(c); |
| 184 |
| 185 CustomElementUpgradeSorter sort; |
| 186 sort.add(a); |
| 187 sort.add(c); |
| 188 |
| 189 HeapVector<Member<Element>> elements; |
| 190 sort.sorted(&elements, document()); |
| 191 EXPECT_EQ(2u, elements.size()); |
| 192 EXPECT_EQ(a, elements[0].get()); |
| 193 EXPECT_EQ(c, elements[1].get()); |
| 194 } |
| 195 |
| 196 TEST_F(CustomElementUpgradeSorterTest, sorter_shadow) |
| 197 { |
| 198 // A* |
| 199 // + {ShadowRoot} |
| 200 // | + B |
| 201 // | + C* |
| 202 // + D* |
| 203 Element* a = createElementWithId("a-a", "a"); |
| 204 Element* b = createElementWithId("a-a", "b"); |
| 205 Element* c = createElementWithId("a-a", "c"); |
| 206 Element* d = createElementWithId("a-a", "d"); |
| 207 |
| 208 document()->documentElement()->appendChild(a); |
| 209 ShadowRoot* s = attachShadowTo(a); |
| 210 a->appendChild(d); |
| 211 |
| 212 s->appendChild(b); |
| 213 b->appendChild(c); |
| 214 |
| 215 CustomElementUpgradeSorter sort; |
| 216 sort.add(a); |
| 217 sort.add(c); |
| 218 sort.add(d); |
| 219 |
| 220 HeapVector<Member<Element>> elements; |
| 221 sort.sorted(&elements, document()); |
| 222 EXPECT_EQ(3u, elements.size()); |
| 223 EXPECT_EQ(a, elements[0].get()); |
| 224 EXPECT_EQ(c, elements[1].get()); |
| 225 EXPECT_EQ(d, elements[2].get()); |
| 226 } |
| 227 |
| 228 } // namespace blink |
OLD | NEW |