File: EventListenerTest.cpp

package info (click to toggle)
qtwebkit 2.3.4.dfsg-9.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 290,564 kB
  • ctags: 273,642
  • sloc: cpp: 1,417,509; python: 85,048; ansic: 39,357; perl: 38,862; ruby: 10,313; objc: 9,505; xml: 8,679; asm: 3,864; yacc: 2,458; sh: 1,237; lex: 813; makefile: 592; java: 228; php: 79
file content (252 lines) | stat: -rw-r--r-- 9,348 bytes parent folder | download | duplicates (3)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "FrameTestHelpers.h"
#include "URLTestHelpers.h"
#include "WebDOMEvent.h"
#include "WebDOMEventListener.h"
#include "WebDOMMutationEvent.h"
#include "WebDocument.h"
#include "WebElement.h"
#include "WebFrame.h"
#include "WebScriptSource.h"
#include "WebView.h"
#include <gtest/gtest.h>
#include <webkit/support/webkit_support.h>

using namespace WebKit;

namespace {

class TestWebDOMEventListener : public WebDOMEventListener {
public:
    TestWebDOMEventListener() { }
    virtual ~TestWebDOMEventListener() { }

    virtual void handleEvent(const WebDOMEvent& event) OVERRIDE
    {
      m_events.push_back(event);
    }

    size_t eventCount() const { return m_events.size(); }

    WebDOMEvent eventAt(int index) const { return m_events.at(index); }

    void clearEvents() { m_events.clear(); }

private:
    std::vector<WebDOMEvent> m_events;
};

class WebDOMEventListenerTest : public testing::Test {
public:
    WebDOMEventListenerTest()
        : m_webView(0)
    {
    }

    virtual void SetUp() OVERRIDE
    {
        std::string baseURL("http://www.example.com/");
        std::string fileName("listener/mutation_event_listener.html");
        bool executeScript = true;
        URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(baseURL.c_str()), WebString::fromUTF8(fileName.c_str()));
        m_webView = FrameTestHelpers::createWebViewAndLoad(baseURL + fileName, executeScript);
    }

    virtual void TearDown() OVERRIDE
    {
        m_webView->close();
        webkit_support::UnregisterAllMockedURLs();
    }

    WebFrame* mainFrame() const
    {
        return m_webView->mainFrame();
    }

    WebDocument document() const
    {
        return mainFrame()->document();
    }

    void executeScript(const char* code)
    {
        mainFrame()->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");
    }

protected:
    WebView* m_webView;
};


