Bug 1025146 - Update ViewSource tests to account for refactor. r=jryans.
authorMike Conley <mconley@mozilla.com>
Fri, 24 Apr 2015 16:47:08 -0400
changeset 262152 aa21a0c4e49b00faf2a2000455442614817b3727
parent 262151 0b714eb86b1457d02925ec7f26ad5cb0e3837224
child 550846 84153208422ee7e74301153600cfee2ea3a336ba
push id1962
push usermconley@mozilla.com
push dateSat, 09 May 2015 22:46:15 +0000
reviewersjryans
bugs1025146
milestone40.0a1
Bug 1025146 - Update ViewSource tests to account for refactor. r=jryans.
toolkit/components/viewsource/test/browser/browser.ini
toolkit/components/viewsource/test/browser/browser_contextmenu.js
toolkit/components/viewsource/test/browser/browser_gotoline.js
toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js
toolkit/components/viewsource/test/browser/browser_viewsourceprefs_nonhtml.js
toolkit/components/viewsource/test/browser/head.js
--- a/toolkit/components/viewsource/test/browser/browser.ini
+++ b/toolkit/components/viewsource/test/browser/browser.ini
@@ -5,9 +5,8 @@ support-files = head.js
 [browser_bug464222.js]
 [browser_bug699356.js]
 [browser_bug713810.js]
 [browser_contextmenu.js]
 skip-if = e10s # occasional timeouts with dead CPOW in console
 [browser_gotoline.js]
 [browser_viewsourceprefs.js]
 skip-if = e10s # Bug ?????? - obscure failure (Syntax highlighting off - Got true, expected false)
