|
1 /* Any copyright is dedicated to the public domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 // Test that an iframe with the |mozbrowser| attribute emits mozbrowserloadX |
|
5 // events when this page is in the whitelist. |
|
6 |
|
7 "use strict"; |
|
8 SimpleTest.waitForExplicitFinish(); |
|
9 browserElementTestHelpers.setEnabledPref(true); |
|
10 browserElementTestHelpers.addPermission(); |
|
11 |
|
12 function runTest() { |
|
13 // Load emptypage1 into the iframe, wait for that to finish loading, then |
|
14 // call runTest2. |
|
15 // |
|
16 // This should trigger loadstart, locationchange, and loadend events. |
|
17 |
|
18 var seenLoadEnd = false; |
|
19 var seenLoadStart = false; |
|
20 var seenLocationChange = false; |
|
21 |
|
22 var iframe = document.createElement('iframe'); |
|
23 SpecialPowers.wrap(iframe).mozbrowser = true; |
|
24 iframe.id = 'iframe'; |
|
25 iframe.src = 'http://example.com/tests/dom/browser-element/mochitest/file_browserElement_LoadEvents.html'; |
|
26 |
|
27 function loadstart(e) { |
|
28 ok(e.isTrusted, 'Event should be trusted.'); |
|
29 ok(!seenLoadEnd, 'loadstart before loadend.'); |
|
30 ok(!seenLoadStart, 'Just one loadstart event.'); |
|
31 ok(!seenLocationChange, 'loadstart before locationchange.'); |
|
32 seenLoadStart = true; |
|
33 } |
|
34 |
|
35 function locationchange(e) { |
|
36 ok(e.isTrusted, 'Event should be trusted.'); |
|
37 ok(!seenLocationChange, 'Just one locationchange event.'); |
|
38 seenLocationChange = true; |
|
39 ok(seenLoadStart, 'Location change after load start.'); |
|
40 ok(!seenLoadEnd, 'Location change before load end.'); |
|
41 ok(e.detail, browserElementTestHelpers.emptyPage1, "event's reported location"); |
|
42 } |
|
43 |
|
44 function loadend(e) { |
|
45 ok(e.isTrusted, 'Event should be trusted.'); |
|
46 ok(seenLoadStart, 'loadend after loadstart.'); |
|
47 ok(!seenLoadEnd, 'Just one loadend event.'); |
|
48 ok(seenLocationChange, 'loadend after locationchange.'); |
|
49 is(e.detail.backgroundColor, 'rgb(0, 128, 0)', 'Expected background color reported') |
|
50 seenLoadEnd = true; |
|
51 } |
|
52 |
|
53 iframe.addEventListener('mozbrowserloadstart', loadstart); |
|
54 iframe.addEventListener('mozbrowserlocationchange', locationchange); |
|
55 iframe.addEventListener('mozbrowserloadend', loadend); |
|
56 |
|
57 function waitForAllCallbacks() { |
|
58 if (!seenLoadStart || !seenLoadEnd) { |
|
59 SimpleTest.executeSoon(waitForAllCallbacks); |
|
60 return; |
|
61 } |
|
62 |
|
63 iframe.removeEventListener('mozbrowserloadstart', loadstart); |
|
64 iframe.removeEventListener('mozbrowserlocationchange', locationchange); |
|
65 iframe.removeEventListener('mozbrowserloadend', loadend); |
|
66 runTest2(); |
|
67 } |
|
68 |
|
69 document.body.appendChild(iframe); |
|
70 waitForAllCallbacks(); |
|
71 } |
|
72 |
|
73 function runTest2() { |
|
74 var seenLoadStart = false; |
|
75 var seenLoadEnd = false; |
|
76 var seenLocationChange = false; |
|
77 |
|
78 // Add this event listener to the document; the events should bubble. |
|
79 document.addEventListener('mozbrowserloadstart', function(e) { |
|
80 ok(e.isTrusted, 'Event should be trusted.'); |
|
81 ok(!seenLoadStart, 'Just one loadstart event.'); |
|
82 seenLoadStart = true; |
|
83 ok(!seenLoadEnd, 'Got mozbrowserloadstart before loadend.'); |
|
84 ok(!seenLocationChange, 'Got mozbrowserloadstart before locationchange.'); |
|
85 }); |
|
86 |
|
87 var iframe = document.getElementById('iframe'); |
|
88 iframe.addEventListener('mozbrowserlocationchange', function(e) { |
|
89 ok(e.isTrusted, 'Event should be trusted.'); |
|
90 ok(!seenLocationChange, 'Just one locationchange event.'); |
|
91 seenLocationChange = true; |
|
92 ok(seenLoadStart, 'Location change after load start.'); |
|
93 ok(!seenLoadEnd, 'Location change before load end.'); |
|
94 ok(e.detail, browserElementTestHelpers.emptyPage2, "event's reported location"); |
|
95 }); |
|
96 |
|
97 iframe.addEventListener('mozbrowserloadend', function(e) { |
|
98 ok(e.isTrusted, 'Event should be trusted.'); |
|
99 ok(!seenLoadEnd, 'Just one load end event.'); |
|
100 seenLoadEnd = true; |
|
101 ok(seenLoadStart, 'Load end after load start.'); |
|
102 ok(seenLocationChange, 'Load end after location change.'); |
|
103 is(e.detail.backgroundColor, 'transparent', 'Expected background color reported') |
|
104 }); |
|
105 |
|
106 iframe.src = browserElementTestHelpers.emptyPage2; |
|
107 |
|
108 function waitForAllCallbacks() { |
|
109 if (!seenLoadStart || !seenLoadEnd || !seenLocationChange) { |
|
110 SimpleTest.executeSoon(waitForAllCallbacks); |
|
111 return; |
|
112 } |
|
113 |
|
114 SimpleTest.finish(); |
|
115 } |
|
116 |
|
117 waitForAllCallbacks(); |
|
118 } |
|
119 |
|
120 addEventListener('testready', runTest); |