toolkit/components/downloads/test/unit/test_bug_401430.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 // This tests the "add to recent documents" functionality of the DM
     7 const nsIDownloadManager = Ci.nsIDownloadManager;
     8 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager);
    10 // Make sure Unicode is supported:
    11 // U+00E3 : LATIN SMALL LETTER A WITH TILDE
    12 // U+041B : CYRILLIC CAPITAL LETTER EL
    13 // U+3056 : HIRAGANA LETTER ZA
    14 const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc";
    16 // Milliseconds between polls.
    17 const POLL_REGISTRY_TIMEOUT = 200;
    18 // Max number of polls.
    19 const POLL_REGISTRY_MAX_LOOPS = 25;
    21 function checkResult() {
    22   // delete the saved file (this doesn't affect the "recent documents" list)
    23   var resultFile = do_get_file(resultFileName);
    24   resultFile.remove(false);
    26   // Need to poll RecentDocs value because the SHAddToRecentDocs call
    27   // doesn't update the registry immediately.
    28   do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs);
    29 }
    31 var gPollsCount = 0;
    32 function pollRecentDocs() {
    33   if (++gPollsCount > POLL_REGISTRY_MAX_LOOPS) {
    34     do_throw("Maximum time elapsed while polling RecentDocs.");
    35     do_test_finished();
    36     return;
    37   }
    39   if (checkRecentDocsFor(resultFileName)) {
    40     print("Document found in RecentDocs");
    41     do_test_finished();
    42   }
    43   else
    44     do_timeout(POLL_REGISTRY_TIMEOUT, pollRecentDocs);
    45 }
    47 function checkRecentDocsFor(aFileName) {
    48   var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"].
    49                         createInstance(Ci.nsIWindowsRegKey);
    50   var recentDocsPath =
    51         "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs";
    52   recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    53                      recentDocsPath,
    54                      Ci.nsIWindowsRegKey.ACCESS_READ);
    55   var count = recentDocsKey.valueCount;
    56   for (var i = 0; i < count; ++i) {
    57     var valueName = recentDocsKey.getValueName(i);
    58     var binValue = recentDocsKey.readBinaryValue(valueName);
    60     // "fields" in the data are separated by \0 wide characters, which are
    61     // returned as two \0 "bytes" by readBinaryValue. Use only the first field.
    62     var fileNameRaw = binValue.split("\0\0")[0];
    64     // Convert the filename from UTF-16LE.
    65     var fileName = "";
    66     for (var c = 0; c < fileNameRaw.length; c += 2)
    67       fileName += String.fromCharCode(fileNameRaw.charCodeAt(c) |
    68                                       fileNameRaw.charCodeAt(c+1) * 256);
    70     if (aFileName == fileName)
    71       return true;
    72   }
    73   return false;
    74 }
    76 var httpserv = null;
    77 function run_test()
    78 {
    79   if (oldDownloadManagerDisabled()) {
    80     return;
    81   }
    83   // This test functionality only implemented on Windows.
    84   // Is there a better way of doing this?
    85   var httpPH = Cc["@mozilla.org/network/protocol;1?name=http"].
    86                getService(Ci.nsIHttpProtocolHandler);
    87   if (httpPH.platform != "Windows")
    88     return;
    90   // Don't finish until the download is finished
    91   do_test_pending();
    93   httpserv = new HttpServer();
    94   httpserv.registerDirectory("/", do_get_cwd());
    95   httpserv.start(-1);
    97   var listener = {
    98     onDownloadStateChange: function test_401430_odsc(aState, aDownload) {
    99       if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
   100         checkResult();
   101       }
   102     },
   103     onStateChange: function(a, b, c, d, e) { },
   104     onProgressChange: function(a, b, c, d, e, f, g) { },
   105     onSecurityChange: function(a, b, c, d) { }
   106   };
   108   dm.addListener(listener);
   109   dm.addListener(getDownloadListener());
   111   // need to save the file to the CWD, because the profile dir is in $TEMP,
   112   // and Windows apparently doesn't like putting things from $TEMP into
   113   // the recent files list.
   114   var dl = addDownload(httpserv,
   115                        {resultFileName: resultFileName,
   116                         targetFile: do_get_file(resultFileName, true)});
   117 }

mercurial