-[browser_viewsourceprefs_nonhtml.js]
--- a/toolkit/components/viewsource/test/browser/browser_contextmenu.js
+++ b/toolkit/components/viewsource/test/browser/browser_contextmenu.js
@@ -55,17 +55,17 @@ function checkMenuItems(popupNode, copyL
   popupNode.scrollIntoView();
 
   let cachedEvent = null;
   let mouseFn = function(event) {
     cachedEvent = event;
   };
 
   gViewSourceWindow.gBrowser.contentWindow.addEventListener("mousedown", mouseFn, false);
-  EventUtils.synthesizeMouseAtCenter(popupNode, { button: 2 }, gViewSourceWindow.gBrowser.contentWindow);
+  EventUtils.synthesizeMouseAtCenter(popupNode, { type: "contextmenu", button: 2 }, gViewSourceWindow.gBrowser.contentWindow);
   gViewSourceWindow.gBrowser.contentWindow.removeEventListener("mousedown", mouseFn, false);
 
   gContextMenu.openPopup(popupNode, "after_start", 0, 0, false, false, cachedEvent);
 
   is(gCopyLinkMenuItem.hidden, !copyLinkExpected, "Copy link menuitem is " + (copyLinkExpected ? "not hidden" : "hidden"));
   is(gCopyEmailMenuItem.hidden, !copyEmailExpected, "Copy email menuitem is " + (copyEmailExpected ? "not hidden" : "hidden"));
 
   if (!copyLinkExpected && !copyEmailExpected) {
--- a/toolkit/components/viewsource/test/browser/browser_gotoline.js
+++ b/toolkit/components/viewsource/test/browser/browser_gotoline.js
@@ -1,27 +1,39 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
-let content = "line 1\nline 2\nline 3";
-let runningPlainText = false;
+Cu.import("resource://testing-common/ContentTaskUtils.jsm", this);
 
-function test() {
-  waitForExplicitFinish();
+let content = "line 1\nline 2\nline 3";
 
-  testViewSourceWindow("data:text/html," + encodeURIComponent(content), checkViewSource, function() {
-    testViewSourceWindow("data:text/plain," + encodeURIComponent(content), checkViewSource, finish);
-  });
-}
+add_task(function*() {
+  // First test with text with the text/html mimetype.
+  let win = yield loadViewSourceWindow("data:text/html," + encodeURIComponent(content));
+  yield checkViewSource(win);
+  yield BrowserTestUtils.closeWindow(win);
 
-function checkViewSource(aWindow) {
+  win = yield loadViewSourceWindow("data:text/plain," + encodeURIComponent(content));
+  yield checkViewSource(win);
+  yield BrowserTestUtils.closeWindow(win);
+});
+
+let checkViewSource = Task.async(function* (aWindow) {
   is(aWindow.gBrowser.contentDocument.body.textContent, content, "Correct content loaded");
-
   let selection = aWindow.gBrowser.contentWindow.getSelection();
   let statusPanel = aWindow.document.getElementById("statusbar-line-col");
   is(statusPanel.getAttribute("label"), "", "Correct status bar text");
+
   for (let i = 1; i <= 3; i++) {
-    aWindow.goToLine(i);
-    is(selection.toString(), "line " + i, "Correct text selected");
-    is(statusPanel.getAttribute("label"), "Line " + i + ", Col 1", "Correct status bar text");
+    aWindow.ViewSourceChrome.goToLine(i);
+    let result = yield ContentTask.spawn(aWindow.gBrowser, i, function*(i) {
+      let selection = content.getSelection();
+      return (selection.toString() == "line " + i);
+    });
+
+    ok(result, "Correct text selected");
+
+    yield ContentTaskUtils.waitForCondition(() => {
+      return (statusPanel.getAttribute("label") == "Line " + i + ", Col 1");
+    }, "Correct status bar text");
   }
-}
+});
--- a/toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js
+++ b/toolkit/components/viewsource/test/browser/browser_viewsourceprefs.js
@@ -1,135 +1,134 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
-const source = "about:mozilla";
-let mWindow, wrapMenuItem, syntaxMenuItem;
+let plaintextURL = "data:text/plain,hello+world";
+let htmlURL = "about:mozilla";
 
-// Check the default values are set.
-function test() {
-  waitForExplicitFinish();
-
+add_task(function* setup() {
   registerCleanupFunction(function() {
     SpecialPowers.clearUserPref("view_source.tab_size");
     SpecialPowers.clearUserPref("view_source.wrap_long_lines");
     SpecialPowers.clearUserPref("view_source.syntax_highlight");
   });
-
-  openViewSourceWindow(source, function(aWindow) {
-    mWindow = aWindow;
-    wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
-    syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
+});
 
-    // Strip checked="false" attributes, since we're not interested in them.
-    if (wrapMenuItem.getAttribute("checked") == "false")
-      wrapMenuItem.removeAttribute("checked");
-    if (syntaxMenuItem.getAttribute("checked") == "false")
-      syntaxMenuItem.removeAttribute("checked");
+add_task(function*() {
+  yield exercisePrefs(plaintextURL, false);
+  yield exercisePrefs(htmlURL, true);
+});
+
+let exercisePrefs = Task.async(function* (source, highlightable) {
+  let win = yield loadViewSourceWindow(source);
+  let wrapMenuItem = win.document.getElementById("menu_wrapLongLines");
+  let syntaxMenuItem = win.document.getElementById("menu_highlightSyntax");
 
-    is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item not checked by default");
-    is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked by default");
-    checkStyle(aWindow, "-moz-tab-size", 4);
-    checkStyle(aWindow, "white-space", "pre");
+  // Strip checked="false" attributes, since we're not interested in them.
+  if (wrapMenuItem.getAttribute("checked") == "false") {
+    wrapMenuItem.removeAttribute("checked");
+  }
+  if (syntaxMenuItem.getAttribute("checked") == "false") {
+    syntaxMenuItem.removeAttribute("checked");
+  }
 
-    test1();
-  });
-}
+  // Test the default states of these menu items.
+  is(wrapMenuItem.hasAttribute("checked"), false,
+     "Wrap menu item not checked by default");
+  is(syntaxMenuItem.hasAttribute("checked"), true,
+     "Syntax menu item checked by default");
 
-// Check that the Wrap Long Lines menu item works.
-function test1() {
+  yield checkStyle(win, "-moz-tab-size", 4);
+  yield checkStyle(win, "white-space", "pre");
+
+  // Next, test that the Wrap Long Lines menu item works.
   simulateClick(wrapMenuItem);
-
   is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
   is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), true, "Wrap pref set");
-  checkStyle(mWindow, "white-space", "pre-wrap");
-  test2();
-}
 
-function test2() {
+  yield checkStyle(win, "white-space", "pre-wrap");
+
   simulateClick(wrapMenuItem);
-
   is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item unchecked");
   is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), false, "Wrap pref set");
-  checkStyle(mWindow, "white-space", "pre");
-  test3();
-}
+  yield checkStyle(win, "white-space", "pre");
 
