Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 function gc() {
6 Cu.forceGC();
7 let wu = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
8 .getInterface(Components.interfaces.nsIDOMWindowUtils);
9 wu.garbageCollect();
10 }
12 // Support for going on and offline.
13 // (via browser/base/content/test/browser_bookmark_titles.js)
14 let origProxyType = Services.prefs.getIntPref('network.proxy.type');
16 function goOffline() {
17 // Simulate a network outage with offline mode. (Localhost is still
18 // accessible in offline mode, so disable the test proxy as well.)
19 if (!Services.io.offline)
20 BrowserOffline.toggleOfflineStatus();
21 Services.prefs.setIntPref('network.proxy.type', 0);
22 // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
23 Services.cache2.clear();
24 }
26 function goOnline(callback) {
27 Services.prefs.setIntPref('network.proxy.type', origProxyType);
28 if (Services.io.offline)
29 BrowserOffline.toggleOfflineStatus();
30 if (callback)
31 callback();
32 }
34 function openPanel(url, panelCallback, loadCallback) {
35 // open a flyout
36 SocialFlyout.open(url, 0, panelCallback);
37 SocialFlyout.panel.firstChild.addEventListener("load", function panelLoad() {
38 SocialFlyout.panel.firstChild.removeEventListener("load", panelLoad, true);
39 loadCallback();
40 }, true);
41 }
43 function openChat(url, panelCallback, loadCallback) {
44 // open a chat window
45 SocialChatBar.openChat(SocialSidebar.provider, url, panelCallback);
46 SocialChatBar.chatbar.firstChild.addEventListener("DOMContentLoaded", function panelLoad() {
47 SocialChatBar.chatbar.firstChild.removeEventListener("DOMContentLoaded", panelLoad, true);
48 loadCallback();
49 }, true);
50 }
52 function onSidebarLoad(callback) {
53 let sbrowser = document.getElementById("social-sidebar-browser");
54 sbrowser.addEventListener("load", function load() {
55 sbrowser.removeEventListener("load", load, true);
56 callback();
57 }, true);
58 }
60 function ensureWorkerLoaded(provider, callback) {
61 // once the worker responds to a ping we know it must be up.
62 let port = provider.getWorkerPort();
63 port.onmessage = function(msg) {
64 if (msg.data.topic == "pong") {
65 port.close();
66 callback();
67 }
68 }
69 port.postMessage({topic: "ping"})
70 }
72 let manifest = { // normal provider
73 name: "provider 1",
74 origin: "https://example.com",
75 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
76 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
77 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
78 };
80 function test() {
81 waitForExplicitFinish();
83 runSocialTestWithProvider(manifest, function (finishcb) {
84 runSocialTests(tests, undefined, goOnline, finishcb);
85 });
86 }
88 var tests = {
89 testSidebar: function(next) {
90 let sbrowser = document.getElementById("social-sidebar-browser");
91 onSidebarLoad(function() {
92 ok(sbrowser.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
93 gc();
94 // Add a new load listener, then find and click the "try again" button.
95 onSidebarLoad(function() {
96 // should still be on the error page.
97 ok(sbrowser.contentDocument.location.href.indexOf("about:socialerror?")==0, "is still on social error page");
98 // go online and try again - this should work.
99 goOnline();
100 onSidebarLoad(function() {
101 // should now be on the correct page.
102 is(sbrowser.contentDocument.location.href, manifest.sidebarURL, "is now on social sidebar page");
103 next();
104 });
105 sbrowser.contentDocument.getElementById("btnTryAgain").click();
106 });
107 sbrowser.contentDocument.getElementById("btnTryAgain").click();
108 });
109 // we want the worker to be fully loaded before going offline, otherwise
110 // it might fail due to going offline.
111 ensureWorkerLoaded(SocialSidebar.provider, function() {
112 // go offline then attempt to load the sidebar - it should fail.
113 goOffline();
114 SocialSidebar.show();
115 });
116 },
118 testFlyout: function(next) {
119 let panelCallbackCount = 0;
120 let panel = document.getElementById("social-flyout-panel");
121 // go offline and open a flyout.
122 goOffline();
123 openPanel(
124 "https://example.com/browser/browser/base/content/test/social/social_panel.html",
125 function() { // the panel api callback
126 panelCallbackCount++;
127 },
128 function() { // the "load" callback.
129 executeSoon(function() {
130 todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
131 ok(panel.firstChild.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
132 // Bug 832943 - the listeners previously stopped working after a GC, so
133 // force a GC now and try again.
134 gc();
135 openPanel(
136 "https://example.com/browser/browser/base/content/test/social/social_panel.html",
137 function() { // the panel api callback
138 panelCallbackCount++;
139 },
140 function() { // the "load" callback.
141 executeSoon(function() {
142 todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
143 ok(panel.firstChild.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
144 gc();
145 SocialFlyout.unload();
146 next();
147 });
148 }
149 );
150 });
151 }
152 );
153 },
155 testChatWindow: function(next) {
156 let panelCallbackCount = 0;
157 // go offline and open a flyout.
158 goOffline();
159 openChat(
160 "https://example.com/browser/browser/base/content/test/social/social_chat.html",
161 function() { // the panel api callback
162 panelCallbackCount++;
163 },
164 function() { // the "load" callback.
165 executeSoon(function() {
166 todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
167 let chat = SocialChatBar.chatbar.selectedChat;
168 waitForCondition(function() chat.contentDocument.location.href.indexOf("about:socialerror?")==0,
169 function() {
170 chat.close();
171 next();
172 },
173 "error page didn't appear");
174 });
175 }
176 );
177 }
178 }