toolkit/components/downloads/test/browser/browser_download_history.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/browser/browser_download_history.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function test() {
     1.9 +  try {
    1.10 +    if (Services.prefs.getBoolPref("browser.download.useJSTransfer")) {
    1.11 +      return;
    1.12 +    }
    1.13 +  } catch (ex) { }
    1.14 +
    1.15 +  waitForExplicitFinish();
    1.16 +
    1.17 +  let privateWin = null;
    1.18 +  let itemCount = 0;
    1.19 +  let sourceURL =
    1.20 +    "http://example.org/tests/toolkit/components/downloads/test/browser/download.html";
    1.21 +  let linkURL =
    1.22 +    "http://mochi.test:8888/tests/toolkit/components/downloads/test/browser/download.test";
    1.23 +  let linkURI = Services.io.newURI(linkURL, null, null);
    1.24 +  let downloadDialogURL =
    1.25 +    "chrome://mozapps/content/downloads/unknownContentType.xul";
    1.26 +
    1.27 +  let downloadListener = {
    1.28 +    onDownloadStateChange: function(aState, aDownload) {
    1.29 +      switch (aDownload.state) {
    1.30 +        case Services.downloads.DOWNLOAD_FINISHED:
    1.31 +          info("Download finished");
    1.32 +          Services.downloads.removeListener(downloadListener);
    1.33 +          executeSoon(function() { checkDownload(aDownload); });
    1.34 +          break;
    1.35 +      }
    1.36 +    },
    1.37 +    onStateChange: function(a, b, c, d, e) { },
    1.38 +    onProgressChange: function(a, b, c, d, e, f, g) { },
    1.39 +    onSecurityChange: function(a, b, c, d) { }
    1.40 +  };
    1.41 +
    1.42 +  let historyObserver = {
    1.43 +    onVisit: function (aURI, aVisitID, aTime, aSessionID, aReferringID, aTransitionType) {
    1.44 +      ok(false, "Download should not fired a visit notification: " + aURI.spec);
    1.45 +    },
    1.46 +    onBeginUpdateBatch: function () {},
    1.47 +    onEndUpdateBatch: function () {},
    1.48 +    onTitleChanged: function () {},
    1.49 +    onBeforeDeleteURI: function () {},
    1.50 +    onDeleteURI: function () {},
    1.51 +    onClearHistory: function () {},
    1.52 +    onPageChanged: function () {},
    1.53 +    onDeleteVisits: function () {},
    1.54 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
    1.55 +  };
    1.56 +
    1.57 +  let windowListener = {
    1.58 +    onOpenWindow: function(aXULWindow) {
    1.59 +      info("Window opened");
    1.60 +      Services.wm.removeListener(windowListener);
    1.61 +
    1.62 +      let domWindow =
    1.63 +        aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor).
    1.64 +          getInterface(Ci.nsIDOMWindow);
    1.65 +      waitForFocus(function() {
    1.66 +        is(domWindow.document.location.href, downloadDialogURL,
    1.67 +           "Should have seen the right window open");
    1.68 +
    1.69 +        executeSoon(function() {
    1.70 +          let button = domWindow.document.documentElement.getButton("accept");
    1.71 +          button.disabled = false;
    1.72 +          domWindow.document.documentElement.acceptDialog();
    1.73 +        });
    1.74 +      }, domWindow);
    1.75 +    },
    1.76 +    onCloseWindow: function(aXULWindow) {},
    1.77 +    onWindowTitleChange: function(aXULWindow, aNewTitle) {}
    1.78 +  };
    1.79 +
    1.80 +  registerCleanupFunction(function() {
    1.81 +    privateWin.close();
    1.82 +    Services.prefs.clearUserPref("browser.download.manager.showAlertOnComplete");
    1.83 +  });
    1.84 +
    1.85 +  function getHistoryItemCount() {
    1.86 +    let options = PlacesUtils.history.getNewQueryOptions();
    1.87 +    options.includeHidden = true;
    1.88 +    options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
    1.89 +    let query = PlacesUtils.history.getNewQuery();
    1.90 +    let root = PlacesUtils.history.executeQuery(query, options).root;
    1.91 +    root.containerOpen = true;
    1.92 +    let cc = root.childCount;
    1.93 +    root.containerOpen = false;
    1.94 +    return cc;
    1.95 +  }
    1.96 +
    1.97 +  function whenNewWindowLoaded(aIsPrivate, aCallback) {
    1.98 +    let win = OpenBrowserWindow({private: aIsPrivate});
    1.99 +    win.addEventListener("load", function onLoad() {
   1.100 +      win.removeEventListener("load", onLoad, false);
   1.101 +      executeSoon(function() aCallback(win));
   1.102 +    }, false);
   1.103 +  }
   1.104 +
   1.105 +  function whenPageLoad(aWin, aURL, aCallback) {
   1.106 +    let browser = aWin.gBrowser.selectedBrowser;
   1.107 +    browser.addEventListener("load", function onLoad() {
   1.108 +      browser.removeEventListener("load", onLoad, true);
   1.109 +      executeSoon(function() aCallback(browser.contentDocument));
   1.110 +    }, true);
   1.111 +    browser.loadURI(aURL);
   1.112 +  }
   1.113 +
   1.114 +  function checkDownload(aDownload) {
   1.115 +    PlacesUtils.history.removeObserver(historyObserver);
   1.116 +    ok(aDownload.isPrivate, "Download should be private");
   1.117 +    is(getHistoryItemCount(), itemCount,
   1.118 +       "History items count should not change after a download");
   1.119 +    PlacesUtils.asyncHistory.isURIVisited(linkURI, function(aURI, aIsVisited) {
   1.120 +      is(aIsVisited, false, "Download source should not be set as visited");
   1.121 +      // Clean up
   1.122 +      if (aDownload.targetFile.exists()) {
   1.123 +        aDownload.targetFile.remove(false);
   1.124 +      }
   1.125 +      waitForFocus(function() {
   1.126 +        if (privateWin.DownloadsPanel.isPanelShowing) {
   1.127 +          privateWin.DownloadsPanel.hidePanel();
   1.128 +        }
   1.129 +        finish();
   1.130 +      }, privateWin);
   1.131 +    });
   1.132 +  }
   1.133 +
   1.134 +  // Disable alert service notifications
   1.135 +  Services.prefs.setBoolPref("browser.download.manager.showAlertOnComplete", false);
   1.136 +
   1.137 +  whenNewWindowLoaded(true, function(win) {
   1.138 +    info("Start listeners");
   1.139 +    privateWin = win;
   1.140 +    Services.wm.addListener(windowListener);
   1.141 +    Services.downloads.addPrivacyAwareListener(downloadListener);
   1.142 +    PlacesUtils.history.addObserver(historyObserver, false);
   1.143 +    info("Load test page");
   1.144 +    whenPageLoad(win, sourceURL, function(doc) {
   1.145 +      info("Start download");
   1.146 +      itemCount = getHistoryItemCount();
   1.147 +      doc.getElementById("download-link").click();
   1.148 +    });
   1.149 +  });
   1.150 +}

mercurial