-// Check that the Syntax Highlighting menu item works.
-function test3() {
-  mWindow.gBrowser.addEventListener("pageshow", function test3Handler() {
-    mWindow.gBrowser.removeEventListener("pageshow", test3Handler, false);
-    is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
-    is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set");
-
-    checkHighlight(mWindow, false);
-    test4();
-  }, false);
-
+  // Check that the Syntax Highlighting menu item works.
+  let pageShowPromise = BrowserTestUtils.waitForEvent(win.gBrowser, "pageshow");
   simulateClick(syntaxMenuItem);
-}
+  yield pageShowPromise;
 
-function test4() {
-  mWindow.gBrowser.addEventListener("pageshow", function test4Handler() {
-    mWindow.gBrowser.removeEventListener("pageshow", test4Handler, false);
-    is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked");
-    is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set");
+  is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
+  is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set");
+  yield checkHighlight(win, false);
 
-    checkHighlight(mWindow, true);
-    closeViewSourceWindow(mWindow, test5);
-  }, false);
-
+  pageShowPromise = BrowserTestUtils.waitForEvent(win.gBrowser, "pageshow");
   simulateClick(syntaxMenuItem);
-}
+  yield pageShowPromise;
+
+  is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked");
+  is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set");
 
-// Open a new view-source window to check prefs are obeyed.
-function test5() {
+  yield checkHighlight(win, highlightable);
+  yield BrowserTestUtils.closeWindow(win);
+
+  // Open a new view-source window to check that the prefs are obeyed.
   SpecialPowers.setIntPref("view_source.tab_size", 2);
   SpecialPowers.setBoolPref("view_source.wrap_long_lines", true);
   SpecialPowers.setBoolPref("view_source.syntax_highlight", false);
 
-  executeSoon(function() {
-    openViewSourceWindow(source, function(aWindow) {
-      wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
-      syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
+  win = yield loadViewSourceWindow(source);
+  wrapMenuItem = win.document.getElementById("menu_wrapLongLines");
+  syntaxMenuItem = win.document.getElementById("menu_highlightSyntax");
+
+  // Strip checked="false" attributes, since we're not interested in them.
+  if (wrapMenuItem.getAttribute("checked") == "false") {
+    wrapMenuItem.removeAttribute("checked");
+  }
+  if (syntaxMenuItem.getAttribute("checked") == "false") {
+    syntaxMenuItem.removeAttribute("checked");
+  }
 
-      // Strip checked="false" attributes, since we're not interested in them.
-      if (wrapMenuItem.getAttribute("checked") == "false")
-        wrapMenuItem.removeAttribute("checked");
-      if (syntaxMenuItem.getAttribute("checked") == "false")
-        syntaxMenuItem.removeAttribute("checked");
+  is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
+  is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
+  yield checkStyle(win, "-moz-tab-size", 2);
+  yield checkStyle(win, "white-space", "pre-wrap");
+  yield checkHighlight(win, false);
 
-      is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
-      is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
-      checkStyle(aWindow, "-moz-tab-size", 2);
-      checkStyle(aWindow, "white-space", "pre-wrap");
-      checkHighlight(aWindow, false);
-      closeViewSourceWindow(aWindow, finish);
-    });
-  });
-}
+  SpecialPowers.clearUserPref("view_source.tab_size");
+  SpecialPowers.clearUserPref("view_source.wrap_long_lines");
+  SpecialPowers.clearUserPref("view_source.syntax_highlight");
+
+  yield BrowserTestUtils.closeWindow(win);
+});
 
 // Simulate a menu item click, including toggling the checked state.
 // This saves us from opening the menu and trying to click on the item,
 // which doesn't work on Mac OS X.
 function simulateClick(aMenuItem) {
   if (aMenuItem.hasAttribute("checked"))
     aMenuItem.removeAttribute("checked");
   else
     aMenuItem.setAttribute("checked", "true");
 
   aMenuItem.click();
 }
 
