mock-facedetection.js (4134B)
1 import {FaceDetectionReceiver, LandmarkType} from '/gen/services/shape_detection/public/mojom/facedetection.mojom.m.js'; 2 import {FaceDetectionProvider, FaceDetectionProviderReceiver} from '/gen/services/shape_detection/public/mojom/facedetection_provider.mojom.m.js'; 3 4 self.FaceDetectionTest = (() => { 5 // Class that mocks FaceDetectionProvider interface defined in 6 // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/facedetection_provider.mojom 7 class MockFaceDetectionProvider { 8 constructor() { 9 this.receiver_ = new FaceDetectionProviderReceiver(this); 10 11 this.interceptor_ = new MojoInterfaceInterceptor( 12 FaceDetectionProvider.$interfaceName); 13 this.interceptor_.oninterfacerequest = 14 e => this.receiver_.$.bindHandle(e.handle); 15 this.interceptor_.start(); 16 } 17 18 createFaceDetection(request, options) { 19 this.mockService_ = new MockFaceDetection(request, options); 20 } 21 22 getFrameData() { 23 return this.mockService_.bufferData_; 24 } 25 26 getMaxDetectedFaces() { 27 return this.mockService_.maxDetectedFaces_; 28 } 29 30 getFastMode () { 31 return this.mockService_.fastMode_; 32 } 33 34 reset() { 35 this.mockService_ = null; 36 this.receiver_.$.close(); 37 this.interceptor_.stop(); 38 } 39 } 40 41 // Class that mocks FaceDetection interface defined in 42 // https://cs.chromium.org/chromium/src/services/shape_detection/public/mojom/facedetection.mojom 43 class MockFaceDetection { 44 constructor(request, options) { 45 this.maxDetectedFaces_ = options.maxDetectedFaces; 46 this.fastMode_ = options.fastMode; 47 this.receiver_ = new FaceDetectionReceiver(this); 48 this.receiver_.$.bindHandle(request.handle); 49 } 50 51 detect(bitmapData) { 52 this.bufferData_ = 53 new Uint32Array(getArrayBufferFromBigBuffer(bitmapData.pixelData)); 54 return Promise.resolve({ 55 results: [ 56 { 57 boundingBox: {x: 1.0, y: 1.0, width: 100.0, height: 100.0}, 58 landmarks: [{ 59 type: LandmarkType.EYE, 60 locations: [{x: 4.0, y: 5.0}] 61 }, 62 { 63 type: LandmarkType.EYE, 64 locations: [ 65 {x: 4.0, y: 5.0}, {x: 5.0, y: 4.0}, {x: 6.0, y: 3.0}, 66 {x: 7.0, y: 4.0}, {x: 8.0, y: 5.0}, {x: 7.0, y: 6.0}, 67 {x: 6.0, y: 7.0}, {x: 5.0, y: 6.0} 68 ] 69 }] 70 }, 71 { 72 boundingBox: {x: 2.0, y: 2.0, width: 200.0, height: 200.0}, 73 landmarks: [{ 74 type: LandmarkType.NOSE, 75 locations: [{x: 100.0, y: 50.0}] 76 }, 77 { 78 type: LandmarkType.NOSE, 79 locations: [ 80 {x: 80.0, y: 50.0}, {x: 70.0, y: 60.0}, {x: 60.0, y: 70.0}, 81 {x: 70.0, y: 60.0}, {x: 80.0, y: 70.0}, {x: 90.0, y: 80.0}, 82 {x: 100.0, y: 70.0}, {x: 90.0, y: 60.0}, {x: 80.0, y: 50.0} 83 ] 84 }] 85 }, 86 { 87 boundingBox: {x: 3.0, y: 3.0, width: 300.0, height: 300.0}, 88 landmarks: [] 89 }, 90 ] 91 }); 92 } 93 } 94 95 let testInternal = { 96 initialized: false, 97 MockFaceDetectionProvider: null 98 } 99 100 class FaceDetectionTestChromium { 101 constructor() { 102 Object.freeze(this); // Make it immutable. 103 } 104 105 initialize() { 106 if (testInternal.initialized) 107 throw new Error('Call reset() before initialize().'); 108 109 testInternal.MockFaceDetectionProvider = new MockFaceDetectionProvider; 110 testInternal.initialized = true; 111 } 112 113 // Resets state of face detection mocks between test runs. 114 async reset() { 115 if (!testInternal.initialized) 116 throw new Error('Call initialize() before reset().'); 117 testInternal.MockFaceDetectionProvider.reset(); 118 testInternal.MockFaceDetectionProvider = null; 119 testInternal.initialized = false; 120 121 await new Promise(resolve => setTimeout(resolve, 0)); 122 } 123 124 MockFaceDetectionProvider() { 125 return testInternal.MockFaceDetectionProvider; 126 } 127 } 128 129 return FaceDetectionTestChromium; 130 })();