tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fetchpriority.js (4018B)


      1 import * as scriptTestsData from "./support/script-tests-data.js";
      2 import * as linkTestsData from "./support/link-tests-data.js";
      3 import * as fontfaceTestsData from "./support/font-face-tests-data.js";
      4 import * as imageTestsData from "./support/image-tests-data.js";
      5 import * as fetchTestsData from "./support/fetch-tests-data.js";
      6 
      7 const kTopicHttpOnOpeningRequest = "http-on-opening-request";
      8 
      9 function getFileNameAndSuffixOf(aStr) {
     10  return aStr.substr(aStr.lastIndexOf("/") + 1);
     11 }
     12 
     13 let httpOnOpeningRequests = [];
     14 
     15 const observeHttpOnOpeningRequest = { observe(aSubject, aTopic) {
     16  assert_equals(aTopic, kTopicHttpOnOpeningRequest, "Observed '" +
     17        kTopicHttpOnOpeningRequest + "'");
     18 
     19  const fileNameAndSuffix = getFileNameAndSuffixOf(
     20    aSubject.QueryInterface(SpecialPowers.Ci.nsIChannel).URI.spec);
     21  const internalPriority =
     22    aSubject.QueryInterface(SpecialPowers.Ci.nsISupportsPriority).priority;
     23 
     24  httpOnOpeningRequests.push(
     25    { fileNameAndSuffix: fileNameAndSuffix,
     26    internalPriority: internalPriority});
     27 }};
     28 
     29 SpecialPowers.addObserver(observeHttpOnOpeningRequest,
     30      kTopicHttpOnOpeningRequest);
     31 
     32 function containsOnlyUniqueFileNames(aRequests) {
     33  const fileNames = aRequests.map((r) => r.fileNameAndSuffix);
     34  return (new Set(fileNames)).size == fileNames.length;
     35 }
     36 
     37 const kTestGroups = [
     38  scriptTestsData, linkTestsData, fontfaceTestsData, imageTestsData, fetchTestsData
     39 ];
     40 
     41 const kSupportFolderName = "support";
     42 
     43 function runSingleTest(aTestData, aTestFolderName) {
     44  promise_test((t) => {
     45    return new Promise(resolve => {
     46      const testPath = kSupportFolderName + "/" + aTestFolderName + "/" +
     47        aTestData.testFileName;
     48      var childWindow = window.open(testPath);
     49 
     50      t.add_cleanup(() => {
     51            httpOnOpeningRequests = [];
     52            childWindow.close();
     53      });
     54 
     55      window.addEventListener("message", resolve);
     56    }).then(e => {
     57      assert_true(typeof e.data === "string", "String message received");
     58      assert_equals(e.data, "ChildLoaded", "Child loaded");
     59 
     60      assert_greater_than(aTestData.expectedRequests.length, 0,
     61        "Test data should be non-empty");
     62 
     63      assert_true(containsOnlyUniqueFileNames(aTestData.expectedRequests),
     64        "Test data contains only unique filenames")
     65 
     66      assert_greater_than(httpOnOpeningRequests.length, 0,
     67        "Observed HTTP requests should be non-empty");
     68 
     69      assert_true(containsOnlyUniqueFileNames(httpOnOpeningRequests),
     70        "Observed only one HTTP request per filename");
     71 
     72      // The actual order of the "http-on-opening-request"s is not checked,
     73      // since the corresponding notification is sent when the resource is
     74      // started to be loaded. However, since the resources might be too
     75      // quick to load, depending on the machine and network, it can't be
     76      // ensured that the server can reflect the priorities correctly.
     77      // Hence, here only the internal priority sent to the server is
     78      // checked.
     79      aTestData.expectedRequests.forEach(
     80        (expectedRequest) => {
     81          const actualRequest =
     82            httpOnOpeningRequests.find(
     83              (actualRequest) => actualRequest.fileNameAndSuffix ==
     84                                 expectedRequest.fileNameAndSuffix);
     85          assert_not_equals(actualRequest, undefined,
     86            "Found request for \"" + expectedRequest.fileNameAndSuffix +
     87            "\"");
     88          assert_equals(actualRequest.internalPriority,
     89            expectedRequest.internalPriority,
     90            "Check internal priority for '" +
     91            expectedRequest.fileNameAndSuffix + "'");
     92      });
     93    });
     94  }, aTestData.testFileName + ": test different 'fetchpriority' values");
     95 }
     96 
     97 export function runTests(aRunConfig) {
     98  for (const testGroup of kTestGroups) {
     99    const testDataKey = aRunConfig.testDataKey;
    100    if (!testGroup[testDataKey]) {
    101      continue;
    102    }
    103    for (const singleTestData of testGroup[testDataKey]) {
    104      runSingleTest(singleTestData, testGroup.kTestFolderName);
    105    }
    106  }
    107 }