toolkit/mozapps/extensions/test/browser/browser_bug567127.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_bug567127.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     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 +// Tests bug 567127 - Add install button to the add-ons manager
     1.9 +
    1.10 +var MockFilePicker = SpecialPowers.MockFilePicker;
    1.11 +MockFilePicker.init(window);
    1.12 +
    1.13 +var gManagerWindow;
    1.14 +var gSawInstallNotification = false;
    1.15 +
    1.16 +// This listens for the next opened window and checks it is of the right url.
    1.17 +// opencallback is called when the new window is fully loaded
    1.18 +// closecallback is called when the window is closed
    1.19 +function WindowOpenListener(url, opencallback, closecallback) {
    1.20 +  this.url = url;
    1.21 +  this.opencallback = opencallback;
    1.22 +  this.closecallback = closecallback;
    1.23 +
    1.24 +  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    1.25 +                     .getService(Components.interfaces.nsIWindowMediator);
    1.26 +  wm.addListener(this);
    1.27 +}
    1.28 +
    1.29 +WindowOpenListener.prototype = {
    1.30 +  url: null,
    1.31 +  opencallback: null,
    1.32 +  closecallback: null,
    1.33 +  window: null,
    1.34 +  domwindow: null,
    1.35 +
    1.36 +  handleEvent: function(event) {
    1.37 +    is(this.domwindow.document.location.href, this.url, "Should have opened the correct window");
    1.38 +
    1.39 +    this.domwindow.removeEventListener("load", this, false);
    1.40 +    // Allow any other load handlers to execute
    1.41 +    var self = this;
    1.42 +    executeSoon(function() { self.opencallback(self.domwindow); } );
    1.43 +  },
    1.44 +
    1.45 +  onWindowTitleChange: function(window, title) {
    1.46 +  },
    1.47 +
    1.48 +  onOpenWindow: function(window) {
    1.49 +    if (this.window)
    1.50 +      return;
    1.51 +
    1.52 +    this.window = window;
    1.53 +    this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    1.54 +                           .getInterface(Components.interfaces.nsIDOMWindow);
    1.55 +    this.domwindow.addEventListener("load", this, false);
    1.56 +  },
    1.57 +
    1.58 +  onCloseWindow: function(window) {
    1.59 +    if (this.window != window)
    1.60 +      return;
    1.61 +
    1.62 +    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    1.63 +                       .getService(Components.interfaces.nsIWindowMediator);
    1.64 +    wm.removeListener(this);
    1.65 +    this.opencallback = null;
    1.66 +    this.window = null;
    1.67 +    this.domwindow = null;
    1.68 +
    1.69 +    // Let the window close complete
    1.70 +    executeSoon(this.closecallback);
    1.71 +    this.closecallback = null;
    1.72 +  }
    1.73 +};
    1.74 +
    1.75 +
    1.76 +var gInstallNotificationObserver = {
    1.77 +  observe: function(aSubject, aTopic, aData) {
    1.78 +    var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo);
    1.79 +    isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow");
    1.80 +    gSawInstallNotification = true;
    1.81 +    Services.obs.removeObserver(this, "addon-install-started");
    1.82 +  }
    1.83 +};
    1.84 +
    1.85 +
    1.86 +function test_confirmation(aWindow, aExpectedURLs) {
    1.87 +  var list = aWindow.document.getElementById("itemList");
    1.88 +  is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs");
    1.89 +
    1.90 +  for (let url of aExpectedURLs) {
    1.91 +    let found = false;
    1.92 +    for (let node of list.children) {
    1.93 +      if (node.url == url) {
    1.94 +        found = true;
    1.95 +        break;
    1.96 +      }
    1.97 +    }
    1.98 +    ok(found, "Should have seen " + url + " in the list");
    1.99 +  }
   1.100 +
   1.101 +  aWindow.document.documentElement.cancelDialog();
   1.102 +}
   1.103 +
   1.104 +add_task(function* test_install_from_file() {
   1.105 +  gManagerWindow = yield open_manager("addons://list/extension");
   1.106 +
   1.107 +  var filePaths = [
   1.108 +                   get_addon_file_url("browser_bug567127_1.xpi"),
   1.109 +                   get_addon_file_url("browser_bug567127_2.xpi")
   1.110 +                  ];
   1.111 +  MockFilePicker.returnFiles = filePaths.map(function(aPath) aPath.file);
   1.112 +  
   1.113 +  Services.obs.addObserver(gInstallNotificationObserver,
   1.114 +                           "addon-install-started", false);
   1.115 +
   1.116 +  // Set handler that executes the core test after the window opens,
   1.117 +  // and resolves the promise when the window closes
   1.118 +  let pInstallURIClosed = new Promise((resolve, reject) => {
   1.119 +    new WindowOpenListener(INSTALL_URI, function(aWindow) {
   1.120 +      try {
   1.121 +        test_confirmation(aWindow, filePaths.map(function(aPath) aPath.spec));
   1.122 +      } catch(e) {
   1.123 +        reject(e);
   1.124 +      }
   1.125 +    }, resolve);
   1.126 +  });
   1.127 +
   1.128 +  gManagerWindow.gViewController.doCommand("cmd_installFromFile");
   1.129 +
   1.130 +  yield pInstallURIClosed;
   1.131 +
   1.132 +  is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
   1.133 +
   1.134 +  MockFilePicker.cleanup();
   1.135 +  yield close_manager(gManagerWindow);
   1.136 +});

mercurial