toolkit/mozapps/downloads/tests/chrome/test_taskbarprogress_downloadstates.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/test_taskbarprogress_downloadstates.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,243 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!-- This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +   - License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +   - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     1.8 +<!--
     1.9 + * This tests that the Windows 7 Taskbar Progress is correctly updated when
    1.10 + * the download state changes.
    1.11 +-->
    1.12 +
    1.13 +<window title="Win7 Taskbar Progress"
    1.14 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.15 +        onload="test();">
    1.16 +
    1.17 +  <script type="application/javascript"
    1.18 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.19 +  <script type="application/javascript"
    1.20 +          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.21 +  <script type="application/javascript"
    1.22 +          src="utils.js"/>
    1.23 +
    1.24 +  <script type="application/javascript">
    1.25 +  <![CDATA[
    1.26 +
    1.27 +const kTaskbarID = "@mozilla.org/windows-taskbar;1";
    1.28 +const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
    1.29 +const DLMGR_UI_DONE = "download-manager-ui-done";
    1.30 +
    1.31 +const Cu = Components.utils;
    1.32 +const nsITP = Ci.nsITaskbarProgress;
    1.33 +
    1.34 +let DownloadTaskbarProgress, dm;
    1.35 +let downloadA, downloadB, gGen;
    1.36 +
    1.37 +let testState = {
    1.38 +  activeDownloadCount: 0,
    1.39 +  pausedDownloadCount: 0,
    1.40 +  finishedDownloadCount: 0,
    1.41 +
    1.42 +  regularStateTested: false,
    1.43 +  pausedStateTested: false,
    1.44 +  noDownloadsStateTested: false
    1.45 +}
    1.46 +
    1.47 +function continueTest() {
    1.48 +  SimpleTest.executeSoon(function(){
    1.49 +    gGen.next();
    1.50 +  });
    1.51 +}
    1.52 +
    1.53 +let wasPaused = {};
    1.54 +
    1.55 +let downloadListener = {
    1.56 +
    1.57 +  onStateChange: function(a, b, c, d, e) {
    1.58 +    checkCorrectState();
    1.59 +  },
    1.60 +
    1.61 +  onDownloadStateChange: function(aState, aDownload)
    1.62 +  {
    1.63 +
    1.64 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) {
    1.65 +      wasPaused[aDownload.id] = true;
    1.66 +      testState.pausedDownloadCount++;
    1.67 +      testState.activeDownloadCount--;
    1.68 +
    1.69 +      continueTest();
    1.70 +    }
    1.71 +
    1.72 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
    1.73 +      testState.activeDownloadCount--;
    1.74 +      testState.finishedDownloadCount++;
    1.75 +      aDownload.targetFile.remove(false);
    1.76 +
    1.77 +      SimpleTest.executeSoon(checkCorrectState);
    1.78 +      if(testState.finishedDownloadCount == 2) {
    1.79 +        SimpleTest.executeSoon(finishTest);
    1.80 +      }
    1.81 +
    1.82 +    }
    1.83 +
    1.84 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) {
    1.85 +      if (!wasPaused[aDownload.id])
    1.86 +        dm.pauseDownload(aDownload.id);
    1.87 +      continueTest();
    1.88 +    }
    1.89 +
    1.90 +  },
    1.91 +
    1.92 +  onProgressChange: function(a, b, c, d, e, f, g) { },
    1.93 +  onSecurityChange: function(a, b, c, d) { }
    1.94 +};
    1.95 +
    1.96 +function testSteps() {
    1.97 +
    1.98 +  // Step 1 - Add downloads and pause
    1.99 +
   1.100 +  testState.activeDownloadCount++;
   1.101 +  downloadA = addDownload();
   1.102 +  yield; // added
   1.103 +  yield; // paused
   1.104 +
   1.105 +  testState.activeDownloadCount++;
   1.106 +  downloadB = addDownload();
   1.107 +  yield; // added
   1.108 +  yield; // paused
   1.109 +
   1.110 +  // Step 2 - Resume downloads
   1.111 + 
   1.112 +  testState.activeDownloadCount++;
   1.113 +  testState.pausedDownloadCount--;
   1.114 +  dm.resumeDownload(downloadA.id);
   1.115 +  yield; 
   1.116 +
   1.117 +  testState.activeDownloadCount++;
   1.118 +  testState.pausedDownloadCount--;
   1.119 +  dm.resumeDownload(downloadB.id);
   1.120 +  yield;
   1.121 +
   1.122 +  yield;
   1.123 +}
   1.124 +
   1.125 +function finishTest() {
   1.126 +  ok(testState.regularStateTested, "Tests went through regular download state");
   1.127 +  ok(testState.pausedStateTested, "Tests went through paused download state");
   1.128 +  ok(testState.noDownloadsStateTested, "Tests went through finished downloads state");
   1.129 +  dm.removeListener(downloadListener);
   1.130 +  gGen.close();
   1.131 +  SimpleTest.finish();
   1.132 +}
   1.133 +
   1.134 +function checkCorrectState() {
   1.135 +
   1.136 +  if(testState.activeDownloadCount < 0 || testState.pausedDownloadCount < 0) {
   1.137 +    ok(false, "There shouldn't be negative download counts");
   1.138 +    SimpleTest.finish();
   1.139 +  }
   1.140 +
   1.141 +  let taskbarState = DownloadTaskbarProgress.taskbarState;
   1.142 +
   1.143 +  if (testState.activeDownloadCount) {
   1.144 +    //There's at least one active download
   1.145 +    ok(taskbarState == nsITP.STATE_NORMAL || taskbarState == nsITP.STATE_INDETERMINATE, "Correct downloading state");
   1.146 +    testState.regularStateTested = true;
   1.147 +  } else if (testState.pausedDownloadCount) {
   1.148 +    //There are no active downloads but there are paused ones
   1.149 +    ok(taskbarState == nsITP.STATE_PAUSED, "Correct paused state");
   1.150 +    testState.pausedStateTested = true;
   1.151 +  } else {
   1.152 +    //No more downloads
   1.153 +    ok(taskbarState == nsITP.STATE_NO_PROGRESS, "Correct finished downloads state");
   1.154 +    testState.noDownloadsStateTested = true;
   1.155 +  }
   1.156 +
   1.157 +}
   1.158 +
   1.159 +function test() {
   1.160 +  testSetup();
   1.161 +}
   1.162 +
   1.163 +function testSetup()
   1.164 +{
   1.165 +  //Test setup
   1.166 +  let dmui = getDMUI();
   1.167 +  if (!dmui) {
   1.168 +    todo(false, "skip test for toolkit download manager UI");
   1.169 +    return;
   1.170 +  }
   1.171 +
   1.172 +  let isWin = /Win/.test(navigator.platform);
   1.173 +  if (isWin) {
   1.174 +    let isWin7OrHigher = false;
   1.175 +    try {
   1.176 +      let version = Cc["@mozilla.org/system-info;1"]
   1.177 +                      .getService(Ci.nsIPropertyBag2)
   1.178 +                      .getProperty("version");
   1.179 +      isWin7OrHigher = (parseFloat(version) >= 6.1);
   1.180 +    } catch (ex) { }
   1.181 +
   1.182 +    if (!isWin7OrHigher) {
   1.183 +      ok(true, "This test only runs on Windows 7 or higher");
   1.184 +      return;
   1.185 +    }
   1.186 +  }
   1.187 +
   1.188 +  let tempScope = {};
   1.189 +  Cu.import("resource://gre/modules/DownloadTaskbarProgress.jsm", tempScope);
   1.190 +  Cu.import("resource://gre/modules/Services.jsm");
   1.191 +
   1.192 +  DownloadTaskbarProgress = tempScope.DownloadTaskbarProgress;
   1.193 +  isnot(DownloadTaskbarProgress, null, "Download taskbar progress service exists");
   1.194 +  DownloadTaskbarProgress.init();
   1.195 +
   1.196 +  if (isWin) {
   1.197 +    let TaskbarService =  Cc[kTaskbarID].getService(Ci.nsIWinTaskbar);
   1.198 +    is(TaskbarService.available, true, "Taskbar Service is available");
   1.199 +  }
   1.200 +
   1.201 +  dm = Cc["@mozilla.org/download-manager;1"].
   1.202 +           getService(Ci.nsIDownloadManager);
   1.203 +
   1.204 +  // First, we clear out the database
   1.205 +  dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
   1.206 +
   1.207 +  // See if the DM is already open, and if it is, close it!
   1.208 +  let win = Services.wm.getMostRecentWindow("Download:Manager");
   1.209 +  if (win) {
   1.210 +    win.close();
   1.211 +  }
   1.212 +  let os = Services.obs;
   1.213 +  const DLMGR_UI_DONE = "download-manager-ui-done";
   1.214 +
   1.215 +  gGen = testSteps();
   1.216 +  dm.addListener(downloadListener);
   1.217 +
   1.218 +  let testObs = {
   1.219 +    observe: function(aSubject, aTopic, aData)
   1.220 +    {
   1.221 +      if (aTopic != DLMGR_UI_DONE) {
   1.222 +        return;
   1.223 +      }
   1.224 +      os.removeObserver(testObs, DLMGR_UI_DONE);
   1.225 +      continueTest();
   1.226 +    }
   1.227 +  };
   1.228 +
   1.229 +  // Register with the observer service
   1.230 +  os.addObserver(testObs, DLMGR_UI_DONE, false);
   1.231 +
   1.232 +  // Show the Download Manager UI
   1.233 +  dmui.show();
   1.234 +
   1.235 +  SimpleTest.waitForExplicitFinish();
   1.236 +}
   1.237 +
   1.238 +  ]]>
   1.239 +  </script>
   1.240 +
   1.241 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.242 +    <p id="display"></p>
   1.243 +    <div id="content" style="display:none;"></div>
   1.244 +    <pre id="test"></pre>
   1.245 +  </body>
   1.246 +</window>

mercurial