Debian Codegraph @

testing/web-platform/tests/resources/chromium/mock-imagecapture.js

raw
1 import {BackgroundBlurMode, FillLightMode, ImageCapture, ImageCaptureReceiver, MeteringMode, RedEyeReduction} from '/gen/media/capture/mojom/image_capture.mojom.m.js';
2
3 self.ImageCaptureTest = (() => {
4 // Class that mocks ImageCapture interface defined in
5 // https://cs.chromium.org/chromium/src/media/capture/mojom/image_capture.mojom
6 class MockImageCapture {
7 constructor() {
8 this.interceptor_ =
9 new MojoInterfaceInterceptor(ImageCapture.$interfaceName);
10 this.interceptor_.oninterfacerequest =
11 e => this.receiver_.$.bindHandle(e.handle);
12 this.interceptor_.start();
13
14 this.state_ = {
15 state: {
16 supportedWhiteBalanceModes: [
17 MeteringMode.SINGLE_SHOT,
18 MeteringMode.CONTINUOUS
19 ],
20 currentWhiteBalanceMode: MeteringMode.CONTINUOUS,
21 supportedExposureModes: [
22 MeteringMode.MANUAL,
23 MeteringMode.SINGLE_SHOT,
24 MeteringMode.CONTINUOUS
25 ],
26 currentExposureMode: MeteringMode.MANUAL,
27 supportedFocusModes: [
28 MeteringMode.MANUAL,
29 MeteringMode.SINGLE_SHOT
30 ],
31 currentFocusMode: MeteringMode.MANUAL,
32 pointsOfInterest: [{
33 x: 0.4,
34 y: 0.6
35 }],
36
37 exposureCompensation: {
38 min: -200.0,
39 max: 200.0,
40 current: 33.0,
41 step: 33.0
42 },
43 exposureTime: {
44 min: 100.0,
45 max: 100000.0,
46 current: 1000.0,
47 step: 100.0
48 },
49 colorTemperature: {
50 min: 2500.0,
51 max: 6500.0,
52 current: 6000.0,
53 step: 1000.0
54 },
55 iso: {
56 min: 100.0,
57 max: 12000.0,
58 current: 400.0,
59 step: 1.0
60 },
61
62 brightness: {
63 min: 1.0,
64 max: 10.0,
65 current: 5.0,
66 step: 1.0
67 },
68 contrast: {
69 min: 2.0,
70 max: 9.0,
71 current: 5.0,
72 step: 1.0
73 },
74 saturation: {
75 min: 3.0,
76 max: 8.0,
77 current: 6.0,
78 step: 1.0
79 },
80 sharpness: {
81 min: 4.0,
82 max: 7.0,
83 current: 7.0,
84 step: 1.0
85 },
86
87 focusDistance: {
88 min: 1.0,
89 max: 10.0,
90 current: 3.0,
91 step: 1.0
92 },
93
94 pan: {
95 min: 0.0,
96 max: 10.0,
97 current: 5.0,
98 step: 2.0
99 },
100
101 tilt: {
102 min: 0.0,
103 max: 10.0,
104 current: 5.0,
105 step: 2.0
106 },
107
108 zoom: {
109 min: 0.0,
110 max: 10.0,
111 current: 5.0,
112 step: 5.0
113 },
114
115 supportsTorch: true,
116 torch: false,
117
118 redEyeReduction: RedEyeReduction.CONTROLLABLE,
119 height: {
120 min: 240.0,
121 max: 2448.0,
122 current: 240.0,
123 step: 2.0
124 },
125 width: {
126 min: 320.0,
127 max: 3264.0,
128 current: 320.0,
129 step: 3.0
130 },
131 fillLightMode: [FillLightMode.AUTO, FillLightMode.FLASH],
132
133 supportedBackgroundBlurModes: [
134 BackgroundBlurMode.OFF,
135 BackgroundBlurMode.BLUR
136 ],
137 backgroundBlurMode: BackgroundBlurMode.OFF,
138 }
139 };
140 this.panTiltZoomPermissionStatus_ = null;
141 this.settings_ = null;
142 this.receiver_ = new ImageCaptureReceiver(this);
143 }
144
145 reset() {
146 this.receiver_.$.close();
147 this.interceptor_.stop();
148 }
149
150 async getPhotoState(source_id) {
151 const shouldKeepPanTiltZoom = await this.isPanTiltZoomPermissionGranted();
152 if (shouldKeepPanTiltZoom)
153 return Promise.resolve(this.state_);
154
155 const newState = {...this.state_};
156 newState.state.pan = {};
157 newState.state.tilt = {};
158 newState.state.zoom = {};
159 return Promise.resolve(newState);
160 }
161
162 async setPhotoOptions(source_id, settings) {
163 const isAllowedToControlPanTiltZoom = await this.isPanTiltZoomPermissionGranted();
164 if (!isAllowedToControlPanTiltZoom &&
165 (settings.hasPan || settings.hasTilt || settings.hasZoom)) {
166 return Promise.resolve({ success: false });
167 }
168 this.settings_ = settings;
169 if (settings.hasIso)
170 this.state_.state.iso.current = settings.iso;
171 if (settings.hasHeight)
172 this.state_.state.height.current = settings.height;
173 if (settings.hasWidth)
174 this.state_.state.width.current = settings.width;
175 if (settings.hasPan)
176 this.state_.state.pan.current = settings.pan;
177 if (settings.hasTilt)
178 this.state_.state.tilt.current = settings.tilt;
179 if (settings.hasZoom)
180 this.state_.state.zoom.current = settings.zoom;
181 if (settings.hasFocusMode)
182 this.state_.state.currentFocusMode = settings.focusMode;
183 if (settings.hasFocusDistance)
184 this.state_.state.focusDistance.current = settings.focusDistance;
185
186 if (settings.pointsOfInterest.length > 0) {
187 this.state_.state.pointsOfInterest =
188 settings.pointsOfInterest;
189 }
190
191 if (settings.hasExposureMode)
192 this.state_.state.currentExposureMode = settings.exposureMode;
193
194 if (settings.hasExposureCompensation) {
195 this.state_.state.exposureCompensation.current =
196 settings.exposureCompensation;
197 }
198 if (settings.hasExposureTime) {
199 this.state_.state.exposureTime.current =
200 settings.exposureTime;
201 }
202 if (settings.hasWhiteBalanceMode) {
203 this.state_.state.currentWhiteBalanceMode =
204 settings.whiteBalanceMode;
205 }
206 if (settings.hasFillLightMode)
207 this.state_.state.fillLightMode = [settings.fillLightMode];
208 if (settings.hasRedEyeReduction)
209 this.state_.state.redEyeReduction = settings.redEyeReduction;
210 if (settings.hasColorTemperature) {
211 this.state_.state.colorTemperature.current =
212 settings.colorTemperature;
213 }
214 if (settings.hasBrightness)
215 this.state_.state.brightness.current = settings.brightness;
216 if (settings.hasContrast)
217 this.state_.state.contrast.current = settings.contrast;
218 if (settings.hasSaturation)
219 this.state_.state.saturation.current = settings.saturation;
220 if (settings.hasSharpness)
221 this.state_.state.sharpness.current = settings.sharpness;
222
223 if (settings.hasTorch)
224 this.state_.state.torch = settings.torch;
225
226 if (settings.hasBackgroundBlurMode)
227 this.state_.state.backgroundBlurMode = [settings.backgroundBlurMode];
228
229 return Promise.resolve({
230 success: true
231 });
232 }
233
234 takePhoto(source_id) {
235 return Promise.resolve({
236 blob: {
237 mimeType: 'image/cat',
238 data: new Array(2)
239 }
240 });
241 }
242
243 async isPanTiltZoomPermissionGranted() {
244 if (!this.panTiltZoomPermissionStatus_) {
245 this.panTiltZoomPermissionStatus_ = await navigator.permissions.query({
246 name: "camera",
247 panTiltZoom: true
248 });
249 }
250 return this.panTiltZoomPermissionStatus_.state == "granted";
251 }
252
253 state() {
254 return this.state_.state;
255 }
256
257 turnOffBackgroundBlurMode() {
258 this.state_.state.backgroundBlurMode = BackgroundBlurMode.OFF;
259 }
260 turnOnBackgroundBlurMode() {
261 this.state_.state.backgroundBlurMode = BackgroundBlurMode.BLUR;
262 }
263 turnOffSupportedBackgroundBlurModes() {
264 this.state_.state.supportedBackgroundBlurModes = [BackgroundBlurMode.OFF];
265 }
266 turnOnSupportedBackgroundBlurModes() {
267 this.state_.state.supportedBackgroundBlurModes = [BackgroundBlurMode.BLUR];
268 }
269
270 options() {
271 return this.settings_;
272 }
273 }
274
275 let testInternal = {
276 initialized: false,
277 mockImageCapture: null
278 }
279
280 class ImageCaptureTestChromium {
281
282 constructor() {
283 Object.freeze(this); // Make it immutable.
284 }
285
286 initialize() {
287 if (testInternal.initialized)
288 throw new Error('Call reset() before initialize().');
289
290 testInternal.mockImageCapture = new MockImageCapture;
291 testInternal.initialized = true;
292 }
293 // Resets state of image capture mocks between test runs.
294 async reset() {
295 if (!testInternal.initialized)
296 throw new Error('Call initialize() before reset().');
297 testInternal.mockImageCapture.reset();
298 testInternal.mockImageCapture = null;
299 testInternal.initialized = false;
300
301 await new Promise(resolve => setTimeout(resolve, 0));
302 }
303 mockImageCapture() {
304 return testInternal.mockImageCapture;
305 }
306 }
307
308 return ImageCaptureTestChromium;
309 })();