OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013, 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 |
11 * copyright notice, this list of conditions and the following disclaimer | 11 * copyright notice, this list of conditions and the following disclaimer |
12 * in the documentation and/or other materials provided with the | 12 * in the documentation and/or other materials provided with the |
13 * distribution. | 13 * distribution. |
14 * * Neither the name of Google Inc. nor the names of its | 14 * * Neither the name of Google Inc. nor the names of its |
15 * contributors may be used to endorse or promote products derived from | 15 * contributors may be used to endorse or promote products derived from |
16 * this software without specific prior written permission. | 16 * this software without specific prior written permission. |
17 * | 17 * |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "core/dom/DocumentMarkerController.h" | 32 #include "core/dom/Document.h" |
33 | 33 |
34 #include "bindings/v8/ExceptionStatePlaceholder.h" | |
35 #include "core/dom/Document.h" | |
36 #include "core/dom/Range.h" | |
37 #include "core/dom/Text.h" | |
38 #include "core/html/HTMLElement.h" | |
39 #include "core/testing/DummyPageHolder.h" | 34 #include "core/testing/DummyPageHolder.h" |
40 #include "wtf/PassRefPtr.h" | 35 #include <gmock/gmock.h> |
41 #include "wtf/RefPtr.h" | |
42 #include "wtf/testing/WTFTestHelpers.h" | |
43 #include <gtest/gtest.h> | 36 #include <gtest/gtest.h> |
44 | 37 |
45 using namespace WebCore; | 38 using namespace WebCore; |
46 | 39 |
47 namespace { | 40 namespace { |
48 | 41 |
49 class DocumentMarkerControllerTest : public ::testing::Test { | 42 class DocumentTest : public ::testing::Test { |
50 protected: | 43 protected: |
51 virtual void SetUp() OVERRIDE; | 44 virtual void SetUp() OVERRIDE; |
52 | 45 |
53 Document& document() const { return *m_document; } | 46 Document& document() const { return m_dummyPageHolder->document(); } |
54 DocumentMarkerController& markerController() const { return *m_document->mar
kers(); } | 47 Page& page() const { return m_dummyPageHolder->page(); } |
55 | |
56 PassRefPtr<Text> createTextNode(const char*); | |
57 void markNodeContents(PassRefPtr<Node>); | |
58 void setBodyInnerHTML(const char*); | |
59 | 48 |
60 private: | 49 private: |
61 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 50 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
62 Document* m_document; | |
63 }; | 51 }; |
64 | 52 |
65 void DocumentMarkerControllerTest::SetUp() | 53 void DocumentTest::SetUp() |
66 { | 54 { |
67 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | 55 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); |
68 m_document = &m_dummyPageHolder->document(); | |
69 ASSERT(m_document); | |
70 } | 56 } |
71 | 57 |
72 PassRefPtr<Text> DocumentMarkerControllerTest::createTextNode(const char* textCo
ntents) | 58 class MockDocumentVisibilityObserver : public DocumentVisibilityObserver { |
| 59 public: |
| 60 MockDocumentVisibilityObserver(Document& document) : DocumentVisibilityObser
ver(document) { } |
| 61 |
| 62 MOCK_METHOD1(didChangeVisibilityState, void(PageVisibilityState)); |
| 63 }; |
| 64 |
| 65 TEST_F(DocumentTest, VisibilityOberver) |
73 { | 66 { |
74 return document().createTextNode(String::fromUTF8(textContents)); | 67 page().setVisibilityState(PageVisibilityStateVisible, true); // initial stat
e |
| 68 MockDocumentVisibilityObserver observer1(document()); |
| 69 |
| 70 { |
| 71 MockDocumentVisibilityObserver observer2(document()); |
| 72 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 73 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 74 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 75 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 76 ::testing::Mock::VerifyAndClearExpectations(&observer1); |
| 77 ::testing::Mock::VerifyAndClearExpectations(&observer2); |
| 78 |
| 79 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(1); |
| 80 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 81 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(1); |
| 82 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 83 page().setVisibilityState(PageVisibilityStateHidden, false); |
| 84 ::testing::Mock::VerifyAndClearExpectations(&observer1); |
| 85 ::testing::Mock::VerifyAndClearExpectations(&observer2); |
| 86 |
| 87 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 88 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 89 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 90 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 91 page().setVisibilityState(PageVisibilityStateHidden, false); |
| 92 ::testing::Mock::VerifyAndClearExpectations(&observer1); |
| 93 ::testing::Mock::VerifyAndClearExpectations(&observer2); |
| 94 |
| 95 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 96 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(1); |
| 97 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(0); |
| 98 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 99 OwnPtr<DummyPageHolder> alternatePage = DummyPageHolder::create(IntSize(
800, 600)); |
| 100 Document& alternateDocument = alternatePage->document(); |
| 101 observer2.setObservedDocument(alternateDocument); |
| 102 page().setVisibilityState(PageVisibilityStateVisible, false); |
| 103 ::testing::Mock::VerifyAndClearExpectations(&observer1); |
| 104 ::testing::Mock::VerifyAndClearExpectations(&observer2); |
| 105 |
| 106 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(1); |
| 107 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 108 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateHidde
n)).Times(1); |
| 109 EXPECT_CALL(observer2, didChangeVisibilityState(PageVisibilityStateVisib
le)).Times(0); |
| 110 observer2.setObservedDocument(document()); |
| 111 page().setVisibilityState(PageVisibilityStateHidden, false); |
| 112 ::testing::Mock::VerifyAndClearExpectations(&observer1); |
| 113 ::testing::Mock::VerifyAndClearExpectations(&observer2); |
| 114 } |
| 115 |
| 116 // observer2 destroyed |
| 117 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateHidden)).
Times(0); |
| 118 EXPECT_CALL(observer1, didChangeVisibilityState(PageVisibilityStateVisible))
.Times(1); |
| 119 page().setVisibilityState(PageVisibilityStateVisible, false); |
75 } | 120 } |
76 | 121 |
77 void DocumentMarkerControllerTest::markNodeContents(PassRefPtr<Node> node) | 122 } // unnamed namespace |
78 { | |
79 // Force renderers to be created; TextIterator, which is used in | |
80 // DocumentMarkerControllerTest::addMarker(), needs them. | |
81 document().updateLayout(); | |
82 RefPtr<Range> range = rangeOfContents(node.get()); | |
83 markerController().addMarker(range.get(), DocumentMarker::Spelling); | |
84 } | |
85 | |
86 void DocumentMarkerControllerTest::setBodyInnerHTML(const char* bodyContent) | |
87 { | |
88 document().body()->setInnerHTML(String::fromUTF8(bodyContent), ASSERT_NO_EXC
EPTION); | |
89 } | |
90 | |
91 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedMarkedByNormalize) | |
92 { | |
93 setBodyInnerHTML("<b><i>foo</i></b>"); | |
94 { | |
95 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
96 parent->appendChild(createTextNode("bar").get()); | |
97 markNodeContents(parent.get()); | |
98 EXPECT_EQ(2u, markerController().markers().size()); | |
99 parent->normalize(); | |
100 } | |
101 // No more reference to marked node. | |
102 EXPECT_EQ(1u, markerController().markers().size()); | |
103 } | |
104 | |
105 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedMarkedByRemoveChildren) | |
106 { | |
107 setBodyInnerHTML("<b><i>foo</i></b>"); | |
108 RefPtr<Element> parent = toElement(document().body()->firstChild()->firstChi
ld()); | |
109 markNodeContents(parent.get()); | |
110 EXPECT_EQ(1u, markerController().markers().size()); | |
111 parent->removeChildren(); | |
112 // No more reference to marked node. | |
113 EXPECT_EQ(0u, markerController().markers().size()); | |
114 } | |
115 | |
116 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedByRemoveMarked) | |
117 { | |
118 setBodyInnerHTML("<b><i>foo</i></b>"); | |
119 { | |
120 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
121 markNodeContents(parent); | |
122 EXPECT_EQ(1u, markerController().markers().size()); | |
123 parent->removeChild(parent->firstChild()); | |
124 } | |
125 // No more reference to marked node. | |
126 EXPECT_EQ(0u, markerController().markers().size()); | |
127 } | |
128 | |
129 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedMarkedByRemoveAncestor) | |
130 { | |
131 setBodyInnerHTML("<b><i>foo</i></b>"); | |
132 { | |
133 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
134 markNodeContents(parent); | |
135 EXPECT_EQ(1u, markerController().markers().size()); | |
136 parent->parentNode()->parentNode()->removeChild(parent->parentNode()); | |
137 } | |
138 // No more reference to marked node. | |
139 EXPECT_EQ(0u, markerController().markers().size()); | |
140 } | |
141 | |
142 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedMarkedByRemoveParent) | |
143 { | |
144 setBodyInnerHTML("<b><i>foo</i></b>"); | |
145 { | |
146 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
147 markNodeContents(parent); | |
148 EXPECT_EQ(1u, markerController().markers().size()); | |
149 parent->parentNode()->removeChild(parent.get()); | |
150 } | |
151 // No more reference to marked node. | |
152 EXPECT_EQ(0u, markerController().markers().size()); | |
153 } | |
154 | |
155 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedMarkedByReplaceChild) | |
156 { | |
157 setBodyInnerHTML("<b><i>foo</i></b>"); | |
158 { | |
159 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
160 markNodeContents(parent.get()); | |
161 EXPECT_EQ(1u, markerController().markers().size()); | |
162 parent->replaceChild(createTextNode("bar").get(), parent->firstChild()); | |
163 } | |
164 // No more reference to marked node. | |
165 EXPECT_EQ(0u, markerController().markers().size()); | |
166 } | |
167 | |
168 TEST_F(DocumentMarkerControllerTest, NodeWillBeRemovedBySetInnerHTML) | |
169 { | |
170 setBodyInnerHTML("<b><i>foo</i></b>"); | |
171 { | |
172 RefPtr<Element> parent = toElement(document().body()->firstChild()->firs
tChild()); | |
173 markNodeContents(parent); | |
174 EXPECT_EQ(1u, markerController().markers().size()); | |
175 setBodyInnerHTML(""); | |
176 } | |
177 // No more reference to marked node. | |
178 EXPECT_EQ(0u, markerController().markers().size()); | |
179 } | |
180 | |
181 } | |
OLD | NEW |