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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 - License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
michael@0 5 <!--
michael@0 6 * This tests that the Windows 7 Taskbar Progress is correctly updated when
michael@0 7 * the download state changes.
michael@0 8 -->
michael@0 9
michael@0 10 <window title="Win7 Taskbar Progress"
michael@0 11 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 12 onload="test();">
michael@0 13
michael@0 14 <script type="application/javascript"
michael@0 15 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 16 <script type="application/javascript"
michael@0 17 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
michael@0 18 <script type="application/javascript"
michael@0 19 src="utils.js"/>
michael@0 20
michael@0 21 <script type="application/javascript">
michael@0 22 <![CDATA[
michael@0 23
michael@0 24 const kTaskbarID = "@mozilla.org/windows-taskbar;1";
michael@0 25 const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
michael@0 26 const DLMGR_UI_DONE = "download-manager-ui-done";
michael@0 27
michael@0 28 const Cu = Components.utils;
michael@0 29 const nsITP = Ci.nsITaskbarProgress;
michael@0 30
michael@0 31 let DownloadTaskbarProgress, dm;
michael@0 32 let downloadA, downloadB, gGen;
michael@0 33
michael@0 34 let testState = {
michael@0 35 activeDownloadCount: 0,
michael@0 36 pausedDownloadCount: 0,
michael@0 37 finishedDownloadCount: 0,
michael@0 38
michael@0 39 regularStateTested: false,
michael@0 40 pausedStateTested: false,
michael@0 41 noDownloadsStateTested: false
michael@0 42 }
michael@0 43
michael@0 44 function continueTest() {
michael@0 45 SimpleTest.executeSoon(function(){
michael@0 46 gGen.next();
michael@0 47 });
michael@0 48 }
michael@0 49
michael@0 50 let wasPaused = {};
michael@0 51
michael@0 52 let downloadListener = {
michael@0 53
michael@0 54 onStateChange: function(a, b, c, d, e) {
michael@0 55 checkCorrectState();
michael@0 56 },
michael@0 57
michael@0 58 onDownloadStateChange: function(aState, aDownload)
michael@0 59 {
michael@0 60
michael@0 61 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) {
michael@0 62 wasPaused[aDownload.id] = true;
michael@0 63 testState.pausedDownloadCount++;
michael@0 64 testState.activeDownloadCount--;
michael@0 65
michael@0 66 continueTest();
michael@0 67 }
michael@0 68
michael@0 69 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
michael@0 70 testState.activeDownloadCount--;
michael@0 71 testState.finishedDownloadCount++;
michael@0 72 aDownload.targetFile.remove(false);
michael@0 73
michael@0 74 SimpleTest.executeSoon(checkCorrectState);
michael@0 75 if(testState.finishedDownloadCount == 2) {
michael@0 76 SimpleTest.executeSoon(finishTest);
michael@0 77 }
michael@0 78
michael@0 79 }
michael@0 80
michael@0 81 if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) {
michael@0 82 if (!wasPaused[aDownload.id])
michael@0 83 dm.pauseDownload(aDownload.id);
michael@0 84 continueTest();
michael@0 85 }
michael@0 86
michael@0 87 },
michael@0 88
michael@0 89 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 90 onSecurityChange: function(a, b, c, d) { }
michael@0 91 };
michael@0 92
michael@0 93 function testSteps() {
michael@0 94
michael@0 95 // Step 1 - Add downloads and pause
michael@0 96
michael@0 97 testState.activeDownloadCount++;
michael@0 98 downloadA = addDownload();
michael@0 99 yield; // added
michael@0 100 yield; // paused
michael@0 101
michael@0 102 testState.activeDownloadCount++;
michael@0 103 downloadB = addDownload();
michael@0 104 yield; // added
michael@0 105 yield; // paused
michael@0 106
michael@0 107 // Step 2 - Resume downloads
michael@0 108
michael@0 109 testState.activeDownloadCount++;
michael@0 110 testState.pausedDownloadCount--;
michael@0 111 dm.resumeDownload(downloadA.id);
michael@0 112 yield;
michael@0 113
michael@0 114 testState.activeDownloadCount++;
michael@0 115 testState.pausedDownloadCount--;
michael@0 116 dm.resumeDownload(downloadB.id);
michael@0 117 yield;
michael@0 118
michael@0 119 yield;
michael@0 120 }
michael@0 121
michael@0 122 function finishTest() {
michael@0 123 ok(testState.regularStateTested, "Tests went through regular download state");
michael@0 124 ok(testState.pausedStateTested, "Tests went through paused download state");
michael@0 125 ok(testState.noDownloadsStateTested, "Tests went through finished downloads state");
michael@0 126 dm.removeListener(downloadListener);
michael@0 127 gGen.close();
michael@0 128 SimpleTest.finish();
michael@0 129 }
michael@0 130
michael@0 131 function checkCorrectState() {
michael@0 132
michael@0 133 if(testState.activeDownloadCount < 0 || testState.pausedDownloadCount < 0) {
michael@0 134 ok(false, "There shouldn't be negative download counts");
michael@0 135 SimpleTest.finish();
michael@0 136 }
michael@0 137
michael@0 138 let taskbarState = DownloadTaskbarProgress.taskbarState;
michael@0 139
michael@0 140 if (testState.activeDownloadCount) {
michael@0 141 //There's at least one active download
michael@0 142 ok(taskbarState == nsITP.STATE_NORMAL || taskbarState == nsITP.STATE_INDETERMINATE, "Correct downloading state");
michael@0 143 testState.regularStateTested = true;
michael@0 144 } else if (testState.pausedDownloadCount) {
michael@0 145 //There are no active downloads but there are paused ones
michael@0 146 ok(taskbarState == nsITP.STATE_PAUSED, "Correct paused state");
michael@0 147 testState.pausedStateTested = true;
michael@0 148 } else {
michael@0 149 //No more downloads
michael@0 150 ok(taskbarState == nsITP.STATE_NO_PROGRESS, "Correct finished downloads state");
michael@0 151 testState.noDownloadsStateTested = true;
michael@0 152 }
michael@0 153
michael@0 154 }
michael@0 155
michael@0 156 function test() {
michael@0 157 testSetup();
michael@0 158 }
michael@0 159
michael@0 160 function testSetup()
michael@0 161 {
michael@0 162 //Test setup
michael@0 163 let dmui = getDMUI();
michael@0 164 if (!dmui) {
michael@0 165 todo(false, "skip test for toolkit download manager UI");
michael@0 166 return;
michael@0 167 }
michael@0 168
michael@0 169 let isWin = /Win/.test(navigator.platform);
michael@0 170 if (isWin) {
michael@0 171 let isWin7OrHigher = false;
michael@0 172 try {
michael@0 173 let version = Cc["@mozilla.org/system-info;1"]
michael@0 174 .getService(Ci.nsIPropertyBag2)
michael@0 175 .getProperty("version");
michael@0 176 isWin7OrHigher = (parseFloat(version) >= 6.1);
michael@0 177 } catch (ex) { }
michael@0 178
michael@0 179 if (!isWin7OrHigher) {
michael@0 180 ok(true, "This test only runs on Windows 7 or higher");
michael@0 181 return;
michael@0 182 }
michael@0 183 }
michael@0 184
michael@0 185 let tempScope = {};
michael@0 186 Cu.import("resource://gre/modules/DownloadTaskbarProgress.jsm", tempScope);
michael@0 187 Cu.import("resource://gre/modules/Services.jsm");
michael@0 188
michael@0 189 DownloadTaskbarProgress = tempScope.DownloadTaskbarProgress;
michael@0 190 isnot(DownloadTaskbarProgress, null, "Download taskbar progress service exists");
michael@0 191 DownloadTaskbarProgress.init();
michael@0 192
michael@0 193 if (isWin) {
michael@0 194 let TaskbarService = Cc[kTaskbarID].getService(Ci.nsIWinTaskbar);
michael@0 195 is(TaskbarService.available, true, "Taskbar Service is available");
michael@0 196 }
michael@0 197
michael@0 198 dm = Cc["@mozilla.org/download-manager;1"].
michael@0 199 getService(Ci.nsIDownloadManager);
michael@0 200
michael@0 201 // First, we clear out the database
michael@0 202 dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
michael@0 203
michael@0 204 // See if the DM is already open, and if it is, close it!
michael@0 205 let win = Services.wm.getMostRecentWindow("Download:Manager");
michael@0 206 if (win) {
michael@0 207 win.close();
michael@0 208 }
michael@0 209 let os = Services.obs;
michael@0 210 const DLMGR_UI_DONE = "download-manager-ui-done";
michael@0 211
michael@0 212 gGen = testSteps();
michael@0 213 dm.addListener(downloadListener);
michael@0 214
michael@0 215 let testObs = {
michael@0 216 observe: function(aSubject, aTopic, aData)
michael@0 217 {
michael@0 218 if (aTopic != DLMGR_UI_DONE) {
michael@0 219 return;
michael@0 220 }
michael@0 221 os.removeObserver(testObs, DLMGR_UI_DONE);
michael@0 222 continueTest();
michael@0 223 }
michael@0 224 };
michael@0 225
michael@0 226 // Register with the observer service
michael@0 227 os.addObserver(testObs, DLMGR_UI_DONE, false);
michael@0 228
michael@0 229 // Show the Download Manager UI
michael@0 230 dmui.show();
michael@0 231
michael@0 232 SimpleTest.waitForExplicitFinish();
michael@0 233 }
michael@0 234
michael@0 235 ]]>
michael@0 236 </script>
michael@0 237
michael@0 238 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 239 <p id="display"></p>
michael@0 240 <div id="content" style="display:none;"></div>
michael@0 241 <pre id="test"></pre>
michael@0 242 </body>
michael@0 243 </window>

mercurial