OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/custom/CustomElementsRegistry.h" | 5 #include "core/dom/custom/CustomElementsRegistry.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptValue.h" | 8 #include "bindings/core/v8/ScriptValue.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/Element.h" | 10 #include "core/dom/Element.h" |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 | 261 |
262 DEFINE_INLINE_VIRTUAL_TRACE() | 262 DEFINE_INLINE_VIRTUAL_TRACE() |
263 { | 263 { |
264 TestCustomElementDefinition::trace(visitor); | 264 TestCustomElementDefinition::trace(visitor); |
265 visitor->trace(m_element); | 265 visitor->trace(m_element); |
266 } | 266 } |
267 | 267 |
268 // TODO(dominicc): Make this class collect a vector of what's | 268 // TODO(dominicc): Make this class collect a vector of what's |
269 // upgraded; it will be useful in more tests. | 269 // upgraded; it will be useful in more tests. |
270 Member<Element> m_element; | 270 Member<Element> m_element; |
271 uint32_t m_invocationCount; | 271 uint32_t m_invocationCount; |
dominicc (has gone to gerrit)
2016/06/13 07:59:01
WDYT about making this vector<string> or something
| |
272 uint32_t m_connectedCount = 0; | |
273 uint32_t m_disconnectedCount = 0; | |
274 | |
275 struct AttributeChanged { | |
276 QualifiedName name; | |
277 AtomicString oldValue; | |
278 AtomicString newValue; | |
279 }; | |
280 Vector<AttributeChanged> m_attributeChanged; | |
281 | |
282 void clear() | |
283 { | |
284 m_invocationCount = m_connectedCount = m_disconnectedCount = 0; | |
285 m_attributeChanged.clear(); | |
286 } | |
272 | 287 |
273 bool runConstructor(Element* element) override | 288 bool runConstructor(Element* element) override |
274 { | 289 { |
275 m_invocationCount++; | 290 m_invocationCount++; |
276 m_element = element; | 291 m_element = element; |
277 return TestCustomElementDefinition::runConstructor(element); | 292 return TestCustomElementDefinition::runConstructor(element); |
278 } | 293 } |
294 | |
295 bool hasConnectedCallback() const override { return true; } | |
296 bool hasDisconnectedCallback() const override { return true; } | |
297 bool hasAttributeChangedCallback(const QualifiedName&) const override { retu rn true; } | |
298 | |
299 void runConnectedCallback(Element* element) override | |
300 { | |
301 m_connectedCount++; | |
302 EXPECT_EQ(element, m_element); | |
303 } | |
304 | |
305 void runDisconnectedCallback(Element* element) override | |
306 { | |
307 m_disconnectedCount++; | |
308 EXPECT_EQ(element, m_element); | |
309 } | |
310 | |
311 void runAttributeChangedCallback(Element* element, const QualifiedName& name , const AtomicString& oldValue, const AtomicString& newValue) override | |
312 { | |
313 EXPECT_EQ(element, m_element); | |
314 m_attributeChanged.append(AttributeChanged { name, oldValue, newValue }) ; | |
315 } | |
279 }; | 316 }; |
280 | 317 |
281 class LogUpgradeBuilder final : public CustomElementDefinitionBuilder { | 318 class LogUpgradeBuilder final : public CustomElementDefinitionBuilder { |
282 STACK_ALLOCATED(); | 319 STACK_ALLOCATED(); |
283 WTF_MAKE_NONCOPYABLE(LogUpgradeBuilder); | 320 WTF_MAKE_NONCOPYABLE(LogUpgradeBuilder); |
284 public: | 321 public: |
285 LogUpgradeBuilder() { } | 322 LogUpgradeBuilder() { } |
286 | 323 |
287 bool checkConstructorIntrinsics() override { return true; } | 324 bool checkConstructorIntrinsics() override { return true; } |
288 bool checkConstructorNotRegistered() override { return true; } | 325 bool checkConstructorNotRegistered() override { return true; } |
289 bool checkPrototype() override { return true; } | 326 bool checkPrototype() override { return true; } |
290 bool rememberOriginalProperties() override { return true; } | 327 bool rememberOriginalProperties() override { return true; } |
291 CustomElementDefinition* build( | 328 CustomElementDefinition* build( |
292 const CustomElementDescriptor& descriptor) { | 329 const CustomElementDescriptor& descriptor) { |
293 return new LogUpgradeDefinition(descriptor); | 330 return new LogUpgradeDefinition(descriptor); |
294 } | 331 } |
295 }; | 332 }; |
296 | 333 |
297 TEST_F(CustomElementsRegistryFrameTest, define_upgradesInDocumentElements) | 334 TEST_F(CustomElementsRegistryFrameTest, define_upgradesInDocumentElements) |
298 { | 335 { |
299 ScriptForbiddenScope doNotRelyOnScript; | 336 ScriptForbiddenScope doNotRelyOnScript; |
300 | 337 |
301 Element* element = CreateElement("a-a").inDocument(&document()); | 338 Element* element = CreateElement("a-a").inDocument(&document()); |
339 element->setAttribute(QualifiedName(nullAtom, "attr1", HTMLNames::xhtmlNames paceURI), "v1"); | |
340 element->setBooleanAttribute(HTMLNames::contenteditableAttr, true); | |
302 document().documentElement()->appendChild(element); | 341 document().documentElement()->appendChild(element); |
303 | 342 |
304 LogUpgradeBuilder builder; | 343 LogUpgradeBuilder builder; |
305 NonThrowableExceptionState shouldNotThrow; | 344 NonThrowableExceptionState shouldNotThrow; |
306 { | 345 { |
307 CEReactionsScope reactions; | 346 CEReactionsScope reactions; |
308 registry().define( | 347 registry().define( |
309 "a-a", | 348 "a-a", |
310 builder, | 349 builder, |
311 ElementRegistrationOptions(), | 350 ElementRegistrationOptions(), |
312 shouldNotThrow); | 351 shouldNotThrow); |
313 } | 352 } |
314 LogUpgradeDefinition* definition = | 353 LogUpgradeDefinition* definition = |
315 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); | 354 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); |
316 EXPECT_EQ(1u, definition->m_invocationCount) | 355 EXPECT_EQ(1u, definition->m_invocationCount) |
317 << "defining the element should have 'upgraded' the existing element"; | 356 << "defining the element should have 'upgraded' the existing element"; |
318 EXPECT_EQ(element, definition->m_element) | 357 EXPECT_EQ(element, definition->m_element) |
319 << "the existing a-a element should have been upgraded"; | 358 << "the existing a-a element should have been upgraded"; |
359 EXPECT_EQ(1u, definition->m_connectedCount) | |
360 << "upgrade should invoke connectedCallback"; | |
361 | |
362 EXPECT_EQ(2u, definition->m_attributeChanged.size()) | |
363 << "Upgrade should invoke attributeChangedCallback for all attributes"; | |
364 EXPECT_EQ("attr1", definition->m_attributeChanged[0].name); | |
365 EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue); | |
366 EXPECT_EQ("v1", definition->m_attributeChanged[0].newValue); | |
367 EXPECT_EQ("contenteditable", definition->m_attributeChanged[1].name); | |
368 EXPECT_EQ(nullAtom, definition->m_attributeChanged[1].oldValue); | |
369 EXPECT_EQ(emptyAtom, definition->m_attributeChanged[1].newValue); | |
370 | |
371 EXPECT_EQ(0u, definition->m_disconnectedCount) | |
372 << "upgrade should not invoke disconnectedCallback"; | |
373 } | |
374 | |
375 TEST_F(CustomElementsRegistryFrameTest, attributeChangedCallback) | |
376 { | |
377 ScriptForbiddenScope doNotRelyOnScript; | |
378 | |
379 Element* element = CreateElement("a-a").inDocument(&document()); | |
380 document().documentElement()->appendChild(element); | |
381 | |
382 LogUpgradeBuilder builder; | |
383 NonThrowableExceptionState shouldNotThrow; | |
384 { | |
385 CEReactionsScope reactions; | |
386 registry().define( | |
387 "a-a", | |
388 builder, | |
389 ElementRegistrationOptions(), | |
390 shouldNotThrow); | |
391 } | |
392 LogUpgradeDefinition* definition = | |
393 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); | |
394 | |
395 definition->clear(); | |
396 { | |
397 CEReactionsScope reactions; | |
398 element->setAttribute(QualifiedName(nullAtom, "attr2", HTMLNames::xhtmlN amespaceURI), "v2"); | |
399 } | |
400 EXPECT_EQ(1u, definition->m_attributeChanged.size()) | |
401 << "Adding an attribute should invoke attributeChangedCallback"; | |
402 EXPECT_EQ("attr2", definition->m_attributeChanged[0].name); | |
403 EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue); | |
404 EXPECT_EQ("v2", definition->m_attributeChanged[0].newValue); | |
405 | |
406 EXPECT_EQ(0u, definition->m_invocationCount) | |
407 << "Attribute changes should not invoke upgrade"; | |
408 EXPECT_EQ(0u, definition->m_connectedCount) | |
409 << "Attribute changes should not invoke connectedCallback"; | |
410 EXPECT_EQ(0u, definition->m_disconnectedCount) | |
411 << "Attribute changes should invoke disconnectedCallback"; | |
412 } | |
413 | |
414 TEST_F(CustomElementsRegistryFrameTest, disconnectedCallback) | |
415 { | |
416 ScriptForbiddenScope doNotRelyOnScript; | |
417 | |
418 Element* element = CreateElement("a-a").inDocument(&document()); | |
419 document().documentElement()->appendChild(element); | |
420 | |
421 LogUpgradeBuilder builder; | |
422 NonThrowableExceptionState shouldNotThrow; | |
423 { | |
424 CEReactionsScope reactions; | |
425 registry().define( | |
426 "a-a", | |
427 builder, | |
428 ElementRegistrationOptions(), | |
429 shouldNotThrow); | |
430 } | |
431 LogUpgradeDefinition* definition = | |
432 static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); | |
433 | |
434 definition->clear(); | |
435 { | |
436 CEReactionsScope reactions; | |
437 element->remove(shouldNotThrow); | |
438 } | |
439 EXPECT_EQ(1u, definition->m_disconnectedCount) | |
440 << "remove() should invoke disconnectedCallback"; | |
441 | |
442 EXPECT_EQ(0u, definition->m_invocationCount) | |
443 << "remove() should not invoke upgrade"; | |
444 EXPECT_EQ(0u, definition->m_connectedCount) | |
445 << "remove() should not invoke connectedCallback"; | |
446 EXPECT_EQ(0u, definition->m_attributeChanged.size()) | |
447 << "remove() should not invoke attributeChangedCallback"; | |
320 } | 448 } |
321 | 449 |
322 // TODO(dominicc): Add tests which adjust the "is" attribute when type | 450 // TODO(dominicc): Add tests which adjust the "is" attribute when type |
323 // extensions are implemented. | 451 // extensions are implemented. |
324 | 452 |
325 } // namespace blink | 453 } // namespace blink |
OLD | NEW |