Fix up a test draft
authorMike Conley <mconley@mozilla.com>
Tue, 24 May 2016 18:38:41 -0400
changeset 374663 892e0a1f3d5267e2a4b79bd40df24131104324da
parent 374662 452ffef8af4ca48191c1900667d415c51c7cc4ee
child 374664 e5aecb65f3108f601691f6b5bf1491c2dbe4a063
push id20055
push usermconley@mozilla.com
push dateThu, 02 Jun 2016 19:00:42 +0000
milestone49.0a1
Fix up a test MozReview-Commit-ID: 6S64VvZpwDf
browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js
--- a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js
+++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_downloadLastDir_toggle.js
@@ -1,97 +1,80 @@
-/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+Cu.import("resource://gre/modules/FileUtils.jsm");
+Cu.import("resource://gre/modules/DownloadLastDir.jsm");
 
-function test() {
-  waitForExplicitFinish();
-
-  let FileUtils =
-    Cu.import("resource://gre/modules/FileUtils.jsm", {}).FileUtils;
-  let DownloadLastDir =
-    Cu.import("resource://gre/modules/DownloadLastDir.jsm", {}).DownloadLastDir;
-
+/**
+ * TODO: Document test
+ */
+add_task(function* test_downloads_last_dir_toggle() {
   let tmpDir = FileUtils.getDir("TmpD", [], true);
   let dir1 = newDirectory();
 
   registerCleanupFunction(function () {
     Services.prefs.clearUserPref("browser.download.lastDir");
     dir1.remove(true);
   });
 
-  function testOnWindow(aPrivate, aCallback) {
-    whenNewWindowLoaded({private: aPrivate}, function(win) {
-      let gDownloadLastDir = new DownloadLastDir(win);
-      aCallback(win, gDownloadLastDir);
-      gDownloadLastDir.cleanupPrivateFile();
-      win.close();
-    });
-  }
-
-  function checkDownloadLastDirInit(aWin, gDownloadLastDir, aCallback) {
-    is(typeof gDownloadLastDir, "object",
-       "gDownloadLastDir should be a valid object");
-    is(gDownloadLastDir.file, null,
-       "gDownloadLastDir.file should be null to start with");
+  let win = yield BrowserTestUtils.openNewBrowserWindow();
+  let gDownloadLastDir = new DownloadLastDir(win);
+  is(typeof gDownloadLastDir, "object",
+     "gDownloadLastDir should be a valid object");
+  is(gDownloadLastDir.file, null,
+     "gDownloadLastDir.file should be null to start with");
 
-    gDownloadLastDir.file = tmpDir;
-    is(gDownloadLastDir.file.path, tmpDir.path,
-       "LastDir should point to the temporary directory");
-    isnot(gDownloadLastDir.file, tmpDir,
-          "gDownloadLastDir.file should not be pointing to the tmpDir");
+  gDownloadLastDir.file = tmpDir;
+  is(gDownloadLastDir.file.path, tmpDir.path,
+     "LastDir should point to the temporary directory");
+  isnot(gDownloadLastDir.file, tmpDir,
+        "gDownloadLastDir.file should not be pointing to the tmpDir");
 
-    gDownloadLastDir.file = 1; // not an nsIFile
-    is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null");
+  gDownloadLastDir.file = 1; // not an nsIFile
+  is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null");
 
-    gDownloadLastDir.file = tmpDir;
-    clearHistory();
-    is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null");
+  gDownloadLastDir.file = tmpDir;
+  clearHistory();
+  is(gDownloadLastDir.file, null, "gDownloadLastDir.file should be null");
 
-    gDownloadLastDir.file = tmpDir;
-    aCallback();
-  }
+  gDownloadLastDir.file = tmpDir;
+  yield BrowserTestUtils.closeWindow(win);
 
-  function checkDownloadLastDir(aWin, gDownloadLastDir, aLastDir, aUpdate, aCallback) {
-    if (aUpdate)
-      gDownloadLastDir.file = aLastDir;
-    is(gDownloadLastDir.file.path, aLastDir.path,
-       "gDownloadLastDir should point to the expected last directory");
-    isnot(gDownloadLastDir.file, aLastDir,
-          "gDownloadLastDir.file should not be pointing to the last directory");
-    aCallback();
-  }
+  info("Opening the first private window");
+  yield helperThingNameMe({ private: true, expectedDir: tmpDir });
+  info("Opening a non-private window");
+  yield helperThingNameMe({ private: false, expectedDir: tmpDir });
+  info("Opening a private window and setting download directory");
+  yield helperThingNameMe({ private: true, setDir: dir1, expectedDir: dir1 });
+  info("Opening a non-private window and checking download directory");
+  yield helperThingNameMe({ private: false, expectedDir: tmpDir });
+  info("Opening private window and clearing history");
+  yield helperThingNameMe({ private: true, clearHistory: true, expectedDir: null });
+  info("Opening a non-private window and checking download directory");
+  yield helperThingNameMe({ private: true, expectedDir: null });
+});
 
-  function checkDownloadLastDirNull(aWin, gDownloadLastDir, aCallback) {
-    is(gDownloadLastDir.file, null, "gDownloadLastDir should be null");
-    aCallback();
-  }
+function helperThingNameMe(options) {
+  return new Task.spawn(function() {
+    let win = yield BrowserTestUtils.openNewBrowserWindow(options);
+    let gDownloadLastDir = new DownloadLastDir(win);
 
-  testOnWindow(false, function(win, downloadDir) {
-    checkDownloadLastDirInit(win, downloadDir, function() {
-      testOnWindow(true, function(win, downloadDir) {
-        checkDownloadLastDir(win, downloadDir, tmpDir, false, function() {
-          testOnWindow(false, function(win, downloadDir) {
-            checkDownloadLastDir(win, downloadDir, tmpDir, false, function() {
-              testOnWindow(true, function(win, downloadDir) {
-                checkDownloadLastDir(win, downloadDir, dir1, true, function() {
-                  testOnWindow(false, function(win, downloadDir) {
-                    checkDownloadLastDir(win, downloadDir, tmpDir, false, function() {
-                      testOnWindow(true, function(win, downloadDir) {
-                        clearHistory();
-                        checkDownloadLastDirNull(win, downloadDir, function() {
-                          testOnWindow(false, function(win, downloadDir) {
-                            checkDownloadLastDirNull(win, downloadDir, finish);
-                          });
-                        });
-                      });
-                    });
-                  });
-                });
-              });
-            });
-          });
-        });
-      });
-    });
+    if (options.clearHistory) {
+      clearHistory();
+    }
+
+    if (options.setDir) {
+      gDownloadLastDir.file = options.setDir;
+    }
+
+    let expectedDir = options.expectedDir;
+
+    if (expectedDir) {
+      is(gDownloadLastDir.file.path, expectedDir.path,
+         "gDownloadLastDir should point to the expected last directory");
+      isnot(gDownloadLastDir.file, expectedDir,
+            "gDownloadLastDir.file should not be pointing to the last directory");
+    } else {
+      is(gDownloadLastDir.file, null, "gDownloadLastDir should be null");
+    }
+
+    gDownloadLastDir.cleanupPrivateFile();
+    yield BrowserTestUtils.closeWindow(win);
   });
 }