// 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(WebDOMEventListenerTest, NodeAddedRemovedMutationEvent)
{
    TestWebDOMEventListener eventListener;
    document().addEventListener("DOMSubtreeModified", &eventListener, false);

    // Test adding a node.
    executeScript("addElement('newNode')");
    ASSERT_EQ(1U, eventListener.eventCount());
    WebDOMEvent event = eventListener.eventAt(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()));
    eventListener.clearEvents();

    // Test removing a node.
    executeScript("removeNode('div1')");
    ASSERT_EQ(1U, eventListener.eventCount());
    event = eventListener.eventAt(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(WebDOMEventListenerTest, TextNodeModifiedMutationEvent)
{
    TestWebDOMEventListener eventListener;
    document().addEventListener("DOMSubtreeModified", &eventListener, false);
    executeScript("changeText('div2', 'Hello')");
    ASSERT_EQ(1U, eventListener.eventCount());
    WebDOMEvent event = eventListener.eventAt(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(WebDOMEventListenerTest, AttributeMutationEvent)
{
    TestWebDOMEventListener eventListener;
    document().addEventListener("DOMSubtreeModified", &eventListener, false);
    executeScript("document.getElementById('div2').setAttribute('myAttr',"
                  "'some value')");
    ASSERT_EQ(1U, eventListener.eventCount());
    WebDOMEvent event = eventListener.eventAt(0);
    ASSERT_TRUE(event.isMutationEvent());
    EXPECT_EQ("DIV", event.target().nodeName());
    EXPECT_EQ("div2", GetNodeID(event.target()));
    eventListener.clearEvents();

    executeScript("document.getElementById('div2').removeAttribute('myAttr')");
    ASSERT_EQ(1U, eventListener.eventCount());
    event = eventListener.eventAt(0);
    ASSERT_TRUE(event.isMutationEvent());
    EXPECT_EQ("DIV", event.target().nodeName());
    EXPECT_EQ("div2", GetNodeID(event.target()));
}

// Tests destroying WebDOMEventListener and triggering events, we shouldn't
// crash.
TEST_F(WebDOMEventListenerTest, FireEventDeletedListener)
{
    TestWebDOMEventListener* eventListener = new TestWebDOMEventListener();
    document().addEventListener("DOMSubtreeModified", eventListener, false);
    delete eventListener;
    executeScript("addElement('newNode')"); // That should fire an event.
}

// Tests registering several events on the same WebDOMEventListener and
// triggering events.
TEST_F(WebDOMEventListenerTest, SameListenerMultipleEvents)
{
    TestWebDOMEventListener eventListener;
    const WebString kDOMSubtreeModifiedType("DOMSubtreeModified");
    const WebString kDOMNodeRemovedType("DOMNodeRemoved");
    document().addEventListener(kDOMSubtreeModifiedType, &eventListener, false);
    WebElement div1Elem = document().getElementById("div1");
    div1Elem.addEventListener(kDOMNodeRemovedType, &eventListener, false);

    // Trigger a DOMSubtreeModified event by adding a node.
    executeScript("addElement('newNode')");
    ASSERT_EQ(1U, eventListener.eventCount());
    WebDOMEvent event = eventListener.eventAt(0);
    ASSERT_TRUE(event.isMutationEvent());
    EXPECT_EQ("DIV", event.target().nodeName());
    EXPECT_EQ("topDiv", GetNodeID(event.target()));
    eventListener.clearEvents();

    // Trigger for both event listener by removing the div1 node.
    executeScript("removeNode('div1')");
    ASSERT_EQ(2U, eventListener.eventCount());
    // Not sure if the order of the events is important. Assuming no specific
    // order.
    WebString type1 = eventListener.eventAt(0).type();
    WebString type2 = eventListener.eventAt(1).type();
    EXPECT_TRUE(type1 == kDOMSubtreeModifiedType || type1 == kDOMNodeRemovedType);
    EXPECT_TRUE(type2 == kDOMSubtreeModifiedType || type2 == kDOMNodeRemovedType);
    EXPECT_TRUE(type1 != type2);
}

// Tests removing event listeners.
TEST_F(WebDOMEventListenerTest, RemoveEventListener)
{
    TestWebDOMEventListener eventListener;
    const WebString kDOMSubtreeModifiedType("DOMSubtreeModified");
    // Adding twice the same listener for the same event, should be supported.
    document().addEventListener(kDOMSubtreeModifiedType, &eventListener, false);
    document().addEventListener(kDOMSubtreeModifiedType, &eventListener, false);

    // Add a node, that should trigger 2 events.
    executeScript("addElement('newNode')");
    EXPECT_EQ(2U, eventListener.eventCount());
    eventListener.clearEvents();

    // Remove one listener and trigger an event again.
    document().removeEventListener(
        kDOMSubtreeModifiedType, &eventListener, false);
    executeScript("addElement('newerNode')");
    EXPECT_EQ(1U, eventListener.eventCount());
    eventListener.clearEvents();

    // Remove the last listener and trigger yet another event.
    document().removeEventListener(
        kDOMSubtreeModifiedType, &eventListener, false);
    executeScript("addElement('newererNode')");
    EXPECT_EQ(0U, eventListener.eventCount());
}

} // namespace