Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp

Issue 2277713002: Rename CustomElementsRegistry to CustomElementRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/CustomElementsRegistry.h"
6
7 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/ScriptValue.h"
9 #include "core/dom/Document.h"
10 #include "core/dom/Element.h"
11 #include "core/dom/ElementRegistrationOptions.h"
12 #include "core/dom/custom/CEReactionsScope.h"
13 #include "core/dom/custom/CustomElementDefinition.h"
14 #include "core/dom/custom/CustomElementDefinitionBuilder.h"
15 #include "core/dom/custom/CustomElementDescriptor.h"
16 #include "core/dom/custom/CustomElementTestHelpers.h"
17 #include "core/dom/shadow/ShadowRoot.h"
18 #include "core/dom/shadow/ShadowRootInit.h"
19 #include "core/testing/DummyPageHolder.h"
20 #include "platform/ScriptForbiddenScope.h"
21 #include "platform/heap/Handle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "wtf/text/AtomicString.h"
24 #include <memory>
25
26 namespace blink {
27
28 class CustomElementsRegistryTest : public ::testing::Test {
29 protected:
30 void SetUp()
31 {
32 m_page.reset(DummyPageHolder::create(IntSize(1, 1)).release());
33 }
34
35 void TearDown()
36 {
37 m_page = nullptr;
38 }
39
40 Document& document() { return m_page->document(); }
41
42 CustomElementsRegistry& registry()
43 {
44 return *m_page->frame().localDOMWindow()->customElements();
45 }
46
47 ScriptState* scriptState()
48 {
49 return ScriptState::forMainWorld(&m_page->frame());
50 }
51
52 void collectCandidates(
53 const CustomElementDescriptor& desc,
54 HeapVector<Member<Element>>* elements)
55 {
56 registry().collectCandidates(desc, elements);
57 }
58
59 ShadowRoot* attachShadowTo(Element* element)
60 {
61 NonThrowableExceptionState noExceptions;
62 ShadowRootInit shadowRootInit;
63 return
64 element->attachShadow(scriptState(), shadowRootInit, noExceptions);
65 }
66
67 private:
68 std::unique_ptr<DummyPageHolder> m_page;
69 };
70
71 TEST_F(
72 CustomElementsRegistryTest,
73 collectCandidates_shouldNotIncludeElementsRemovedFromDocument)
74 {
75 Element* element = CreateElement("a-a").inDocument(&document());
76 registry().addCandidate(element);
77
78 HeapVector<Member<Element>> elements;
79 collectCandidates(
80 CustomElementDescriptor("a-a", "a-a"),
81 &elements);
82
83 EXPECT_TRUE(elements.isEmpty())
84 << "no candidates should have been found, but we have "
85 << elements.size();
86 EXPECT_FALSE(elements.contains(element))
87 << "the out-of-document candidate should not have been found";
88 }
89
90 TEST_F(
91 CustomElementsRegistryTest,
92 collectCandidates_shouldNotIncludeElementsInDifferentDocument)
93 {
94 Element* element = CreateElement("a-a").inDocument(&document());
95 registry().addCandidate(element);
96
97 Document* otherDocument = HTMLDocument::create();
98 otherDocument->appendChild(element);
99 EXPECT_EQ(otherDocument, element->ownerDocument())
100 << "sanity: another document should have adopted an element on append";
101
102 HeapVector<Member<Element>> elements;
103 collectCandidates(
104 CustomElementDescriptor("a-a", "a-a"),
105 &elements);
106
107 EXPECT_TRUE(elements.isEmpty())
108 << "no candidates should have been found, but we have "
109 << elements.size();
110 EXPECT_FALSE(elements.contains(element))
111 << "the adopted-away candidate should not have been found";
112 }
113
114 TEST_F(
115 CustomElementsRegistryTest,
116 collectCandidates_shouldOnlyIncludeCandidatesMatchingDescriptor)
117 {
118 CustomElementDescriptor descriptor("hello-world", "hello-world");
119
120 // Does not match: namespace is not HTML
121 Element* elementA = CreateElement("hello-world")
122 .inDocument(&document())
123 .inNamespace("data:text/date,1981-03-10");
124 // Matches
125 Element* elementB = CreateElement("hello-world").inDocument(&document());
126 // Does not match: local name is not hello-world
127 Element* elementC = CreateElement("button")
128 .inDocument(&document())
129 .withIsAttribute("hello-world");
130 document().documentElement()->appendChild(elementA);
131 elementA->appendChild(elementB);
132 elementA->appendChild(elementC);
133
134 registry().addCandidate(elementA);
135 registry().addCandidate(elementB);
136 registry().addCandidate(elementC);
137
138 HeapVector<Member<Element>> elements;
139 collectCandidates(descriptor, &elements);
140
141 EXPECT_EQ(1u, elements.size())
142 << "only one candidates should have been found";
143 EXPECT_EQ(elementB, elements[0])
144 << "the matching element should have been found";
145 }
146
147 TEST_F(CustomElementsRegistryTest, collectCandidates_oneCandidate)
148 {
149 Element* element = CreateElement("a-a").inDocument(&document());
150 registry().addCandidate(element);
151 document().documentElement()->appendChild(element);
152
153 HeapVector<Member<Element>> elements;
154 collectCandidates(
155 CustomElementDescriptor("a-a", "a-a"),
156 &elements);
157
158 EXPECT_EQ(1u, elements.size())
159 << "exactly one candidate should have been found";
160 EXPECT_TRUE(elements.contains(element))
161 << "the candidate should be the element that was added";
162 }
163
164 TEST_F(CustomElementsRegistryTest, collectCandidates_shouldBeInDocumentOrder)
165 {
166 CreateElement factory = CreateElement("a-a");
167 factory.inDocument(&document());
168 Element* elementA = factory.withId("a");
169 Element* elementB = factory.withId("b");
170 Element* elementC = factory.withId("c");
171
172 registry().addCandidate(elementB);
173 registry().addCandidate(elementA);
174 registry().addCandidate(elementC);
175
176 document().documentElement()->appendChild(elementA);
177 elementA->appendChild(elementB);
178 document().documentElement()->appendChild(elementC);
179
180 HeapVector<Member<Element>> elements;
181 collectCandidates(
182 CustomElementDescriptor("a-a", "a-a"),
183 &elements);
184
185 EXPECT_EQ(elementA, elements[0].get());
186 EXPECT_EQ(elementB, elements[1].get());
187 EXPECT_EQ(elementC, elements[2].get());
188 }
189
190 class TestCustomElementDefinition : public CustomElementDefinition {
191 WTF_MAKE_NONCOPYABLE(TestCustomElementDefinition);
192 public:
193 TestCustomElementDefinition(const CustomElementDescriptor& descriptor)
194 : CustomElementDefinition(descriptor)
195 {
196 }
197
198 TestCustomElementDefinition(const CustomElementDescriptor& descriptor,
199 const HashSet<AtomicString>& observedAttributes)
200 : CustomElementDefinition(descriptor, observedAttributes)
201 {
202 }
203
204 ~TestCustomElementDefinition() override = default;
205
206 ScriptValue getConstructorForScript() override
207 {
208 return ScriptValue();
209 }
210
211 bool runConstructor(Element* element) override
212 {
213 if (constructionStack().isEmpty()
214 || constructionStack().last() != element)
215 return false;
216 constructionStack().last().clear();
217 return true;
218 }
219
220 HTMLElement* createElementSync(Document&, const QualifiedName&) override
221 {
222 return nullptr;
223 }
224
225 HTMLElement* createElementSync(Document&, const QualifiedName&, ExceptionSta te&) override
226 {
227 return nullptr;
228 }
229 };
230
231 // Classes which use trace macros cannot be local because of the
232 // traceImpl template.
233 class LogUpgradeDefinition : public TestCustomElementDefinition {
234 WTF_MAKE_NONCOPYABLE(LogUpgradeDefinition);
235 public:
236 LogUpgradeDefinition(const CustomElementDescriptor& descriptor)
237 : TestCustomElementDefinition(descriptor, {
238 "attr1",
239 "attr2",
240 HTMLNames::contenteditableAttr.localName(),
241 })
242 {
243 }
244
245 DEFINE_INLINE_VIRTUAL_TRACE()
246 {
247 TestCustomElementDefinition::trace(visitor);
248 visitor->trace(m_element);
249 }
250
251 // TODO(dominicc): Make this class collect a vector of what's
252 // upgraded; it will be useful in more tests.
253 Member<Element> m_element;
254 enum MethodType {
255 Constructor,
256 ConnectedCallback,
257 DisconnectedCallback,
258 AdoptedCallback,
259 AttributeChangedCallback,
260 };
261 Vector<MethodType> m_logs;
262
263 struct AttributeChanged {
264 QualifiedName name;
265 AtomicString oldValue;
266 AtomicString newValue;
267 };
268 Vector<AttributeChanged> m_attributeChanged;
269
270 void clear()
271 {
272 m_logs.clear();
273 m_attributeChanged.clear();
274 }
275
276 bool runConstructor(Element* element) override
277 {
278 m_logs.append(Constructor);
279 m_element = element;
280 return TestCustomElementDefinition::runConstructor(element);
281 }
282
283 bool hasConnectedCallback() const override { return true; }
284 bool hasDisconnectedCallback() const override { return true; }
285 bool hasAdoptedCallback() const override {return true;}
286
287 void runConnectedCallback(Element* element) override
288 {
289 m_logs.append(ConnectedCallback);
290 EXPECT_EQ(element, m_element);
291 }
292
293 void runDisconnectedCallback(Element* element) override
294 {
295 m_logs.append(DisconnectedCallback);
296 EXPECT_EQ(element, m_element);
297 }
298
299 void runAdoptedCallback(Element* element) override
300 {
301 m_logs.append(AdoptedCallback);
302 EXPECT_EQ(element, m_element);
303 }
304
305 void runAttributeChangedCallback(Element* element, const QualifiedName& name , const AtomicString& oldValue, const AtomicString& newValue) override
306 {
307 m_logs.append(AttributeChangedCallback);
308 EXPECT_EQ(element, m_element);
309 m_attributeChanged.append(AttributeChanged { name, oldValue, newValue }) ;
310 }
311 };
312
313 class LogUpgradeBuilder final : public CustomElementDefinitionBuilder {
314 STACK_ALLOCATED();
315 WTF_MAKE_NONCOPYABLE(LogUpgradeBuilder);
316 public:
317 LogUpgradeBuilder() { }
318
319 bool checkConstructorIntrinsics() override { return true; }
320 bool checkConstructorNotRegistered() override { return true; }
321 bool checkPrototype() override { return true; }
322 bool rememberOriginalProperties() override { return true; }
323 CustomElementDefinition* build(
324 const CustomElementDescriptor& descriptor) {
325 return new LogUpgradeDefinition(descriptor);
326 }
327 };
328
329 TEST_F(CustomElementsRegistryTest, define_upgradesInDocumentElements)
330 {
331 ScriptForbiddenScope doNotRelyOnScript;
332
333 Element* element = CreateElement("a-a").inDocument(&document());
334 element->setAttribute(QualifiedName(nullAtom, "attr1", HTMLNames::xhtmlNames paceURI), "v1");
335 element->setBooleanAttribute(HTMLNames::contenteditableAttr, true);
336 document().documentElement()->appendChild(element);
337
338 LogUpgradeBuilder builder;
339 NonThrowableExceptionState shouldNotThrow;
340 {
341 CEReactionsScope reactions;
342 registry().define(
343 "a-a",
344 builder,
345 ElementRegistrationOptions(),
346 shouldNotThrow);
347 }
348 LogUpgradeDefinition* definition =
349 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a"));
350 EXPECT_EQ(LogUpgradeDefinition::Constructor, definition->m_logs[0])
351 << "defining the element should have 'upgraded' the existing element";
352 EXPECT_EQ(element, definition->m_element)
353 << "the existing a-a element should have been upgraded";
354
355 EXPECT_EQ(LogUpgradeDefinition::AttributeChangedCallback, definition->m_logs [1])
356 << "Upgrade should invoke attributeChangedCallback for all attributes";
357 EXPECT_EQ("attr1", definition->m_attributeChanged[0].name);
358 EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue);
359 EXPECT_EQ("v1", definition->m_attributeChanged[0].newValue);
360
361 EXPECT_EQ(LogUpgradeDefinition::AttributeChangedCallback, definition->m_logs [2])
362 << "Upgrade should invoke attributeChangedCallback for all attributes";
363 EXPECT_EQ("contenteditable", definition->m_attributeChanged[1].name);
364 EXPECT_EQ(nullAtom, definition->m_attributeChanged[1].oldValue);
365 EXPECT_EQ(emptyAtom, definition->m_attributeChanged[1].newValue);
366 EXPECT_EQ(2u, definition->m_attributeChanged.size())
367 << "Upgrade should invoke attributeChangedCallback for all attributes";
368
369 EXPECT_EQ(LogUpgradeDefinition::ConnectedCallback, definition->m_logs[3])
370 << "upgrade should invoke connectedCallback";
371
372 EXPECT_EQ(4u, definition->m_logs.size())
373 << "upgrade should not invoke other callbacks";
374 }
375
376 TEST_F(CustomElementsRegistryTest, attributeChangedCallback)
377 {
378 ScriptForbiddenScope doNotRelyOnScript;
379
380 Element* element = CreateElement("a-a").inDocument(&document());
381 document().documentElement()->appendChild(element);
382
383 LogUpgradeBuilder builder;
384 NonThrowableExceptionState shouldNotThrow;
385 {
386 CEReactionsScope reactions;
387 registry().define(
388 "a-a",
389 builder,
390 ElementRegistrationOptions(),
391 shouldNotThrow);
392 }
393 LogUpgradeDefinition* definition =
394 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a"));
395
396 definition->clear();
397 {
398 CEReactionsScope reactions;
399 element->setAttribute(QualifiedName(nullAtom, "attr2", HTMLNames::xhtmlN amespaceURI), "v2");
400 }
401 EXPECT_EQ(LogUpgradeDefinition::AttributeChangedCallback, definition->m_logs [0])
402 << "Adding an attribute should invoke attributeChangedCallback";
403 EXPECT_EQ(1u, definition->m_attributeChanged.size())
404 << "Adding an attribute should invoke attributeChangedCallback";
405 EXPECT_EQ("attr2", definition->m_attributeChanged[0].name);
406 EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue);
407 EXPECT_EQ("v2", definition->m_attributeChanged[0].newValue);
408
409 EXPECT_EQ(1u, definition->m_logs.size())
410 << "upgrade should not invoke other callbacks";
411 }
412
413 TEST_F(CustomElementsRegistryTest, disconnectedCallback)
414 {
415 ScriptForbiddenScope doNotRelyOnScript;
416
417 Element* element = CreateElement("a-a").inDocument(&document());
418 document().documentElement()->appendChild(element);
419
420 LogUpgradeBuilder builder;
421 NonThrowableExceptionState shouldNotThrow;
422 {
423 CEReactionsScope reactions;
424 registry().define(
425 "a-a",
426 builder,
427 ElementRegistrationOptions(),
428 shouldNotThrow);
429 }
430 LogUpgradeDefinition* definition =
431 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a"));
432
433 definition->clear();
434 {
435 CEReactionsScope reactions;
436 element->remove(shouldNotThrow);
437 }
438 EXPECT_EQ(LogUpgradeDefinition::DisconnectedCallback, definition->m_logs[0])
439 << "remove() should invoke disconnectedCallback";
440
441 EXPECT_EQ(1u, definition->m_logs.size())
442 << "remove() should not invoke other callbacks";
443 }
444
445 TEST_F(CustomElementsRegistryTest, adoptedCallback)
446 {
447 ScriptForbiddenScope doNotRelyOnScript;
448
449 Element* element = CreateElement("a-a").inDocument(&document());
450 document().documentElement()->appendChild(element);
451
452 LogUpgradeBuilder builder;
453 NonThrowableExceptionState shouldNotThrow;
454 {
455 CEReactionsScope reactions;
456 registry().define(
457 "a-a",
458 builder,
459 ElementRegistrationOptions(),
460 shouldNotThrow);
461 }
462 LogUpgradeDefinition* definition =
463 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a"));
464
465 definition->clear();
466 Document* otherDocument = HTMLDocument::create();
467 {
468 CEReactionsScope reactions;
469 otherDocument->adoptNode(element, ASSERT_NO_EXCEPTION);
470 }
471 EXPECT_EQ(LogUpgradeDefinition::DisconnectedCallback, definition->m_logs[0])
472 << "adoptNode() should invoke disconnectedCallback";
473
474 EXPECT_EQ(LogUpgradeDefinition::AdoptedCallback, definition->m_logs[1])
475 << "adoptNode() should invoke adoptedCallback";
476
477 EXPECT_EQ(2u, definition->m_logs.size())
478 << "adoptNode() should not invoke other callbacks";
479 }
480
481 // TODO(dominicc): Add tests which adjust the "is" attribute when type
482 // extensions are implemented.
483
484 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698