|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <!-- |
|
4 https://bugzilla.mozilla.org/show_bug.cgi?id=461710 |
|
5 --> |
|
6 <head> |
|
7 <title>Test for Bug 461710</title> |
|
8 <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
9 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" /> |
|
10 </head> |
|
11 <body> |
|
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=461710">Mozilla Bug 461710</a> |
|
13 <p id="display"></p> |
|
14 <pre id="test"> |
|
15 <script class="testbody" type="text/javascript"> |
|
16 |
|
17 /** Test for Bug 461710 **/ |
|
18 |
|
19 SimpleTest.waitForExplicitFinish(); |
|
20 |
|
21 const Ci = SpecialPowers.Ci; |
|
22 const Cc = SpecialPowers.Cc; |
|
23 const Cr = SpecialPowers.Cr; |
|
24 |
|
25 SpecialPowers.Cu.import("resource://gre/modules/NetUtil.jsm", window); |
|
26 var Services = SpecialPowers.Services; |
|
27 |
|
28 var gIframe; |
|
29 |
|
30 /** |
|
31 * Helper function which waits until another function returns true, and |
|
32 * then notifies a callback. |
|
33 * |
|
34 * Original function stolen from docshell/test/chrome/docshell_helpers.js. |
|
35 * |
|
36 * Parameters: |
|
37 * |
|
38 * fn: a function which is evaluated repeatedly, and when it turns true, |
|
39 * the onWaitComplete callback is notified. |
|
40 * |
|
41 * onWaitComplete: a callback which will be notified when fn() returns |
|
42 * true. |
|
43 */ |
|
44 function waitForTrue(fn, onWaitComplete) { |
|
45 var start = new Date().valueOf(); |
|
46 |
|
47 // Loop until the test function returns true, or until a timeout occurs, |
|
48 // if a timeout is defined. |
|
49 var intervalid = |
|
50 setInterval( |
|
51 function() { |
|
52 if (fn.call()) { |
|
53 // Stop calling the test function and notify the callback. |
|
54 clearInterval(intervalid); |
|
55 onWaitComplete.call(); |
|
56 } |
|
57 }, 20); |
|
58 } |
|
59 |
|
60 const kRed = "rgb(255, 0, 0)"; |
|
61 const kBlue = "rgb(0, 0, 255)"; |
|
62 |
|
63 var testpath = "/tests/toolkit/components/places/tests/mochitest/bug_461710/"; |
|
64 var prefix = "http://mochi.test:8888" + testpath; |
|
65 var subtests = [ |
|
66 "visited_page.html", // 1 |
|
67 "link_page.html", // 2 |
|
68 "link_page-2.html", // 3 |
|
69 "link_page-3.html" // 4 |
|
70 ]; |
|
71 |
|
72 var testNum = 0; |
|
73 function loadNextTest() { |
|
74 // run the initialization code for each test |
|
75 switch (++testNum) { |
|
76 case 1: |
|
77 gIframe = normalWindowIframe; |
|
78 break; |
|
79 |
|
80 case 2: |
|
81 break; |
|
82 |
|
83 case 3: |
|
84 gIframe = privateWindowIframe; |
|
85 break; |
|
86 |
|
87 case 4: |
|
88 gIframe = normalWindowIframe; |
|
89 break; |
|
90 |
|
91 default: |
|
92 ok(false, "Unexpected call to loadNextTest for test #" + testNum); |
|
93 } |
|
94 |
|
95 if (testNum == 1) |
|
96 observer.expectURL(prefix + subtests[0], "uri-visit-saved"); |
|
97 else |
|
98 observer.expectURL(prefix + subtests[0]); |
|
99 |
|
100 waitForTrue(function() observer.resolved, function() { |
|
101 // And the nodes get notified after the "link-visited" topic, so |
|
102 // we need to execute soon... |
|
103 SimpleTest.executeSoon(handleLoad); |
|
104 }); |
|
105 |
|
106 gIframe.src = prefix + subtests[testNum-1]; |
|
107 } |
|
108 |
|
109 function getColor(doc, win, id) { |
|
110 var elem = doc.getElementById(id); |
|
111 var utils = SpecialPowers.getDOMWindowUtils(win); |
|
112 return utils.getVisitedDependentComputedStyle(elem, "", "color"); |
|
113 } |
|
114 |
|
115 function checkTest() { |
|
116 switch (testNum) { |
|
117 case 1: |
|
118 // nothing to do here, we just want to mark the page as visited |
|
119 break; |
|
120 |
|
121 case 2: |
|
122 // run outside of private mode, link should appear as visited |
|
123 var doc = gIframe.contentDocument; |
|
124 var win = doc.defaultView; |
|
125 is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode"); |
|
126 break; |
|
127 |
|
128 case 3: |
|
129 // run inside of private mode, link should appear as not visited |
|
130 var doc = gIframe.contentDocument; |
|
131 var win = doc.defaultView; |
|
132 is(getColor(doc, win, "link"), kBlue, "Visited link coloring should not work inside of private mode"); |
|
133 break; |
|
134 |
|
135 case 4: |
|
136 // run outside of private mode, link should appear as visited |
|
137 var doc = gIframe.contentDocument; |
|
138 var win = doc.defaultView; |
|
139 is(getColor(doc, win, "link"), kRed, "Visited link coloring should work outside of private mode"); |
|
140 break; |
|
141 |
|
142 default: |
|
143 ok(false, "Unexpected call to checkTest for test #" + testNum); |
|
144 } |
|
145 } |
|
146 |
|
147 function handleLoad() { |
|
148 checkTest(); |
|
149 |
|
150 if (testNum < subtests.length) { |
|
151 loadNextTest(); |
|
152 } else { |
|
153 normalWindow.close(); |
|
154 privateWindow.close(); |
|
155 |
|
156 SimpleTest.finish(); |
|
157 } |
|
158 } |
|
159 |
|
160 var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor) |
|
161 .getInterface(Ci.nsIWebNavigation) |
|
162 .QueryInterface(Ci.nsIDocShellTreeItem) |
|
163 .rootTreeItem |
|
164 .QueryInterface(Ci.nsIInterfaceRequestor) |
|
165 .getInterface(Ci.nsIDOMWindow); |
|
166 var contentPage = "http://mochi.test:8888/tests/toolkit/components/places/tests/mochitest/bug_461710/iframe.html"; |
|
167 |
|
168 function testOnWindow(aIsPrivate, aCallback) { |
|
169 var win = mainWindow.OpenBrowserWindow({private: aIsPrivate}); |
|
170 win.addEventListener("load", function onLoad() { |
|
171 win.removeEventListener("load", onLoad, false); |
|
172 win.addEventListener("DOMContentLoaded", function onInnerLoad() { |
|
173 if (win.content.location.href != contentPage) { |
|
174 win.gBrowser.loadURI(contentPage); |
|
175 return; |
|
176 } |
|
177 win.removeEventListener("DOMContentLoaded", onInnerLoad, true); |
|
178 win.gBrowser.selectedBrowser.focus(); |
|
179 SimpleTest.executeSoon(function() { aCallback(win); }); |
|
180 }, true); |
|
181 SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); }); |
|
182 }, true); |
|
183 } |
|
184 |
|
185 const URI_VISITED_RESOLUTION_TOPIC = "visited-status-resolution"; |
|
186 var observer = { |
|
187 uri: null, |
|
188 resolved: true, |
|
189 observe: function (aSubject, aTopic, aData) { |
|
190 |
|
191 if (this.uri.equals(SpecialPowers.wrap(aSubject).QueryInterface(Ci.nsIURI))) { |
|
192 this.resolved = true; |
|
193 |
|
194 Services.obs.removeObserver(this, aTopic); |
|
195 } |
|
196 }, |
|
197 expectURL: function (url, aOverrideTopic) { |
|
198 ok(this.resolved, "Can't set the expected URL when another is yet to be resolved"); |
|
199 this.resolved = false; |
|
200 |
|
201 this.uri = SpecialPowers.wrap(NetUtil).newURI(url); |
|
202 var topic = aOverrideTopic || URI_VISITED_RESOLUTION_TOPIC; |
|
203 Services.obs.addObserver(this, topic, false); |
|
204 } |
|
205 }; |
|
206 |
|
207 var normalWindow; |
|
208 var privateWindow; |
|
209 |
|
210 var normalWindowIframe; |
|
211 var privateWindowIframe; |
|
212 |
|
213 testOnWindow(false, function(aWin) { |
|
214 var selectedBrowser = aWin.gBrowser.selectedBrowser; |
|
215 |
|
216 normalWindow = aWin; |
|
217 normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe"); |
|
218 |
|
219 testOnWindow(true, function(aPrivateWin) { |
|
220 selectedBrowser = aPrivateWin.gBrowser.selectedBrowser; |
|
221 |
|
222 privateWindow = aPrivateWin; |
|
223 privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe"); |
|
224 |
|
225 loadNextTest(); |
|
226 }); |
|
227 }); |
|
228 |
|
229 </script> |
|
230 </pre> |
|
231 </body> |
|
232 </html> |