Bug 1604359 - Rewrite CloudFile preferences test without Mozmill modules; r=mkmelin DONTBUILD
authorGeoff Lankow <geoff@darktrojan.net>
Fri, 17 Jan 2020 14:59:43 +1300
changeset 37975 03dcd29772aab6a64ae5919d313594c45ac391d4
parent 37974 b11d7c703f338e634a1868ae049bf50441567261
child 37976 dfdf71999801276336a242803011278ba6c4d040
push id398
push userclokep@gmail.com
push dateMon, 09 Mar 2020 19:10:28 +0000
reviewersmkmelin
bugs1604359
Bug 1604359 - Rewrite CloudFile preferences test without Mozmill modules; r=mkmelin DONTBUILD
mail/components/preferences/test/browser/browser_cloudfile.js
mail/components/preferences/test/browser/files/management.html
mail/test/browser/cloudfile/browser.ini
mail/test/browser/cloudfile/browser_manager.js
--- a/mail/components/preferences/test/browser/browser_cloudfile.js
+++ b/mail/components/preferences/test/browser/browser_cloudfile.js
@@ -1,40 +1,88 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
 
-add_task(async () => {
-  // Mock the prompt service. We're going to be asked if we're sure
-  // we want to remove an account, so let's say yes.
+let { cloudFileAccounts } = ChromeUtils.import(
+  "resource:///modules/cloudFileAccounts.jsm"
+);
+let { MockRegistrar } = ChromeUtils.import(
+  "resource://testing-common/MockRegistrar.jsm"
+);
 
-  let mockPromptService = {
-    confirmCount: 0,
-    confirm() {
-      this.confirmCount++;
-      return true;
-    },
-    QueryInterface: ChromeUtils.generateQI([Ci.nsIPromptService]),
-  };
+const ICON_URL = getRootDirectory(gTestPath) + "files/icon.svg";
+const MANAGEMENT_URL = getRootDirectory(gTestPath) + "files/management.html";
+let accountIsConfigured = false;
+let provider = {
+  type: "Mochitest",
+  displayName: "Mochitest",
+  iconURL: ICON_URL,
+  initAccount(accountKey) {
+    return {
+      accountKey,
+      type: "Mochitest",
+      get displayName() {
+        return Services.prefs.getCharPref(
+          `mail.cloud_files.accounts.${this.accountKey}.displayName`,
+          "Mochitest Account"
+        );
+      },
+      iconURL: ICON_URL,
+      configured: accountIsConfigured,
+      managementURL: MANAGEMENT_URL,
+    };
+  },
+};
+
+// Mock the prompt service. We're going to be asked if we're sure
+// we want to remove an account, so let's say yes.
 
-  let { MockRegistrar } = ChromeUtils.import(
-    "resource://testing-common/MockRegistrar.jsm"
-  );
-  let mockPromptServiceCID = MockRegistrar.register(
-    "@mozilla.org/embedcomp/prompt-service;1",
-    mockPromptService
-  );
+/** @implements {nsIPromptService} */
+let mockPromptService = {
+  confirmCount: 0,
+  confirm() {
+    this.confirmCount++;
+    return true;
+  },
+  QueryInterface: ChromeUtils.generateQI([Ci.nsIPromptService]),
+};
+/** @implements {nsIExternalProtocolService} */
+let mockExternalProtocolService = {
+  _loadedURLs: [],
+  externalProtocolHandlerExists(aProtocolScheme) {},
+  getApplicationDescription(aScheme) {},
+  getProtocolHandlerInfo(aProtocolScheme) {},
+  getProtocolHandlerInfoFromOS(aProtocolScheme, aFound) {},
+  isExposedProtocol(aProtocolScheme) {},
+  loadURI(aURI, aWindowContext) {
+    this._loadedURLs.push(aURI.spec);
+  },
+  setProtocolHandlerDefaults(aHandlerInfo, aOSHandlerExists) {},
+  urlLoaded(aURL) {
+    return this._loadedURLs.includes(aURL);
+  },
+  QueryInterface: ChromeUtils.generateQI([Ci.nsIExternalProtocolService]),
+};
 
-  registerCleanupFunction(() => {
-    MockRegistrar.unregister(mockPromptServiceCID);
-  });
+let mockPromptServiceCID = MockRegistrar.register(
+  "@mozilla.org/embedcomp/prompt-service;1",
+  mockPromptService
+);
+let mockExternalProtocolServiceCID = MockRegistrar.register(
+  "@mozilla.org/uriloader/external-protocol-service;1",
+  mockExternalProtocolService
+);
 
-  let { cloudFileAccounts } = ChromeUtils.import(
-    "resource:///modules/cloudFileAccounts.jsm"
-  );
+registerCleanupFunction(() => {
+  MockRegistrar.unregister(mockPromptServiceCID);
+  MockRegistrar.unregister(mockExternalProtocolServiceCID);
+});
+
+add_task(async function addRemoveAccounts() {
   is(cloudFileAccounts.providers.length, 1);
   is(cloudFileAccounts.accounts.length, 0);
 
   // Load the preferences tab.
 
   let { prefsDocument, prefsWindow } = await openNewPrefsTab(
     "paneCompose",
     "compositionAttachmentsCategory"
@@ -67,40 +115,16 @@ add_task(async () => {
   let settingsDeck = prefsDocument.getElementById("cloudFileSettingsDeck");
   is(settingsDeck.selectedPanel.id, "cloudFileDefaultPanel");
 
   let iframeWrapper = prefsDocument.getElementById("cloudFileSettingsWrapper");
   is(iframeWrapper.childElementCount, 0);
 
   // Register our test provider.
 
-  const ICON_URL = getRootDirectory(gTestPath) + "files/icon.svg";
-  const MANAGEMENT_URL = getRootDirectory(gTestPath) + "files/management.html";
-
-  let accountIsConfigured = false;
-  let provider = {
-    type: "Mochitest",
-    displayName: "Mochitest",
-    iconURL: ICON_URL,
-    initAccount(accountKey) {
-      return {
-        accountKey,
-        type: "Mochitest",
-        get displayName() {
-          return Services.prefs.getCharPref(
-            `mail.cloud_files.accounts.${this.accountKey}.displayName`,
-            "Mochitest Account"
-          );
-        },
-        iconURL: ICON_URL,
-        configured: accountIsConfigured,
-        managementURL: MANAGEMENT_URL,
-      };
-    },
-  };
   cloudFileAccounts.registerProvider("Mochitest", provider);
   is(cloudFileAccounts.providers.length, 2);
   is(cloudFileAccounts.accounts.length, 0);
 
   await new Promise(resolve => prefsWindow.requestAnimationFrame(resolve));
 
   is(buttonList.childElementCount, 2);
   is(
@@ -157,16 +181,29 @@ add_task(async () => {
   is(accountListItem.querySelector("label").value, "Mochitest Account");
   is(accountListItem.querySelector("image.configuredWarning").hidden, false);
 
   is(settingsDeck.selectedPanel.id, "cloudFileSettingsWrapper");
   is(iframeWrapper.childElementCount, 1);
 
   let iframe = iframeWrapper.firstElementChild;
   is(iframe.src, `${MANAGEMENT_URL}?accountId=${accountKey}`);
+  if (iframe.contentDocument.readyState != "complete") {
+    await BrowserTestUtils.waitForEvent(iframe, "load");
+  }
+  is(iframe.contentDocument.readyState, "complete");
+  EventUtils.synthesizeMouseAtCenter(
+    iframe.contentDocument.getElementById("a"),
+    {},
+    iframe.contentWindow
+  );
+  ok(
+    mockExternalProtocolService.urlLoaded("https://www.example.com/"),
+    "Link click sent to external protocol service."
+  );
 
   // Rename the account.
 
   EventUtils.synthesizeMouseAtCenter(
     accountListItem,
     { clickCount: 1 },
     prefsWindow
   );
@@ -330,47 +367,22 @@ add_task(async () => {
   is(cloudFileAccounts.providers.length, 1);
   is(cloudFileAccounts.accounts.length, 0);
 
   // Close the preferences tab.
 
   await closePrefsTab();
 });
 
-add_task(async () => {
-  let { cloudFileAccounts } = ChromeUtils.import(
-    "resource:///modules/cloudFileAccounts.jsm"
-  );
+add_task(async function accountListOverflow() {
+  is(cloudFileAccounts.providers.length, 1);
+  is(cloudFileAccounts.accounts.length, 0);
 
   // Register our test provider.
 
-  const ICON_URL = getRootDirectory(gTestPath) + "files/icon.svg";
-  const MANAGEMENT_URL = getRootDirectory(gTestPath) + "files/management.html";
-
-  let accountIsConfigured = false;
-  let provider = {
-    type: "Mochitest",
-    displayName: "Mochitest",
-    iconURL: ICON_URL,
-    initAccount(accountKey) {
-      return {
-        accountKey,
-        type: "Mochitest",
-        get displayName() {
-          return Services.prefs.getCharPref(
-            `mail.cloud_files.accounts.${this.accountKey}.displayName`,
-            "Mochitest Account"
-          );
-        },
-        iconURL: ICON_URL,
-        configured: accountIsConfigured,
-        managementURL: MANAGEMENT_URL,
-      };
-    },
-  };
   cloudFileAccounts.registerProvider("Mochitest", provider);
   is(cloudFileAccounts.providers.length, 2);
   is(cloudFileAccounts.accounts.length, 0);
 
   // Load the preferences tab.
 
   let { prefsDocument, prefsWindow } = await openNewPrefsTab(
     "paneCompose",
@@ -426,9 +438,55 @@ add_task(async () => {
   } while (--count > 0);
 
   ok(buttonList.hidden);
   ok(!menuButton.hidden);
 
   // Close the preferences tab.
 
   await closePrefsTab();
+  cloudFileAccounts.unregisterProvider("Mochitest");
+  Services.prefs.deleteBranch("mail.cloud_files.accounts");
 });
+
+add_task(async function accountListOrder() {
+  is(cloudFileAccounts.providers.length, 1);
+  is(cloudFileAccounts.accounts.length, 0);
+
+  for (let [key, displayName] of [
+    ["someKey1", "carl's Account"],
+    ["someKey2", "Amber's Account"],
+    ["someKey3", "alice's Account"],
+    ["someKey4", "Bob's Account"],
+  ]) {
+    Services.prefs.setCharPref(
+      `mail.cloud_files.accounts.${key}.type`,
+      "Mochitest"
+    );
+    Services.prefs.setCharPref(
+      `mail.cloud_files.accounts.${key}.displayName`,
+      displayName
+    );
+  }
+
+  // Register our test provider.
+
+  cloudFileAccounts.registerProvider("Mochitest", provider);
+  is(cloudFileAccounts.providers.length, 2);
+  is(cloudFileAccounts.accounts.length, 4);
+
+  let { prefsDocument } = await openNewPrefsTab(
+    "paneCompose",
+    "compositionAttachmentsCategory"
+  );
+
+  let accountList = prefsDocument.getElementById("cloudFileView");
+  is(accountList.itemCount, 4);
+
+  is(accountList.getItemAtIndex(0).value, "someKey3");
+  is(accountList.getItemAtIndex(1).value, "someKey2");
+  is(accountList.getItemAtIndex(2).value, "someKey4");
+  is(accountList.getItemAtIndex(3).value, "someKey1");
+
+  await closePrefsTab();
+  cloudFileAccounts.unregisterProvider("Mochitest");
+  Services.prefs.deleteBranch("mail.cloud_files.accounts");
+});
--- a/mail/components/preferences/test/browser/files/management.html
+++ b/mail/components/preferences/test/browser/files/management.html
@@ -1,10 +1,10 @@
 <!DOCTYPE html>
 <html>
 <head>
   <meta charset="utf-8"/>
   <title></title>
 </head>
 <body>
-
+  <a id="a" href="https://www.example.com/">Click me!</a>
 </body>
 </html>
--- a/mail/test/browser/cloudfile/browser.ini
+++ b/mail/test/browser/cloudfile/browser.ini
@@ -45,10 +45,9 @@ prefs =
   mailnews.start_page.url=about:blank
 subsuite = thunderbird
 support-files =
   data/**
   html/**
 
 [browser_attachmentItem.js]
 [browser_attachmentUrls.js]
-[browser_manager.js]
 [browser_notifications.js]
deleted file mode 100644
--- a/mail/test/browser/cloudfile/browser_manager.js
+++ /dev/null
@@ -1,143 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-/**
- * Tests the richlistbox in the manager for attachment storage
- * services
- */
-
-"use strict";
-
-var elementslib = ChromeUtils.import(
-  "resource://testing-common/mozmill/elementslib.jsm"
-);
-var mozmill = ChromeUtils.import(
-  "resource://testing-common/mozmill/mozmill.jsm"
-);
-
-var { gMockCloudfileManager } = ChromeUtils.import(
-  "resource://testing-common/mozmill/CloudfileHelpers.jsm"
-);
-var {
-  content_tab_e,
-  content_tab_eid,
-  gMockExtProtSvc,
-  gMockExtProtSvcReg,
-} = ChromeUtils.import(
-  "resource://testing-common/mozmill/ContentTabHelpers.jsm"
-);
-var { mc } = ChromeUtils.import(
-  "resource://testing-common/mozmill/FolderDisplayHelpers.jsm"
-);
-var { close_pref_tab, open_pref_tab } = ChromeUtils.import(
-  "resource://testing-common/mozmill/PrefTabHelpers.jsm"
-);
-var { wait_for_frame_load } = ChromeUtils.import(
-  "resource://testing-common/mozmill/WindowHelpers.jsm"
-);
-
-var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
-
-var controller = mozmill.getMail3PaneController();
-var kTestAccountType = "mock";
-var kSettingsWithLink =
-  "chrome://mochitests/content/browser/comm/mail/test/browser/cloudfile/html/settings-with-link.xhtml";
-
-add_task(function setupModule(module) {
-  gMockCloudfileManager.register(kTestAccountType, {
-    managementURL: kSettingsWithLink,
-  });
-
-  // Let's set up a few dummy accounts;
-  create_dummy_account("someKey1", kTestAccountType, "carl's Account");
-  create_dummy_account("someKey2", kTestAccountType, "Amber's Account");
-  create_dummy_account("someKey3", kTestAccountType, "alice's Account");
-  create_dummy_account("someKey4", kTestAccountType, "Bob's Account");
-});
-
-registerCleanupFunction(function teardownModule(module) {
-  Services.prefs
-    .QueryInterface(Ci.nsIPrefBranch)
-    .deleteBranch("mail.cloud_files.accounts");
-  gMockCloudfileManager.unregister(kTestAccountType);
-
-  let tabmail = document.getElementById("tabmail");
-  if (tabmail.tabModes.preferencesTab.tabs.length == 1) {
-    tabmail.closeTab(tabmail.tabModes.preferencesTab.tabs[0]);
-  }
-});
-
-function create_dummy_account(aKey, aType, aDisplayName) {
-  Services.prefs.setCharPref(
-    "mail.cloud_files.accounts." + aKey + ".type",
-    aType
-  );
-
-  Services.prefs.setCharPref(
-    "mail.cloud_files.accounts." + aKey + ".displayName",
-    aDisplayName
-  );
-}
-
-function destroy_account(aKey) {
-  Services.prefs.clearUserPref("mail.cloud_files.accounts." + aKey);
-}
-
-/**
- * Tests that we load the accounts and display them in the
- * account richlistbox in the correct order (by displayName,
- * case-insensitive)
- */
-add_task(function test_load_accounts_and_properly_order() {
-  let prefTab = open_pref_tab("paneCompose", "compositionAttachmentsCategory");
-  mc.sleep();
-
-  let richList = content_tab_e(prefTab, "cloudFileView");
-  Assert.equal(4, richList.itemCount, "Should be displaying 4 accounts");
-
-  // Since we're sorting alphabetically by the displayName,
-  // case-insensitive, the items should be ordered with the
-  // following accountKeys:
-  //
-  // someKey3, someKey2, someKey4, someKey1
-  const kExpected = ["someKey3", "someKey2", "someKey4", "someKey1"];
-
-  for (let [index, expectedKey] of kExpected.entries()) {
-    let item = richList.getItemAtIndex(index);
-    Assert.equal(expectedKey, item.value, "The account list is out of order");
-  }
-
-  close_pref_tab(prefTab);
-});
-
-/**
- * Tests that a link in the management pane is loaded in
- * a browser and not in the management pane.
- */
-add_task(function test_external_link() {
-  gMockExtProtSvcReg.register();
-
-  let prefTab = open_pref_tab("paneCompose", "compositionAttachmentsCategory");
-  mc.sleep();
-  mc.click(content_tab_eid(prefTab, "cloudFileView"));
-  mc.click(content_tab_eid(prefTab, "cloudFileView"), 5, 5);
-
-  let iframe;
-  mc.waitFor(() => {
-    iframe = content_tab_e(prefTab, "cloudFileSettingsWrapper")
-      .firstElementChild;
-    return !!iframe;
-  });
-  wait_for_frame_load(iframe, kSettingsWithLink + "?accountId=someKey3");
-  mc.click(new elementslib.ID(iframe.contentDocument, "a"));
-
-  let targetHref = "https://www.example.com/";
-  mc.waitFor(
-    () => gMockExtProtSvc.urlLoaded(targetHref),
-    `Timed out waiting for the link ${targetHref} to be opened in the default browser.`
-  );
-  close_pref_tab(prefTab);
-
-  gMockExtProtSvcReg.unregister();
-});