summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/fetch/fetchpriority/fetchpriority.js
blob: dfdfd4a161c61f3206af890a8bb6f59332f62f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as scriptTestsData from "./support/script-tests-data.js";
import * as linkTestsData from "./support/link-tests-data.js";
import * as fontfaceTestsData from "./support/font-face-tests-data.js";
import * as imageTestsData from "./support/image-tests-data.js";
import * as fetchTestsData from "./support/fetch-tests-data.js";

const kTopicHttpOnOpeningRequest = "http-on-opening-request";

function getFileNameAndSuffixOf(aStr) {
  return aStr.substr(aStr.lastIndexOf("/") + 1);
}

let httpOnOpeningRequests = [];

const observeHttpOnOpeningRequest = { observe(aSubject, aTopic) {
  assert_equals(aTopic, kTopicHttpOnOpeningRequest, "Observed '" +
        kTopicHttpOnOpeningRequest + "'");

  const fileNameAndSuffix = getFileNameAndSuffixOf(
    aSubject.QueryInterface(SpecialPowers.Ci.nsIChannel).URI.spec);
  const internalPriority =
    aSubject.QueryInterface(SpecialPowers.Ci.nsISupportsPriority).priority;

  httpOnOpeningRequests.push(
    { fileNameAndSuffix: fileNameAndSuffix,
    internalPriority: internalPriority});
}};

SpecialPowers.addObserver(observeHttpOnOpeningRequest,
      kTopicHttpOnOpeningRequest);

function containsOnlyUniqueFileNames(aRequests) {
  const fileNames = aRequests.map((r) => r.fileNameAndSuffix);
  return (new Set(fileNames)).size == fileNames.length;
}

const kTestGroups = [
  scriptTestsData, linkTestsData, fontfaceTestsData, imageTestsData, fetchTestsData
];

const kSupportFolderName = "support";

function runSingleTest(aTestData, aTestFolderName) {
  promise_test((t) => {
    return new Promise(resolve => {
      const testPath = kSupportFolderName + "/" + aTestFolderName + "/" +
        aTestData.testFileName;
      var childWindow = window.open(testPath);

      t.add_cleanup(() => {
            httpOnOpeningRequests = [];
            childWindow.close();
      });

      window.addEventListener("message", resolve);
    }).then(e => {
      assert_true(typeof e.data === "string", "String message received");
      assert_equals(e.data, "ChildLoaded", "Child loaded");

      assert_greater_than(aTestData.expectedRequests.length, 0,
        "Test data should be non-empty");

      assert_true(containsOnlyUniqueFileNames(aTestData.expectedRequests),
        "Test data contains only unique filenames")

      assert_greater_than(httpOnOpeningRequests.length, 0,
        "Observed HTTP requests should be non-empty");

      assert_true(containsOnlyUniqueFileNames(httpOnOpeningRequests),
        "Observed only one HTTP request per filename");

      // The actual order of the "http-on-opening-request"s is not checked,
      // since the corresponding notification is sent when the resource is
      // started to be loaded. However, since the resources might be too
      // quick to load, depending on the machine and network, it can't be
      // ensured that the server can reflect the priorities correctly.
      // Hence, here only the internal priority sent to the server is
      // checked.
      aTestData.expectedRequests.forEach(
        (expectedRequest) => {
          const actualRequest =
            httpOnOpeningRequests.find(
              (actualRequest) => actualRequest.fileNameAndSuffix ==
                                 expectedRequest.fileNameAndSuffix);
          assert_not_equals(actualRequest, undefined,
            "Found request for \"" + expectedRequest.fileNameAndSuffix +
            "\"");
          assert_equals(actualRequest.internalPriority,
            expectedRequest.internalPriority,
            "Check internal priority for '" +
            expectedRequest.fileNameAndSuffix + "'");
      });
    });
  }, aTestData.testFileName + ": test different 'fetchpriority' values");
}

export function runTests(aRunConfig) {
  for (const testGroup of kTestGroups) {
    const testDataKey = aRunConfig.testDataKey;
    if (!testGroup[testDataKey]) {
      continue;
    }
    for (const singleTestData of testGroup[testDataKey]) {
      runSingleTest(singleTestData, testGroup.kTestFolderName);
    }
  }
}