summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/chrome/test_privbrowsing_perwindowpb.html
blob: 6b7d4abb3a18a4b58b028c1142d42e48e0b68428 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=248970
-->
<head>
  <meta charset="utf-8">
  <title>Test for Private Browsing</title>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="notification_common.js"></script>
  <link rel="stylesheet" type="text/css"
        href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=248970">Mozilla Bug 248970</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="text/javascript">

/** Test for Bug 248970 **/
// based on test_notifications.html

const Ci = SpecialPowers.Ci;
const Cc = SpecialPowers.Cc;
const Cr = SpecialPowers.Cr;

Components.utils.import("resource://gre/modules/Services.jsm");

var testpath = "/chrome/toolkit/components/passwordmgr/test/chrome/";
var prefix = "http://test2.example.com" + testpath;
var subtests = [
                   "subtst_privbrowsing_1.html", // 1
                   "subtst_privbrowsing_1.html", // 2
                   "subtst_privbrowsing_1.html", // 3
                   "subtst_privbrowsing_2.html", // 4
                   "subtst_privbrowsing_2.html", // 5
                   "subtst_privbrowsing_2.html", // 6
                   "subtst_privbrowsing_3.html", // 7
                   "subtst_privbrowsing_3.html", // 8
                   "subtst_privbrowsing_4.html", // 9
                   "subtst_privbrowsing_3.html" // 10
               ];
var observer;

var testNum = 0;
function loadNextTest() {
  // run the initialization code for each test
  switch (++ testNum) {
    case 1:
      popupNotifications = normalWindowPopupNotifications;
      iframe = normalWindowIframe;
      break;

    case 2:
      popupNotifications = privateWindowPopupNotifications;
      iframe = privateWindowIframe;
      break;

    case 3:
      popupNotifications = normalWindowPopupNotifications;
      iframe = normalWindowIframe;
      break;

    case 4:
      pwmgr.addLogin(login);
      break;

    case 5:
      popupNotifications = privateWindowPopupNotifications;
      iframe = privateWindowIframe;
      break;

    case 6:
      popupNotifications = normalWindowPopupNotifications;
      iframe = normalWindowIframe;
      break;

    case 7:
      pwmgr.addLogin(login);
      break;

    case 8:
      popupNotifications = privateWindowPopupNotifications;
      iframe = privateWindowIframe;
      break;

    case 9:
      break;

    case 10:
      popupNotifications = normalWindowPopupNotifications;
      iframe = normalWindowIframe;
      break;

    default:
      ok(false, "Unexpected call to loadNextTest for test #" + testNum);
  }

  if (testNum === 7) {
    observer = SpecialPowers.wrapCallback(function(subject, topic, data) {
      SimpleTest.executeSoon(() => { iframe.contentWindow.postMessage("go", "*"); });
    });
    SpecialPowers.addObserver(observer, "passwordmgr-processed-form", false);
  }

  ok(true, "Starting test #" + testNum);
  iframe.src = prefix + subtests[testNum - 1];
}

function checkTest() {
  var popup;
  var gotUser;
  var gotPass;

  switch (testNum) {
    case 1:
      // run outside of private mode, popup notification should appear
      popup = getPopup(popupNotifications, "password-save");
      ok(popup, "got popup notification");
      popup.remove();
      break;

    case 2:
      // run inside of private mode, popup notification should not appear
      popup = getPopup(popupNotifications, "password-save");
      ok(!popup, "checking for no popup notification");
      break;

    case 3:
      // run outside of private mode, popup notification should appear
      popup = getPopup(popupNotifications, "password-save");
      ok(popup, "got popup notification");
      popup.remove();
      break;

    case 4:
      // run outside of private mode, popup notification should appear
      popup = getPopup(popupNotifications, "password-change");
      ok(popup, "got popup notification");
      popup.remove();
      break;

    case 5:
      // run inside of private mode, popup notification should not appear
      popup = getPopup(popupNotifications, "password-change");
      ok(!popup, "checking for no popup notification");
      break;

    case 6:
      // run outside of private mode, popup notification should appear
      popup = getPopup(popupNotifications, "password-change");
      ok(popup, "got popup notification");
      popup.remove();
      pwmgr.removeLogin(login);
      break;

    case 7:
      // verify that the user/pass pair was autofilled
      gotUser = iframe.contentDocument.getElementById("user").textContent;
      gotPass = iframe.contentDocument.getElementById("pass").textContent;
      is(gotUser, "notifyu1", "Checking submitted username");
      is(gotPass, "notifyp1", "Checking submitted password");
      break;

    case 8:
      // verify that the user/pass pair was not autofilled
      gotUser = iframe.contentDocument.getElementById("user").textContent;
      gotPass = iframe.contentDocument.getElementById("pass").textContent;
      is(gotUser, "", "Checking submitted username");
      is(gotPass, "", "Checking submitted password");
      break;

    case 9:
      // verify that the user/pass pair was available for autocomplete
      gotUser = iframe.contentDocument.getElementById("user").textContent;
      gotPass = iframe.contentDocument.getElementById("pass").textContent;
      is(gotUser, "notifyu1", "Checking submitted username");
      is(gotPass, "notifyp1", "Checking submitted password");
      break;

    case 10:
      // verify that the user/pass pair was autofilled
      gotUser = iframe.contentDocument.getElementById("user").textContent;
      gotPass = iframe.contentDocument.getElementById("pass").textContent;
      is(gotUser, "notifyu1", "Checking submitted username");
      is(gotPass, "notifyp1", "Checking submitted password");
      pwmgr.removeLogin(login);
      break;

    default:
      ok(false, "Unexpected call to checkTest for test #" + testNum);

  }
}

