<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tabs create 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 test_query_index() {
  const extension = ExtensionTestUtils.loadExtension({
    background: function() {
      browser.tabs.onCreated.addListener(async function({index, windowId, id}) {
        browser.test.assertThrows(
          () => browser.tabs.query({index: -1}),
          /-1 is too small \(must be at least 0\)/,
          "tab indices must be non-negative");

        let tabs = await browser.tabs.query({index, windowId});
        browser.test.assertEq(tabs.length, 1, `Got one tab at index ${index}`);
        browser.test.assertEq(tabs[0].id, id, "The tab is the right one");

        tabs = await browser.tabs.query({index: 1e5, windowId});
        browser.test.assertEq(tabs.length, 0, "There is no tab at this index");

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

  await extension.startup();
  const win = window.open("http://example.com");
  await extension.awaitFinish("tabs.query");
  win.close();
  await extension.unload();
});
</script>

</body>
</html>
