summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_session_form_data.html
diff options
context:
space:
mode:
Diffstat (limited to 'mobile/android/tests/browser/chrome/test_session_form_data.html')
-rw-r--r--mobile/android/tests/browser/chrome/test_session_form_data.html274
1 files changed, 274 insertions, 0 deletions
diff --git a/mobile/android/tests/browser/chrome/test_session_form_data.html b/mobile/android/tests/browser/chrome/test_session_form_data.html
new file mode 100644
index 000000000..cf09350c7
--- /dev/null
+++ b/mobile/android/tests/browser/chrome/test_session_form_data.html
@@ -0,0 +1,274 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=671993
+https://bugzilla.mozilla.org/show_bug.cgi?id=1261225
+Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
+-->
+<head>
+ <meta charset="utf-8">
+ <title>Test for Bugs 671993, 1261225</title>
+ <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+ <script type="application/javascript" src="head.js"></script>
+ <script type="application/javascript;version=1.7">
+
+"use strict";
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/Task.jsm");
+
+let gChromeWin;
+let gBrowserApp;
+
+// Waiting for a tab to load or restore can be slow on the emulator.
+SimpleTest.requestLongerTimeout(2);
+
+setup_browser();
+
+function queryElement(contentWindow, data) {
+ let frame = contentWindow;
+ if (data.hasOwnProperty("frame")) {
+ frame = contentWindow.frames[data.frame];
+ }
+
+ let doc = frame.document;
+
+ if (data.hasOwnProperty("id")) {
+ return doc.getElementById(data.id);
+ }
+
+ if (data.hasOwnProperty("selector")) {
+ return doc.querySelector(data.selector);
+ }
+
+ if (data.hasOwnProperty("xpath")) {
+ let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
+ return doc.evaluate(data.xpath, doc, null, xptype, null).singleNodeValue;
+ }
+
+ throw new Error("couldn't query element");
+}
+
+function dispatchUIEvent(input, type) {
+ let event = input.ownerDocument.createEvent("UIEvents");
+ event.initUIEvent(type, true, true, input.ownerDocument.defaultView, 0);
+ input.dispatchEvent(event);
+}
+
+function setInputValue(browser, data) {
+ let input = queryElement(browser.contentWindow, data);
+ input.value = data.value;
+ dispatchUIEvent(input, "input");
+}
+
+function getInputValue(browser, data) {
+ let input = queryElement(browser.contentWindow, data);
+ return input.value;
+}
+
+let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
+
+function setup_browser() {
+ gChromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+ gBrowserApp = gChromeWin.BrowserApp;
+}
+
+/**
+ * This test ensures that form data collection respects the privacy level as
+ * set by the user.
+ */
+add_task(function* test_formdata() {
+ const URL = "http://example.org/chrome/mobile/android/tests/browser/chrome/session_formdata_sample.html";
+
+ const OUTER_VALUE = "browser_formdata_" + Math.random();
+ const INNER_VALUE = "browser_formdata_" + Math.random();
+
+ // Creates a tab, loads a page with some form fields,
+ // modifies their values and closes the tab.
+ function createAndRemoveTab() {
+ return Task.spawn(function () {
+ // Create a new tab.
+ let tab = gBrowserApp.addTab(URL);
+ let browser = tab.browser;
+ yield promiseBrowserEvent(browser, "load");
+
+ // Modify form data.
+ setInputValue(browser, {id: "txt", value: OUTER_VALUE});
+ setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
+
+ // Remove the tab.
+ gBrowserApp.closeTab(tab);
+ yield promiseTabEvent(browser, "SSTabCloseProcessed");
+ });
+ }
+
+ yield createAndRemoveTab();
+ let state = ss.getClosedTabs(gChromeWin);
+ let [{formdata}] = state;
+ is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
+ is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
+
+ // Disable saving data for encrypted sites.
+ Services.prefs.setIntPref("browser.sessionstore.privacy_level", 1);
+
+ yield createAndRemoveTab();
+ state = ss.getClosedTabs(gChromeWin);
+ [{formdata}] = state;
+ is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
+ ok(!formdata.children, "inner value was *not* stored");
+
+ // Disable saving data for any site.
+ Services.prefs.setIntPref("browser.sessionstore.privacy_level", 2);
+
+ yield createAndRemoveTab();
+ state = ss.getClosedTabs(gChromeWin);
+ [{formdata}] = state;
+ ok(!formdata, "form data has *not* been stored");
+
+ // Restore the default privacy level.
+ Services.prefs.clearUserPref("browser.sessionstore.privacy_level");
+});
+
+/**
+ * This test ensures that form data collection restores correctly.
+ */
+add_task(function* test_formdata2() {
+ const URL = "http://example.org/chrome/mobile/android/tests/browser/chrome/session_formdata_sample.html";
+
+ const OUTER_VALUE = "browser_formdata_" + Math.random();
+ const INNER_VALUE = "browser_formdata_" + Math.random();
+
+ // Creates a tab, loads a page with some form fields,
+ // modifies their values and closes the tab.
+ function createAndRemoveTab() {
+ return Task.spawn(function () {
+ // Create a new tab.
+ let tab = gBrowserApp.addTab(URL);
+ let browser = tab.browser;
+ yield promiseBrowserEvent(browser, "load");
+
+ // Modify form data.
+ setInputValue(browser, {id: "txt", value: OUTER_VALUE});
+ setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
+
+ // Remove the tab.
+ gBrowserApp.closeTab(tab);
+ yield promiseTabEvent(browser, "SSTabCloseProcessed");
+ });
+ }
+
+ yield createAndRemoveTab();
+ let state = ss.getClosedTabs(gChromeWin);
+ let [{formdata}] = state;
+ is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
+ is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
+
+ // Restore the closed tab.
+ let closedTabData = ss.getClosedTabs(gChromeWin)[0];
+ let browser = ss.undoCloseTab(gChromeWin, closedTabData);
+ yield promiseBrowserEvent(browser, "load");
+
+ // Check the form data.
+ is(getInputValue(browser, {id: "txt"}), OUTER_VALUE, "outer value restored correctly");
+ is(getInputValue(browser, {id: "txt", frame: 0}), INNER_VALUE, "inner value restored correctly");
+
+ // Remove the tab.
+ gBrowserApp.closeTab(gBrowserApp.getTabForBrowser(browser));
+});
+
+/**
+ * This test ensures that form data collection restores correctly even after
+ * navigating to a different page and then returning via hitting back.
+ */
+add_task(function* test_formdata_navigation() {
+ const URL = "http://example.org/chrome/mobile/android/tests/browser/chrome/session_formdata_sample.html";
+ const otherURL = "http://example.org/chrome/mobile/android/tests/browser/chrome/basic_article.html";
+
+ const OUTER_VALUE = "browser_formdata_" + Math.random();
+ const INNER_VALUE = "browser_formdata_" + Math.random();
+
+ // Make sure the bfcache remains enabled during this test,
+ // otherwise the inner value will not be restored correctly.
+ Services.prefs.setBoolPref("browser.sessionhistory.bfcacheIgnoreMemoryPressure", true);
+ Services.prefs.setIntPref("browser.sessionhistory.max_total_viewers", 1);
+
+ SimpleTest.registerCleanupFunction(function() {
+ // Turn the bfcache memory pressure protection back off once we're finished.
+ Services.prefs.clearUserPref("browser.sessionhistory.bfcacheIgnoreMemoryPressure");
+ Services.prefs.clearUserPref("browser.sessionhistory.max_total_viewers");
+ });
+
+ // Creates a tab, loads a page with some form fields, modifies their values,
+ // navigates to a different page and back again and closes the tab.
+ function createNavigateAndRemoveTab() {
+ return Task.spawn(function () {
+ // Create a new tab.
+ let tab = gBrowserApp.addTab(URL);
+ let browser = tab.browser;
+ yield promiseBrowserEvent(browser, "load");
+
+ // Modify form data.
+ setInputValue(browser, {id: "txt", value: OUTER_VALUE});
+ setInputValue(browser, {id: "txt", value: INNER_VALUE, frame: 0});
+
+ // Visit a different page.
+ gBrowserApp.loadURI(otherURL, browser);
+ yield promiseBrowserEvent(browser, "DOMContentLoaded");
+ is(browser.currentURI.spec, otherURL, "navigated to a different page");
+
+ // Go back.
+ is(browser.canGoBack, true, "can go back");
+ browser.goBack();
+ yield promiseTabEvent(browser, "SSTabDataUpdated");
+ is(browser.currentURI.spec, URL, "navigated back to form data page");
+
+ // Make sure form data is still present.
+ is(getInputValue(browser, {id: "txt"}), OUTER_VALUE, "outer value present after navigation");
+ is(getInputValue(browser, {id: "txt", frame: 0}), INNER_VALUE, "inner value present after navigation");
+
+ // Remove the tab.
+ gBrowserApp.closeTab(tab);
+ yield promiseTabEvent(browser, "SSTabCloseProcessed");
+ });
+ }
+
+ yield createNavigateAndRemoveTab();
+ let state = ss.getClosedTabs(gChromeWin);
+ let [{formdata}] = state;
+ is(formdata.id.txt, OUTER_VALUE, "outer value is correct");
+ is(formdata.children[0].id.txt, INNER_VALUE, "inner value is correct");
+
+ // Restore the closed tab.
+ let closedTabData = ss.getClosedTabs(gChromeWin)[0];
+ let browser = ss.undoCloseTab(gChromeWin, closedTabData);
+ yield promiseBrowserEvent(browser, "load");
+
+ // Check the form data.
+ is(getInputValue(browser, {id: "txt"}), OUTER_VALUE, "outer value restored correctly");
+ is(getInputValue(browser, {id: "txt", frame: 0}), INNER_VALUE, "inner value restored correctly");
+
+ // Remove the tab.
+ gBrowserApp.closeTab(gBrowserApp.getTabForBrowser(browser));
+});
+
+ </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=671993">Mozilla Bug 671993</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1261225">Mozilla Bug 1261225</a>
+<br>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1184186">Migrated from Robocop testSessionFormData</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+</pre>
+</body>
+</html>