PageRenderTime 22ms CodeModel.GetById 18ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
Unknown | 272 lines | 219 code | 53 blank | 0 comment | 0 complexity | b0ebc7f708022b7bfd86de68071ad6a5 MD5 | raw file
  1<?xml version="1.0"?>
  2<!--
  3/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is Windows Download Taskbar Progress.
 17 *
 18 * The Initial Developer of the Original Code is
 19 * the Mozilla Foundation.
 20 * Portions created by the Initial Developer are Copyright (C) 2010
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *   Felipe Gomes <felipc@gmail.com>
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40/**
 41 * This tests that the Windows 7 Taskbar Progress is correctly updated when
 42 * the download state changes.
 43 */
 44-->
 45
 46<window title="Win7 Taskbar Progress"
 47        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
 48        onload="test();">
 49
 50  <script type="application/javascript"
 51          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
 52  <script type="application/javascript"
 53          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
 54  <script type="application/javascript"
 55          src="utils.js"/>
 56
 57  <script type="application/javascript">
 58  <![CDATA[
 59
 60const kTaskbarID = "@mozilla.org/windows-taskbar;1";
 61const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
 62const DLMGR_UI_DONE = "download-manager-ui-done";
 63
 64const Cu = Components.utils;
 65const nsITP = Ci.nsITaskbarProgress;
 66
 67let DownloadTaskbarProgress, dm;
 68let downloadA, downloadB, gGen;
 69
 70let testState = {
 71  activeDownloadCount: 0,
 72  pausedDownloadCount: 0,
 73  finishedDownloadCount: 0,
 74
 75  regularStateTested: false,
 76  pausedStateTested: false,
 77  noDownloadsStateTested: false
 78}
 79
 80function continueTest() {
 81  SimpleTest.executeSoon(function(){
 82    gGen.next();
 83  });
 84}
 85
 86let wasPaused = {};
 87
 88let downloadListener = {
 89
 90  onStateChange: function(a, b, c, d, e) {
 91    checkCorrectState();
 92  },
 93
 94  onDownloadStateChange: function(aState, aDownload)
 95  {
 96
 97    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) {
 98      wasPaused[aDownload.id] = true;
 99      testState.pausedDownloadCount++;
100      testState.activeDownloadCount--;
101
102      continueTest();
103    }
104
105    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
106      testState.activeDownloadCount--;
107      testState.finishedDownloadCount++;
108      aDownload.targetFile.remove(false);
109
110      SimpleTest.executeSoon(checkCorrectState);
111      if(testState.finishedDownloadCount == 2) {
112        SimpleTest.executeSoon(finishTest);
113      }
114
115    }
116
117    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) {
118      if (!wasPaused[aDownload.id])
119        dm.pauseDownload(aDownload.id);
120      continueTest();
121    }
122
123  },
124
125  onProgressChange: function(a, b, c, d, e, f, g) { },
126  onSecurityChange: function(a, b, c, d) { }
127};
128
129function testSteps() {
130
131  // Step 1 - Add downloads and pause
132
133  testState.activeDownloadCount++;
134  downloadA = addDownload();
135  yield; // added
136  yield; // paused
137
138  testState.activeDownloadCount++;
139  downloadB = addDownload();
140  yield; // added
141  yield; // paused
142
143  // Step 2 - Resume downloads
144 
145  testState.activeDownloadCount++;
146  testState.pausedDownloadCount--;
147  dm.resumeDownload(downloadA.id);
148  yield; 
149
150  testState.activeDownloadCount++;
151  testState.pausedDownloadCount--;
152  dm.resumeDownload(downloadB.id);
153  yield;
154
155  yield;
156}
157
158function finishTest() {
159  ok(testState.regularStateTested, "Tests went through regular download state");
160  ok(testState.pausedStateTested, "Tests went through paused download state");
161  ok(testState.noDownloadsStateTested, "Tests went through finished downloads state");
162  dm.removeListener(downloadListener);
163  gGen.close();
164  SimpleTest.finish();
165}
166
167function checkCorrectState() {
168
169  if(testState.activeDownloadCount < 0 || testState.pausedDownloadCount < 0) {
170    ok(false, "There shouldn't be negative download counts");
171    SimpleTest.finish();
172  }
173
174  let taskbarState = DownloadTaskbarProgress.taskbarState;
175
176  if (testState.activeDownloadCount) {
177    //There's at least one active download
178    ok(taskbarState == nsITP.STATE_NORMAL || taskbarState == nsITP.STATE_INDETERMINATE, "Correct downloading state");
179    testState.regularStateTested = true;
180  } else if (testState.pausedDownloadCount) {
181    //There are no active downloads but there are paused ones
182    ok(taskbarState == nsITP.STATE_PAUSED, "Correct paused state");
183    testState.pausedStateTested = true;
184  } else {
185    //No more downloads
186    ok(taskbarState == nsITP.STATE_NO_PROGRESS, "Correct finished downloads state");
187    testState.noDownloadsStateTested = true;
188  }
189
190}
191
192function test() {
193  testSetup();
194}
195
196function testSetup()
197{
198  //Test setup
199  let dmui = getDMUI();
200  if (!dmui) {
201    todo(false, "skip test for toolkit download manager UI");
202    return;
203  }
204
205  let isWin7OrHigher = false;
206  try {
207    let version = Cc["@mozilla.org/system-info;1"]
208                    .getService(Ci.nsIPropertyBag2)
209                    .getProperty("version");
210    isWin7OrHigher = (parseFloat(version) >= 6.1);
211  } catch (ex) { }
212
213  if (!isWin7OrHigher) {
214    return;
215  }
216
217  let tempScope = {};
218  Cu.import("resource://gre/modules/DownloadTaskbarProgress.jsm", tempScope);
219  Cu.import("resource://gre/modules/Services.jsm");
220
221  DownloadTaskbarProgress = tempScope.DownloadTaskbarProgress;
222  let TaskbarService =  Cc[kTaskbarID].getService(Ci.nsIWinTaskbar);
223
224  isnot(DownloadTaskbarProgress, null, "Download taskbar progress service exists");
225  is(TaskbarService.available, true, "Taskbar Service is available");
226
227  dm = Cc["@mozilla.org/download-manager;1"].
228           getService(Ci.nsIDownloadManager);
229
230  // First, we clear out the database
231  dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
232
233  // See if the DM is already open, and if it is, close it!
234  let win = Services.wm.getMostRecentWindow("Download:Manager");
235  if (win) {
236    win.close();
237  }
238  let os = Services.obs;
239  const DLMGR_UI_DONE = "download-manager-ui-done";
240
241  gGen = testSteps();
242  dm.addListener(downloadListener);
243
244  let testObs = {
245    observe: function(aSubject, aTopic, aData)
246    {
247      if (aTopic != DLMGR_UI_DONE) {
248        return;
249      }
250      os.removeObserver(testObs, DLMGR_UI_DONE);
251      continueTest();
252    }
253  };
254
255  // Register with the observer service
256  os.addObserver(testObs, DLMGR_UI_DONE, false);
257
258  // Show the Download Manager UI
259  dmui.show();
260
261  SimpleTest.waitForExplicitFinish();
262}
263
264  ]]>
265  </script>
266
267  <body xmlns="http://www.w3.org/1999/xhtml">
268    <p id="display"></p>
269    <div id="content" style="display:none;"></div>
270    <pre id="test"></pre>
271  </body>
272</window>