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

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

Issue 2641803003: Introduce SynchronousMutationObserver::didChangeAttribute() (Closed)
Patch Set: 2017-01-20T14:34:58 Created 3 years, 11 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/dom/Element.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014, Google Inc. All rights reserved. 2 * Copyright (c) 2014, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 DEFINE_INLINE_TRACE() { visitor->trace(m_node); } 116 DEFINE_INLINE_TRACE() { visitor->trace(m_node); }
117 }; 117 };
118 118
119 TestSynchronousMutationObserver(Document&); 119 TestSynchronousMutationObserver(Document&);
120 virtual ~TestSynchronousMutationObserver() = default; 120 virtual ~TestSynchronousMutationObserver() = default;
121 121
122 int countContextDestroyedCalled() const { 122 int countContextDestroyedCalled() const {
123 return m_contextDestroyedCalledCounter; 123 return m_contextDestroyedCalledCounter;
124 } 124 }
125 125
126 const HeapVector<Member<const Element>>& attributeChangedElements() const {
127 return m_attributeChangedElements;
128 }
129
126 const HeapVector<Member<const ContainerNode>>& childrenChangedNodes() const { 130 const HeapVector<Member<const ContainerNode>>& childrenChangedNodes() const {
127 return m_childrenChangedNodes; 131 return m_childrenChangedNodes;
128 } 132 }
129 133
130 const HeapVector<Member<MergeTextNodesRecord>>& mergeTextNodesRecords() 134 const HeapVector<Member<MergeTextNodesRecord>>& mergeTextNodesRecords()
131 const { 135 const {
132 return m_mergeTextNodesRecords; 136 return m_mergeTextNodesRecords;
133 } 137 }
134 138
135 const HeapVector<Member<const Node>>& moveTreeToNewDocumentNodes() const { 139 const HeapVector<Member<const Node>>& moveTreeToNewDocumentNodes() const {
(...skipping 15 matching lines...) Expand all
151 const HeapVector<Member<UpdateCharacterDataRecord>>& 155 const HeapVector<Member<UpdateCharacterDataRecord>>&
152 updatedCharacterDataRecords() const { 156 updatedCharacterDataRecords() const {
153 return m_updatedCharacterDataRecords; 157 return m_updatedCharacterDataRecords;
154 } 158 }
155 159
156 DECLARE_TRACE(); 160 DECLARE_TRACE();
157 161
158 private: 162 private:
159 // Implement |SynchronousMutationObserver| member functions. 163 // Implement |SynchronousMutationObserver| member functions.
160 void contextDestroyed(Document*) final; 164 void contextDestroyed(Document*) final;
165 void didChangeAttribute(const Element&) final;
161 void didChangeChildren(const ContainerNode&) final; 166 void didChangeChildren(const ContainerNode&) final;
162 void didMergeTextNodes(const Text&, const NodeWithIndex&, unsigned) final; 167 void didMergeTextNodes(const Text&, const NodeWithIndex&, unsigned) final;
163 void didMoveTreeToNewDocument(const Node& root) final; 168 void didMoveTreeToNewDocument(const Node& root) final;
164 void didSplitTextNode(const Text&) final; 169 void didSplitTextNode(const Text&) final;
165 void didUpdateCharacterData(CharacterData*, 170 void didUpdateCharacterData(CharacterData*,
166 unsigned offset, 171 unsigned offset,
167 unsigned oldLength, 172 unsigned oldLength,
168 unsigned newLength) final; 173 unsigned newLength) final;
169 void nodeChildrenWillBeRemoved(ContainerNode&) final; 174 void nodeChildrenWillBeRemoved(ContainerNode&) final;
170 void nodeWillBeRemoved(Node&) final; 175 void nodeWillBeRemoved(Node&) final;
171 176
172 int m_contextDestroyedCalledCounter = 0; 177 int m_contextDestroyedCalledCounter = 0;
178 HeapVector<Member<const Element>> m_attributeChangedElements;
173 HeapVector<Member<const ContainerNode>> m_childrenChangedNodes; 179 HeapVector<Member<const ContainerNode>> m_childrenChangedNodes;
174 HeapVector<Member<MergeTextNodesRecord>> m_mergeTextNodesRecords; 180 HeapVector<Member<MergeTextNodesRecord>> m_mergeTextNodesRecords;
175 HeapVector<Member<const Node>> m_moveTreeToNewDocumentNodes; 181 HeapVector<Member<const Node>> m_moveTreeToNewDocumentNodes;
176 HeapVector<Member<ContainerNode>> m_removedChildrenNodes; 182 HeapVector<Member<ContainerNode>> m_removedChildrenNodes;
177 HeapVector<Member<Node>> m_removedNodes; 183 HeapVector<Member<Node>> m_removedNodes;
178 HeapVector<Member<const Text>> m_splitTextNodes; 184 HeapVector<Member<const Text>> m_splitTextNodes;
179 HeapVector<Member<UpdateCharacterDataRecord>> m_updatedCharacterDataRecords; 185 HeapVector<Member<UpdateCharacterDataRecord>> m_updatedCharacterDataRecords;
180 186
181 DISALLOW_COPY_AND_ASSIGN(TestSynchronousMutationObserver); 187 DISALLOW_COPY_AND_ASSIGN(TestSynchronousMutationObserver);
182 }; 188 };
183 189
184 TestSynchronousMutationObserver::TestSynchronousMutationObserver( 190 TestSynchronousMutationObserver::TestSynchronousMutationObserver(
185 Document& document) { 191 Document& document) {
186 setContext(&document); 192 setContext(&document);
187 } 193 }
188 194
189 void TestSynchronousMutationObserver::contextDestroyed(Document*) { 195 void TestSynchronousMutationObserver::contextDestroyed(Document*) {
190 ++m_contextDestroyedCalledCounter; 196 ++m_contextDestroyedCalledCounter;
191 } 197 }
192 198
199 void TestSynchronousMutationObserver::didChangeAttribute(
200 const Element& element) {
201 m_attributeChangedElements.push_back(&element);
202 }
203
193 void TestSynchronousMutationObserver::didChangeChildren( 204 void TestSynchronousMutationObserver::didChangeChildren(
194 const ContainerNode& container) { 205 const ContainerNode& container) {
195 m_childrenChangedNodes.push_back(&container); 206 m_childrenChangedNodes.push_back(&container);
196 } 207 }
197 208
198 void TestSynchronousMutationObserver::didMergeTextNodes( 209 void TestSynchronousMutationObserver::didMergeTextNodes(
199 const Text& node, 210 const Text& node,
200 const NodeWithIndex& nodeWithIndex, 211 const NodeWithIndex& nodeWithIndex,
201 unsigned offset) { 212 unsigned offset) {
202 m_mergeTextNodesRecords.push_back( 213 m_mergeTextNodesRecords.push_back(
(...skipping 21 matching lines...) Expand all
224 void TestSynchronousMutationObserver::nodeChildrenWillBeRemoved( 235 void TestSynchronousMutationObserver::nodeChildrenWillBeRemoved(
225 ContainerNode& container) { 236 ContainerNode& container) {
226 m_removedChildrenNodes.push_back(&container); 237 m_removedChildrenNodes.push_back(&container);
227 } 238 }
228 239
229 void TestSynchronousMutationObserver::nodeWillBeRemoved(Node& node) { 240 void TestSynchronousMutationObserver::nodeWillBeRemoved(Node& node) {
230 m_removedNodes.push_back(&node); 241 m_removedNodes.push_back(&node);
231 } 242 }
232 243
233 DEFINE_TRACE(TestSynchronousMutationObserver) { 244 DEFINE_TRACE(TestSynchronousMutationObserver) {
245 visitor->trace(m_attributeChangedElements);
234 visitor->trace(m_childrenChangedNodes); 246 visitor->trace(m_childrenChangedNodes);
235 visitor->trace(m_mergeTextNodesRecords); 247 visitor->trace(m_mergeTextNodesRecords);
236 visitor->trace(m_moveTreeToNewDocumentNodes); 248 visitor->trace(m_moveTreeToNewDocumentNodes);
237 visitor->trace(m_removedChildrenNodes); 249 visitor->trace(m_removedChildrenNodes);
238 visitor->trace(m_removedNodes); 250 visitor->trace(m_removedNodes);
239 visitor->trace(m_splitTextNodes); 251 visitor->trace(m_splitTextNodes);
240 visitor->trace(m_updatedCharacterDataRecords); 252 visitor->trace(m_updatedCharacterDataRecords);
241 SynchronousMutationObserver::trace(visitor); 253 SynchronousMutationObserver::trace(visitor);
242 } 254 }
243 255
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 EXPECT_EQ(1u, observer.removedNodes().size()) 510 EXPECT_EQ(1u, observer.removedNodes().size())
499 << "ContainerNode::removeChildren() doesn't call nodeWillBeRemoved()"; 511 << "ContainerNode::removeChildren() doesn't call nodeWillBeRemoved()";
500 ASSERT_EQ(1u, observer.removedChildrenNodes().size()); 512 ASSERT_EQ(1u, observer.removedChildrenNodes().size());
501 EXPECT_EQ(divNode, observer.removedChildrenNodes()[0]); 513 EXPECT_EQ(divNode, observer.removedChildrenNodes()[0]);
502 514
503 document().shutdown(); 515 document().shutdown();
504 EXPECT_EQ(nullptr, observer.lifecycleContext()); 516 EXPECT_EQ(nullptr, observer.lifecycleContext());
505 EXPECT_EQ(1, observer.countContextDestroyedCalled()); 517 EXPECT_EQ(1, observer.countContextDestroyedCalled());
506 } 518 }
507 519
520 TEST_F(DocumentTest, SynchronousMutationNotifierChangeAttribute) {
521 auto& observer = *new TestSynchronousMutationObserver(document());
522 Element* divNode = document().createElement("div");
523 document().body()->appendChild(divNode);
524 divNode->setAttribute(HTMLNames::classAttr, "foo");
525
526 ASSERT_EQ(1u, observer.attributeChangedElements().size());
527 EXPECT_EQ(divNode, observer.attributeChangedElements()[0]);
528
529 divNode->setAttribute(HTMLNames::classAttr, "bar");
530 ASSERT_EQ(2u, observer.attributeChangedElements().size());
531 EXPECT_EQ(divNode, observer.attributeChangedElements()[1]);
532
533 divNode->removeAttribute(HTMLNames::classAttr);
534 ASSERT_EQ(3u, observer.attributeChangedElements().size());
535 EXPECT_EQ(divNode, observer.attributeChangedElements()[2]);
536 }
537
508 TEST_F(DocumentTest, SynchronousMutationNotifieAppendChild) { 538 TEST_F(DocumentTest, SynchronousMutationNotifieAppendChild) {
509 auto& observer = *new TestSynchronousMutationObserver(document()); 539 auto& observer = *new TestSynchronousMutationObserver(document());
510 document().body()->appendChild(document().createTextNode("a123456789")); 540 document().body()->appendChild(document().createTextNode("a123456789"));
511 ASSERT_EQ(1u, observer.childrenChangedNodes().size()); 541 ASSERT_EQ(1u, observer.childrenChangedNodes().size());
512 EXPECT_EQ(document().body(), observer.childrenChangedNodes()[0]); 542 EXPECT_EQ(document().body(), observer.childrenChangedNodes()[0]);
513 } 543 }
514 544
515 TEST_F(DocumentTest, SynchronousMutationNotifieInsertBefore) { 545 TEST_F(DocumentTest, SynchronousMutationNotifieInsertBefore) {
516 auto& observer = *new TestSynchronousMutationObserver(document()); 546 auto& observer = *new TestSynchronousMutationObserver(document());
517 document().documentElement()->insertBefore( 547 document().documentElement()->insertBefore(
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 { 674 {
645 setHtmlInnerHTML( 675 setHtmlInnerHTML(
646 "<body>" 676 "<body>"
647 "<meta name=\"theme-color\" content=\"#00ff00\">"); 677 "<meta name=\"theme-color\" content=\"#00ff00\">");
648 EXPECT_EQ(Color(0, 255, 0), document().themeColor()) 678 EXPECT_EQ(Color(0, 255, 0), document().themeColor())
649 << "Theme color should be bright green."; 679 << "Theme color should be bright green.";
650 } 680 }
651 } 681 }
652 682
653 } // namespace blink 683 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/dom/Element.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698