summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/http/tests/websocket/websocket-event-target.html
blob: 373592935ca15cdb878e54ea9a5d01f061c240fc (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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script type="text/javascript">
description("Make sure WebSocket object acts as EventTarget.");

var ws = new WebSocket("ws://127.0.0.1:8880/simple");
ws.close(); // That is not a test with a WebSocket server, prevent logging an error to console.
var open_event_handled = false;
var message_event_handled = false;
var close_event_handled = false;
function openListener() {
    open_event_handled = true;
};
function messageListener() {
    message_event_handled = true;
};
function closeListener() {
    close_event_handled = true;
}
ws.addEventListener('open', openListener, false);
ws.addEventListener('message', messageListener, false);
ws.addEventListener('close', closeListener, false);

shouldBe("open_event_handled", "false");
shouldBe("message_event_handled", "false");
shouldBe("close_event_handled", "false");

var evt = document.createEvent("Events");
evt.initEvent("open", true, false);
ws.dispatchEvent(evt);
shouldBe("open_event_handled", "true");

open_event_handled = false;
ws.removeEventListener('open', openListener);
ws.dispatchEvent(evt);
shouldBe("open_event_handled", "false");

evt = document.createEvent("MessageEvent");
evt.initEvent("message", true, false);
ws.dispatchEvent(evt);
shouldBe("message_event_handled", "true");

message_event_handled = false;
ws.removeEventListener('message', messageListener);
ws.dispatchEvent(evt);
shouldBe("message_event_handled", "false");

evt = document.createEvent("Events");
evt.initEvent("close", true, false);
ws.dispatchEvent(evt);
shouldBe("close_event_handled", "true");

close_event_handled = false;
ws.removeEventListener('close', closeListener);
ws.dispatchEvent(evt);
shouldBe("close_event_handled", "false");

</script>
</body>
</html>