dom/browser-element/mochitest/browserElement_Titlechange.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/browser-element/mochitest/browserElement_Titlechange.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,68 @@
     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 the onmozbrowsertitlechange event works.
     1.8 +"use strict";
     1.9 +
    1.10 +SimpleTest.waitForExplicitFinish();
    1.11 +browserElementTestHelpers.setEnabledPref(true);
    1.12 +browserElementTestHelpers.addPermission();
    1.13 +
    1.14 +function runTest() {
    1.15 +  var iframe1 = document.createElement('iframe');
    1.16 +  SpecialPowers.wrap(iframe1).mozbrowser = true;
    1.17 +  document.body.appendChild(iframe1);
    1.18 +
    1.19 +  // iframe2 is a red herring; we modify its title but don't listen for
    1.20 +  // titlechanges; we want to make sure that its titlechange events aren't
    1.21 +  // picked up by the listener on iframe1.
    1.22 +  var iframe2 = document.createElement('iframe');
    1.23 +  SpecialPowers.wrap(iframe2).mozbrowser = true;
    1.24 +  document.body.appendChild(iframe2);
    1.25 +
    1.26 +  // iframe3 is another red herring.  It's not a mozbrowser, so we shouldn't
    1.27 +  // get any titlechange events on it.
    1.28 +  var iframe3 = document.createElement('iframe');
    1.29 +  document.body.appendChild(iframe3);
    1.30 +
    1.31 +  var numTitleChanges = 0;
    1.32 +
    1.33 +  iframe1.addEventListener('mozbrowsertitlechange', function(e) {
    1.34 +    // Ignore empty titles; these come from about:blank.
    1.35 +    if (e.detail == '')
    1.36 +      return;
    1.37 +
    1.38 +    numTitleChanges++;
    1.39 +
    1.40 +    if (numTitleChanges == 1) {
    1.41 +      is(e.detail, 'Title');
    1.42 +      SpecialPowers.getBrowserFrameMessageManager(iframe1)
    1.43 +                   .loadFrameScript("data:,content.document.title='New title';",
    1.44 +                                    /* allowDelayedLoad = */ false);
    1.45 +      SpecialPowers.getBrowserFrameMessageManager(iframe2)
    1.46 +                   .loadFrameScript("data:,content.document.title='BAD TITLE 2';",
    1.47 +                                    /* allowDelayedLoad = */ false);
    1.48 +    }
    1.49 +    else if (numTitleChanges == 2) {
    1.50 +      is(e.detail, 'New title');
    1.51 +      iframe1.src = 'data:text/html,<html><head><title>Title 3</title></head><body></body></html>';
    1.52 +    }
    1.53 +    else if (numTitleChanges == 3) {
    1.54 +      is(e.detail, 'Title 3');
    1.55 +      SimpleTest.finish();
    1.56 +    }
    1.57 +    else {
    1.58 +      ok(false, 'Too many titlechange events.');
    1.59 +    }
    1.60 +  });
    1.61 +
    1.62 +  iframe3.addEventListener('mozbrowsertitlechange', function(e) {
    1.63 +    ok(false, 'Should not get a titlechange event for iframe3.');
    1.64 +  });
    1.65 +
    1.66 +  iframe1.src = 'data:text/html,<html><head><title>Title</title></head><body></body></html>';
    1.67 +  iframe2.src = 'data:text/html,<html><head><title>BAD TITLE</title></head><body></body></html>';
    1.68 +  iframe3.src = 'data:text/html,<html><head><title>SHOULD NOT GET EVENT</title></head><body></body></html>';
    1.69 +}
    1.70 +
    1.71 +addEventListener('testready', runTest);

mercurial