Bug 1354698 - Add smoketests for View Source in both the tab and window mode. r?jryans draft
authorMike Conley <mconley@mozilla.com>
Sun, 09 Apr 2017 01:31:10 -0400
changeset 559184 649c76ea14b8a5cb52e5aa7ffdd792dd7d026800
parent 559161 fa701d4a14ef9f50c7e28e2d7f8ddf08b644dea5
child 623324 52c30f84339578e80b9247b945266b7d279cfa6a
push id53015
push usermconley@mozilla.com
push dateSun, 09 Apr 2017 05:32:33 +0000
reviewersjryans
bugs1354698
milestone55.0a1
Bug 1354698 - Add smoketests for View Source in both the tab and window mode. r?jryans MozReview-Commit-ID: BDTYuEMho6L
toolkit/components/viewsource/test/browser/browser.ini
toolkit/components/viewsource/test/browser/browser_smoketest.js
toolkit/components/viewsource/test/browser/head.js
--- a/toolkit/components/viewsource/test/browser/browser.ini
+++ b/toolkit/components/viewsource/test/browser/browser.ini
@@ -3,10 +3,11 @@ support-files = head.js
   file_bug464222.html
 
 [browser_bug464222.js]
 [browser_bug699356.js]
 [browser_bug713810.js]
 [browser_contextmenu.js]
 subsuite = clipboard
 [browser_gotoline.js]
+[browser_smoketest.js]
 [browser_srcdoc.js]
 [browser_viewsourceprefs.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/viewsource/test/browser/browser_smoketest.js
@@ -0,0 +1,61 @@
+"use strict";
+
+/**
+ * Very basic smoketests for the View Source feature.
+ */
+
+/**
+ * Tests that we can open View Source in a tab.
+ */
+add_task(function* test_view_source_in_tab() {
+  yield SpecialPowers.pushPrefEnv({
+    set: [
+      ["view_source.tab", true],
+    ],
+  });
+
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com",
+  }, function* (browser) {
+    let sourceTab = yield openViewSource(browser);
+    let sourceBrowser = sourceTab.linkedBrowser;
+    yield waitForSourceLoaded(sourceBrowser);
+
+    yield ContentTask.spawn(sourceBrowser, null, function*() {
+      Assert.equal(content.document.body.id, "viewsource",
+                   "View source mode enabled");
+    });
+
+    yield BrowserTestUtils.removeTab(sourceTab);
+  });
+
+  yield SpecialPowers.popPrefEnv();
+});
+
+/**
+ * Tests that we can open View Source in a window.
+ */
+add_task(function* test_view_source_in_window() {
+  yield SpecialPowers.pushPrefEnv({
+    set: [
+      ["view_source.tab", false],
+    ],
+  });
+
+  yield BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: "http://example.com",
+  }, function* (browser) {
+    let sourceWin = yield openViewSource(browser);
+    yield waitForSourceLoaded(sourceWin);
+    yield ContentTask.spawn(sourceWin.gBrowser, null, function*() {
+      Assert.equal(content.document.body.id, "viewsource",
+                   "View source mode enabled");
+    });
+
+    yield closeViewSourceWindow(sourceWin);
+  });
+
+  yield SpecialPowers.popPrefEnv();
+});
--- a/toolkit/components/viewsource/test/browser/head.js
+++ b/toolkit/components/viewsource/test/browser/head.js
@@ -21,23 +21,28 @@ function openViewSourceWindow(aURI, aCal
 
 function loadViewSourceWindow(URL) {
   return new Promise((resolve) => {
     openViewSourceWindow(URL, resolve);
   })
 }
 
 function closeViewSourceWindow(aWindow, aCallback) {
-  Services.wm.addListener({
-    onCloseWindow() {
-      Services.wm.removeListener(this);
-      executeSoon(aCallback);
-    }
+  return new Promise(resolve => {
+    Services.wm.addListener({
+      onCloseWindow() {
+        Services.wm.removeListener(this);
+        if (aCallback) {
+          executeSoon(aCallback);
+        }
+        resolve();
+      }
+    });
+    aWindow.close();
   });
-  aWindow.close();
 }
 
 function testViewSourceWindow(aURI, aTestCallback, aCloseCallback) {
   openViewSourceWindow(aURI, function(aWindow) {
     aTestCallback(aWindow);
     closeViewSourceWindow(aWindow, aCloseCallback);
   });
 }
@@ -61,16 +66,35 @@ function waitForViewSourceWindow() {
       onCloseWindow() {},
       onWindowTitleChange() {}
     };
     Services.wm.addListener(windowListener);
   });
 }
 
 /**
+ * Opens a view source tab for a browser.
+ *
+ * @param browser - the <xul:browser> to open view source for.
+ * @returns the new tab or window which shows the source.
+ */
+function* openViewSource(browser) {
+  let openPromise;
+  if (Services.prefs.getBoolPref("view_source.tab")) {
+    openPromise = BrowserTestUtils.waitForNewTab(gBrowser, null);
+  } else {
+    openPromise = waitForViewSourceWindow();
+  }
+
+  window.BrowserViewSource(browser);
+
+  return (yield openPromise);
+}
+
+/**
  * Opens a view source tab / window for a selection (View Selection Source)
  * within the currently selected browser in gBrowser.
  *
  * @param aCSSSelector - used to specify a node within the selection to
  *                       view the source of. It is expected that this node is
  *                       within an existing selection.
  * @returns the new tab / window which shows the source.
  */