Upstream version 7.35.138.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / test / data / eventapi / main.js
1 var assert = xwalk.app.test.assert;
2 var dummy_called = false;
3 var eventListenerTest = function(resolve) {
4   function dummy(arg1, arg2) {
5     dummy_called = true;
6   };
7
8   onMockEvent = new xwalk.app.events.Event("onMockEvent");
9   var onMockEvent2 = new xwalk.app.events.Event("onMockEvent");
10
11   // Add/remove listener.
12   onMockEvent.addListener(dummy);
13   assert(onMockEvent.hasListener(dummy));
14   assert(onMockEvent.hasListeners());
15   onMockEvent.removeListener(dummy);
16   assert(!onMockEvent.hasListeners());
17
18   // Register a named event twice should fail.
19   onMockEvent.addListener(dummy);
20   try {
21     onMockEvent2.addListener(dummy);
22     assert(false);
23   } catch (e) {
24     assert(e.message.search("already registered") >= 0);
25   }
26
27   resolve();
28 };
29
30 var eventDispatchTest = function(resolve) {
31   function bad() {
32     assert(false); 
33   };
34
35   function foo(arg1, arg2) {
36     assert(dummy_called === true);
37     assert(arg1 === 1234);
38     assert(arg2 === false);
39     resolve();
40   };
41
42   // The "bad" handler will triggers exception which catched by
43   // Event.dispatchEvent.
44   onMockEvent.addListener(bad);
45   onMockEvent.addListener(foo);
46
47   // Send a notification here to make sure the event listeners are set before
48   // event sending.
49   xwalk.app.test.notifyPass();
50 };
51
52 var tests = [
53   eventListenerTest,
54   eventDispatchTest,
55 ];
56
57 // Wait at most 10 seconds before sending TIMEOUT fail. When multiple browser
58 // tests running parallelly the finish time may longer than expected.
59 xwalk.app.test.runTests(tests, 10000);