toolkit/mozapps/extensions/test/browser/browser_dragdrop.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 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // This tests simulated drag and drop of files into the add-ons manager.
michael@0 6 // We test with the add-ons manager in its own tab if in Firefox otherwise
michael@0 7 // in its own window.
michael@0 8 // Tests are only simulations of the drag and drop events, we cannot really do
michael@0 9 // this automatically.
michael@0 10
michael@0 11 // Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
michael@0 12 // we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
michael@0 13 var gManagerWindow;
michael@0 14 var ChromeUtils = {};
michael@0 15 this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
michael@0 16 getService(Ci.mozIJSSubScriptLoader);
michael@0 17 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils);
michael@0 18
michael@0 19 // This listens for the next opened window and checks it is of the right url.
michael@0 20 // opencallback is called when the new window is fully loaded
michael@0 21 // closecallback is called when the window is closed
michael@0 22 function WindowOpenListener(url, opencallback, closecallback) {
michael@0 23 this.url = url;
michael@0 24 this.opencallback = opencallback;
michael@0 25 this.closecallback = closecallback;
michael@0 26
michael@0 27 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
michael@0 28 .getService(Components.interfaces.nsIWindowMediator);
michael@0 29 wm.addListener(this);
michael@0 30 }
michael@0 31
michael@0 32 WindowOpenListener.prototype = {
michael@0 33 url: null,
michael@0 34 opencallback: null,
michael@0 35 closecallback: null,
michael@0 36 window: null,
michael@0 37 domwindow: null,
michael@0 38
michael@0 39 handleEvent: function(event) {
michael@0 40 is(this.domwindow.document.location.href, this.url, "Should have opened the correct window");
michael@0 41
michael@0 42 this.domwindow.removeEventListener("load", this, false);
michael@0 43 // Allow any other load handlers to execute
michael@0 44 var self = this;
michael@0 45 executeSoon(function() { self.opencallback(self.domwindow); } );
michael@0 46 },
michael@0 47
michael@0 48 onWindowTitleChange: function(window, title) {
michael@0 49 },
michael@0 50
michael@0 51 onOpenWindow: function(window) {
michael@0 52 if (this.window)
michael@0 53 return;
michael@0 54
michael@0 55 this.window = window;
michael@0 56 this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 57 .getInterface(Components.interfaces.nsIDOMWindow);
michael@0 58 this.domwindow.addEventListener("load", this, false);
michael@0 59 },
michael@0 60
michael@0 61 onCloseWindow: function(window) {
michael@0 62 if (this.window != window)
michael@0 63 return;
michael@0 64
michael@0 65 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
michael@0 66 .getService(Components.interfaces.nsIWindowMediator);
michael@0 67 wm.removeListener(this);
michael@0 68 this.opencallback = null;
michael@0 69 this.window = null;
michael@0 70 this.domwindow = null;
michael@0 71
michael@0 72 // Let the window close complete
michael@0 73 executeSoon(this.closecallback);
michael@0 74 this.closecallback = null;
michael@0 75 }
michael@0 76 };
michael@0 77
michael@0 78 var gSawInstallNotification = false;
michael@0 79 var gInstallNotificationObserver = {
michael@0 80 observe: function(aSubject, aTopic, aData) {
michael@0 81 var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo);
michael@0 82 isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow");
michael@0 83 gSawInstallNotification = true;
michael@0 84 Services.obs.removeObserver(this, "addon-install-started");
michael@0 85 }
michael@0 86 };
michael@0 87
michael@0 88
michael@0 89 function test() {
michael@0 90 waitForExplicitFinish();
michael@0 91
michael@0 92 open_manager("addons://list/extension", function(aWindow) {
michael@0 93 gManagerWindow = aWindow;
michael@0 94 run_next_test();
michael@0 95 });
michael@0 96 }
michael@0 97
michael@0 98 function end_test() {
michael@0 99 close_manager(gManagerWindow, function() {
michael@0 100 finish();
michael@0 101 });
michael@0 102 }
michael@0 103
michael@0 104 function test_confirmation(aWindow, aExpectedURLs) {
michael@0 105 var list = aWindow.document.getElementById("itemList");
michael@0 106 is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs");
michael@0 107
michael@0 108 for (let url of aExpectedURLs) {
michael@0 109 let found = false;
michael@0 110 for (let node of list.children) {
michael@0 111 if (node.url == url) {
michael@0 112 found = true;
michael@0 113 break;
michael@0 114 }
michael@0 115 }
michael@0 116 ok(found, "Should have seen " + url + " in the list");
michael@0 117 }
michael@0 118
michael@0 119 aWindow.document.documentElement.cancelDialog();
michael@0 120 }
michael@0 121
michael@0 122 // Simulates dropping a URL onto the manager
michael@0 123 add_test(function() {
michael@0 124 var url = TESTROOT + "addons/browser_dragdrop1.xpi";
michael@0 125
michael@0 126 Services.obs.addObserver(gInstallNotificationObserver,
michael@0 127 "addon-install-started", false);
michael@0 128
michael@0 129 new WindowOpenListener(INSTALL_URI, function(aWindow) {
michael@0 130 test_confirmation(aWindow, [url]);
michael@0 131 }, function() {
michael@0 132 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
michael@0 133 run_next_test();
michael@0 134 });
michael@0 135
michael@0 136 var viewContainer = gManagerWindow.document.getElementById("view-port");
michael@0 137 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
michael@0 138 [[{type: "text/x-moz-url", data: url}]],
michael@0 139 "copy", gManagerWindow);
michael@0 140 is(effect, "copy", "Drag should be accepted");
michael@0 141 });
michael@0 142
michael@0 143 // Simulates dropping a file onto the manager
michael@0 144 add_test(function() {
michael@0 145 var fileurl = get_addon_file_url("browser_dragdrop1.xpi");
michael@0 146
michael@0 147 Services.obs.addObserver(gInstallNotificationObserver,
michael@0 148 "addon-install-started", false);
michael@0 149
michael@0 150 new WindowOpenListener(INSTALL_URI, function(aWindow) {
michael@0 151 test_confirmation(aWindow, [fileurl.spec]);
michael@0 152 }, function() {
michael@0 153 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
michael@0 154 run_next_test();
michael@0 155 });
michael@0 156
michael@0 157 var viewContainer = gManagerWindow.document.getElementById("view-port");
michael@0 158 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
michael@0 159 [[{type: "application/x-moz-file", data: fileurl.file}]],
michael@0 160 "copy", gManagerWindow);
michael@0 161 is(effect, "copy", "Drag should be accepted");
michael@0 162 });
michael@0 163
michael@0 164 // Simulates dropping two urls onto the manager
michael@0 165 add_test(function() {
michael@0 166 var url1 = TESTROOT + "addons/browser_dragdrop1.xpi";
michael@0 167 var url2 = TESTROOT2 + "addons/browser_dragdrop2.xpi";
michael@0 168
michael@0 169 Services.obs.addObserver(gInstallNotificationObserver,
michael@0 170 "addon-install-started", false);
michael@0 171
michael@0 172 new WindowOpenListener(INSTALL_URI, function(aWindow) {
michael@0 173 test_confirmation(aWindow, [url1, url2]);
michael@0 174 }, function() {
michael@0 175 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
michael@0 176 run_next_test();
michael@0 177 });
michael@0 178
michael@0 179 var viewContainer = gManagerWindow.document.getElementById("view-port");
michael@0 180 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
michael@0 181 [[{type: "text/x-moz-url", data: url1}],
michael@0 182 [{type: "text/x-moz-url", data: url2}]],
michael@0 183 "copy", gManagerWindow);
michael@0 184 is(effect, "copy", "Drag should be accepted");
michael@0 185 });
michael@0 186
michael@0 187 // Simulates dropping two files onto the manager
michael@0 188 add_test(function() {
michael@0 189 var fileurl1 = get_addon_file_url("browser_dragdrop1.xpi");
michael@0 190 var fileurl2 = get_addon_file_url("browser_dragdrop2.xpi");
michael@0 191
michael@0 192 Services.obs.addObserver(gInstallNotificationObserver,
michael@0 193 "addon-install-started", false);
michael@0 194
michael@0 195 new WindowOpenListener(INSTALL_URI, function(aWindow) {
michael@0 196 test_confirmation(aWindow, [fileurl1.spec, fileurl2.spec]);
michael@0 197 }, function() {
michael@0 198 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
michael@0 199 run_next_test();
michael@0 200 });
michael@0 201
michael@0 202 var viewContainer = gManagerWindow.document.getElementById("view-port");
michael@0 203 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
michael@0 204 [[{type: "application/x-moz-file", data: fileurl1.file}],
michael@0 205 [{type: "application/x-moz-file", data: fileurl2.file}]],
michael@0 206 "copy", gManagerWindow);
michael@0 207 is(effect, "copy", "Drag should be accepted");
michael@0 208 });
michael@0 209
michael@0 210 // Simulates dropping a file and a url onto the manager (weird, but should still work)
michael@0 211 add_test(function() {
michael@0 212 var url = TESTROOT + "addons/browser_dragdrop1.xpi";
michael@0 213 var fileurl = get_addon_file_url("browser_dragdrop2.xpi");
michael@0 214
michael@0 215 Services.obs.addObserver(gInstallNotificationObserver,
michael@0 216 "addon-install-started", false);
michael@0 217
michael@0 218 new WindowOpenListener(INSTALL_URI, function(aWindow) {
michael@0 219 test_confirmation(aWindow, [url, fileurl.spec]);
michael@0 220 }, function() {
michael@0 221 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
michael@0 222 run_next_test();
michael@0 223 });
michael@0 224
michael@0 225 var viewContainer = gManagerWindow.document.getElementById("view-port");
michael@0 226 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
michael@0 227 [[{type: "text/x-moz-url", data: url}],
michael@0 228 [{type: "application/x-moz-file", data: fileurl.file}]],
michael@0 229 "copy", gManagerWindow);
michael@0 230 is(effect, "copy", "Drag should be accepted");
michael@0 231 });

mercurial