Bug 1551490 - Fix enterprise policy test failures when HTML about:addons is enabled by default. r=mkaply,rpl, a=test-only
authorLuca Greco <lgreco@mozilla.com>
Fri, 07 Jun 2019 15:56:08 +0000
changeset 536894 633c7ce9740c8f428812052468b8ecaae12b9a14
parent 536893 3d31a0e1b47e48c382364a560c65fe44fee0954a
child 536895 83af41aaeb7df6bfd934b335676e14a9bf191634
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkaply, rpl, test-only
bugs1551490
milestone68.0
Bug 1551490 - Fix enterprise policy test failures when HTML about:addons is enabled by default. r=mkaply,rpl, a=test-only Differential Revision: https://phabricator.services.mozilla.com/D31776
browser/components/enterprisepolicies/tests/browser/browser_policy_disable_flash_plugin.js
browser/components/enterprisepolicies/tests/browser/browser_policy_extensions.js
browser/components/enterprisepolicies/tests/browser/head.js
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_flash_plugin.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_disable_flash_plugin.js
@@ -6,129 +6,172 @@ const labelTextAlwaysActivate = "Always 
 const labelTextAskToActivate = "Ask to Activate";
 const labelTextNeverActivate = "Never Activate";
 
 function restore_prefs() {
   Services.prefs.clearUserPref("plugin.state.flash");
 }
 registerCleanupFunction(restore_prefs);
 
