browser/base/content/test/social/browser_chat_tearoff.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 function test() {
michael@0 6 requestLongerTimeout(2); // only debug builds seem to need more time...
michael@0 7 waitForExplicitFinish();
michael@0 8
michael@0 9 let manifest = { // normal provider
michael@0 10 name: "provider 1",
michael@0 11 origin: "https://example.com",
michael@0 12 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
michael@0 13 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
michael@0 14 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
michael@0 15 };
michael@0 16
michael@0 17 let postSubTest = function(cb) {
michael@0 18 let chats = document.getElementById("pinnedchats");
michael@0 19 ok(chats.children.length == 0, "no chatty children left behind");
michael@0 20 cb();
michael@0 21 };
michael@0 22 runSocialTestWithProvider(manifest, function (finishcb) {
michael@0 23 SocialSidebar.show();
michael@0 24 ok(SocialSidebar.provider, "sidebar provider exists");
michael@0 25 runSocialTests(tests, undefined, postSubTest, function() {
michael@0 26 finishcb();
michael@0 27 });
michael@0 28 });
michael@0 29 }
michael@0 30
michael@0 31 var tests = {
michael@0 32 testTearoffChat: function(next) {
michael@0 33 let chats = document.getElementById("pinnedchats");
michael@0 34 let chatTitle;
michael@0 35 let port = SocialSidebar.provider.getWorkerPort();
michael@0 36 ok(port, "provider has a port");
michael@0 37 port.onmessage = function (e) {
michael@0 38 let topic = e.data.topic;
michael@0 39 switch (topic) {
michael@0 40 case "got-sidebar-message":
michael@0 41 port.postMessage({topic: "test-chatbox-open"});
michael@0 42 break;
michael@0 43 case "got-chatbox-visibility":
michael@0 44 // chatbox is open, lets detach. The new chat window will be caught in
michael@0 45 // the window watcher below
michael@0 46 let doc = chats.selectedChat.contentDocument;
michael@0 47 // This message is (sometimes!) received a second time
michael@0 48 // before we start our tests from the onCloseWindow
michael@0 49 // callback.
michael@0 50 if (doc.location == "about:blank")
michael@0 51 return;
michael@0 52 chatTitle = doc.title;
michael@0 53 ok(chats.selectedChat.getAttribute("label") == chatTitle,
michael@0 54 "the new chatbox should show the title of the chat window");
michael@0 55 let div = doc.createElement("div");
michael@0 56 div.setAttribute("id", "testdiv");
michael@0 57 div.setAttribute("test", "1");
michael@0 58 doc.body.appendChild(div);
michael@0 59 let swap = document.getAnonymousElementByAttribute(chats.selectedChat, "anonid", "swap");
michael@0 60 swap.click();
michael@0 61 port.close();
michael@0 62 break;
michael@0 63 case "got-chatbox-message":
michael@0 64 ok(true, "got chatbox message");
michael@0 65 ok(e.data.result == "ok", "got chatbox windowRef result: "+e.data.result);
michael@0 66 chats.selectedChat.toggle();
michael@0 67 break;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 Services.wm.addListener({
michael@0 72 onWindowTitleChange: function() {},
michael@0 73 onCloseWindow: function(xulwindow) {},
michael@0 74 onOpenWindow: function(xulwindow) {
michael@0 75 var domwindow = xulwindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 76 .getInterface(Components.interfaces.nsIDOMWindow);
michael@0 77 Services.wm.removeListener(this);
michael@0 78 // wait for load to ensure the window is ready for us to test
michael@0 79 domwindow.addEventListener("load", function _load(event) {
michael@0 80 let doc = domwindow.document;
michael@0 81 if (event.target != doc)
michael@0 82 return;
michael@0 83
michael@0 84 domwindow.removeEventListener("load", _load, false);
michael@0 85
michael@0 86 domwindow.addEventListener("unload", function _close(event) {
michael@0 87 if (event.target != doc)
michael@0 88 return;
michael@0 89 domwindow.removeEventListener("unload", _close, false);
michael@0 90 info("window has been closed");
michael@0 91 waitForCondition(function() {
michael@0 92 return chats.selectedChat && chats.selectedChat.contentDocument &&
michael@0 93 chats.selectedChat.contentDocument.readyState == "complete";
michael@0 94 },function () {
michael@0 95 ok(chats.selectedChat, "should have a chatbox in our window again");
michael@0 96 ok(chats.selectedChat.getAttribute("label") == chatTitle,
michael@0 97 "the new chatbox should show the title of the chat window again");
michael@0 98 let testdiv = chats.selectedChat.contentDocument.getElementById("testdiv");
michael@0 99 is(testdiv.getAttribute("test"), "2", "docshell should have been swapped");
michael@0 100 chats.selectedChat.close();
michael@0 101 waitForCondition(function() {
michael@0 102 return chats.children.length == 0;
michael@0 103 },function () {
michael@0 104 next();
michael@0 105 });
michael@0 106 });
michael@0 107 }, false);
michael@0 108
michael@0 109 is(doc.documentElement.getAttribute("windowtype"), "Social:Chat", "Social:Chat window opened");
michael@0 110 // window is loaded, but the docswap does not happen until after load,
michael@0 111 // and we have no event to wait on, so we'll wait for document state
michael@0 112 // to be ready
michael@0 113 let chatbox = doc.getElementById("chatter");
michael@0 114 waitForCondition(function() {
michael@0 115 return chats.selectedChat == null &&
michael@0 116 chatbox.contentDocument &&
michael@0 117 chatbox.contentDocument.readyState == "complete";
michael@0 118 },function() {
michael@0 119 ok(chatbox.getAttribute("label") == chatTitle,
michael@0 120 "detached window should show the title of the chat window");
michael@0 121 let testdiv = chatbox.contentDocument.getElementById("testdiv");
michael@0 122 is(testdiv.getAttribute("test"), "1", "docshell should have been swapped");
michael@0 123 testdiv.setAttribute("test", "2");
michael@0 124 // swap the window back to the chatbar
michael@0 125 let swap = doc.getAnonymousElementByAttribute(chatbox, "anonid", "swap");
michael@0 126 swap.click();
michael@0 127 }, domwindow);
michael@0 128 }, false);
michael@0 129 }
michael@0 130 });
michael@0 131
michael@0 132 port.postMessage({topic: "test-init", data: { id: 1 }});
michael@0 133 },
michael@0 134
michael@0 135 testCloseOnLogout: function(next) {
michael@0 136 let chats = document.getElementById("pinnedchats");
michael@0 137 const chatUrl = "https://example.com/browser/browser/base/content/test/social/social_chat.html";
michael@0 138 let port = SocialSidebar.provider.getWorkerPort();
michael@0 139 ok(port, "provider has a port");
michael@0 140 port.postMessage({topic: "test-init"});
michael@0 141 port.onmessage = function (e) {
michael@0 142 let topic = e.data.topic;
michael@0 143 switch (topic) {
michael@0 144 case "got-chatbox-visibility":
michael@0 145 // chatbox is open, lets detach. The new chat window will be caught in
michael@0 146 // the window watcher below
michael@0 147 let doc = chats.selectedChat.contentDocument;
michael@0 148 // This message is (sometimes!) received a second time
michael@0 149 // before we start our tests from the onCloseWindow
michael@0 150 // callback.
michael@0 151 if (doc.location == "about:blank")
michael@0 152 return;
michael@0 153 info("chatbox is open, detach from window");
michael@0 154 let swap = document.getAnonymousElementByAttribute(chats.selectedChat, "anonid", "swap");
michael@0 155 swap.click();
michael@0 156 break;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 Services.wm.addListener({
michael@0 161 onWindowTitleChange: function() {},
michael@0 162 onCloseWindow: function(xulwindow) {},
michael@0 163 onOpenWindow: function(xulwindow) {
michael@0 164 let domwindow = xulwindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 165 .getInterface(Components.interfaces.nsIDOMWindow);
michael@0 166 Services.wm.removeListener(this);
michael@0 167 // wait for load to ensure the window is ready for us to test, make sure
michael@0 168 // we're not getting called for about:blank
michael@0 169 domwindow.addEventListener("load", function _load(event) {
michael@0 170 let doc = domwindow.document;
michael@0 171 if (event.target != doc)
michael@0 172 return;
michael@0 173 domwindow.removeEventListener("load", _load, false);
michael@0 174
michael@0 175 domwindow.addEventListener("unload", function _close(event) {
michael@0 176 if (event.target != doc)
michael@0 177 return;
michael@0 178 domwindow.removeEventListener("unload", _close, false);
michael@0 179 ok(true, "window has been closed");
michael@0 180 next();
michael@0 181 }, false);
michael@0 182
michael@0 183 is(doc.documentElement.getAttribute("windowtype"), "Social:Chat", "Social:Chat window opened");
michael@0 184 // window is loaded, but the docswap does not happen until after load,
michael@0 185 // and we have no event to wait on, so we'll wait for document state
michael@0 186 // to be ready
michael@0 187 let chatbox = doc.getElementById("chatter");
michael@0 188 waitForCondition(function() {
michael@0 189 return chats.children.length == 0 &&
michael@0 190 chatbox.contentDocument &&
michael@0 191 chatbox.contentDocument.readyState == "complete";
michael@0 192 },function() {
michael@0 193 // logout, we should get unload next
michael@0 194 port.postMessage({topic: "test-logout"});
michael@0 195 port.close();
michael@0 196 }, domwindow);
michael@0 197
michael@0 198 }, false);
michael@0 199 }
michael@0 200 });
michael@0 201
michael@0 202 port.postMessage({topic: "test-worker-chat", data: chatUrl});
michael@0 203 },
michael@0 204
michael@0 205 testReattachTwice: function(next) {
michael@0 206 let chats = document.getElementById("pinnedchats");
michael@0 207 const chatUrl = "https://example.com/browser/browser/base/content/test/social/social_chat.html";
michael@0 208 let chatBoxCount = 0, reattachCount = 0;
michael@0 209 let port = SocialSidebar.provider.getWorkerPort();
michael@0 210 ok(port, "provider has a port");
michael@0 211 port.postMessage({topic: "test-init"});
michael@0 212 port.onmessage = function (e) {
michael@0 213 let topic = e.data.topic;
michael@0 214 switch (topic) {
michael@0 215 case "got-chatbox-visibility":
michael@0 216 // chatbox is open, lets detach. The new chat window will be caught in
michael@0 217 // the window watcher below
michael@0 218 let doc = chats.selectedChat.contentDocument;
michael@0 219 // This message is (sometimes!) received a second time
michael@0 220 // before we start our tests from the onCloseWindow
michael@0 221 // callback.
michael@0 222 if (doc.location == "about:blank")
michael@0 223 return;
michael@0 224 if (++chatBoxCount != 2) {
michael@0 225 // open the second chat window
michael@0 226 port.postMessage({topic: "test-worker-chat", data: chatUrl + "?id=2"});
michael@0 227 return;
michael@0 228 }
michael@0 229 info("chatbox is open, detach from window");
michael@0 230 let chat1 = chats.firstChild;
michael@0 231 let chat2 = chat1.nextSibling;
michael@0 232 document.getAnonymousElementByAttribute(chat1, "anonid", "swap").click();
michael@0 233 document.getAnonymousElementByAttribute(chat2, "anonid", "swap").click();
michael@0 234 break;
michael@0 235 }
michael@0 236 };
michael@0 237
michael@0 238 let firstChatWindowDoc;
michael@0 239 Services.wm.addListener({
michael@0 240 onWindowTitleChange: function() {},
michael@0 241 onCloseWindow: function(xulwindow) {},
michael@0 242 onOpenWindow: function(xulwindow) {
michael@0 243 let listener = this;
michael@0 244 let domwindow = xulwindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 245 .getInterface(Components.interfaces.nsIDOMWindow);
michael@0 246 // wait for load to ensure the window is ready for us to test, make sure
michael@0 247 // we're not getting called for about:blank
michael@0 248 domwindow.addEventListener("load", function _load(event) {
michael@0 249 let doc = domwindow.document;
michael@0 250 if (event.target != doc)
michael@0 251 return;
michael@0 252 domwindow.removeEventListener("load", _load, false);
michael@0 253
michael@0 254 domwindow.addEventListener("unload", function _close(event) {
michael@0 255 if (event.target != doc)
michael@0 256 return;
michael@0 257 domwindow.removeEventListener("unload", _close, false);
michael@0 258 ok(true, "window has been closed");
michael@0 259 waitForCondition(function() {
michael@0 260 return chats.selectedChat && chats.selectedChat.contentDocument &&
michael@0 261 chats.selectedChat.contentDocument.readyState == "complete";
michael@0 262 }, function () {
michael@0 263 ++reattachCount;
michael@0 264 if (reattachCount == 1) {
michael@0 265 info("reattaching second chat window");
michael@0 266 let chatbox = firstChatWindowDoc.getElementById("chatter");
michael@0 267 firstChatWindowDoc.getAnonymousElementByAttribute(chatbox, "anonid", "swap").click();
michael@0 268 firstChatWindowDoc = null;
michael@0 269 }
michael@0 270 else if (reattachCount == 2) {
michael@0 271 is(chats.children.length, 2, "both chat windows should be reattached");
michael@0 272 chats.removeAll();
michael@0 273 waitForCondition(() => chats.children.length == 0, function () {
michael@0 274 info("no chat window left");
michael@0 275 is(chats.chatboxForURL.size, 0, "chatboxForURL map should be empty");
michael@0 276 next();
michael@0 277 });
michael@0 278 }
michael@0 279 }, "waited too long for the window to reattach");
michael@0 280 }, false);
michael@0 281
michael@0 282 is(doc.documentElement.getAttribute("windowtype"), "Social:Chat", "Social:Chat window opened");
michael@0 283 if (!firstChatWindowDoc) {
michael@0 284 firstChatWindowDoc = doc;
michael@0 285 return;
michael@0 286 }
michael@0 287 Services.wm.removeListener(listener);
michael@0 288
michael@0 289 // window is loaded, but the docswap does not happen until after load,
michael@0 290 // and we have no event to wait on, so we'll wait for document state
michael@0 291 // to be ready
michael@0 292 let chatbox = doc.getElementById("chatter");
michael@0 293 waitForCondition(function() {
michael@0 294 return chats.children.length == 0 &&
michael@0 295 chatbox.contentDocument &&
michael@0 296 chatbox.contentDocument.readyState == "complete";
michael@0 297 },function() {
michael@0 298 info("reattaching chat window");
michael@0 299 doc.getAnonymousElementByAttribute(chatbox, "anonid", "swap").click();
michael@0 300 }, "waited too long for the chat window to be detached");
michael@0 301
michael@0 302 }, false);
michael@0 303 }
michael@0 304 });
michael@0 305
michael@0 306 port.postMessage({topic: "test-worker-chat", data: chatUrl + "?id=1"});
michael@0 307 }
michael@0 308 };

mercurial