Bug 578580: Add category utilities for add-ons manager browser tests. r=dtownsend
authorBen Parr <bparr@mozilla.com>
Fri, 16 Jul 2010 11:17:35 -0700
changeset 47823 5fd0b1867f79c77d22d38b8232d77ea65f8ad6f1
parent 47822 aece5ded5654bf0840489bdb302a3cb405dcae3e
child 47824 0c4c442e5bb8d54814afbd5a3b008a5fac4ac3a6
push id14430
push userdtownsend@mozilla.com
push dateFri, 16 Jul 2010 18:18:41 +0000
treeherdermozilla-central@5fd0b1867f79 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdtownsend
bugs578580
milestone2.0b2pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 578580: Add category utilities for add-ons manager browser tests. r=dtownsend
toolkit/mozapps/extensions/test/browser/browser_bug562899.js
toolkit/mozapps/extensions/test/browser/browser_bug572561.js
toolkit/mozapps/extensions/test/browser/head.js
--- a/toolkit/mozapps/extensions/test/browser/browser_bug562899.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_bug562899.js
@@ -5,81 +5,76 @@
 // Simulates quickly switching between different list views to verify that only
 // the last selected is displayed
 
 Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm");
 
 const xpi = "browser/toolkit/mozapps/extensions/test/browser/browser_installssl.xpi";
 
 var gManagerWindow;
