<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tabs lastAccessed 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 testLastAccessed() {
  const past = Date.now();

  window.open("https://example.com/?1");
  window.open("https://example.com/?2");

  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
    },
    async background() {
      browser.test.onMessage.addListener(async function(msg, past) {
        if (msg !== "past") {
          return;
        }

        const [tab1] = await browser.tabs.query({url: "https://example.com/?1"});
        const [tab2] = await browser.tabs.query({url: "https://example.com/?2"});

        browser.test.assertTrue(tab1 && tab2, "Expected tabs were found");

        const now = Date.now();

        browser.test.assertTrue(typeof tab1.lastAccessed == "number",
                                "tab1 lastAccessed should be a number");

        browser.test.assertTrue(typeof tab2.lastAccessed == "number",
                                "tab2 lastAccessed should be a number");

        browser.test.assertTrue(past <= tab1.lastAccessed &&
                                tab1.lastAccessed <= tab2.lastAccessed &&
                                tab2.lastAccessed <= now,
                                "lastAccessed timestamps are recent and in the right order");

        await browser.tabs.remove([tab1.id, tab2.id]);

        browser.test.notifyPass("tabs.lastAccessed");
      });
    },
  });

  await extension.startup();
  await extension.sendMessage("past", past);
  await extension.awaitFinish("tabs.lastAccessed");
  await extension.unload();
});
</script>

</body>
