browser/base/content/test/general/browser_bug676619.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_bug676619.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +function test () {
     1.5 +  waitForExplicitFinish();
     1.6 +
     1.7 +  var isHTTPS = false;
     1.8 +
     1.9 +  gBrowser.selectedTab = gBrowser.addTab();
    1.10 +  gBrowser.selectedBrowser.addEventListener("load", function () {
    1.11 +    if (isHTTPS) {
    1.12 +      gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true);
    1.13 +    }
    1.14 +    let doc = gBrowser.contentDocument;
    1.15 +
    1.16 +
    1.17 +    function testLocation(link, url, next) {
    1.18 +      var tabOpenListener = new TabOpenListener(url, function () {
    1.19 +          gBrowser.removeTab(this.tab);
    1.20 +      }, function () {
    1.21 +        next();
    1.22 +      });
    1.23 +
    1.24 +      doc.getElementById(link).click();
    1.25 +    }
    1.26 +
    1.27 +    function testLink(link, name, next) {
    1.28 +        addWindowListener("chrome://mozapps/content/downloads/unknownContentType.xul", function (win) {
    1.29 +            is(doc.getElementById("unload-flag").textContent, "Okay", "beforeunload shouldn't have fired");
    1.30 +            is(win.document.getElementById("location").value, name, "file name should match");
    1.31 +            win.close();
    1.32 +            next();
    1.33 +        });
    1.34 +
    1.35 +        doc.getElementById(link).click();
    1.36 +    }
    1.37 +
    1.38 +    testLink("link1", "test.txt",
    1.39 +      testLink.bind(null, "link2", "video.ogg",
    1.40 +        testLink.bind(null, "link3", "just some video",
    1.41 +          testLink.bind(null, "link4", "with-target.txt",
    1.42 +            testLink.bind(null, "link5", "javascript.txt",
    1.43 +              testLink.bind(null, "link6", "test.blob",
    1.44 +                testLocation.bind(null, "link7", "http://example.com/",
    1.45 +                  function () {
    1.46 +                    if (isHTTPS) {
    1.47 +                      gBrowser.removeCurrentTab();
    1.48 +                      finish();
    1.49 +                    } else {
    1.50 +                      // same test again with https:
    1.51 +                      isHTTPS = true;
    1.52 +                      content.location = "https://example.com:443/browser/browser/base/content/test/general/download_page.html";
    1.53 +                    }
    1.54 +                  })))))));
    1.55 +
    1.56 +  }, true);
    1.57 +
    1.58 +  content.location = "http://mochi.test:8888/browser/browser/base/content/test/general/download_page.html";
    1.59 +}
    1.60 +
    1.61 +
    1.62 +function addWindowListener(aURL, aCallback) {
    1.63 +  Services.wm.addListener({
    1.64 +    onOpenWindow: function(aXULWindow) {
    1.65 +      info("window opened, waiting for focus");
    1.66 +      Services.wm.removeListener(this);
    1.67 +
    1.68 +      var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.69 +                                .getInterface(Ci.nsIDOMWindow);
    1.70 +      waitForFocus(function() {
    1.71 +        is(domwindow.document.location.href, aURL, "should have seen the right window open");
    1.72 +        aCallback(domwindow);
    1.73 +      }, domwindow);
    1.74 +    },
    1.75 +    onCloseWindow: function(aXULWindow) { },
    1.76 +    onWindowTitleChange: function(aXULWindow, aNewTitle) { }
    1.77 +  });
    1.78 +}
    1.79 +
    1.80 +// This listens for the next opened tab and checks it is of the right url.
    1.81 +// opencallback is called when the new tab is fully loaded
    1.82 +// closecallback is called when the tab is closed
    1.83 +function TabOpenListener(url, opencallback, closecallback) {
    1.84 +  this.url = url;
    1.85 +  this.opencallback = opencallback;
    1.86 +  this.closecallback = closecallback;
    1.87 +
    1.88 +  gBrowser.tabContainer.addEventListener("TabOpen", this, false);
    1.89 +}
    1.90 +
    1.91 +TabOpenListener.prototype = {
    1.92 +  url: null,
    1.93 +  opencallback: null,
    1.94 +  closecallback: null,
    1.95 +  tab: null,
    1.96 +  browser: null,
    1.97 +
    1.98 +  handleEvent: function(event) {
    1.99 +    if (event.type == "TabOpen") {
   1.100 +      gBrowser.tabContainer.removeEventListener("TabOpen", this, false);
   1.101 +      this.tab = event.originalTarget;
   1.102 +      this.browser = this.tab.linkedBrowser;
   1.103 +      gBrowser.addEventListener("pageshow", this, false);
   1.104 +    } else if (event.type == "pageshow") {
   1.105 +      if (event.target.location.href != this.url)
   1.106 +        return;
   1.107 +      gBrowser.removeEventListener("pageshow", this, false);
   1.108 +      this.tab.addEventListener("TabClose", this, false);
   1.109 +      var url = this.browser.contentDocument.location.href;
   1.110 +      is(url, this.url, "Should have opened the correct tab");
   1.111 +      this.opencallback(this.tab, this.browser.contentWindow);
   1.112 +    } else if (event.type == "TabClose") {
   1.113 +      if (event.originalTarget != this.tab)
   1.114 +        return;
   1.115 +      this.tab.removeEventListener("TabClose", this, false);
   1.116 +      this.opencallback = null;
   1.117 +      this.tab = null;
   1.118 +      this.browser = null;
   1.119 +      // Let the window close complete
   1.120 +      executeSoon(this.closecallback);
   1.121 +      this.closecallback = null;
   1.122 +    }
   1.123 +  }
   1.124 +};

mercurial