summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_LoadEvents.js
blob: 8b43d8e753e57b46789cdf732ebd0b06986db38d (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
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that an iframe with the |mozbrowser| attribute emits mozbrowserloadX
// events when this page is in the whitelist.

"use strict";
SimpleTest.waitForExplicitFinish();
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addPermission();

function runTest() {
  // Load emptypage1 into the iframe, wait for that to finish loading, then
  // call runTest2.
  //
  // This should trigger loadstart, locationchange, and loadend events.

  var seenLoadEnd = false;
  var seenLoadStart = false;
  var seenLocationChange = false;

  var iframe = document.createElement('iframe');
  iframe.setAttribute('mozbrowser', 'true');
  iframe.id = 'iframe';
  iframe.src = 'http://example.com/tests/dom/browser-element/mochitest/file_browserElement_LoadEvents.html';

  function loadstart(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(!seenLoadEnd, 'loadstart before loadend.');
    ok(!seenLoadStart, 'Just one loadstart event.');
    ok(!seenLocationChange, 'loadstart before locationchange.');
    seenLoadStart = true;
  }

  function locationchange(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(!seenLocationChange, 'Just one locationchange event.');
    seenLocationChange = true;
    ok(seenLoadStart, 'Location change after load start.');
    ok(!seenLoadEnd, 'Location change before load end.');
    ok(e.detail.url, browserElementTestHelpers.emptyPage1, "event's reported location");
  }

  function loadend(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(seenLoadStart, 'loadend after loadstart.');
    ok(!seenLoadEnd, 'Just one loadend event.');
    ok(seenLocationChange, 'loadend after locationchange.');
    is(e.detail.backgroundColor, 'rgb(0, 128, 0)', 'Expected background color reported')
    seenLoadEnd = true;
  }

  iframe.addEventListener('mozbrowserloadstart', loadstart);
  iframe.addEventListener('mozbrowserlocationchange', locationchange);
  iframe.addEventListener('mozbrowserloadend', loadend);

  function waitForAllCallbacks() {
    if (!seenLoadStart || !seenLoadEnd) {
      SimpleTest.executeSoon(waitForAllCallbacks);
      return;
    }

    iframe.removeEventListener('mozbrowserloadstart', loadstart);
    iframe.removeEventListener('mozbrowserlocationchange', locationchange);
    iframe.removeEventListener('mozbrowserloadend', loadend);
    runTest2();
  }

  document.body.appendChild(iframe);
  waitForAllCallbacks();
}

function runTest2() {
  var seenLoadStart = false;
  var seenLoadEnd = false;
  var seenLocationChange = false;

  // Add this event listener to the document; the events should bubble.
  document.addEventListener('mozbrowserloadstart', function(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(!seenLoadStart, 'Just one loadstart event.');
    seenLoadStart = true;
    ok(!seenLoadEnd, 'Got mozbrowserloadstart before loadend.');
    ok(!seenLocationChange, 'Got mozbrowserloadstart before locationchange.');
  });

  var iframe = document.getElementById('iframe');
  iframe.addEventListener('mozbrowserlocationchange', function(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(!seenLocationChange, 'Just one locationchange event.');
    seenLocationChange = true;
    ok(seenLoadStart, 'Location change after load start.');
    ok(!seenLoadEnd, 'Location change before load end.');
    ok(e.detail.url, browserElementTestHelpers.emptyPage2, "event's reported location");
  });

  iframe.addEventListener('mozbrowserloadend', function(e) {
    ok(e.isTrusted, 'Event should be trusted.');
    ok(!seenLoadEnd, 'Just one load end event.');
    seenLoadEnd = true;
    ok(seenLoadStart, 'Load end after load start.');
    ok(seenLocationChange, 'Load end after location change.');
    is(e.detail.backgroundColor, 'transparent', 'Expected background color reported')
  });

  iframe.src = browserElementTestHelpers.emptyPage2;

  function waitForAllCallbacks() {
    if (!seenLoadStart || !seenLoadEnd || !seenLocationChange) {
      SimpleTest.executeSoon(waitForAllCallbacks);
      return;
    }

    SimpleTest.finish();
  }

  waitForAllCallbacks();
}

addEventListener('testready', runTest);