1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "base/file_path.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebEvent.h"
#include "third_party/WebKit/WebKit/chromium/public/WebEventListener.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebMutationEvent.h"
#include "third_party/WebKit/WebKit/chromium/public/WebScriptSource.h"
#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
#include "webkit/tools/test_shell/test_shell.h"
#include "webkit/tools/test_shell/test_shell_request_context.h"
#include "webkit/tools/test_shell/test_shell_test.h"
namespace {
using namespace WebKit;
// This test exercices the event listener API from the WebKit API.
class WebEventListenerTest : public TestShellTest {
public:
virtual void SetUp() {
TestShellTest::SetUp();
FilePath event_test_file;
ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &event_test_file));
event_test_file = event_test_file.Append(FILE_PATH_LITERAL("webkit"))
.Append(FILE_PATH_LITERAL("data"))
.Append(FILE_PATH_LITERAL("listener"))
.Append(FILE_PATH_LITERAL("mutation_event_listener.html"));
test_shell_->LoadFile(event_test_file);
test_shell_->WaitTestFinished();
}
virtual void TearDown() {
TestShellTest::TearDown();
}
WebFrame* main_frame() const {
return test_shell_->webView()->mainFrame();
}
WebDocument document() const {
return main_frame()->document();
}
void ExecuteScript(const char* code) {
main_frame()->executeScript(WebScriptSource(WebString::fromUTF8(code)));
}
static WebString GetNodeID(const WebNode& node) {
if (node.nodeType() != WebNode::ElementNode)
return WebString();
WebElement element = node.toConst<WebElement>();
return element.getAttribute("id");
}
};
class TestWebEventListener : public WebEventListener {
public:
TestWebEventListener() {}
virtual ~TestWebEventListener() {}
virtual void handleEvent(const WebEvent& event) {
events_.push_back(event);
}
size_t event_count() const { return events_.size(); }
WebEvent GetEventAt(int index) const { return events_.at(index); }
void ClearEvents() { events_.clear(); }
private:
std::vector<WebEvent> events_;
};
// Tests that the right mutation events are fired when a node is added/removed.
// Note that the DOMSubtreeModified event is fairly vage, it only tells you
// something changed for the target node.
TEST_F(WebEventListenerTest, NodeAddedRemovedMutationEvent) {
TestWebEventListener event_listener;
document().addEventListener("DOMSubtreeModified", &event_listener, false);
// Test adding a node.
ExecuteScript("addElement('newNode')");
ASSERT_EQ(1U, event_listener.event_count());
WebEvent event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
// No need to check any of the MutationEvent, WebKit does not set any.
EXPECT_EQ("DIV", event.target().nodeName());
EXPECT_EQ("topDiv", GetNodeID(event.target()));
event_listener.ClearEvents();
// Test removing a node.
ExecuteScript("removeNode('div1')");
ASSERT_EQ(1U, event_listener.event_count());
event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
EXPECT_EQ("DIV", event.target().nodeName());
EXPECT_EQ("topDiv", GetNodeID(event.target()));
}
// Tests the right mutation event is fired when a text node is modified.
TEST_F(WebEventListenerTest, TextNodeModifiedMutationEvent) {
TestWebEventListener event_listener;
document().addEventListener("DOMSubtreeModified", &event_listener, false);
ExecuteScript("changeText('div2', 'Hello')");
ASSERT_EQ(1U, event_listener.event_count());
WebEvent event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
ASSERT_EQ(WebNode::TextNode, event.target().nodeType());
}
// Tests the right mutation events are fired when an attribute is added/removed.
TEST_F(WebEventListenerTest, AttributeMutationEvent) {
TestWebEventListener event_listener;
document().addEventListener("DOMSubtreeModified", &event_listener, false);
ExecuteScript("document.getElementById('div2').setAttribute('myAttr',"
"'some value')");
ASSERT_EQ(1U, event_listener.event_count());
WebEvent event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
EXPECT_EQ("DIV", event.target().nodeName());
EXPECT_EQ("div2", GetNodeID(event.target()));
event_listener.ClearEvents();
ExecuteScript("document.getElementById('div2').removeAttribute('myAttr')");
ASSERT_EQ(1U, event_listener.event_count());
event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
EXPECT_EQ("DIV", event.target().nodeName());
EXPECT_EQ("div2", GetNodeID(event.target()));
}
// Tests destroying WebEventListener and triggering events, we shouldn't crash.
TEST_F(WebEventListenerTest, FireEventDeletedListener) {
TestWebEventListener* event_listener = new TestWebEventListener();
document().addEventListener("DOMSubtreeModified", event_listener, false);
delete event_listener;
ExecuteScript("addElement('newNode')"); // That should fire an event.
}
// Tests registering several events on the same WebEventListener and triggering
// events.
TEST_F(WebEventListenerTest, SameListenerMultipleEvents) {
TestWebEventListener event_listener;
const WebString kDOMSubtreeModifiedType("DOMSubtreeModified");
const WebString kDOMNodeRemovedType("DOMNodeRemoved");
document().addEventListener(kDOMSubtreeModifiedType, &event_listener, false);
WebElement div1_elem = document().getElementById("div1");
div1_elem.addEventListener(kDOMNodeRemovedType, &event_listener, false);
// Trigger a DOMSubtreeModified event by adding a node.
ExecuteScript("addElement('newNode')");
ASSERT_EQ(1U, event_listener.event_count());
WebEvent event = event_listener.GetEventAt(0);
ASSERT_TRUE(event.isMutationEvent());
EXPECT_EQ("DIV", event.target().nodeName());
EXPECT_EQ("topDiv", GetNodeID(event.target()));
event_listener.ClearEvents();
// Trigger for both event listener by removing the div1 node.
ExecuteScript("removeNode('div1')");
ASSERT_EQ(2U, event_listener.event_count());
// Not sure if the order of the events is important. Assuming no specific
// order.
WebString type1 = event_listener.GetEventAt(0).type();
WebString type2 = event_listener.GetEventAt(1).type();
EXPECT_TRUE(type1 == kDOMSubtreeModifiedType || type1 == kDOMNodeRemovedType);
EXPECT_TRUE(type2 == kDOMSubtreeModifiedType || type2 == kDOMNodeRemovedType);
EXPECT_TRUE(type1 != type2);
}
// Tests removing event listeners.
TEST_F(WebEventListenerTest, RemoveEventListener) {
TestWebEventListener event_listener;
const WebString kDOMSubtreeModifiedType("DOMSubtreeModified");
// Adding twice the same listener for the same event, should be supported.
document().addEventListener(kDOMSubtreeModifiedType, &event_listener, false);
document().addEventListener(kDOMSubtreeModifiedType, &event_listener, false);
// Add a node, that should trigger 2 events.
ExecuteScript("addElement('newNode')");
EXPECT_EQ(2U, event_listener.event_count());
event_listener.ClearEvents();
// Remove one listener and trigger an event again.
document().removeEventListener(kDOMSubtreeModifiedType,
&event_listener, false);
ExecuteScript("addElement('newerNode')");
EXPECT_EQ(1U, event_listener.event_count());
event_listener.ClearEvents();
// Remove the last listener and trigger yet another event.
document().removeEventListener(kDOMSubtreeModifiedType,
&event_listener, false);
ExecuteScript("addElement('newererNode')");
EXPECT_EQ(0U, event_listener.event_count());
}
} // namespace
|