dom/browser-element/mochitest/browserElement_LoadEvents.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/browserElement_LoadEvents.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* Any copyright is dedicated to the public domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +// Test that an iframe with the |mozbrowser| attribute emits mozbrowserloadX
     1.8 +// events when this page is in the whitelist.
     1.9 +
    1.10 +"use strict";
    1.11 +SimpleTest.waitForExplicitFinish();
    1.12 +browserElementTestHelpers.setEnabledPref(true);
    1.13 +browserElementTestHelpers.addPermission();
    1.14 +
    1.15 +function runTest() {
    1.16 +  // Load emptypage1 into the iframe, wait for that to finish loading, then
    1.17 +  // call runTest2.
    1.18 +  //
    1.19 +  // This should trigger loadstart, locationchange, and loadend events.
    1.20 +
    1.21 +  var seenLoadEnd = false;
    1.22 +  var seenLoadStart = false;
    1.23 +  var seenLocationChange = false;
    1.24 +
    1.25 +  var iframe = document.createElement('iframe');
    1.26 +  SpecialPowers.wrap(iframe).mozbrowser = true;
    1.27 +  iframe.id = 'iframe';
    1.28 +  iframe.src = 'http://example.com/tests/dom/browser-element/mochitest/file_browserElement_LoadEvents.html';
    1.29 +
    1.30 +  function loadstart(e) {
    1.31 +    ok(e.isTrusted, 'Event should be trusted.');
    1.32 +    ok(!seenLoadEnd, 'loadstart before loadend.');
    1.33 +    ok(!seenLoadStart, 'Just one loadstart event.');
    1.34 +    ok(!seenLocationChange, 'loadstart before locationchange.');
    1.35 +    seenLoadStart = true;
    1.36 +  }
    1.37 +
    1.38 +  function locationchange(e) {
    1.39 +    ok(e.isTrusted, 'Event should be trusted.');
    1.40 +    ok(!seenLocationChange, 'Just one locationchange event.');
    1.41 +    seenLocationChange = true;
    1.42 +    ok(seenLoadStart, 'Location change after load start.');
    1.43 +    ok(!seenLoadEnd, 'Location change before load end.');
    1.44 +    ok(e.detail, browserElementTestHelpers.emptyPage1, "event's reported location");
    1.45 +  }
    1.46 +
    1.47 +  function loadend(e) {
    1.48 +    ok(e.isTrusted, 'Event should be trusted.');
    1.49 +    ok(seenLoadStart, 'loadend after loadstart.');
    1.50 +    ok(!seenLoadEnd, 'Just one loadend event.');
    1.51 +    ok(seenLocationChange, 'loadend after locationchange.');
    1.52 +    is(e.detail.backgroundColor, 'rgb(0, 128, 0)', 'Expected background color reported')
    1.53 +    seenLoadEnd = true;
    1.54 +  }
    1.55 +
    1.56 +  iframe.addEventListener('mozbrowserloadstart', loadstart);
    1.57 +  iframe.addEventListener('mozbrowserlocationchange', locationchange);
    1.58 +  iframe.addEventListener('mozbrowserloadend', loadend);
    1.59 +
    1.60 +  function waitForAllCallbacks() {
    1.61 +    if (!seenLoadStart || !seenLoadEnd) {
    1.62 +      SimpleTest.executeSoon(waitForAllCallbacks);
    1.63 +      return;
    1.64 +    }
    1.65 +
    1.66 +    iframe.removeEventListener('mozbrowserloadstart', loadstart);
    1.67 +    iframe.removeEventListener('mozbrowserlocationchange', locationchange);
    1.68 +    iframe.removeEventListener('mozbrowserloadend', loadend);
    1.69 +    runTest2();
    1.70 +  }
    1.71 +
    1.72 +  document.body.appendChild(iframe);
    1.73 +  waitForAllCallbacks();
    1.74 +}
    1.75 +
    1.76 +function runTest2() {
    1.77 +  var seenLoadStart = false;
    1.78 +  var seenLoadEnd = false;
    1.79 +  var seenLocationChange = false;
    1.80 +
    1.81 +  // Add this event listener to the document; the events should bubble.
    1.82 +  document.addEventListener('mozbrowserloadstart', function(e) {
    1.83 +    ok(e.isTrusted, 'Event should be trusted.');
    1.84 +    ok(!seenLoadStart, 'Just one loadstart event.');
    1.85 +    seenLoadStart = true;
    1.86 +    ok(!seenLoadEnd, 'Got mozbrowserloadstart before loadend.');
    1.87 +    ok(!seenLocationChange, 'Got mozbrowserloadstart before locationchange.');
    1.88 +  });
    1.89 +
    1.90 +  var iframe = document.getElementById('iframe');
    1.91 +  iframe.addEventListener('mozbrowserlocationchange', function(e) {
    1.92 +    ok(e.isTrusted, 'Event should be trusted.');
    1.93 +    ok(!seenLocationChange, 'Just one locationchange event.');
    1.94 +    seenLocationChange = true;
    1.95 +    ok(seenLoadStart, 'Location change after load start.');
    1.96 +    ok(!seenLoadEnd, 'Location change before load end.');
    1.97 +    ok(e.detail, browserElementTestHelpers.emptyPage2, "event's reported location");
    1.98 +  });
    1.99 +
   1.100 +  iframe.addEventListener('mozbrowserloadend', function(e) {
   1.101 +    ok(e.isTrusted, 'Event should be trusted.');
   1.102 +    ok(!seenLoadEnd, 'Just one load end event.');
   1.103 +    seenLoadEnd = true;
   1.104 +    ok(seenLoadStart, 'Load end after load start.');
   1.105 +    ok(seenLocationChange, 'Load end after location change.');
   1.106 +    is(e.detail.backgroundColor, 'transparent', 'Expected background color reported')
   1.107 +  });
   1.108 +
   1.109 +  iframe.src = browserElementTestHelpers.emptyPage2;
   1.110 +
   1.111 +  function waitForAllCallbacks() {
   1.112 +    if (!seenLoadStart || !seenLoadEnd || !seenLocationChange) {
   1.113 +      SimpleTest.executeSoon(waitForAllCallbacks);
   1.114 +      return;
   1.115 +    }
   1.116 +
   1.117 +    SimpleTest.finish();
   1.118 +  }
   1.119 +
   1.120 +  waitForAllCallbacks();
   1.121 +}
   1.122 +
   1.123 +addEventListener('testready', runTest);

mercurial