From d413f521fb11b4e2aff32c6784ec9ed53852572d Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Wed, 23 Sep 2015 15:42:44 -0500 Subject: [PATCH] Bug 1201535 - Test view source of srcdoc frames. r=mconley --- .../viewsource/test/browser/browser.ini | 1 + .../viewsource/test/browser/browser_srcdoc.js | 30 ++++++++ .../viewsource/test/browser/head.js | 74 +++++++++++++++---- 3 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 toolkit/components/viewsource/test/browser/browser_srcdoc.js diff --git a/toolkit/components/viewsource/test/browser/browser.ini b/toolkit/components/viewsource/test/browser/browser.ini index 55eb4e2fa7a..ba02ca7b5b9 100644 --- a/toolkit/components/viewsource/test/browser/browser.ini +++ b/toolkit/components/viewsource/test/browser/browser.ini @@ -7,5 +7,6 @@ support-files = head.js [browser_bug713810.js] [browser_contextmenu.js] [browser_gotoline.js] +[browser_srcdoc.js] [browser_viewsourceprefs.js] skip-if = e10s # Bug ?????? - obscure failure (Syntax highlighting off - Got true, expected false) diff --git a/toolkit/components/viewsource/test/browser/browser_srcdoc.js b/toolkit/components/viewsource/test/browser/browser_srcdoc.js new file mode 100644 index 00000000000..542741ffc3f --- /dev/null +++ b/toolkit/components/viewsource/test/browser/browser_srcdoc.js @@ -0,0 +1,30 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const frameSource = `good`; +const source = ``; + +add_task(function*() { + let url = `data:text/html,${source}`; + yield BrowserTestUtils.withNewTab({ gBrowser, url }, checkFrameSource); +}); + +function* checkFrameSource() { + let sourceTab = yield openViewFrameSourceTab("#f"); + registerCleanupFunction(function() { + gBrowser.removeTab(sourceTab); + }); + + yield waitForSourceLoaded(sourceTab); + + let browser = gBrowser.selectedBrowser; + let textContent = yield ContentTask.spawn(browser, {}, function*() { + return content.document.body.textContent; + }); + is(textContent, frameSource, "Correct content loaded"); + let id = yield ContentTask.spawn(browser, {}, function*() { + return content.document.body.id; + }); + is(id, "viewsource", "View source mode enabled") +} diff --git a/toolkit/components/viewsource/test/browser/head.js b/toolkit/components/viewsource/test/browser/head.js index dd1ff01b37f..a5b9301b9fe 100644 --- a/toolkit/components/viewsource/test/browser/head.js +++ b/toolkit/components/viewsource/test/browser/head.js @@ -49,17 +49,54 @@ function testViewSourceWindow(aURI, aTestCallback, aCloseCallback) { * within an existing selection. * @returns the new tab which shows the source. */ -function* openViewPartialSourceWindow(aCSSSelector) { - var contentAreaContextMenu = document.getElementById("contentAreaContextMenu"); - let popupShownPromise = BrowserTestUtils.waitForEvent(contentAreaContextMenu, "popupshown"); +function* openViewPartialSourceTab(aCSSSelector) { + let contentAreaContextMenuPopup = + document.getElementById("contentAreaContextMenu"); + let popupShownPromise = + BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popupshown"); yield BrowserTestUtils.synthesizeMouseAtCenter(aCSSSelector, - { type: "contextmenu", button: 2}, gBrowser.selectedBrowser); + { type: "contextmenu", button: 2 }, gBrowser.selectedBrowser); yield popupShownPromise; let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null); - let popupHiddenPromise = BrowserTestUtils.waitForEvent(contentAreaContextMenu, "popuphidden"); - EventUtils.synthesizeMouseAtCenter(document.getElementById("context-viewpartialsource-selection"), {}); + let popupHiddenPromise = + BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popuphidden"); + let item = document.getElementById("context-viewpartialsource-selection"); + EventUtils.synthesizeMouseAtCenter(item, {}); + yield popupHiddenPromise; + + return (yield newTabPromise); +} + +/** + * Opens a view source tab for a frame (View Frame Source) within the + * currently selected browser in gBrowser. + * + * @param aCSSSelector - used to specify the frame to view the source of. + * @returns the new tab which shows the source. + */ +function* openViewFrameSourceTab(aCSSSelector) { + let contentAreaContextMenuPopup = + document.getElementById("contentAreaContextMenu"); + let popupShownPromise = + BrowserTestUtils.waitForEvent(contentAreaContextMenuPopup, "popupshown"); + yield BrowserTestUtils.synthesizeMouseAtCenter(aCSSSelector, + { type: "contextmenu", button: 2 }, gBrowser.selectedBrowser); + yield popupShownPromise; + + let frameContextMenu = document.getElementById("frame"); + popupShownPromise = + BrowserTestUtils.waitForEvent(frameContextMenu, "popupshown"); + EventUtils.synthesizeMouseAtCenter(frameContextMenu, {}); + yield popupShownPromise; + + let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null); + + let popupHiddenPromise = + BrowserTestUtils.waitForEvent(frameContextMenu, "popuphidden"); + let item = document.getElementById("context-viewframesource"); + EventUtils.synthesizeMouseAtCenter(item, {}); yield popupHiddenPromise; return (yield newTabPromise); @@ -72,6 +109,19 @@ registerCleanupFunction(function() { windows.getNext().close(); }); +/** + * For a given view source tab, wait for the source loading step to complete. + */ +function waitForSourceLoaded(tab) { + return new Promise(resolve => { + let mm = tab.linkedBrowser.messageManager; + mm.addMessageListener("ViewSource:SourceLoaded", function sourceLoaded() { + mm.removeMessageListener("ViewSource:SourceLoaded", sourceLoaded); + setTimeout(resolve, 0); + }); + }); +} + /** * Open a new document in a new tab, select part of it, and view the source of * that selection. The document is not closed afterwards. @@ -80,7 +130,7 @@ registerCleanupFunction(function() { * @param aCSSSelector - used to specify a node to select. All of this node's * children will be selected. * @returns the new tab which shows the source. - */ + */ function* openDocumentSelect(aURI, aCSSSelector) { let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, aURI); registerCleanupFunction(function() { @@ -92,16 +142,10 @@ function* openDocumentSelect(aURI, aCSSSelector) { content.getSelection().selectAllChildren(element); }); - let newtab = yield openViewPartialSourceWindow(aCSSSelector); + let newtab = yield openViewPartialSourceTab(aCSSSelector); // Wait until the source has been loaded. - yield new Promise(resolve => { - let mm = newtab.linkedBrowser.messageManager; - mm.addMessageListener("ViewSource:SourceLoaded", function selectionDrawn() { - mm.removeMessageListener("ViewSource:SourceLoaded", selectionDrawn); - setTimeout(resolve, 0); - }); - }); + yield waitForSourceLoaded(newtab); return newtab; }