mock-textdetection.js (2556B)
1 import {TextDetection, TextDetectionReceiver} from '/gen/services/shape_detection/public/mojom/textdetection.mojom.m.js'; 2 3 self.TextDetectionTest = (() => { 4 // Class that mocks TextDetection interface defined in 5 // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/textdetection.mojom 6 class MockTextDetection { 7 constructor() { 8 this.receiver_ = new TextDetectionReceiver(this); 9 this.interceptor_ = 10 new MojoInterfaceInterceptor(TextDetection.$interfaceName); 11 this.interceptor_.oninterfacerequest = 12 e => this.receiver_.$.bindHandle(e.handle); 13 this.interceptor_.start(); 14 } 15 16 detect(bitmapData) { 17 this.bufferData_ = 18 new Uint32Array(getArrayBufferFromBigBuffer(bitmapData.pixelData)); 19 return Promise.resolve({ 20 results: [ 21 { 22 rawValue : "cats", 23 boundingBox: { x: 1.0, y: 1.0, width: 100.0, height: 100.0 }, 24 cornerPoints: [ 25 { x: 1.0, y: 1.0 }, 26 { x: 101.0, y: 1.0 }, 27 { x: 101.0, y: 101.0 }, 28 { x: 1.0, y: 101.0 } 29 ] 30 }, 31 { 32 rawValue : "dogs", 33 boundingBox: { x: 2.0, y: 2.0, width: 50.0, height: 50.0 }, 34 cornerPoints: [ 35 { x: 2.0, y: 2.0 }, 36 { x: 52.0, y: 2.0 }, 37 { x: 52.0, y: 52.0 }, 38 { x: 2.0, y: 52.0 } 39 ] 40 }, 41 ], 42 }); 43 } 44 45 getFrameData() { 46 return this.bufferData_; 47 } 48 49 reset() { 50 this.receiver_.$.close(); 51 this.interceptor_.stop(); 52 } 53 54 } 55 56 let testInternal = { 57 initialized: false, 58 MockTextDetection: null 59 } 60 61 class TextDetectionTestChromium { 62 constructor() { 63 Object.freeze(this); // Make it immutable. 64 } 65 66 initialize() { 67 if (testInternal.initialized) 68 throw new Error('Call reset() before initialize().'); 69 70 testInternal.MockTextDetection = new MockTextDetection; 71 testInternal.initialized = true; 72 } 73 74 // Resets state of text detection mocks between test runs. 75 async reset() { 76 if (!testInternal.initialized) 77 throw new Error('Call initialize() before reset().'); 78 testInternal.MockTextDetection.reset(); 79 testInternal.MockTextDetection = null; 80 testInternal.initialized = false; 81 82 await new Promise(resolve => setTimeout(resolve, 0)); 83 } 84 85 MockTextDetection() { 86 return testInternal.MockTextDetection; 87 } 88 } 89 90 return TextDetectionTestChromium; 91 92 })();