+var gCategoryUtilities;
 
 function test() {
   waitForExplicitFinish();
 
   // Add a lightweight theme so at least one theme exists
   LightweightThemeManager.currentTheme = {
     id: "test",
     name: "Test lightweight theme",
     headerURL: "http://example.com/header.png"
   };
 
   open_manager(null, function(aWindow) {
     gManagerWindow = aWindow;
+    gCategoryUtilities = new CategoryUtilities(gManagerWindow);
     run_next_test();
   });
 }
 
 function end_test() {
   close_manager(gManagerWindow, function() {
     LightweightThemeManager.forgetUsedTheme("test");
     finish();
   });
 }
 
 // Tests that loading a second view before the first has not finished loading
 // does not merge the results
 add_test(function() {
   var themeCount = null;
   var pluginCount = null;
-  var themeItem = gManagerWindow.document.getElementById("category-themes");
-  var pluginItem = gManagerWindow.document.getElementById("category-plugins");
+  var themeItem = gCategoryUtilities.get("theme");
+  var pluginItem = gCategoryUtilities.get("plugin");
   var list = gManagerWindow.document.getElementById("addon-list");
 
-  EventUtils.synthesizeMouse(themeItem, 2, 2, { }, gManagerWindow);
-
-  wait_for_view_load(gManagerWindow, function() {
+  gCategoryUtilities.open(themeItem, function() {
     themeCount = list.childNodes.length;
     ok(themeCount > 0, "Test is useless if there are no themes");
 
-    EventUtils.synthesizeMouse(pluginItem, 2, 2, { }, gManagerWindow);
-
-    wait_for_view_load(gManagerWindow, function() {
+    gCategoryUtilities.open(pluginItem, function() {
       pluginCount = list.childNodes.length;
       ok(pluginCount > 0, "Test is useless if there are no plugins");
 
-      EventUtils.synthesizeMouse(themeItem, 2, 2, { }, gManagerWindow);
-      EventUtils.synthesizeMouse(pluginItem, 2, 2, { }, gManagerWindow);
+      gCategoryUtilities.open(themeItem);
 
-      wait_for_view_load(gManagerWindow, function() {
+      gCategoryUtilities.open(pluginItem, function() {
         is(list.childNodes.length, pluginCount, "Should only see the plugins");
 
         var item = list.firstChild;
         while (item) {
           is(item.getAttribute("type"), "plugin", "All items should be plugins");
           item = item.nextSibling;
         }
 
         // Tests that switching to, from, to the same pane in quick succession
         // still only shows the right number of results
 
-        EventUtils.synthesizeMouse(themeItem, 2, 2, { }, gManagerWindow);
-        EventUtils.synthesizeMouse(pluginItem, 2, 2, { }, gManagerWindow);
-        EventUtils.synthesizeMouse(themeItem, 2, 2, { }, gManagerWindow);
-
-        wait_for_view_load(gManagerWindow, function() {
+        gCategoryUtilities.open(themeItem);
+        gCategoryUtilities.open(pluginItem);
+        gCategoryUtilities.open(themeItem, function() {
           is(list.childNodes.length, themeCount, "Should only see the theme");
 
           var item = list.firstChild;
           while (item) {
             is(item.getAttribute("type"), "theme", "All items should be theme");
             item = item.nextSibling;
           }
 
--- a/toolkit/mozapps/extensions/test/browser/browser_bug572561.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_bug572561.js
@@ -1,16 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 // Tests that the locale category is shown if there are no locale packs
 // installed but some are pending install
 
 var gManagerWindow;
+var gCategoryUtilities;
 var gProvider;
 var gInstallProperties = [{
   name: "Locale Category Test",
   type: "locale"
 }];
 var gInstall;
 var gExpectedCancel = false;
 var gTestInstallListener = {
@@ -36,59 +37,62 @@ var gTestInstallListener = {
 
 function test() {
   waitForExplicitFinish();
 
   gProvider = new MockProvider();
 
   open_manager(null, function(aWindow) {
     gManagerWindow = aWindow;
+    gCategoryUtilities = new CategoryUtilities(gManagerWindow);
     run_next_test();
   });
 }
 
 function end_test() {
   close_manager(gManagerWindow, function() {
     finish();
   });
 }
 
 function check_hidden(aExpectedHidden) {
-  var category = gManagerWindow.gCategories.get("addons://list/locale");
-  is(category.hidden, aExpectedHidden, "Should have correct hidden state");
+  var hidden = !gCategoryUtilities.isTypeVisible("locale");
+  is(hidden, aExpectedHidden, "Should have correct hidden state");
 }
 
 // Tests that a non-active install does not make the locale category show
 add_test(function() {
   check_hidden(true);
   gInstall = gProvider.createInstalls(gInstallProperties)[0];
   gInstall.addTestListener(gTestInstallListener);
   check_hidden(true);
   run_next_test();
 });
 
 // Test that restarting the add-on manager with a non-active install
 // does not cause the locale category to show
 add_test(function() {
   restart_manager(gManagerWindow, null, function(aWindow) {
     gManagerWindow = aWindow;
+    gCategoryUtilities = new CategoryUtilities(gManagerWindow);
     check_hidden(true);
     run_next_test();
   });
 });
 
 // Test that installing the install shows the locale category
 add_test(function() {
   gInstall.install();
 });
 
 // Test that restarting the add-on manager does not cause the locale category
 // to become hidden
 add_test(function() {
   restart_manager(gManagerWindow, null, function(aWindow) {
     gManagerWindow = aWindow;
+    gCategoryUtilities = new CategoryUtilities(gManagerWindow);
     check_hidden(false);
 
     gExpectedCancel = true;
     gInstall.cancel();
   });
 });
 
--- a/toolkit/mozapps/extensions/test/browser/head.js
+++ b/toolkit/mozapps/extensions/test/browser/head.js
@@ -102,16 +102,89 @@ function close_manager(aManagerWindow, a
 
   aManagerWindow.close();
 }
 
 function restart_manager(aManagerWindow, aView, aCallback) {
   close_manager(aManagerWindow, function() { open_manager(aView, aCallback); });
 }
 
+function CategoryUtilities(aManagerWindow) {
+  this.window = aManagerWindow;
+
+  var self = this;
+  this.window.addEventListener("unload", function() {
+    self.removeEventListener("unload", arguments.callee, false);
+    self.window = null;
+  }, false);
+}
+
+CategoryUtilities.prototype = {
+  window: null,
+
+  get selectedCategory() {
+    isnot(this.window, null, "Should not get selected category when manager window is not loaded");
+    var selectedItem = this.window.document.getElementById("categories").selectedItem;
+    isnot(selectedItem, null, "A category should be selected");
+    var view = this.window.gViewController.parseViewId(selectedItem.value);
+    return (view.type == "list") ? view.param : view.type;
+  },
+
+  get: function(aCategoryType) {
+    isnot(this.window, null, "Should not get category when manager window is not loaded");
+    var categories = this.window.document.getElementById("categories");
+
+    var viewId = "addons://list/" + aCategoryType;
+    var items = categories.getElementsByAttribute("value", viewId);
+    if (items.length)
+      return items[0];
+
+    viewId = "addons://" + aCategoryType + "/";
+    items = categories.getElementsByAttribute("value", viewId);
+    if (items.length)
+      return items[0];
+
+    ok(false, "Should have found a category with type " + aCategoryType);
+    return null;
+  },
+
+  getViewId: function(aCategoryType) {
+    isnot(this.window, null, "Should not get view id when manager window is not loaded");
+    return this.get(aCategoryType).value;
+  },
+
+  isVisible: function(aCategory) {
+    isnot(this.window, null, "Should not check visible state when manager window is not loaded");
+    if (aCategory.hasAttribute("disabled") &&
+        aCategory.getAttribute("disabled") == "true")
+      return false;
+
+    var style = this.window.document.defaultView.getComputedStyle(aCategory, "");
+    return style.display != "none" && style.visibility == "visible";
+  },
+
+  isTypeVisible: function(aCategoryType) {
+    return this.isVisible(this.get(aCategoryType));
+  },
+
+  open: function(aCategory, aCallback) {
+    isnot(this.window, null, "Should not open category when manager window is not loaded");
+    ok(this.isVisible(aCategory), "Category should be visible if attempting to open it");
+
+    EventUtils.synthesizeMouse(aCategory, 2, 2, { }, this.window);
+
+    if (aCallback)
+      wait_for_view_load(this.window, aCallback);
+  },
+
+  openType: function(aCategoryType, aCallback) {
+    this.open(this.get(aCategoryType), aCallback);
+  },
+}
+
 function CertOverrideListener(host, bits) {
   this.host = host;
   this.bits = bits;
 }
 
 CertOverrideListener.prototype = {
   host: null,
   bits: null,