toolkit/mozapps/extensions/test/browser/browser_dragdrop.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/browser/browser_dragdrop.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.6 + */
     1.7 +
     1.8 +// This tests simulated drag and drop of files into the add-ons manager.
     1.9 +// We test with the add-ons manager in its own tab if in Firefox otherwise
    1.10 +// in its own window.
    1.11 +// Tests are only simulations of the drag and drop events, we cannot really do
    1.12 +// this automatically.
    1.13 +
    1.14 +// Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
    1.15 +// we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
    1.16 +var gManagerWindow;
    1.17 +var ChromeUtils = {};
    1.18 +this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
    1.19 +                     getService(Ci.mozIJSSubScriptLoader);
    1.20 +this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils);
    1.21 +
    1.22 +// This listens for the next opened window and checks it is of the right url.
    1.23 +// opencallback is called when the new window is fully loaded
    1.24 +// closecallback is called when the window is closed
    1.25 +function WindowOpenListener(url, opencallback, closecallback) {
    1.26 +  this.url = url;
    1.27 +  this.opencallback = opencallback;
    1.28 +  this.closecallback = closecallback;
    1.29 +
    1.30 +  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    1.31 +                     .getService(Components.interfaces.nsIWindowMediator);
    1.32 +  wm.addListener(this);
    1.33 +}
    1.34 +
    1.35 +WindowOpenListener.prototype = {
    1.36 +  url: null,
    1.37 +  opencallback: null,
    1.38 +  closecallback: null,
    1.39 +  window: null,
    1.40 +  domwindow: null,
    1.41 +
    1.42 +  handleEvent: function(event) {
    1.43 +    is(this.domwindow.document.location.href, this.url, "Should have opened the correct window");
    1.44 +
    1.45 +    this.domwindow.removeEventListener("load", this, false);
    1.46 +    // Allow any other load handlers to execute
    1.47 +    var self = this;
    1.48 +    executeSoon(function() { self.opencallback(self.domwindow); } );
    1.49 +  },
    1.50 +
    1.51 +  onWindowTitleChange: function(window, title) {
    1.52 +  },
    1.53 +
    1.54 +  onOpenWindow: function(window) {
    1.55 +    if (this.window)
    1.56 +      return;
    1.57 +
    1.58 +    this.window = window;
    1.59 +    this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    1.60 +                           .getInterface(Components.interfaces.nsIDOMWindow);
    1.61 +    this.domwindow.addEventListener("load", this, false);
    1.62 +  },
    1.63 +
    1.64 +  onCloseWindow: function(window) {
    1.65 +    if (this.window != window)
    1.66 +      return;
    1.67 +
    1.68 +    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    1.69 +                       .getService(Components.interfaces.nsIWindowMediator);
    1.70 +    wm.removeListener(this);
    1.71 +    this.opencallback = null;
    1.72 +    this.window = null;
    1.73 +    this.domwindow = null;
    1.74 +
    1.75 +    // Let the window close complete
    1.76 +    executeSoon(this.closecallback);
    1.77 +    this.closecallback = null;
    1.78 +  }
    1.79 +};
    1.80 +
    1.81 +var gSawInstallNotification = false;
    1.82 +var gInstallNotificationObserver = {
    1.83 +  observe: function(aSubject, aTopic, aData) {
    1.84 +    var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo);
    1.85 +    isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow");
    1.86 +    gSawInstallNotification = true;
    1.87 +    Services.obs.removeObserver(this, "addon-install-started");
    1.88 +  }
    1.89 +};
    1.90 +
    1.91 +
    1.92 +function test() {
    1.93 +  waitForExplicitFinish();
    1.94 +
    1.95 +  open_manager("addons://list/extension", function(aWindow) {
    1.96 +    gManagerWindow = aWindow;
    1.97 +    run_next_test();
    1.98 +  });
    1.99 +}
   1.100 +
   1.101 +function end_test() {
   1.102 +  close_manager(gManagerWindow, function() {
   1.103 +    finish();
   1.104 +  });
   1.105 +}
   1.106 +
   1.107 +function test_confirmation(aWindow, aExpectedURLs) {
   1.108 +  var list = aWindow.document.getElementById("itemList");
   1.109 +  is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs");
   1.110 +
   1.111 +  for (let url of aExpectedURLs) {
   1.112 +    let found = false;
   1.113 +    for (let node of list.children) {
   1.114 +      if (node.url == url) {
   1.115 +        found = true;
   1.116 +        break;
   1.117 +      }
   1.118 +    }
   1.119 +    ok(found, "Should have seen " + url + " in the list");
   1.120 +  }
   1.121 +
   1.122 +  aWindow.document.documentElement.cancelDialog();
   1.123 +}
   1.124 +
   1.125 +// Simulates dropping a URL onto the manager
   1.126 +add_test(function() {
   1.127 +  var url = TESTROOT + "addons/browser_dragdrop1.xpi";
   1.128 +
   1.129 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.130 +                           "addon-install-started", false);
   1.131 +
   1.132 +  new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.133 +    test_confirmation(aWindow, [url]);
   1.134 +  }, function() {
   1.135 +    is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.136 +    run_next_test();
   1.137 +  });
   1.138 +
   1.139 +  var viewContainer = gManagerWindow.document.getElementById("view-port");
   1.140 +  var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
   1.141 +               [[{type: "text/x-moz-url", data: url}]],
   1.142 +               "copy", gManagerWindow);
   1.143 +  is(effect, "copy", "Drag should be accepted");
   1.144 +});
   1.145 +
   1.146 +// Simulates dropping a file onto the manager
   1.147 +add_test(function() {
   1.148 +  var fileurl = get_addon_file_url("browser_dragdrop1.xpi");
   1.149 +
   1.150 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.151 +                           "addon-install-started", false);
   1.152 +
   1.153 +  new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.154 +    test_confirmation(aWindow, [fileurl.spec]);
   1.155 +  }, function() {
   1.156 +    is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.157 +    run_next_test();
   1.158 +  });
   1.159 +
   1.160 +  var viewContainer = gManagerWindow.document.getElementById("view-port");
   1.161 +  var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
   1.162 +               [[{type: "application/x-moz-file", data: fileurl.file}]],
   1.163 +               "copy", gManagerWindow);
   1.164 +  is(effect, "copy", "Drag should be accepted");
   1.165 +});
   1.166 +
   1.167 +// Simulates dropping two urls onto the manager
   1.168 +add_test(function() {
   1.169 +  var url1 = TESTROOT + "addons/browser_dragdrop1.xpi";
   1.170 +  var url2 = TESTROOT2 + "addons/browser_dragdrop2.xpi";
   1.171 +
   1.172 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.173 +                           "addon-install-started", false);
   1.174 +
   1.175 +  new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.176 +    test_confirmation(aWindow, [url1, url2]);
   1.177 +  }, function() {
   1.178 +    is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.179 +    run_next_test();
   1.180 +  });
   1.181 +
   1.182 +  var viewContainer = gManagerWindow.document.getElementById("view-port");
   1.183 +  var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
   1.184 +               [[{type: "text/x-moz-url", data: url1}],
   1.185 +                [{type: "text/x-moz-url", data: url2}]],
   1.186 +               "copy", gManagerWindow);
   1.187 +  is(effect, "copy", "Drag should be accepted");
   1.188 +});
   1.189 +
   1.190 +// Simulates dropping two files onto the manager
   1.191 +add_test(function() {
   1.192 +  var fileurl1 = get_addon_file_url("browser_dragdrop1.xpi");
   1.193 +  var fileurl2 = get_addon_file_url("browser_dragdrop2.xpi");
   1.194 +
   1.195 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.196 +                           "addon-install-started", false);
   1.197 +
   1.198 +  new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.199 +    test_confirmation(aWindow, [fileurl1.spec, fileurl2.spec]);
   1.200 +  }, function() {
   1.201 +    is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.202 +    run_next_test();
   1.203 +  });
   1.204 +
   1.205 +  var viewContainer = gManagerWindow.document.getElementById("view-port");
   1.206 +  var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
   1.207 +               [[{type: "application/x-moz-file", data: fileurl1.file}],
   1.208 +                [{type: "application/x-moz-file", data: fileurl2.file}]],
   1.209 +               "copy", gManagerWindow);
   1.210 +  is(effect, "copy", "Drag should be accepted");
   1.211 +});
   1.212 +
   1.213 +// Simulates dropping a file and a url onto the manager (weird, but should still work)
   1.214 +add_test(function() {
   1.215 +  var url = TESTROOT + "addons/browser_dragdrop1.xpi";
   1.216 +  var fileurl = get_addon_file_url("browser_dragdrop2.xpi");
   1.217 +
   1.218 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.219 +                           "addon-install-started", false);
   1.220 +
   1.221 +  new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.222 +    test_confirmation(aWindow, [url, fileurl.spec]);
   1.223 +  }, function() {
   1.224 +    is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.225 +    run_next_test();
   1.226 +  });
   1.227 +
   1.228 +  var viewContainer = gManagerWindow.document.getElementById("view-port");
   1.229 +  var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer,
   1.230 +               [[{type: "text/x-moz-url", data: url}],
   1.231 +                [{type: "application/x-moz-file", data: fileurl.file}]],
   1.232 +               "copy", gManagerWindow);
   1.233 +  is(effect, "copy", "Drag should be accepted");
   1.234 +});

mercurial