<script src=../resources/testharness.js></script>
<script src=../resources/testharnessreport.js></script>
<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=100"></script>
<script src="http://127.0.0.1:8000/resources/checkResourcePriority.js"></script>

<script>
const priority_tests = [
  {
    url: new URL('../resources/dummy.css', location),
    expected_priority: "ResourceLoadPriorityMedium",
    description: 'low fetchpriority on <link rel=stylesheet> must load with medium priority'
  },
  {
    url: new URL('../resources/dummy.css?1', location),
    expected_priority: "ResourceLoadPriorityVeryHigh",
    description: 'high fetchpriority on <link rel=stylesheet> must load with very high priority'
  },
  {
    url: new URL('../resources/dummy.css?2', location),
    expected_priority: "ResourceLoadPriorityHigh",
    description: 'auto fetchpriority on <link rel=stylesheet> must have no effect on resource load priority'
  },
  {
    url: new URL('../resources/dummy.css?3', location),
    expected_priority: "ResourceLoadPriorityHigh",
    description: 'invalid fetchpriority on <link rel=stylesheet> must have no effect on resource load priority'
  },
  {
    url: new URL('../resources/dummy.css?4', location),
    expected_priority: "ResourceLoadPriorityHigh",
    description: 'missing fetchpriority on <link rel=stylesheet> must have no effect on resource load priority'
  }
];
</script>

<link id=link1 fetchpriority=low rel=stylesheet href=../resources/dummy.css>
<link id=link2 fetchpriority=high rel=stylesheet href=../resources/dummy.css?1>
<link id=link3 fetchpriority=auto rel=stylesheet href=../resources/dummy.css?2>
<link id=link4 fetchpriority=xyz rel=stylesheet href=../resources/dummy.css?3>
<link id=link5 rel=stylesheet href=../resources/dummy.css?4>

<script>
  promise_test(async (t) => {
    await new Promise(resolve => {
      addEventListener('DOMContentLoaded', resolve);
    });

    const base_msg = ' was fetched by the preload scanner';
    assert_true(internals.isPreloaded(link1.href), link1.href + base_msg);
    assert_true(internals.isPreloaded(link2.href), link2.href + base_msg);
    assert_true(internals.isPreloaded(link3.href), link3.href + base_msg);
    assert_true(internals.isPreloaded(link4.href), link4.href + base_msg);
    assert_true(internals.isPreloaded(link5.href), link5.href + base_msg);
  }, 'all stylesheets must be fetched by the preload scanner');

  // Setup the tests described by |priority_tests|.
  for (const test of priority_tests) {
    async_test(t => {
      checkResourcePriority(test.url, test.expected_priority, test.description);
      t.done();
    });
  }
</script>
