Bug 1351111 - Add unit tests for setTitle and getTitle r=mixedpuppy
authorMatthew Wein <mwein@mozilla.com>
Thu, 25 May 2017 17:14:28 -0400
changeset 361498 f066295fa2d3d0be3063de361d509564fa77c42d
parent 361497 8e9c427717c8c5f294a48d63abb33e196695e631
child 361499 b30c003db0c82b34bbb42c68dfdc7337f78691ba
push id90875
push usercbook@mozilla.com
push dateWed, 31 May 2017 12:23:54 +0000
treeherdermozilla-inbound@7061759a0b57 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1351111
milestone55.0a1
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 1351111 - Add unit tests for setTitle and getTitle r=mixedpuppy MozReview-Commit-ID: KsdqHBzuWJi
mobile/android/components/extensions/test/mochitest/chrome.ini
mobile/android/components/extensions/test/mochitest/test_ext_browserAction_getTitle_setTitle.html
--- a/mobile/android/components/extensions/test/mochitest/chrome.ini
+++ b/mobile/android/components/extensions/test/mochitest/chrome.ini
@@ -1,9 +1,10 @@
 [DEFAULT]
 support-files =
   head.js
   ../../../../../../toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
 tags = webextensions
 
+[test_ext_browserAction_getTitle_setTitle.html]
 [test_ext_browserAction_onClicked.html]
 [test_ext_pageAction.html]
 [test_ext_pageAction_popup.html]
new file mode 100644
--- /dev/null
+++ b/mobile/android/components/extensions/test/mochitest/test_ext_browserAction_getTitle_setTitle.html
@@ -0,0 +1,130 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>BrowserAction Test</title>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="text/javascript">
+"use strict";
+
+var {BrowserActions} = SpecialPowers.Cu.import("resource://gre/modules/BrowserActions.jsm", {});
+
+add_task(async function test_setTitle_and_getTitle() {
+  async function background() {
+    let tabCreatedPromise = new Promise(resolve => {
+      let onTabCreated = tab => {
+        browser.tabs.onCreated.removeListener(onTabCreated);
+        resolve();
+      };
+      browser.tabs.onCreated.addListener(onTabCreated);
+    });
+
+    async function createAndTestNewTab(title, url) {
+      // First make sure the default title is correct.
+      let defaultTitle = await browser.browserAction.getTitle({});
+      browser.test.assertEq("Browser Action", defaultTitle, `Expected the default title to be ${defaultTitle}`);
+
+      // Create a tab.
+      let [tab] = await Promise.all([
+        browser.tabs.create({url}),
+        tabCreatedPromise,
+      ]);
+
+      // Test that the default title is returned before the title is set for the tab.
+      let tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
+      browser.test.assertEq("Browser Action", tabTitle, "Expected the default title to be returned");
+
+      // Set the title for the new tab and test that getTitle returns the correct title.
+      await browser.browserAction.setTitle({tabId: tab.id, title});
+      tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
+      browser.test.assertEq(title, tabTitle, `Expected the new tab title to be ${title}`);
+
+      return tab;
+    }
+
+    // Create and test 3 new tabs.
+    let tab1 = await createAndTestNewTab("tab 1", "about:blank");
+    let tab2 = await createAndTestNewTab("tab 2", "about:blank");
+    let tab3 = await createAndTestNewTab("tab 3", "about:blank");
+
+    // Test the default title again.
+    let title = await browser.browserAction.getTitle({});
+    browser.test.assertEq("Browser Action", title, `Expected the default title to be "Browser Action"`);
+
+    // Update the default title and confirm that the new title is returned.
+    await browser.browserAction.setTitle({title: "Updated Title"});
+    title = await browser.browserAction.getTitle({});
+    browser.test.assertEq("Updated Title", title, `Expected the default title to be "Updated Title"`);
+
+    // Try setting the default title to an empty string and confirm that the original title is still used.
+    browser.browserAction.setTitle({title: ""});
+    title = await browser.browserAction.getTitle({});
+    browser.test.assertEq("Updated Title", title, `Expected the default title to still be "Updated Title"`);
+
+    // Check all of the created tabs now.
+    title = await browser.browserAction.getTitle({tabId: tab1.id});
+    browser.test.assertEq("tab 1", title, `Expected the first tab title to be ${title}`);
+    title = await browser.browserAction.getTitle({tabId: tab2.id});
+    browser.test.assertEq("tab 2", title, `Expected the second tab title to be ${title}`);
+    title = await browser.browserAction.getTitle({tabId: tab3.id});
+    browser.test.assertEq("tab 3", title, `Expected the third tab title to be ${title}`);
+
+    // Unset the title for the first tab and confirm that it is unset.
+    browser.browserAction.setTitle({tabId: tab1.id, title: ""});
+    title = await browser.browserAction.getTitle({tabId: tab1.id});
+    browser.test.assertEq("Updated Title", title, `Expected the default title to be returned`);
+
+    browser.test.onMessage.addListener(async (msg, data) => {
+      if (msg === "select-tab") {
+        await browser.tabs.update(data, {active: true});
+        browser.test.sendMessage("tab-selected");
+      } else if (msg == "finish") {
+        // Close the tabs
+        await browser.tabs.remove([tab1.id, tab2.id, tab3.id]);
+        browser.test.notifyPass("browserAction.setTitleAndGetTitle");
+      }
+    });
+
+    browser.test.sendMessage("tabs", {tab1, tab2, tab3});
+  }
+
+  const extension = ExtensionTestUtils.loadExtension({
+    background,
+    manifest: {
+      "name": name,
+      "browser_action": {
+        "default_title": "Browser Action",
+      },
+    },
+  });
+
+  await extension.startup();
+
+  let tabs = await extension.awaitMessage("tabs");
+
+  async function checkTab(tab, name) {
+    extension.sendMessage("select-tab", tab.id);
+    await extension.awaitMessage("tab-selected");
+    is(BrowserActions.getNameForActiveTab(`{${extension.uuid}}`), name, "Got the expected name for the active browser action");
+  }
+
+  await checkTab(tabs.tab1, "Updated Title");
+  await checkTab(tabs.tab2, "tab 2");
+  await checkTab(tabs.tab3, "tab 3");
+  await checkTab(tabs.tab1, "Updated Title");
+
+  extension.sendMessage("finish");
+  await extension.awaitFinish("browserAction.setTitleAndGetTitle");
+
+  await extension.unload();
+});
+</script>
+
+</body>
+</html>