Chromium Code Reviews| Index: third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp |
| diff --git a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp |
| index ea1d265c8cbb8f2f6920e49d58ba72bef9f3ca70..04529f44063e342f3f9ab67812aa5047f6e286fc 100644 |
| --- a/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp |
| +++ b/third_party/WebKit/Source/core/dom/custom/CustomElementsRegistryTest.cpp |
| @@ -269,6 +269,21 @@ public: |
| // upgraded; it will be useful in more tests. |
| Member<Element> m_element; |
| uint32_t m_invocationCount; |
|
dominicc (has gone to gerrit)
2016/06/13 07:59:01
WDYT about making this vector<string> or something
|
| + uint32_t m_connectedCount = 0; |
| + uint32_t m_disconnectedCount = 0; |
| + |
| + struct AttributeChanged { |
| + QualifiedName name; |
| + AtomicString oldValue; |
| + AtomicString newValue; |
| + }; |
| + Vector<AttributeChanged> m_attributeChanged; |
| + |
| + void clear() |
| + { |
| + m_invocationCount = m_connectedCount = m_disconnectedCount = 0; |
| + m_attributeChanged.clear(); |
| + } |
| bool runConstructor(Element* element) override |
| { |
| @@ -276,6 +291,28 @@ public: |
| m_element = element; |
| return TestCustomElementDefinition::runConstructor(element); |
| } |
| + |
| + bool hasConnectedCallback() const override { return true; } |
| + bool hasDisconnectedCallback() const override { return true; } |
| + bool hasAttributeChangedCallback(const QualifiedName&) const override { return true; } |
| + |
| + void runConnectedCallback(Element* element) override |
| + { |
| + m_connectedCount++; |
| + EXPECT_EQ(element, m_element); |
| + } |
| + |
| + void runDisconnectedCallback(Element* element) override |
| + { |
| + m_disconnectedCount++; |
| + EXPECT_EQ(element, m_element); |
| + } |
| + |
| + void runAttributeChangedCallback(Element* element, const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue) override |
| + { |
| + EXPECT_EQ(element, m_element); |
| + m_attributeChanged.append(AttributeChanged { name, oldValue, newValue }); |
| + } |
| }; |
| class LogUpgradeBuilder final : public CustomElementDefinitionBuilder { |
| @@ -299,6 +336,8 @@ TEST_F(CustomElementsRegistryFrameTest, define_upgradesInDocumentElements) |
| ScriptForbiddenScope doNotRelyOnScript; |
| Element* element = CreateElement("a-a").inDocument(&document()); |
| + element->setAttribute(QualifiedName(nullAtom, "attr1", HTMLNames::xhtmlNamespaceURI), "v1"); |
| + element->setBooleanAttribute(HTMLNames::contenteditableAttr, true); |
| document().documentElement()->appendChild(element); |
| LogUpgradeBuilder builder; |
| @@ -317,6 +356,95 @@ TEST_F(CustomElementsRegistryFrameTest, define_upgradesInDocumentElements) |
| << "defining the element should have 'upgraded' the existing element"; |
| EXPECT_EQ(element, definition->m_element) |
| << "the existing a-a element should have been upgraded"; |
| + EXPECT_EQ(1u, definition->m_connectedCount) |
| + << "upgrade should invoke connectedCallback"; |
| + |
| + EXPECT_EQ(2u, definition->m_attributeChanged.size()) |
| + << "Upgrade should invoke attributeChangedCallback for all attributes"; |
| + EXPECT_EQ("attr1", definition->m_attributeChanged[0].name); |
| + EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue); |
| + EXPECT_EQ("v1", definition->m_attributeChanged[0].newValue); |
| + EXPECT_EQ("contenteditable", definition->m_attributeChanged[1].name); |
| + EXPECT_EQ(nullAtom, definition->m_attributeChanged[1].oldValue); |
| + EXPECT_EQ(emptyAtom, definition->m_attributeChanged[1].newValue); |
| + |
| + EXPECT_EQ(0u, definition->m_disconnectedCount) |
| + << "upgrade should not invoke disconnectedCallback"; |
| +} |
| + |
| +TEST_F(CustomElementsRegistryFrameTest, attributeChangedCallback) |
| +{ |
| + ScriptForbiddenScope doNotRelyOnScript; |
| + |
| + Element* element = CreateElement("a-a").inDocument(&document()); |
| + document().documentElement()->appendChild(element); |
| + |
| + LogUpgradeBuilder builder; |
| + NonThrowableExceptionState shouldNotThrow; |
| + { |
| + CEReactionsScope reactions; |
| + registry().define( |
| + "a-a", |
| + builder, |
| + ElementRegistrationOptions(), |
| + shouldNotThrow); |
| + } |
| + LogUpgradeDefinition* definition = |
| + static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); |
| + |
| + definition->clear(); |
| + { |
| + CEReactionsScope reactions; |
| + element->setAttribute(QualifiedName(nullAtom, "attr2", HTMLNames::xhtmlNamespaceURI), "v2"); |
| + } |
| + EXPECT_EQ(1u, definition->m_attributeChanged.size()) |
| + << "Adding an attribute should invoke attributeChangedCallback"; |
| + EXPECT_EQ("attr2", definition->m_attributeChanged[0].name); |
| + EXPECT_EQ(nullAtom, definition->m_attributeChanged[0].oldValue); |
| + EXPECT_EQ("v2", definition->m_attributeChanged[0].newValue); |
| + |
| + EXPECT_EQ(0u, definition->m_invocationCount) |
| + << "Attribute changes should not invoke upgrade"; |
| + EXPECT_EQ(0u, definition->m_connectedCount) |
| + << "Attribute changes should not invoke connectedCallback"; |
| + EXPECT_EQ(0u, definition->m_disconnectedCount) |
| + << "Attribute changes should invoke disconnectedCallback"; |
| +} |
| + |
| +TEST_F(CustomElementsRegistryFrameTest, disconnectedCallback) |
| +{ |
| + ScriptForbiddenScope doNotRelyOnScript; |
| + |
| + Element* element = CreateElement("a-a").inDocument(&document()); |
| + document().documentElement()->appendChild(element); |
| + |
| + LogUpgradeBuilder builder; |
| + NonThrowableExceptionState shouldNotThrow; |
| + { |
| + CEReactionsScope reactions; |
| + registry().define( |
| + "a-a", |
| + builder, |
| + ElementRegistrationOptions(), |
| + shouldNotThrow); |
| + } |
| + LogUpgradeDefinition* definition = |
| + static_cast<LogUpgradeDefinition*>(registry().definitionForName("a-a")); |
| + |
| + definition->clear(); |
| + { |
| + CEReactionsScope reactions; |
| + element->remove(shouldNotThrow); |
| + } |
| + EXPECT_EQ(1u, definition->m_disconnectedCount) |
| + << "remove() should invoke disconnectedCallback"; |
| + |
| + EXPECT_EQ(0u, definition->m_invocationCount) |
| + << "remove() should not invoke upgrade"; |
| + EXPECT_EQ(0u, definition->m_connectedCount) |
| + << "remove() should not invoke connectedCallback"; |
| + EXPECT_EQ(0u, definition->m_attributeChanged.size()) |
| + << "remove() should not invoke attributeChangedCallback"; |
| } |
| // TODO(dominicc): Add tests which adjust the "is" attribute when type |