toolkit/devtools/tests/mochitest/test_eventemitter_basic.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE html>
michael@0 2 <!--
michael@0 3 Any copyright is dedicated to the Public Domain.
michael@0 4 http://creativecommons.org/publicdomain/zero/1.0/
michael@0 5 -->
michael@0 6
michael@0 7 <html>
michael@0 8
michael@0 9 <head>
michael@0 10 <meta charset="utf8">
michael@0 11 <title></title>
michael@0 12
michael@0 13 <script type="application/javascript"
michael@0 14 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
michael@0 15 <link rel="stylesheet" type="text/css"
michael@0 16 href="chrome://mochikit/content/tests/SimpleTest/test.css">
michael@0 17 </head>
michael@0 18
michael@0 19 <body>
michael@0 20
michael@0 21 <script type="application/javascript;version=1.8">
michael@0 22 "use strict";
michael@0 23
michael@0 24 const { utils: Cu } = Components;
michael@0 25 const { Promise: promise } =
michael@0 26 Cu.import("resource://gre/modules/devtools/deprecated-sync-thenables.js", {});
michael@0 27 const { EventEmitter } =
michael@0 28 Cu.import("resource://gre/modules/devtools/event-emitter.js", {});
michael@0 29 const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
michael@0 30
michael@0 31 SimpleTest.waitForExplicitFinish();
michael@0 32
michael@0 33 testEmitter();
michael@0 34 testEmitter({});
michael@0 35
michael@0 36 Task.spawn(testPromise)
michael@0 37 .then(null, ok.bind(null, false))
michael@0 38 .then(SimpleTest.finish);
michael@0 39
michael@0 40 function testEmitter(aObject) {
michael@0 41 let emitter;
michael@0 42
michael@0 43 if (aObject) {
michael@0 44 emitter = aObject;
michael@0 45 EventEmitter.decorate(emitter);
michael@0 46 } else {
michael@0 47 emitter = new EventEmitter();
michael@0 48 }
michael@0 49
michael@0 50 ok(emitter, "We have an event emitter");
michael@0 51
michael@0 52 emitter.on("next", next);
michael@0 53 emitter.emit("next", "abc", "def");
michael@0 54
michael@0 55 let beenHere1 = false;
michael@0 56 function next(eventName, str1, str2) {
michael@0 57 is(eventName, "next", "Got event");
michael@0 58 is(str1, "abc", "Argument 1 is correct");
michael@0 59 is(str2, "def", "Argument 2 is correct");
michael@0 60
michael@0 61 ok(!beenHere1, "first time in next callback");
michael@0 62 beenHere1 = true;
michael@0 63
michael@0 64 emitter.off("next", next);
michael@0 65
michael@0 66 emitter.emit("next");
michael@0 67
michael@0 68 emitter.once("onlyonce", onlyOnce);
michael@0 69
michael@0 70 emitter.emit("onlyonce");
michael@0 71 emitter.emit("onlyonce");
michael@0 72 }
michael@0 73
michael@0 74 let beenHere2 = false;
michael@0 75 function onlyOnce() {
michael@0 76 ok(!beenHere2, "\"once\" listner has been called once");
michael@0 77 beenHere2 = true;
michael@0 78 emitter.emit("onlyonce");
michael@0 79
michael@0 80 killItWhileEmitting();
michael@0 81 }
michael@0 82
michael@0 83 function killItWhileEmitting() {
michael@0 84 function c1() {
michael@0 85 ok(true, "c1 called");
michael@0 86 }
michael@0 87 function c2() {
michael@0 88 ok(true, "c2 called");
michael@0 89 emitter.off("tick", c3);
michael@0 90 }
michael@0 91 function c3() {
michael@0 92 ok(false, "c3 should not be called");
michael@0 93 }
michael@0 94 function c4() {
michael@0 95 ok(true, "c4 called");
michael@0 96 }
michael@0 97
michael@0 98 emitter.on("tick", c1);
michael@0 99 emitter.on("tick", c2);
michael@0 100 emitter.on("tick", c3);
michael@0 101 emitter.on("tick", c4);
michael@0 102
michael@0 103 emitter.emit("tick");
michael@0 104
michael@0 105 offAfterOnce();
michael@0 106 }
michael@0 107
michael@0 108 function offAfterOnce() {
michael@0 109 let enteredC1 = false;
michael@0 110
michael@0 111 function c1() {
michael@0 112 enteredC1 = true;
michael@0 113 }
michael@0 114
michael@0 115 emitter.once("oao", c1);
michael@0 116 emitter.off("oao", c1);
michael@0 117
michael@0 118 emitter.emit("oao");
michael@0 119
michael@0 120 ok(!enteredC1, "c1 should not be called");
michael@0 121 }
michael@0 122 }
michael@0 123
michael@0 124 function testPromise() {
michael@0 125 let emitter = new EventEmitter();
michael@0 126 let p = emitter.once("thing");
michael@0 127
michael@0 128 // Check that the promise is only resolved once event though we
michael@0 129 // emit("thing") more than once
michael@0 130 let firstCallbackCalled = false;
michael@0 131 let check1 = p.then(arg => {
michael@0 132 is(firstCallbackCalled, false, "first callback called only once");
michael@0 133 firstCallbackCalled = true;
michael@0 134 is(arg, "happened", "correct arg in promise");
michael@0 135 return "rval from c1";
michael@0 136 });
michael@0 137
michael@0 138 emitter.emit("thing", "happened", "ignored");
michael@0 139
michael@0 140 // Check that the promise is resolved asynchronously
michael@0 141 let secondCallbackCalled = false;
michael@0 142 let check2 = p.then(arg => {
michael@0 143 ok(true, "second callback called");
michael@0 144 is(arg, "happened", "correct arg in promise");
michael@0 145 secondCallbackCalled = true;
michael@0 146 is(arg, "happened", "correct arg in promise (a second time)");
michael@0 147 return "rval from c2";
michael@0 148 });
michael@0 149
michael@0 150 // Shouldn't call any of the above listeners
michael@0 151 emitter.emit("thing", "trashinate");
michael@0 152
michael@0 153 // Check that we can still separate events with different names
michael@0 154 // and that it works with no parameters
michael@0 155 let pfoo = emitter.once("foo");
michael@0 156 let pbar = emitter.once("bar");
michael@0 157
michael@0 158 let check3 = pfoo.then(arg => {
michael@0 159 ok(arg === undefined, "no arg for foo event");
michael@0 160 return "rval from c3";
michael@0 161 });
michael@0 162
michael@0 163 pbar.then(() => {
michael@0 164 ok(false, "pbar should not be called");
michael@0 165 });
michael@0 166
michael@0 167 emitter.emit("foo");
michael@0 168
michael@0 169 is(secondCallbackCalled, false, "second callback not called yet");
michael@0 170
michael@0 171 return promise.all([ check1, check2, check3 ]).then(args => {
michael@0 172 is(args[0], "rval from c1", "callback 1 done good");
michael@0 173 is(args[1], "rval from c2", "callback 2 done good");
michael@0 174 is(args[2], "rval from c3", "callback 3 done good");
michael@0 175 });
michael@0 176 }
michael@0 177 </script>
michael@0 178 </body>
michael@0 179 </html>

mercurial