summaryrefslogtreecommitdiffstats
path: root/toolkit/components/prompts/test/test_subresources_prompts.html
blob: 134af95c6dc46e964fe6468012d7a75113b92ca7 (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
<html>
<head>
  <title>Test subresources prompts (Bug 625187 and bug 1230462)</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
<!--
   - Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/
   -
   - Contributor(s):
   -   Mihai Sucan <mihai.sucan@gmail.com>
   -->
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=625187">Mozilla Bug 625187</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1230462">Mozilla Bug 1230462</a>

<p><button id="topbutton" onclick="alert('hello world')">Button</button></p>

<iframe id="iframe_diff_origin" src="http://example.com/tests/toolkit/components/prompts/test/bug625187_iframe.html"></iframe>

<iframe id="iframe_same_origin" src="bug625187_iframe.html"></iframe>

<iframe id="iframe_prompt"></iframe>

<pre id="test"></pre>

<script class="testbody" type="text/javascript">
var iframe1Loaded = onloadPromiseFor("iframe_diff_origin");
var iframe2Loaded = onloadPromiseFor("iframe_same_origin");
var iframe_prompt = document.getElementById("iframe_prompt");

add_task(async function runTest() {
  modalType = Ci.nsIPrompt.MODAL_TYPE_CONTENT;

  info("Ensuring iframe1 has loaded...");
  await iframe1Loaded;
  info("Ensuring iframe2 has loaded...");
  await iframe2Loaded;
  let state, action;

  state = {
    msg: "hello world",
    iconClass: "alert-icon",
    titleHidden: true,
    textHidden: true,
    passHidden: true,
    checkHidden: true,
    textValue: "",
    passValue: "",
    checkMsg: "",
    checked: false,
    focused: "button0",
    defButton: "button0",
  };
  action = {
    buttonClick: "ok",
  };

  let promptDone = handlePrompt(state, action);

  await SpecialPowers.spawn(window, ["topbutton", "click"], dispatchMouseEvent);

  await promptDone;

  // mostly reusing same state/action
  state.titleHidden = false;
  state.msg = "hello world 2";
  promptDone = handlePrompt(state, action);

  var iframe = document.getElementById("iframe_diff_origin");
  await SpecialPowers.spawn(iframe.contentWindow, ["btn1", "click"], dispatchMouseEvent);

  await promptDone;

  // mostly reusing same state/action
  state.titleHidden = true;
  state.msg = "hello world 2";
  promptDone = handlePrompt(state, action);

  iframe = document.getElementById("iframe_same_origin");
  await SpecialPowers.spawn(iframe.contentWindow, ["btn1", "click"], dispatchMouseEvent);

  await promptDone;

  // mostly reusing same state/action
  state.msg = "hello world 3";
  promptDone = handlePrompt(state, action);
  await SpecialPowers.spawn(iframe.contentWindow, ["btn2", "click"], dispatchMouseEvent);

  await promptDone;
});

add_task(async function runTestAuth() {
  // Following tests check prompt message for a cross-origin and not
  // cross-origin subresources load

  // Let prompt_common know what kind of modal type is enabled for auth prompts.
  modalType = Ci.nsIPrompt.MODAL_TYPE_TAB;

  let state, action;

  state = {
    msg: "This site is asking you to sign in.",
    title: "Authentication Required",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: false,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "textField",
    defButton: "button0",
  };

  action = {
    buttonClick: "ok",
    setCheckbox: false,
    textField: "mochiuser1",
    passField: "mochipass1",
  };

  let promptDone = handlePrompt(state, action);

  var iframe3Loaded = onloadPromiseFor("iframe_prompt");
  iframe_prompt.src = "authenticate.sjs?user=mochiuser1&pass=mochipass1";
  await promptDone;
  await iframe3Loaded;
  await checkEchoedAuthInfo({user: "mochiuser1", pass: "mochipass1"},
                            iframe_prompt);

  // Cross-origin subresourse test.
  state = {
    msg: "This site is asking you to sign in. Warning: Your login information " +
      "will be shared with example.com, not the website you are currently visiting.",
    title: "Authentication Required",
    textValue: "",
    passValue: "",
    iconClass: "authentication-icon question-icon",
    titleHidden: true,
    textHidden: false,
    passHidden: false,
    checkHidden: true,
    checkMsg: "",
    checked: false,
    focused: "textField",
    defButton: "button0",
  };

  action = {
    buttonClick: "ok",
    setCheckbox: false,
    textField: "mochiuser2",
    passField: "mochipass2",
  };

  promptDone = handlePrompt(state, action);

  iframe3Loaded = onloadPromiseFor("iframe_prompt");
  iframe_prompt.src = "http://example.com/tests/toolkit/components/prompts/test/authenticate.sjs?user=mochiuser2&pass=mochipass2&realm=mochitest";
  await promptDone;
  await iframe3Loaded;
  await checkEchoedAuthInfo({user: "mochiuser2", pass: "mochipass2"},
                            iframe_prompt);
});

/**
 * Function to be passed to SpecialPowers.spawn that dispatches a MouseEvent
 * of a certain type to some element in a subframe.
 *
 * @param {String} targetID The ID of the element that will have the event
 * dispatched on.
 * @param {String} type The type of MouseEvent.
 * @returns Promise
 * @resolves Once the event has been dispatched.
 */
async function dispatchMouseEvent(targetID, type) {
  /* eslint-disable no-undef */
  let document = content.document;
  let element = document.getElementById(targetID);
  let event = document.createEvent("MouseEvent");
  event.initEvent(type, false, false, content, 0, 1, 1, 1, 1,
                  false, false, false, false, 0, null);
  content.windowUtils.dispatchDOMEventViaPresShellForTesting(element, event);
  /* eslint-enable no-undef */
}
</script>
</body>
</html>