tor-browser

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

mock-barcodedetection.js (4142B)


      1 import {BarcodeDetectionReceiver, BarcodeFormat} from '/gen/services/shape_detection/public/mojom/barcodedetection.mojom.m.js';
      2 import {BarcodeDetectionProvider, BarcodeDetectionProviderReceiver} from '/gen/services/shape_detection/public/mojom/barcodedetection_provider.mojom.m.js';
      3 
      4 self.BarcodeDetectionTest = (() => {
      5  // Class that mocks BarcodeDetectionProvider interface defined in
      6  // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/barcodedetection_provider.mojom
      7  class MockBarcodeDetectionProvider {
      8    constructor() {
      9      this.receiver_ = new BarcodeDetectionProviderReceiver(this);
     10 
     11      this.interceptor_ = new MojoInterfaceInterceptor(
     12          BarcodeDetectionProvider.$interfaceName);
     13      this.interceptor_.oninterfacerequest = e => {
     14        if (this.should_close_pipe_on_request_)
     15          e.handle.close();
     16        else
     17          this.receiver_.$.bindHandle(e.handle);
     18      }
     19      this.interceptor_.start();
     20      this.should_close_pipe_on_request_ = false;
     21    }
     22 
     23    createBarcodeDetection(request, options) {
     24      this.mockService_ = new MockBarcodeDetection(request, options);
     25    }
     26 
     27    enumerateSupportedFormats() {
     28      return {
     29        supportedFormats: [
     30          BarcodeFormat.AZTEC,
     31          BarcodeFormat.DATA_MATRIX,
     32          BarcodeFormat.QR_CODE,
     33        ]
     34      };
     35    }
     36 
     37    getFrameData() {
     38      return this.mockService_.bufferData_;
     39    }
     40 
     41    getFormats() {
     42     return this.mockService_.options_.formats;
     43    }
     44 
     45    reset() {
     46      this.mockService_ = null;
     47      this.should_close_pipe_on_request_ = false;
     48      this.receiver_.$.close();
     49      this.interceptor_.stop();
     50    }
     51 
     52    // simulate a 'no implementation available' case
     53    simulateNoImplementation() {
     54      this.should_close_pipe_on_request_ = true;
     55    }
     56  }
     57 
     58  // Class that mocks BarcodeDetection interface defined in
     59  // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/barcodedetection.mojom
     60  class MockBarcodeDetection {
     61    constructor(request, options) {
     62      this.options_ = options;
     63      this.receiver_ = new BarcodeDetectionReceiver(this);
     64      this.receiver_.$.bindHandle(request.handle);
     65    }
     66 
     67    detect(bitmapData) {
     68      this.bufferData_ =
     69          new Uint32Array(getArrayBufferFromBigBuffer(bitmapData.pixelData));
     70      return {
     71        results: [
     72          {
     73            rawValue : "cats",
     74            boundingBox: { x: 1.0, y: 1.0, width: 100.0, height: 100.0 },
     75            format: BarcodeFormat.QR_CODE,
     76            cornerPoints: [
     77              { x: 1.0, y: 1.0 },
     78              { x: 101.0, y: 1.0 },
     79              { x: 101.0, y: 101.0 },
     80              { x: 1.0, y: 101.0 }
     81            ],
     82          },
     83          {
     84            rawValue : "dogs",
     85            boundingBox: { x: 2.0, y: 2.0, width: 50.0, height: 50.0 },
     86            format: BarcodeFormat.CODE_128,
     87            cornerPoints: [
     88              { x: 2.0, y: 2.0 },
     89              { x: 52.0, y: 2.0 },
     90              { x: 52.0, y: 52.0 },
     91              { x: 2.0, y: 52.0 }
     92            ],
     93          },
     94        ],
     95      };
     96    }
     97  }
     98 
     99  let testInternal = {
    100    initialized: false,
    101    MockBarcodeDetectionProvider: null
    102  }
    103 
    104  class BarcodeDetectionTestChromium {
    105    constructor() {
    106      Object.freeze(this); // Make it immutable.
    107    }
    108 
    109    initialize() {
    110      if (testInternal.initialized)
    111        throw new Error('Call reset() before initialize().');
    112 
    113      testInternal.MockBarcodeDetectionProvider = new MockBarcodeDetectionProvider;
    114      testInternal.initialized = true;
    115    }
    116 
    117    // Resets state of barcode detection mocks between test runs.
    118    async reset() {
    119      if (!testInternal.initialized)
    120        throw new Error('Call initialize() before reset().');
    121      testInternal.MockBarcodeDetectionProvider.reset();
    122      testInternal.MockBarcodeDetectionProvider = null;
    123      testInternal.initialized = false;
    124 
    125      await new Promise(resolve => setTimeout(resolve, 0));
    126    }
    127 
    128    MockBarcodeDetectionProvider() {
    129      return testInternal.MockBarcodeDetectionProvider;
    130    }
    131  }
    132 
    133  return BarcodeDetectionTestChromium;
    134 })();
    135 
    136 self.BarcodeFormat = BarcodeFormat;