<!DOCTYPE HTML>
<html>
<head>
  <title>Tabs executeScript Test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

async function testHasPermission(params) {
  let contentSetup = params.contentSetup || (() => Promise.resolve());

  async function background(contentSetup) {
    browser.runtime.onMessage.addListener((msg, sender) => {
      browser.test.assertEq(msg, "script ran", "script ran");
      browser.test.notifyPass("executeScript");
    });

    browser.test.onMessage.addListener(msg => {
      browser.test.assertEq(msg, "execute-script");

      browser.tabs.executeScript({
        file: "script.js",
      });
    });

    await contentSetup();

    browser.test.sendMessage("ready");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: params.manifest,

    background: `(${background})(${contentSetup})`,

    files: {
      "panel.html": `<!DOCTYPE html>
        <html>
          <head><meta charset="utf-8"></head>
          <body>
          </body>
        </html>`,
      "script.js": function() {
        browser.runtime.sendMessage("script ran");
      },
    },
  });

  await extension.startup();
  await extension.awaitMessage("ready");

  if (params.setup) {
    await params.setup(extension);
  }

  extension.sendMessage("execute-script");

  await extension.awaitFinish("executeScript");

  if (params.tearDown) {
    await params.tearDown(extension);
  }

  await extension.unload();
}

add_task(async function testGoodPermissions() {
  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "http://mochi.test:8888/",
    true
  );

  info("Test explicit host permission");
  await testHasPermission({
    manifest: { permissions: ["http://mochi.test/"] },
  });

  info("Test explicit host subdomain permission");
  await testHasPermission({
    manifest: { permissions: ["http://*.mochi.test/"] },
  });

  info("Test explicit <all_urls> permission");
  await testHasPermission({
    manifest: { permissions: ["<all_urls>"] },
  });

  info("Test activeTab permission with a browser action click");
  await testHasPermission({
    manifest: {
      permissions: ["activeTab"],
      browser_action: {},
    },
    contentSetup: function() {
      browser.browserAction.onClicked.addListener(() => {
        browser.test.log("Clicked.");
      });
      return Promise.resolve();
    },
    setup: extension => AppTestDelegate.clickBrowserAction(window, extension),
    tearDown: extension => AppTestDelegate.closeBrowserAction(window, extension),
  });

  info("Test activeTab permission with a page action click");
  await testHasPermission({
    manifest: {
      permissions: ["activeTab"],
      page_action: {},
    },
    contentSetup: async () => {
      let [tab] = await browser.tabs.query({
        active: true,
        currentWindow: true,
      });
      await browser.pageAction.show(tab.id);
    },
    setup: extension => AppTestDelegate.clickPageAction(window, extension),
    tearDown: extension => AppTestDelegate.closePageAction(window, extension),
  });

  info("Test activeTab permission with a browser action w/popup click");
  await testHasPermission({
    manifest: {
      permissions: ["activeTab"],
      browser_action: { default_popup: "panel.html" },
    },
    setup: async extension => {
      let promise = AppTestDelegate.awaitExtensionPanel(window, extension);
      await AppTestDelegate.clickBrowserAction(window, extension);
      await promise;
    },
    tearDown: extension => AppTestDelegate.closeBrowserAction(window, extension),
  });

  info("Test activeTab permission with a page action w/popup click");
  await testHasPermission({
    manifest: {
      permissions: ["activeTab"],
      page_action: { default_popup: "panel.html" },
    },
    contentSetup: async () => {
      let [tab] = await browser.tabs.query({
        active: true,
        currentWindow: true,
      });
      await browser.pageAction.show(tab.id);
    },
    setup: async extension => {
      let promise = AppTestDelegate.awaitExtensionPanel(window, extension);
      await AppTestDelegate.clickPageAction(window, extension);
      await promise;
    },
    tearDown: extension => AppTestDelegate.closePageAction(window, extension),
  });

  await AppTestDelegate.removeTab(window, tab);
});
</script>
</body>
</html>
