OLD | NEW |
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 Loading... |
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 | |
130 const HeapVector<Member<const ContainerNode>>& childrenChangedNodes() const { | 126 const HeapVector<Member<const ContainerNode>>& childrenChangedNodes() const { |
131 return m_childrenChangedNodes; | 127 return m_childrenChangedNodes; |
132 } | 128 } |
133 | 129 |
134 const HeapVector<Member<MergeTextNodesRecord>>& mergeTextNodesRecords() | 130 const HeapVector<Member<MergeTextNodesRecord>>& mergeTextNodesRecords() |
135 const { | 131 const { |
136 return m_mergeTextNodesRecords; | 132 return m_mergeTextNodesRecords; |
137 } | 133 } |
138 | 134 |
139 const HeapVector<Member<const Node>>& moveTreeToNewDocumentNodes() const { | 135 const HeapVector<Member<const Node>>& moveTreeToNewDocumentNodes() const { |
(...skipping 15 matching lines...) Expand all Loading... |
155 const HeapVector<Member<UpdateCharacterDataRecord>>& | 151 const HeapVector<Member<UpdateCharacterDataRecord>>& |
156 updatedCharacterDataRecords() const { | 152 updatedCharacterDataRecords() const { |
157 return m_updatedCharacterDataRecords; | 153 return m_updatedCharacterDataRecords; |
158 } | 154 } |
159 | 155 |
160 DECLARE_TRACE(); | 156 DECLARE_TRACE(); |
161 | 157 |
162 private: | 158 private: |
163 // Implement |SynchronousMutationObserver| member functions. | 159 // Implement |SynchronousMutationObserver| member functions. |
164 void contextDestroyed(Document*) final; | 160 void contextDestroyed(Document*) final; |
165 void didChangeAttribute(const Element&) final; | |
166 void didChangeChildren(const ContainerNode&) final; | 161 void didChangeChildren(const ContainerNode&) final; |
167 void didMergeTextNodes(const Text&, const NodeWithIndex&, unsigned) final; | 162 void didMergeTextNodes(const Text&, const NodeWithIndex&, unsigned) final; |
168 void didMoveTreeToNewDocument(const Node& root) final; | 163 void didMoveTreeToNewDocument(const Node& root) final; |
169 void didSplitTextNode(const Text&) final; | 164 void didSplitTextNode(const Text&) final; |
170 void didUpdateCharacterData(CharacterData*, | 165 void didUpdateCharacterData(CharacterData*, |
171 unsigned offset, | 166 unsigned offset, |
172 unsigned oldLength, | 167 unsigned oldLength, |
173 unsigned newLength) final; | 168 unsigned newLength) final; |
174 void nodeChildrenWillBeRemoved(ContainerNode&) final; | 169 void nodeChildrenWillBeRemoved(ContainerNode&) final; |
175 void nodeWillBeRemoved(Node&) final; | 170 void nodeWillBeRemoved(Node&) final; |
176 | 171 |
177 int m_contextDestroyedCalledCounter = 0; | 172 int m_contextDestroyedCalledCounter = 0; |
178 HeapVector<Member<const Element>> m_attributeChangedElements; | |
179 HeapVector<Member<const ContainerNode>> m_childrenChangedNodes; | 173 HeapVector<Member<const ContainerNode>> m_childrenChangedNodes; |
180 HeapVector<Member<MergeTextNodesRecord>> m_mergeTextNodesRecords; | 174 HeapVector<Member<MergeTextNodesRecord>> m_mergeTextNodesRecords; |
181 HeapVector<Member<const Node>> m_moveTreeToNewDocumentNodes; | 175 HeapVector<Member<const Node>> m_moveTreeToNewDocumentNodes; |
182 HeapVector<Member<ContainerNode>> m_removedChildrenNodes; | 176 HeapVector<Member<ContainerNode>> m_removedChildrenNodes; |
183 HeapVector<Member<Node>> m_removedNodes; | 177 HeapVector<Member<Node>> m_removedNodes; |
184 HeapVector<Member<const Text>> m_splitTextNodes; | 178 HeapVector<Member<const Text>> m_splitTextNodes; |
185 HeapVector<Member<UpdateCharacterDataRecord>> m_updatedCharacterDataRecords; | 179 HeapVector<Member<UpdateCharacterDataRecord>> m_updatedCharacterDataRecords; |
186 | 180 |
187 DISALLOW_COPY_AND_ASSIGN(TestSynchronousMutationObserver); | 181 DISALLOW_COPY_AND_ASSIGN(TestSynchronousMutationObserver); |
188 }; | 182 }; |
189 | 183 |
190 TestSynchronousMutationObserver::TestSynchronousMutationObserver( | 184 TestSynchronousMutationObserver::TestSynchronousMutationObserver( |
191 Document& document) { | 185 Document& document) { |
192 setContext(&document); | 186 setContext(&document); |
193 } | 187 } |
194 | 188 |
195 void TestSynchronousMutationObserver::contextDestroyed(Document*) { | 189 void TestSynchronousMutationObserver::contextDestroyed(Document*) { |
196 ++m_contextDestroyedCalledCounter; | 190 ++m_contextDestroyedCalledCounter; |
197 } | 191 } |
198 | 192 |
199 void TestSynchronousMutationObserver::didChangeAttribute( | |
200 const Element& element) { | |
201 m_attributeChangedElements.push_back(&element); | |
202 } | |
203 | |
204 void TestSynchronousMutationObserver::didChangeChildren( | 193 void TestSynchronousMutationObserver::didChangeChildren( |
205 const ContainerNode& container) { | 194 const ContainerNode& container) { |
206 m_childrenChangedNodes.push_back(&container); | 195 m_childrenChangedNodes.push_back(&container); |
207 } | 196 } |
208 | 197 |
209 void TestSynchronousMutationObserver::didMergeTextNodes( | 198 void TestSynchronousMutationObserver::didMergeTextNodes( |
210 const Text& node, | 199 const Text& node, |
211 const NodeWithIndex& nodeWithIndex, | 200 const NodeWithIndex& nodeWithIndex, |
212 unsigned offset) { | 201 unsigned offset) { |
213 m_mergeTextNodesRecords.push_back( | 202 m_mergeTextNodesRecords.push_back( |
(...skipping 21 matching lines...) Expand all Loading... |
235 void TestSynchronousMutationObserver::nodeChildrenWillBeRemoved( | 224 void TestSynchronousMutationObserver::nodeChildrenWillBeRemoved( |
236 ContainerNode& container) { | 225 ContainerNode& container) { |
237 m_removedChildrenNodes.push_back(&container); | 226 m_removedChildrenNodes.push_back(&container); |
238 } | 227 } |
239 | 228 |
240 void TestSynchronousMutationObserver::nodeWillBeRemoved(Node& node) { | 229 void TestSynchronousMutationObserver::nodeWillBeRemoved(Node& node) { |
241 m_removedNodes.push_back(&node); | 230 m_removedNodes.push_back(&node); |
242 } | 231 } |
243 | 232 |
244 DEFINE_TRACE(TestSynchronousMutationObserver) { | 233 DEFINE_TRACE(TestSynchronousMutationObserver) { |
245 visitor->trace(m_attributeChangedElements); | |
246 visitor->trace(m_childrenChangedNodes); | 234 visitor->trace(m_childrenChangedNodes); |
247 visitor->trace(m_mergeTextNodesRecords); | 235 visitor->trace(m_mergeTextNodesRecords); |
248 visitor->trace(m_moveTreeToNewDocumentNodes); | 236 visitor->trace(m_moveTreeToNewDocumentNodes); |
249 visitor->trace(m_removedChildrenNodes); | 237 visitor->trace(m_removedChildrenNodes); |
250 visitor->trace(m_removedNodes); | 238 visitor->trace(m_removedNodes); |
251 visitor->trace(m_splitTextNodes); | 239 visitor->trace(m_splitTextNodes); |
252 visitor->trace(m_updatedCharacterDataRecords); | 240 visitor->trace(m_updatedCharacterDataRecords); |
253 SynchronousMutationObserver::trace(visitor); | 241 SynchronousMutationObserver::trace(visitor); |
254 } | 242 } |
255 | 243 |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 EXPECT_EQ(1u, observer.removedNodes().size()) | 498 EXPECT_EQ(1u, observer.removedNodes().size()) |
511 << "ContainerNode::removeChildren() doesn't call nodeWillBeRemoved()"; | 499 << "ContainerNode::removeChildren() doesn't call nodeWillBeRemoved()"; |
512 ASSERT_EQ(1u, observer.removedChildrenNodes().size()); | 500 ASSERT_EQ(1u, observer.removedChildrenNodes().size()); |
513 EXPECT_EQ(divNode, observer.removedChildrenNodes()[0]); | 501 EXPECT_EQ(divNode, observer.removedChildrenNodes()[0]); |
514 | 502 |
515 document().shutdown(); | 503 document().shutdown(); |
516 EXPECT_EQ(nullptr, observer.lifecycleContext()); | 504 EXPECT_EQ(nullptr, observer.lifecycleContext()); |
517 EXPECT_EQ(1, observer.countContextDestroyedCalled()); | 505 EXPECT_EQ(1, observer.countContextDestroyedCalled()); |
518 } | 506 } |
519 | 507 |
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 | |
538 TEST_F(DocumentTest, SynchronousMutationNotifieAppendChild) { | 508 TEST_F(DocumentTest, SynchronousMutationNotifieAppendChild) { |
539 auto& observer = *new TestSynchronousMutationObserver(document()); | 509 auto& observer = *new TestSynchronousMutationObserver(document()); |
540 document().body()->appendChild(document().createTextNode("a123456789")); | 510 document().body()->appendChild(document().createTextNode("a123456789")); |
541 ASSERT_EQ(1u, observer.childrenChangedNodes().size()); | 511 ASSERT_EQ(1u, observer.childrenChangedNodes().size()); |
542 EXPECT_EQ(document().body(), observer.childrenChangedNodes()[0]); | 512 EXPECT_EQ(document().body(), observer.childrenChangedNodes()[0]); |
543 } | 513 } |
544 | 514 |
545 TEST_F(DocumentTest, SynchronousMutationNotifieInsertBefore) { | 515 TEST_F(DocumentTest, SynchronousMutationNotifieInsertBefore) { |
546 auto& observer = *new TestSynchronousMutationObserver(document()); | 516 auto& observer = *new TestSynchronousMutationObserver(document()); |
547 document().documentElement()->insertBefore( | 517 document().documentElement()->insertBefore( |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 { | 644 { |
675 setHtmlInnerHTML( | 645 setHtmlInnerHTML( |
676 "<body>" | 646 "<body>" |
677 "<meta name=\"theme-color\" content=\"#00ff00\">"); | 647 "<meta name=\"theme-color\" content=\"#00ff00\">"); |
678 EXPECT_EQ(Color(0, 255, 0), document().themeColor()) | 648 EXPECT_EQ(Color(0, 255, 0), document().themeColor()) |
679 << "Theme color should be bright green."; | 649 << "Theme color should be bright green."; |
680 } | 650 } |
681 } | 651 } |
682 | 652 |
683 } // namespace blink | 653 } // namespace blink |
OLD | NEW |