toolkit/mozapps/downloads/tests/chrome/test_bug_412360.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.

     1 <?xml version="1.0"?>
     2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     3    - License, v. 2.0. If a copy of the MPL was not distributed with this
     4    - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     5 <!--
     6  * This tests bug 412360, which caused problems when trying to save javascript
     7  * URIs.  This is not an xpcshell unit test because it triggers an assertion,
     8  * which causes xpcshell test cases to fail.
     9 -->
    11 <window title="Download Manager Test"
    12         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    13         onload="test();">
    15   <script type="application/javascript"
    16           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    17   <script type="application/javascript"
    18           src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    19   <script type="application/javascript"
    20           src="utils.js"/>
    22   <script type="application/javascript">
    23   <![CDATA[
    25 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    27 let didFail = false;
    28 var file;
    30 let prefBranch = Cc["@mozilla.org/preferences-service;1"].
    31                  getService(Ci.nsIPrefBranch);
    33 let factory = {
    34   createInstance: function(aOuter, aIid) {
    35     if (aOuter != null)
    36       throw Components.results.NS_ERROR_NO_AGGREGATION;
    37     return promptService.QueryInterface(aIid);
    38   }
    39 };
    41 let promptService = {
    42   QueryInterface: XPCOMUtils.generateQI([Ci.nsIPromptService]),
    43   alert: function() {
    44     ok(didFail, "javascript: uri failed and showed a message");
    45     file.remove(false);
    47     cleanUpFactory();
    48     SimpleTest.finish();
    49   }
    50 };
    52 Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
    53 registerFactory(Components.ID("{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}"),
    54   "PromptService", "@mozilla.org/embedcomp/prompt-service;1",
    55   factory
    56 );
    58 function cleanUpFactory() {
    59     // Unregister the factory so we do not leak
    60     Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
    61     unregisterFactory(
    62       Components.ID("{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}"),
    63       factory
    64     );
    65 }
    67 function test()
    68 {
    69   var dmui = getDMUI();
    70   if (!dmui) {
    71     cleanUpFactory();
    72     todo(false, "skip test for toolkit download manager UI");
    73     return;
    74   }
    76   let dm = Cc["@mozilla.org/download-manager;1"].
    77            getService(Ci.nsIDownloadManager);
    78   let db = dm.DBConnection;
    80   // Empty any old downloads
    81   db.executeSimpleSQL("DELETE FROM moz_downloads");
    83   let stmt = db.createStatement(
    84     "INSERT INTO moz_downloads (source, target, state, endTime) " +
    85     "VALUES (?1, ?2, ?3, ?4)");
    87   try {
    88     // Saving javascript URIs doesn't work
    89     stmt.bindStringParameter(0, "javascript:5");
    91     // Download to a temp local file
    92     file = Cc["@mozilla.org/file/directory_service;1"].
    93            getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    94     file.append("javascriptURI");
    95     file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    96     stmt.bindStringParameter(1, Cc["@mozilla.org/network/io-service;1"].
    97       getService(Ci.nsIIOService).newFileURI(file).spec);
    99     // Start it as canceled
   100     stmt.bindInt32Parameter(2, dm.DOWNLOAD_CANCELED);
   102     stmt.bindInt32Parameter(3, Date.now() * 1000);
   104     // Add it!
   105     stmt.execute();
   106   }
   107   finally {
   108     stmt.finalize();
   109   }
   111   let listener = {
   112     onDownloadStateChange: function(aState, aDownload)
   113     {
   114       switch (aDownload.state) {
   115         case dm.DOWNLOAD_FAILED:
   116           // Remember that we failed for the prompt service
   117           didFail = true;
   119           ok(true, "javascript: uri failed instead of getting stuck");
   120           dm.removeListener(listener);
   121           break;
   122       }
   123     }
   124   };
   125   dm.addListener(listener);
   127   // Close the UI if necessary
   128   let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   129            getService(Ci.nsIWindowMediator);
   130   let win = wm.getMostRecentWindow("Download:Manager");
   131   if (win) win.close();
   134   let os = Cc["@mozilla.org/observer-service;1"].
   135            getService(Ci.nsIObserverService);
   136   const DLMGR_UI_DONE = "download-manager-ui-done";
   138   let testObs = {
   139     observe: function(aSubject, aTopic, aData)
   140     {
   141       if (aTopic != DLMGR_UI_DONE)
   142         return;
   144       os.removeObserver(testObs, DLMGR_UI_DONE);
   146       SimpleTest.waitForFocus(function () {
   147         let win = aSubject;
   149         // Down arrow to select the download
   150         synthesizeKey("VK_DOWN", {}, win);
   152         // Enter key to retry the download
   153         synthesizeKey("VK_RETURN", {}, win);
   154       }, aSubject);
   155     }
   156   };
   158   // Register with the observer service
   159   os.addObserver(testObs, DLMGR_UI_DONE, false);
   161   // Show the Download Manager UI
   162   dmui.show();
   164   SimpleTest.waitForExplicitFinish();
   165 }
   167   ]]>
   168   </script>
   170   <body xmlns="http://www.w3.org/1999/xhtml">
   171     <p id="display"></p>
   172     <div id="content" style="display:none;"></div>
   173     <pre id="test"></pre>
   174   </body>
   175 </window>

mercurial