+async function assert_flash_locked_status(win, locked, expectedLabelText) {
+  if (win.useHtmlViews) {
+    // Tests while running on HTML about:addons page.
+    let addonCard = await BrowserTestUtils.waitForCondition(async () => {
+      let doc = win.getHtmlBrowser().contentDocument;
+      await win.htmlBrowserLoaded;
+      return doc.querySelector(`addon-card[addon-id*="Shockwave Flash"]`);
+    }, "Get HTML about:addons card for flash plugin");
+
+    const pluginOptions = addonCard.querySelector("plugin-options");
+    const pluginAction = pluginOptions.querySelector("panel-item[checked]");
+    ok(pluginAction.textContent.includes(expectedLabelText),
+       `Got plugin action "${expectedLabelText}"`);
+
+    // All other buttons (besides the checked one and the expand action)
+    // are expected to be disabled if locked is true.
+    for (const item of pluginOptions.querySelectorAll("panel-item")) {
+      const actionName = item.getAttribute("action");
+      if (!item.hasAttribute("checked") && actionName !== "expand" &&
+          actionName !== "preferences") {
+        is(item.shadowRoot.querySelector("button").disabled, locked,
+           `Plugin action "${actionName}" should be ${locked ? "disabled" : "enabled"}`);
+      }
+    }
+  } else {
+    // Tests while running on XUL about:addons page.
+    let list = win.document.getElementById("addon-list");
+    let flashEntry = await BrowserTestUtils.waitForCondition(() => {
+      return list.getElementsByAttribute("name", "Shockwave Flash")[0];
+    }, "Get XUL about:addons entry for flash plugin");
+    let dropDown = win.document.getAnonymousElementByAttribute(flashEntry, "anonid", "state-menulist");
+    is(dropDown.label, expectedLabelText,
+      "Flash setting text should match the expected value");
+    is(dropDown.disabled, locked,
+      "Flash controls disabled state should match policy locked state");
+  }
+}
+
 async function test_flash_status({expectedLabelText, locked}) {
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
-  await BrowserOpenAddonsMgr("addons://list/plugin");
-  await ContentTask.spawn(tab.linkedBrowser, {aExpectedLabelText: expectedLabelText, aLocked: locked}, async function({aExpectedLabelText, aLocked}) {
-    let list = content.document.getElementById("addon-list");
-    let flashEntry = list.getElementsByAttribute("name", "Shockwave Flash")[0];
-    let dropDown = content.document.getAnonymousElementByAttribute(flashEntry, "anonid", "state-menulist");
+  const win = await BrowserOpenAddonsMgr("addons://list/plugin");
 
-    is(dropDown.label, aExpectedLabelText,
-       "Flash setting text should match the expected value");
-    is(dropDown.disabled, aLocked,
-       "Flash controls disabled state should match policy locked state");
-  });
+  await assert_flash_locked_status(win, locked, expectedLabelText);
+
   BrowserTestUtils.removeTab(tab);
 
   is(Services.prefs.prefIsLocked("plugin.state.flash"), locked,
-     "Flash pref lock state should match policy lock state");
+    "Flash pref lock state should match policy lock state");
 }
 
 add_task(async function test_enabled() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
         "Default": true,
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextAlwaysActivate,
     locked: false,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
 
 add_task(async function test_enabled_locked() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
         "Default": true,
         "Locked": true,
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextAlwaysActivate,
     locked: true,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
 
 add_task(async function test_disabled() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
         "Default": false,
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextNeverActivate,
     locked: false,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
 
 add_task(async function test_disabled_locked() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
         "Default": false,
         "Locked": true,
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextNeverActivate,
     locked: true,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
 
 add_task(async function test_ask() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextAskToActivate,
     locked: false,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
 
 add_task(async function test_ask_locked() {
   await setupPolicyEngineWithJson({
     "policies": {
       "FlashPlugin": {
         "Locked": true,
       },
     },
   });
 
-  await test_flash_status({
+  const testCase = () => test_flash_status({
     expectedLabelText: labelTextAskToActivate,
     locked: true,
   });
+  await testOnAboutAddonsType("XUL", testCase);
+  await testOnAboutAddonsType("HTML", testCase);
 
   restore_prefs();
 });
--- a/browser/components/enterprisepolicies/tests/browser/browser_policy_extensions.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_extensions.js
@@ -1,105 +1,134 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 "use strict";
 
-const addonID = "policytest@mozilla.com";
+const ADDON_ID = "policytest@mozilla.com";
 const BASE_URL = "http://mochi.test:8888/browser/browser/components/enterprisepolicies/tests/browser";
 
+async function isExtensionLocked(win, addonID) {
+  if (win.useHtmlViews) {
+    // Test on HTML about:addons page.
+    let addonCard = await BrowserTestUtils.waitForCondition(async () => {
+      let doc = win.getHtmlBrowser().contentDocument;
+      await win.htmlBrowserLoaded;
+      return doc.querySelector(`addon-card[addon-id="${addonID}"]`);
+    }, `Get addon-card for "${addonID}"`);
+    let disableBtn = addonCard.querySelector('panel-item[action="toggle-disabled"]');
+    let removeBtn = addonCard.querySelector('panel-item[action="remove"]');
+    ok(removeBtn.hidden, "Remove button should be hidden");
+    ok(disableBtn.hidden, "Disable button should be hidden");
+  } else {
+    // Test on XUL about:addons page.
+    const doc = win.document;
+    let list = doc.getElementById("addon-list");
+    let addonEntry = await BrowserTestUtils.waitForCondition(
+      () => list.getElementsByAttribute("value", addonID)[0],
+      `Get addon entry for "${addonID}"`);
+    let disableBtn = doc.getAnonymousElementByAttribute(addonEntry, "anonid", "disable-btn");
+    let removeBtn = doc.getAnonymousElementByAttribute(addonEntry, "anonid", "remove-btn");
+    ok(removeBtn.hidden, "Remove button should be hidden");
+    ok(disableBtn.hidden, "Disable button should be hidden");
+  }
+}
+
+// This test case will run on both the XUL and HTML about:addons views.
+async function test_addon_locked() {
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
+  const win = await BrowserOpenAddonsMgr("addons://list/extension");
+
+  await isExtensionLocked(win, ADDON_ID);
+
+  BrowserTestUtils.removeTab(tab);
+}
+
 add_task(async function test_addon_install() {
   let installPromise = wait_for_addon_install();
   await setupPolicyEngineWithJson({
     "policies": {
       "Extensions": {
         "Install": [
           `${BASE_URL}/policytest_v0.1.xpi`,
         ],
         "Locked": [
-          addonID,
+          ADDON_ID,
         ],
       },
     },
   });
   await installPromise;
-  let addon = await AddonManager.getAddonByID(addonID);
+  let addon = await AddonManager.getAddonByID(ADDON_ID);
   isnot(addon, null, "Addon not installed.");
   is(addon.version, "0.1", "Addon version is correct");
 
   Assert.deepEqual(addon.installTelemetryInfo, {source: "enterprise-policy"},
                    "Got the expected addon.installTelemetryInfo");
 });
 
-add_task(async function test_addon_locked() {
-  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
-  await BrowserOpenAddonsMgr("addons://list/extension");
-  // eslint-disable-next-line no-shadow
-  await ContentTask.spawn(tab.linkedBrowser, {addonID}, async function({addonID}) {
-    let list = content.document.getElementById("addon-list");
-    let flashEntry = list.getElementsByAttribute("value", addonID)[0];
-    let disableBtn = content.document.getAnonymousElementByAttribute(flashEntry, "anonid", "disable-btn");
-    let removeBtn = content.document.getAnonymousElementByAttribute(flashEntry, "anonid", "remove-btn");
-    is(removeBtn.hidden, true, "Remove button should be hidden");
-    is(disableBtn.hidden, true, "Disable button should be hidden");
-  });
-  BrowserTestUtils.removeTab(tab);
+add_task(async function test_XUL_aboutaddons_addon_locked() {
+  await testOnAboutAddonsType("XUL", test_addon_locked);
+});
+
+add_task(async function test_HTML_aboutaddons_addon_locked() {
+  await testOnAboutAddonsType("HTML", test_addon_locked);
 });
 
 add_task(async function test_addon_reinstall() {
   // Test that uninstalling and reinstalling the same addon ID works as expected.
   // This can be used to update an addon.
 
   let uninstallPromise = wait_for_addon_uninstall();
   let installPromise = wait_for_addon_install();
   await setupPolicyEngineWithJson({
     "policies": {
       "Extensions": {
         "Uninstall": [
-          addonID,
+          ADDON_ID,
         ],
         "Install": [
           `${BASE_URL}/policytest_v0.2.xpi`,
         ],
       },
     },
   });
 
   // Older version was uninstalled
   await uninstallPromise;
 
   // New version was installed
   await installPromise;
 
-  let addon = await AddonManager.getAddonByID(addonID);
+  let addon = await AddonManager.getAddonByID(ADDON_ID);
   isnot(addon, null, "Addon still exists because the policy was used to update it.");
   is(addon.version, "0.2", "New version is correct");
 });
 
 
 add_task(async function test_addon_uninstall() {
   EnterprisePolicyTesting.resetRunOnceState();
 
   let uninstallPromise = wait_for_addon_uninstall();
   await setupPolicyEngineWithJson({
     "policies": {
       "Extensions": {
         "Uninstall": [
-          addonID,
+          ADDON_ID,
         ],
       },
     },
   });
   await uninstallPromise;
-  let addon = await AddonManager.getAddonByID(addonID);
+  let addon = await AddonManager.getAddonByID(ADDON_ID);
   is(addon, null, "Addon should be uninstalled.");
 });
 
 add_task(async function test_addon_download_failure() {
   // Test that if the download fails, the runOnce pref
-  // is cleared so that the dowbnload will happen again
+  // is cleared so that the download will happen again.
 
   let installPromise = wait_for_addon_install();
   await setupPolicyEngineWithJson({
     "policies": {
       "Extensions": {
         "Install": [
           `${BASE_URL}/policytest_invalid.xpi`,
         ],
@@ -110,17 +139,17 @@ add_task(async function test_addon_downl
   await installPromise;
   is(Services.prefs.prefHasUserValue("browser.policies.runOncePerModification.extensionsInstall"), false, "runOnce pref should be unset");
 });
 
 function wait_for_addon_install() {
   return new Promise(resolve => {
     let listener = {
       onInstallEnded(install, addon) {
-        if (addon.id == addonID) {
+        if (addon.id == ADDON_ID) {
           AddonManager.removeInstallListener(listener);
           resolve();
         }
       },
       onDownloadFailed() {
         AddonManager.removeInstallListener(listener);
         resolve();
       },
@@ -132,16 +161,16 @@ function wait_for_addon_install() {
     AddonManager.addInstallListener(listener);
   });
 }
 
 function wait_for_addon_uninstall() {
  return new Promise(resolve => {
     let listener = {};
     listener.onUninstalled = addon => {
-      if (addon.id == addonID) {
+      if (addon.id == ADDON_ID) {
         AddonManager.removeAddonListener(listener);
         resolve();
       }
     };
     AddonManager.addAddonListener(listener);
   });
 }
--- a/browser/components/enterprisepolicies/tests/browser/head.js
+++ b/browser/components/enterprisepolicies/tests/browser/head.js
@@ -57,8 +57,30 @@ registerCleanupFunction(async function p
   if (Services.policies.status != Ci.nsIEnterprisePolicies.INACTIVE) {
     await setupPolicyEngineWithJson("");
   }
   is(Services.policies.status, Ci.nsIEnterprisePolicies.INACTIVE, "Engine is inactive at the end of the test");
 
   EnterprisePolicyTesting.resetRunOnceState();
   PoliciesPrefTracker.stop();
 });
+
+async function testOnAboutAddonsType(type, fn) {
+  let useHtmlAboutAddons;
+  switch (type) {
+    case "XUL":
+      useHtmlAboutAddons = false;
+      break;
+    case "HTML":
+      useHtmlAboutAddons = true;
+      break;
+    default:
+      throw new Error(`Unknown about:addons type ${type}`);
+  }
+  await SpecialPowers.pushPrefEnv({
+    set: [["extensions.htmlaboutaddons.enabled", useHtmlAboutAddons]],
+  });
+  info(`Run tests on ${type} about:addons`);
+  await fn();
+  await SpecialPowers.popPrefEnv();
+}
+
+