-function checkStyle(aWindow, aStyleProperty, aExpectedValue) {
-  let gBrowser = aWindow.gBrowser;
-  let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null);
-
-  is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty);
-}
+let checkStyle = Task.async(function* (win, styleProperty, expected) {
+  let browser = win.gBrowser;
+  let value = yield ContentTask.spawn(browser, styleProperty, function* (styleProperty) {
+    let style = content.getComputedStyle(content.document.body, null);
+    return style.getPropertyValue(styleProperty);
+  });
+  is(value, expected, "Correct value of " + styleProperty);
+});
 
-function checkHighlight(aWindow, aExpected) {
-  let spans = aWindow.gBrowser.contentDocument.getElementsByTagName("span");
-  is(Array.some(spans, function(aSpan) {
-    return aSpan.className != "";
-  }), aExpected, "Syntax highlighting " + (aExpected ? "on" : "off"));
-}
+let checkHighlight = Task.async(function* (win, expected) {
+  let browser = win.gBrowser;
+  let highlighted = yield ContentTask.spawn(browser, {}, function* () {
+    let spans = content.document.getElementsByTagName("span");
+    return Array.some(spans, (span) => {
+      return span.className != "";
+    });
+  });
+  is(highlighted, expected, "Syntax highlighting " + (expected ? "on" : "off"));
+});
deleted file mode 100644
--- a/toolkit/components/viewsource/test/browser/browser_viewsourceprefs_nonhtml.js
+++ /dev/null
@@ -1,135 +0,0 @@
-/* Any copyright is dedicated to the Public Domain.
- * http://creativecommons.org/publicdomain/zero/1.0/
- */
-
-let source = "data:text/plain,hello+world";
-let mWindow, wrapMenuItem, syntaxMenuItem;
-
-// Check the default values are set.
-function test() {
-  waitForExplicitFinish();
-
-  registerCleanupFunction(function() {
-    SpecialPowers.clearUserPref("view_source.tab_size");
-    SpecialPowers.clearUserPref("view_source.wrap_long_lines");
-    SpecialPowers.clearUserPref("view_source.syntax_highlight");
-  });
-
-  openViewSourceWindow(source, function(aWindow) {
-    mWindow = aWindow;
-    wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
-    syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
-
-    // Strip checked="false" attributes, since we're not interested in them.
-    if (wrapMenuItem.getAttribute("checked") == "false")
-      wrapMenuItem.removeAttribute("checked");
-    if (syntaxMenuItem.getAttribute("checked") == "false")
-      syntaxMenuItem.removeAttribute("checked");
-
-    is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item not checked by default");
-    is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked by default");
-    checkStyle(aWindow, "-moz-tab-size", 4);
-    checkStyle(aWindow, "white-space", "pre");
-
-    test1();
-  });
-}
-
-// Check that the Wrap Long Lines menu item works.
-function test1() {
-  simulateClick(wrapMenuItem);
-
-  is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
-  is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), true, "Wrap pref set");
-  checkStyle(mWindow, "white-space", "pre-wrap");
-  test2();
-}
-
-function test2() {
-  simulateClick(wrapMenuItem);
-
-  is(wrapMenuItem.hasAttribute("checked"), false, "Wrap menu item unchecked");
-  is(SpecialPowers.getBoolPref("view_source.wrap_long_lines"), false, "Wrap pref set");
-  checkStyle(mWindow, "white-space", "pre");
-  test3();
-}
-
-// Check that the Syntax Highlighting menu item works.
-function test3() {
-  mWindow.gBrowser.addEventListener("pageshow", function test3Handler() {
-    mWindow.gBrowser.removeEventListener("pageshow", test3Handler, false);
-    is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
-    is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), false, "Syntax highlighting pref set");
-
-    checkHighlight(mWindow, false);
-    test4();
-  }, false);
-
-  simulateClick(syntaxMenuItem);
-}
-
-function test4() {
-  mWindow.gBrowser.addEventListener("pageshow", function test4Handler() {
-    mWindow.gBrowser.removeEventListener("pageshow", test4Handler, false);
-    is(syntaxMenuItem.hasAttribute("checked"), true, "Syntax menu item checked");
-    is(SpecialPowers.getBoolPref("view_source.syntax_highlight"), true, "Syntax highlighting pref set");
-
-    checkHighlight(mWindow, false);
-    closeViewSourceWindow(mWindow, test5);
-  }, false);
-
-  simulateClick(syntaxMenuItem);
-}
-
-// Open a new view-source window to check prefs are obeyed.
-function test5() {
-  SpecialPowers.setIntPref("view_source.tab_size", 2);
-  SpecialPowers.setBoolPref("view_source.wrap_long_lines", true);
-  SpecialPowers.setBoolPref("view_source.syntax_highlight", false);
-
-  executeSoon(function() {
-    openViewSourceWindow(source, function(aWindow) {
-      wrapMenuItem = aWindow.document.getElementById('menu_wrapLongLines');
-      syntaxMenuItem = aWindow.document.getElementById('menu_highlightSyntax');
-
-      // Strip checked="false" attributes, since we're not interested in them.
-      if (wrapMenuItem.getAttribute("checked") == "false")
-        wrapMenuItem.removeAttribute("checked");
-      if (syntaxMenuItem.getAttribute("checked") == "false")
-        syntaxMenuItem.removeAttribute("checked");
-
-      is(wrapMenuItem.hasAttribute("checked"), true, "Wrap menu item checked");
-      is(syntaxMenuItem.hasAttribute("checked"), false, "Syntax menu item unchecked");
-      checkStyle(aWindow, "-moz-tab-size", 2);
-      checkStyle(aWindow, "white-space", "pre-wrap");
-      checkHighlight(aWindow, false);
-      closeViewSourceWindow(aWindow, finish);
-    });
-  });
-}
-
-// Simulate a menu item click, including toggling the checked state.
-// This saves us from opening the menu and trying to click on the item,
-// which doesn't work on Mac OS X.
-function simulateClick(aMenuItem) {
-  if (aMenuItem.hasAttribute("checked"))
-    aMenuItem.removeAttribute("checked");
-  else
-    aMenuItem.setAttribute("checked", "true");
-
-  aMenuItem.click();
-}
-
-function checkStyle(aWindow, aStyleProperty, aExpectedValue) {
-  let gBrowser = aWindow.gBrowser;
-  let computedStyle = gBrowser.contentWindow.getComputedStyle(gBrowser.contentDocument.body, null);
-
-  is(computedStyle.getPropertyValue(aStyleProperty), aExpectedValue, "Correct value of " + aStyleProperty);
-}
-
-function checkHighlight(aWindow, aExpected) {
-  let spans = aWindow.gBrowser.contentDocument.getElementsByTagName("span");
-  is(Array.some(spans, function(aSpan) {
-    return aSpan.className != "";
-  }), aExpected, "Syntax highlighting " + (aExpected ? "on" : "off"));
-}
--- a/toolkit/components/viewsource/test/browser/head.js
+++ b/toolkit/components/viewsource/test/browser/head.js
@@ -9,16 +9,22 @@ function openViewSourceWindow(aURI, aCal
     if (event.target.location == "view-source:" + aURI) {
       info("View source window opened: " + event.target.location);
       viewSourceWindow.removeEventListener("pageshow", pageShowHandler, false);
       aCallback(viewSourceWindow);
     }
   }, false);
 }
 
+function loadViewSourceWindow(URL) {
+  return new Promise((resolve) => {
+    openViewSourceWindow(URL, resolve);
+  })
+}
+
 function closeViewSourceWindow(aWindow, aCallback) {
   Services.wm.addListener({
     onCloseWindow: function() {
       Services.wm.removeListener(this);
       executeSoon(aCallback);
     }
   });
   aWindow.close();