Bug 450807 - [Windows] xpcshell-tests: test_bug_401430.js fails intermittently, r=mardak
authorMarco Bonardo <mbonardo@mozilla.com>
Mon, 25 Oct 2010 18:50:22 +0200
changeset 27171 75b0292a274a703fba7231893797dd2e403d1609
parent 27170 ed4e2b396665a7f02afd53c865a5827e6e06151f
child 27172 48f46881338a036d2220770be18606bb6931c8e1
push id2544
push usersgautherie.bz@free.fr
push dateMon, 25 Oct 2010 17:12:43 +0000
reviewersmardak
bugs450807
milestone1.9.1.15pre
Bug 450807 - [Windows] xpcshell-tests: test_bug_401430.js fails intermittently, r=mardak
toolkit/components/downloads/test/unit/test_bug_401430.js
--- a/toolkit/components/downloads/test/unit/test_bug_401430.js
+++ b/toolkit/components/downloads/test/unit/test_bug_401430.js
@@ -41,23 +41,45 @@ const nsIDownloadManager = Ci.nsIDownloa
 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager);
 
 // Make sure Unicode is supported:
 // U+00E3 : LATIN SMALL LETTER A WITH TILDE
 // U+041B : CYRILLIC CAPITAL LETTER EL
 // U+3056 : HIRAGANA LETTER ZA
 const resultFileName = "test\u00e3\u041b\u3056" + Date.now() + ".doc";
 
+// Milliseconds between polls.
+const POLL_REGISTRY_TIMEOUT = 200;
+// Max number of polls.
+const POLL_REGISTRY_MAX_LOOPS = 25;
+
 function checkResult() {
   // delete the saved file (this doesn't affect the "recent documents" list)
   var resultFile = do_get_file(resultFileName);
   resultFile.remove(false);
 
-  do_check_true(checkRecentDocsFor(resultFileName));
-  do_test_finished();
+  // Need to poll RecentDocs value because the SHAddToRecentDocs call
+  // doesn't update the registry immediately.
+  do_timeout(POLL_REGISTRY_TIMEOUT, "pollRecentDocs();");
+}
+
+var gPollsCount = 0;
+function pollRecentDocs() {
+  if (++gPollsCount > POLL_REGISTRY_MAX_LOOPS) {
+    do_throw("Maximum time elapsed while polling RecentDocs.");
+    do_test_finished();
+    return;
+  }
+
+  if (checkRecentDocsFor(resultFileName)) {
+    print("Document found in RecentDocs");
+    do_test_finished();
+  }
+  else
+    do_timeout(POLL_REGISTRY_TIMEOUT, "pollRecentDocs();");
 }
 
 function checkRecentDocsFor(aFileName) {
   var recentDocsKey = Cc["@mozilla.org/windows-registry-key;1"].
                         createInstance(Ci.nsIWindowsRegKey);
   var recentDocsPath =
         "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs";
   recentDocsKey.open(Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
@@ -99,19 +121,17 @@ function run_test()
 
   httpserv = new nsHttpServer();
   httpserv.registerDirectory("/", do_get_cwd());
   httpserv.start(4444);
 
   var listener = {
     onDownloadStateChange: function test_401430_odsc(aState, aDownload) {
       if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
-        // Need to run this from a timeout, because the SHAddToRecentDocs call
-        // doesn't update the registry immediately.
-        do_timeout(1000, "checkResult();");
+        checkResult();
       }
     },
     onStateChange: function(a, b, c, d, e) { },
     onProgressChange: function(a, b, c, d, e, f, g) { },
     onSecurityChange: function(a, b, c, d) { }
   };
 
   dm.addListener(listener);