michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // This tests simulated drag and drop of files into the add-ons manager. michael@0: // We test with the add-ons manager in its own tab if in Firefox otherwise michael@0: // in its own window. michael@0: // Tests are only simulations of the drag and drop events, we cannot really do michael@0: // this automatically. michael@0: michael@0: // Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests, michael@0: // we only need ChromeUtils.js for a few files which is why we are using loadSubScript. michael@0: var gManagerWindow; michael@0: var ChromeUtils = {}; michael@0: this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. michael@0: getService(Ci.mozIJSSubScriptLoader); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils); michael@0: michael@0: // This listens for the next opened window and checks it is of the right url. michael@0: // opencallback is called when the new window is fully loaded michael@0: // closecallback is called when the window is closed michael@0: function WindowOpenListener(url, opencallback, closecallback) { michael@0: this.url = url; michael@0: this.opencallback = opencallback; michael@0: this.closecallback = closecallback; michael@0: michael@0: var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Components.interfaces.nsIWindowMediator); michael@0: wm.addListener(this); michael@0: } michael@0: michael@0: WindowOpenListener.prototype = { michael@0: url: null, michael@0: opencallback: null, michael@0: closecallback: null, michael@0: window: null, michael@0: domwindow: null, michael@0: michael@0: handleEvent: function(event) { michael@0: is(this.domwindow.document.location.href, this.url, "Should have opened the correct window"); michael@0: michael@0: this.domwindow.removeEventListener("load", this, false); michael@0: // Allow any other load handlers to execute michael@0: var self = this; michael@0: executeSoon(function() { self.opencallback(self.domwindow); } ); michael@0: }, michael@0: michael@0: onWindowTitleChange: function(window, title) { michael@0: }, michael@0: michael@0: onOpenWindow: function(window) { michael@0: if (this.window) michael@0: return; michael@0: michael@0: this.window = window; michael@0: this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) michael@0: .getInterface(Components.interfaces.nsIDOMWindow); michael@0: this.domwindow.addEventListener("load", this, false); michael@0: }, michael@0: michael@0: onCloseWindow: function(window) { michael@0: if (this.window != window) michael@0: return; michael@0: michael@0: var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Components.interfaces.nsIWindowMediator); michael@0: wm.removeListener(this); michael@0: this.opencallback = null; michael@0: this.window = null; michael@0: this.domwindow = null; michael@0: michael@0: // Let the window close complete michael@0: executeSoon(this.closecallback); michael@0: this.closecallback = null; michael@0: } michael@0: }; michael@0: michael@0: var gSawInstallNotification = false; michael@0: var gInstallNotificationObserver = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo); michael@0: isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow"); michael@0: gSawInstallNotification = true; michael@0: Services.obs.removeObserver(this, "addon-install-started"); michael@0: } michael@0: }; michael@0: michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: open_manager("addons://list/extension", function(aWindow) { michael@0: gManagerWindow = aWindow; michael@0: run_next_test(); michael@0: }); michael@0: } michael@0: michael@0: function end_test() { michael@0: close_manager(gManagerWindow, function() { michael@0: finish(); michael@0: }); michael@0: } michael@0: michael@0: function test_confirmation(aWindow, aExpectedURLs) { michael@0: var list = aWindow.document.getElementById("itemList"); michael@0: is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs"); michael@0: michael@0: for (let url of aExpectedURLs) { michael@0: let found = false; michael@0: for (let node of list.children) { michael@0: if (node.url == url) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: ok(found, "Should have seen " + url + " in the list"); michael@0: } michael@0: michael@0: aWindow.document.documentElement.cancelDialog(); michael@0: } michael@0: michael@0: // Simulates dropping a URL onto the manager michael@0: add_test(function() { michael@0: var url = TESTROOT + "addons/browser_dragdrop1.xpi"; michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: test_confirmation(aWindow, [url]); michael@0: }, function() { michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: var viewContainer = gManagerWindow.document.getElementById("view-port"); michael@0: var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, michael@0: [[{type: "text/x-moz-url", data: url}]], michael@0: "copy", gManagerWindow); michael@0: is(effect, "copy", "Drag should be accepted"); michael@0: }); michael@0: michael@0: // Simulates dropping a file onto the manager michael@0: add_test(function() { michael@0: var fileurl = get_addon_file_url("browser_dragdrop1.xpi"); michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: test_confirmation(aWindow, [fileurl.spec]); michael@0: }, function() { michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: var viewContainer = gManagerWindow.document.getElementById("view-port"); michael@0: var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, michael@0: [[{type: "application/x-moz-file", data: fileurl.file}]], michael@0: "copy", gManagerWindow); michael@0: is(effect, "copy", "Drag should be accepted"); michael@0: }); michael@0: michael@0: // Simulates dropping two urls onto the manager michael@0: add_test(function() { michael@0: var url1 = TESTROOT + "addons/browser_dragdrop1.xpi"; michael@0: var url2 = TESTROOT2 + "addons/browser_dragdrop2.xpi"; michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: test_confirmation(aWindow, [url1, url2]); michael@0: }, function() { michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: var viewContainer = gManagerWindow.document.getElementById("view-port"); michael@0: var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, michael@0: [[{type: "text/x-moz-url", data: url1}], michael@0: [{type: "text/x-moz-url", data: url2}]], michael@0: "copy", gManagerWindow); michael@0: is(effect, "copy", "Drag should be accepted"); michael@0: }); michael@0: michael@0: // Simulates dropping two files onto the manager michael@0: add_test(function() { michael@0: var fileurl1 = get_addon_file_url("browser_dragdrop1.xpi"); michael@0: var fileurl2 = get_addon_file_url("browser_dragdrop2.xpi"); michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: test_confirmation(aWindow, [fileurl1.spec, fileurl2.spec]); michael@0: }, function() { michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: var viewContainer = gManagerWindow.document.getElementById("view-port"); michael@0: var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, michael@0: [[{type: "application/x-moz-file", data: fileurl1.file}], michael@0: [{type: "application/x-moz-file", data: fileurl2.file}]], michael@0: "copy", gManagerWindow); michael@0: is(effect, "copy", "Drag should be accepted"); michael@0: }); michael@0: michael@0: // Simulates dropping a file and a url onto the manager (weird, but should still work) michael@0: add_test(function() { michael@0: var url = TESTROOT + "addons/browser_dragdrop1.xpi"; michael@0: var fileurl = get_addon_file_url("browser_dragdrop2.xpi"); michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: test_confirmation(aWindow, [url, fileurl.spec]); michael@0: }, function() { michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: var viewContainer = gManagerWindow.document.getElementById("view-port"); michael@0: var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, michael@0: [[{type: "text/x-moz-url", data: url}], michael@0: [{type: "application/x-moz-file", data: fileurl.file}]], michael@0: "copy", gManagerWindow); michael@0: is(effect, "copy", "Drag should be accepted"); michael@0: });