summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/inspector-protocol/debugger/domdebugger-getEventListeners.html
blob: a6eda1f6860aeba5c9e864813a7a37993884aa73 (plain)
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
<html>
<head>
<script type="text/javascript" src="../../http/tests/inspector-protocol/inspector-protocol-test.js"></script>
<script>

function test()
{
    // A general-purpose engine for sending a sequence of protocol commands.
    // The clients provide requests and response handlers, while the engine catches
    // errors and makes sure that once there's nothing to do completeTest() is called.
    // @param step is an object with command, params and callback fields.
    function runRequestSeries(step)
    {
        processStep(step);

        function processStep(s)
        {
            try {
                processStepOrFail(s);
            } catch (e) {
                InspectorTest.log(e.stack);
                InspectorTest.completeTest();
            }
        }

        function processStepOrFail(s)
        {
            if (!s) {
                InspectorTest.completeTest();
                return;
            }
            if (!s.command) {
                // A simple loopback step.
                var next = s.callback();
                processStep(next);
                return;
            }

            var innerCallback = function(response)
            {
                if ("error" in response) {
                    InspectorTest.log(response.error.message);
                    InspectorTest.completeTest();
                    return;
                }
                var next;
                try {
                    next = s.callback(response.result);
                } catch (e) {
                    InspectorTest.log(e.stack);
                    InspectorTest.completeTest();
                    return;
                }
                processStep(next);
            }
            InspectorTest.sendCommand(s.command, s.params, innerCallback);
        }
    }

    runRequestSeries({ callback: callbackStartWindow });

    function callbackStartWindow()
    {
        var expression = "(function(){\n\
            window.addEventListener('scroll', function(){ consol.log(42) }, false);\n\
            window.addEventListener('scroll', function(){ consol.log(42) }, false);\n\
            function clickHandler(event) { console.log('click - button - bubbling (registered before attribute)'); }\n\
            window.addEventListener(\"click\", clickHandler, true);\n\
            window.addEventListener(\"hover\", function hoverHandler(event) { console.log(\"hover - button - bubbling\"); }, true);\n\
            return window;\n\
        })()";
        return { command: "Runtime.evaluate", params: {expression: expression, objectGroup: "event-listeners-test"}, callback: callbackEvalWindow };
    }
    function callbackEvalWindow(result)
    {
        var id = result.result.objectId;
        if (id === undefined)
            throw new Error("objectId is expected");
        return {
            command: "DOMDebugger.getEventListeners", params: {objectId: id}, callback: callbackListenersWindow
        };
    }
    function callbackListenersWindow(result)
    {
        logGetListenersResult("window", result);
        return {callback: calbackStartDivWithListeners};
    }

    function calbackStartDivWithListeners()
    {
        var expression = "(function(){\n\
            var div = document.getElementById(\"listeners1\");\n\
            function clickHandler(event) { console.log('click - button - bubbling (registered before attribute)'); }\n\
            div.addEventListener(\"click\", clickHandler, true);\n\
            div.addEventListener(\"hover\", function hoverHandler(event) { console.log(\"hover - button - bubbling\"); }, true);\n\
            return div;\n\
        })()";
        return { command: "Runtime.evaluate", params: {expression: expression, objectGroup: "event-listeners-test"}, callback: callbackEvalDivWithListeners };
    }
    function callbackEvalDivWithListeners(result)
    {
        var id = result.result.objectId;
        if (id === undefined)
            throw new Error("objectId is expected");
        return {
            command: "DOMDebugger.getEventListeners", params: {objectId: id}, callback: callbackListenersDivWithListeners
        };
    }
    function callbackListenersDivWithListeners(result)
    {
        logGetListenersResult("div#listeners1", result);
        return {callback: calbackStartDivWithoutListeners};
    }

    function calbackStartDivWithoutListeners()
    {
        var expression = "(function(){\n\
            return document.getElementById(\"listeners2\");\n\
        })()";
        return { command: "Runtime.evaluate", params: {expression: expression, objectGroup: "event-listeners-test"}, callback: callbackEvalDivWithoutListeners };
    }
    function callbackEvalDivWithoutListeners(result)
    {
        var id = result.result.objectId;
        if (id === undefined)
            throw new Error("objectId is expected");
        return {
            command: "DOMDebugger.getEventListeners", params: {objectId: id}, callback: callbackListenersDivWithoutListeners
        };
    }
    function callbackListenersDivWithoutListeners(result)
    {
        logGetListenersResult("div#listeners2", result);
    }

    function logGetListenersResult(title, protocolResult)
    {
        InspectorTest.log("Event listeners of " + title);
        var listenersArray = protocolResult.listeners;
        listenersArray.sort(TypedThingComparator);
        for (var i = 0; i < listenersArray.length; i++) {
            var l = listenersArray[i];
            InspectorTest.log("  type:" + l.type);
            InspectorTest.log("  useCapture:" + l.useCapture);
            InspectorTest.log("  lineNumber:" + l.location.lineNumber);
            InspectorTest.log("  columnNumber:" + l.location.columnNumber);
            if (l.handler) {
                InspectorTest.log("  handler.type:" + l.handler.type);
                InspectorTest.log("  handler.className:" + l.handler.className);
                InspectorTest.log("  handler.description:" + l.handler.description.replace(/(\r\n|\n|\r)/gm,""));
            }
            InspectorTest.log("");
        }
        InspectorTest.log("");
        function TypedThingComparator(o1, o2)
        {
            return o1.type === o2.type ? 0 : (o1.type < o2.type ? -1 : 1);
        }
    }
}
</script>
</head>
<div id="listeners1" onload="return 42;"></div>
<div id="listeners2"></div>
<body onLoad="runTest();">
</body>
</html>