var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .rootTreeItem
                    .QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIDOMWindow);
var contentPage = "http://mochi.test:8888/chrome/toolkit/components/passwordmgr/test/chrome/privbrowsing_perwindowpb_iframe.html";
var testWindows = [];

function whenDelayedStartupFinished(aWindow, aCallback) {
  Services.obs.addObserver(function obs(aSubject, aTopic) {
    if (aWindow == aSubject) {
      Services.obs.removeObserver(obs, aTopic);
      setTimeout(aCallback, 0);
    }
  }, "browser-delayed-startup-finished", false);
}

function testOnWindow(aIsPrivate, aCallback) {
  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
  win.addEventListener("load", function onLoad() {
    win.removeEventListener("load", onLoad, false);
    whenDelayedStartupFinished(win, function() {
      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
        if (win.content.location.href != contentPage) {
          win.gBrowser.loadURI(contentPage);
          return;
        }
        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);

        win.content.addEventListener('load', function innerLoad2() {
          win.content.removeEventListener('load', innerLoad2, false);
          testWindows.push(win);
          SimpleTest.executeSoon(function() { aCallback(win); });
        }, false, true);
      }, true);
      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
    });
  }, true);
}

var ignoreLoad = false;
function handleLoad(aEvent) {
  // ignore every other load event ... We get one for loading the subtest (which
  // we want to ignore), and another when the subtest's form submits itself
  // (which we want to handle, to start the next test).
  ignoreLoad = !ignoreLoad;
  if (ignoreLoad) {
    ok(true, "Ignoring load of subtest #" + testNum);
    return;
  }
  ok(true, "Processing submission of subtest #" + testNum);

  checkTest();

  if (testNum < subtests.length) {
    loadNextTest();
  } else {
    ok(true, "private browsing notification tests finished.");

    testWindows.forEach(function(aWin) {
      aWin.close();
    });

    SpecialPowers.removeObserver(observer, "passwordmgr-processed-form");
    SimpleTest.finish();
  }
}

var pwmgr = Cc["@mozilla.org/login-manager;1"].
            getService(Ci.nsILoginManager);
ok(pwmgr != null, "Access pwmgr");

// We need to make sure no logins have been stored by previous tests
// for forms in |url|, otherwise the change password notification
// would turn into a prompt, and the test will fail.
var url = "http://test2.example.com";
is(pwmgr.countLogins(url, "", null), 0, "No logins should be stored for " + url);

var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1",
                                             Ci.nsILoginInfo, "init");
var login = new nsLoginInfo(url, url, null, "notifyu1", "notifyp1", "user", "pass");

var normalWindow;
var privateWindow;

var iframe;
var normalWindowIframe;
var privateWindowIframe;

var popupNotifications;
var normalWindowPopupNotifications;
var privateWindowPopupNotifications;

testOnWindow(false, function(aWin) {
  var selectedBrowser = aWin.gBrowser.selectedBrowser;
  normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
  normalWindowIframe.onload = handleLoad;
  selectedBrowser.focus();

  normalWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
  ok(normalWindowPopupNotifications, "Got popupNotifications in normal window");
  // ignore the first load for this window;
  ignoreLoad = false;

  testOnWindow(true, function(aPrivateWin) {
    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
    privateWindowIframe.onload = handleLoad;
    selectedBrowser.focus();

    privateWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
    ok(privateWindowPopupNotifications, "Got popupNotifications in private window");
    // ignore the first load for this window;
    ignoreLoad = false;

    SimpleTest.executeSoon(loadNextTest);
  });
});

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>