summaryrefslogtreecommitdiffstats
path: root/layout/forms/test/test_bug536567_perwindowpb.html
blob: e27803157e3684943fa40052bafb11c0f79e035c (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=536567
-->
<head>
  <title>Test for Bug 536567</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.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=536567">Mozilla Bug 536567</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 536567 **/

const Cm = Components.manager;

var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(SpecialPowers.wrap(window).browsingContext);

var tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile);
var homeDir = Services.dirsvc.get("Desk", Ci.nsIFile);

function newDir() {
  var dir = tmpDir.clone();
  dir.append("testdir" + Math.floor(Math.random() * 10000));
  dir.QueryInterface(Ci.nsIFile);
  dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o700);
  return dir;
}

var dirs = [];
for(let i = 0; i < 6; i++) {
  dirs.push(newDir());
}
dirs.push(homeDir);
var domains = ['http://mochi.test:8888', 'http://example.org:80', 'http://example.com:80'];
/*
 * These tests take 3 args each:
 * - which domain to load
 * - the filePicker displayDirectory we expect to be set
 * - the file to pick (in most cases this will show up in the next test,
 *   as indicated by the comments)
 */
var tests = [
  "clear history",
  [0, 6, 0], // 0 -> 3
  [1, 6, 1], // 1 -> 4
  [2, 6, 2], // 2 -> 5
  [0, 0, 3], // 3 -> 6
  [1, 1, 1], // 4 -> 8
  [2, 2, 2], // 5 -> 9
  [0, 3, 1], // 6 -> 7
  [0, 1, 0], // 7 -> x
  [1, 1, 1], // 8 -> x
  [2, 2, 2], // 9 -> x
  "clear history",
  [0, 6, 0], // 11 -> 15
  [1, 6, 1], // 12 -> 16
  [2, 6, 2], // 13 -> 17
  "pb on",
  [0, 0, 3], // 15 -> 18
  [1, 1, 4], // 16 -> 19
  [2, 2, 5], // 17 -> 20
  [0, 3, 3], // 18 -> x
  [1, 4, 4], // 19 -> x
  [2, 5, 5], // 20 -> x
  "pb off",
  [0, 0, 5], // 22 -> 26
  [1, 1, 4], // 23 -> 27
  [2, 2, 3], // 24 -> 28
  "pb on",
  [0, 3, 5], // 26 -> x
  [1, 4, 4], // 27 -> x
  [2, 5, 3], // 28 -> x
  "clear history",
  // Not checking after clear history because browser.download.lastDir content
  // pref is not being clear properly in private windows.
  //[0, 6, 0], // 30 -> x
  //[1, 6, 1], // 31 -> x
  //[2, 6, 2], // 32 -> x
  "pb off"
];

var testIndex = 0;
var content;
var normalWindow;
var privateWindow;
var normalWindowIframe;
var privateWindowIframe;

function runTest() {
  var test = tests[testIndex];
  if (test == undefined) {
    endTest();
  } else if (test == "pb on") {
    content = privateWindowIframe;
    testIndex++;
    runTest();
  } else if (test == "pb off") {
    content = normalWindowIframe;
    testIndex++;
    runTest();
  } else if (test == "clear history") {
    Services.obs.notifyObservers(null, "browser:purge-session-history");
    testIndex++;
    runTest();
  } else {
    var file = dirs[test[2]].clone();
    file.append("file.file");
    MockFilePicker.setFiles([file]);
    content.setAttribute('src', domains[test[0]] + '/chrome/layout/forms/test/bug536567_subframe.html');
  }
}

function endTest() {
  for(let i = 0; i < dirs.length - 1; i++) {
    dirs[i].remove(true);
  }

  normalWindow.close();
  privateWindow.close();
  MockFilePicker.cleanup();
  SimpleTest.finish();
}

var mainWindow = window.browsingContext.topChromeWindow;
var contentPage = "http://mochi.test:8888/chrome/layout/forms/test/bug536567_iframe.html";

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

function testOnWindow(aIsPrivate, aCallback) {
  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
  whenDelayedStartupFinished(win, function() {
    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
      if (win.content.location.href != contentPage) {
        win.gBrowser.loadURI(Services.io.newURI(contentPage), {
          triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
        });
        return;
      }
      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
      win.gBrowser.selectedBrowser.focus();
      SimpleTest.info("DOMContentLoaded's window: " + win.location + " vs. " + window.location);
      win.setTimeout(function() { aCallback(win); }, 0);
    }, true);
    SimpleTest.info("load's window: " + win.location + " vs. " + window.location);
    win.setTimeout(function() {
      win.gBrowser.loadURI(Services.io.newURI(contentPage), {
        triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({}),
      });
    }, 0);
  });
}

MockFilePicker.showCallback = function(filepicker) {
  var test = tests[testIndex];
  var returned = -1;
  for (let i = 0; i < dirs.length; i++) {
     var dir = MockFilePicker.displayDirectory
                 ? MockFilePicker.displayDirectory
                 : Services.dirsvc.get(MockFilePicker.displaySpecialDirectory, Ci.nsIFile);
    if (dirs[i].path == dir.path) {
      returned = i;
      break;
    }
  }
  if (test[1] == -1) {
    ok(false, "We should never get an unknown directory back");
  } else {
    is(returned, test[1], 'test ' + testIndex);
  }

  filepicker.window.setTimeout(function() {
    testIndex++;
    runTest();
  }, 0);
};

window.onload = function() {
  SimpleTest.waitForExplicitFinish();
  testOnWindow(false, function(aWin) {
    var selectedBrowser = aWin.gBrowser.selectedBrowser;

    normalWindow = aWin;
    normalWindowIframe =
      selectedBrowser.contentDocument.getElementById("content");

    testOnWindow(true, function(aPrivateWin) {
      selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;

      privateWindow = aPrivateWin;
      privateWindowIframe =
        selectedBrowser.contentDocument.getElementById("content");

      content = normalWindowIframe;
      selectedBrowser.contentWindow.setTimeout(runTest, 0);
    });
  });
};

</script>
</pre>
</body>
</html>