summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/chrome/test_session_form_data.html
blob: cf09350c7dca57d624d4b4de017ca2da311f7971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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>