<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tabs reload 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";

add_task(async function() {
  const extension = ExtensionTestUtils.loadExtension({
    files: {
      "tab.js": function() {
        browser.runtime.sendMessage("tab-loaded");
      },
      "tab.html":
        `<head>
          <meta charset="utf-8">
          <script src="tab.js"><\/script>
        </head>`,
    },

    async background() {
      let tabLoadedCount = 0;
      // eslint-disable-next-line prefer-const
      let tab;

      browser.runtime.onMessage.addListener(msg => {
        if (msg == "tab-loaded") {
          tabLoadedCount++;

          if (tabLoadedCount == 1) {
            // Reload the tab once passing no arguments.
            return browser.tabs.reload();
          }

          if (tabLoadedCount == 2) {
            // Reload the tab again with explicit arguments.
            return browser.tabs.reload(tab.id, {
              bypassCache: false,
            });
          }

          if (tabLoadedCount == 3) {
            browser.test.notifyPass("tabs.reload");
          }
        }
      });
      tab = await browser.tabs.create({url: "tab.html", active: true});
    },
  });

  await extension.startup();
  await extension.awaitFinish("tabs.reload");
  await extension.unload();
});
</script>

</body>
</html>
