tor-browser

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

shapedetection-helpers.js (2377B)


      1 'use strict';
      2 
      3 // These tests rely on the User Agent providing an implementation of
      4 // platform shape detection backends.
      5 //
      6 // In Chromium-based browsers this implementation is provided by a polyfill
      7 // in order to reduce the amount of test-only code shipped to users. To enable
      8 // these tests the browser must be run with these options:
      9 //
     10 //   --enable-blink-features=MojoJS,MojoJSTest
     11 
     12 async function loadChromiumResources() {
     13  await import('/resources/chromium/mock-barcodedetection.js');
     14  await import('/resources/chromium/mock-facedetection.js');
     15  await import('/resources/chromium/mock-textdetection.js');
     16 }
     17 
     18 /**
     19 * @param {String} detectionTestName
     20 * name of mock shape detection test interface,
     21 * must be the item of ["FaceDetectionTest", "BarcodeDetectionTest",
     22 * "TextDetectionTest"]
     23 */
     24 async function initialize_detection_tests(detectionTestName) {
     25  let detectionTest;
     26  if (typeof document === 'undefined') {
     27    // Use 'self' for workers.
     28    if (typeof self[detectionTestName] === 'undefined') {
     29      // test-only-api.js is already loaded in worker.js
     30      if (isChromiumBased) {
     31        await loadChromiumResources();
     32      }
     33    }
     34    detectionTest = new self[detectionTestName]();
     35  } else {
     36    if (typeof window[detectionTestName] === 'undefined') {
     37      const script = document.createElement('script');
     38      script.src = '/resources/test-only-api.js';
     39      script.async = false;
     40      const p = new Promise((resolve, reject) => {
     41        script.onload = () => { resolve(); };
     42        script.onerror = e => { reject(e); };
     43      })
     44      document.head.appendChild(script);
     45      await p;
     46 
     47      if (isChromiumBased) {
     48        await loadChromiumResources();
     49      }
     50 
     51    }
     52    detectionTest = new window[detectionTestName]();
     53  }
     54  await detectionTest.initialize();
     55  return detectionTest;
     56 }
     57 
     58 function detection_test(detectionTestName, func, name, properties) {
     59  promise_test(async t => {
     60    let detectionTest = await initialize_detection_tests(detectionTestName);
     61    try {
     62      await func(t, detectionTest);
     63    } finally {
     64      await detectionTest.reset();
     65    };
     66  }, name, properties);
     67 }
     68 
     69 function getArrayBufferFromBigBuffer(bigBuffer) {
     70  if (bigBuffer.bytes !== undefined) {
     71    return new Uint8Array(bigBuffer.bytes).buffer;
     72  }
     73  return bigBuffer.sharedMemory.bufferHandle.mapBuffer(0,
     74      bigBuffer.sharedMemory.size).buffer;
     75 }