summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/resources/chromium/generic_sensor_mocks.js
blob: 98a29c2104fd91eccce12d10ecd58561755cbf82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import {ReportingMode, Sensor, SensorClientRemote, SensorReceiver, SensorRemote, SensorType} from '/gen/services/device/public/mojom/sensor.mojom.m.js';
import {SensorCreationResult, SensorInitParams_READ_BUFFER_SIZE_FOR_TESTS, SensorProvider, SensorProviderReceiver} from '/gen/services/device/public/mojom/sensor_provider.mojom.m.js';

// A "sliding window" that iterates over |data| and returns one item at a
// time, advancing and wrapping around as needed. |data| must be an array of
// arrays.
self.RingBuffer = class {
  constructor(data) {
    this.bufferPosition_ = 0;
    // Validate |data|'s format and deep-copy every element.
    this.data_ = Array.from(data, element => {
      if (!Array.isArray(element)) {
        throw new TypeError('Every |data| element must be an array.');
      }
      return Array.from(element);
    })
  }

  next() {
    const value = this.data_[this.bufferPosition_];
    this.bufferPosition_ = (this.bufferPosition_ + 1) % this.data_.length;
    return { done: false, value: value };
  }

  value() {
    return this.data_[this.bufferPosition_];
  }

  [Symbol.iterator]() {
    return this;
  }
};

class DefaultSensorTraits {
  // https://w3c.github.io/sensors/#threshold-check-algorithm
  static isSignificantlyDifferent(reading1, reading2) {
    return true;
  }

  // https://w3c.github.io/sensors/#reading-quantization-algorithm
  static roundToMultiple(reading) {
    return reading;
  }

  // https://w3c.github.io/ambient-light/#ambient-light-threshold-check-algorithm
  static areReadingsEqual(reading1, reading2) {
    return false;
  }
}

class AmbientLightSensorTraits extends DefaultSensorTraits {
  // https://w3c.github.io/ambient-light/#reduce-sensor-accuracy
  static #ROUNDING_MULTIPLE = 50;
  static #SIGNIFICANCE_THRESHOLD = 25;

  // https://w3c.github.io/ambient-light/#ambient-light-threshold-check-algorithm
  static isSignificantlyDifferent([illuminance1], [illuminance2]) {
    return Math.abs(illuminance1 - illuminance2) >=
        this.#SIGNIFICANCE_THRESHOLD;
  }

  // https://w3c.github.io/ambient-light/#ambient-light-reading-quantization-algorithm
  static roundToMultiple(reading) {
    const illuminance = reading[0];
    const scaledValue =
        illuminance / AmbientLightSensorTraits.#ROUNDING_MULTIPLE;
    let roundedReading = reading.splice();

    if (illuminance < 0.0) {
      roundedReading[0] = -AmbientLightSensorTraits.#ROUNDING_MULTIPLE *
          Math.floor(-scaledValue + 0.5);
    } else {
      roundedReading[0] = AmbientLightSensorTraits.#ROUNDING_MULTIPLE *
          Math.floor(scaledValue + 0.5);
    }

    return roundedReading;
  }

  // https://w3c.github.io/ambient-light/#ambient-light-threshold-check-algorithm
  static areReadingsEqual([illuminance1], [illuminance2]) {
    return illuminance1 === illuminance2;
  }
}

self.GenericSensorTest = (() => {
  // Default sensor frequency in default configurations.
  const DEFAULT_FREQUENCY = 5;

  // Class that mocks Sensor interface defined in
  // https://cs.chromium.org/chromium/src/services/device/public/mojom/sensor.mojom
  class MockSensor {
    static #BUFFER_OFFSET_TIMESTAMP = 1;
    static #BUFFER_OFFSET_READINGS = 2;

    constructor(sensorRequest, buffer, reportingMode, sensorType) {
      this.client_ = null;
      this.startShouldFail_ = false;
      this.notifyOnReadingChange_ = true;
      this.reportingMode_ = reportingMode;
      this.sensorType_ = sensorType;
      this.sensorReadingTimerId_ = null;
      this.readingData_ = null;
      this.requestedFrequencies_ = [];
      // The Blink implementation (third_party/blink/renderer/modules/sensor/sensor.cc)
      // sets a timestamp by creating a DOMHighResTimeStamp from a given platform timestamp.
      // In this mock implementation we use a starting value
      // and an increment step value that resemble a platform timestamp reasonably enough.
      this.timestamp_ = window.performance.timeOrigin;
      // |buffer| represents a SensorReadingSharedBuffer on the C++ side in
      // Chromium. It consists, in this order, of a
      // SensorReadingField<OneWriterSeqLock> (an 8-byte union that includes
      // 32-bit integer used by the lock class), and a SensorReading consisting
      // of an 8-byte timestamp and 4 8-byte reading fields.
      //
      // |this.buffer_[0]| is zeroed by default, which allows OneWriterSeqLock
      // to work with our custom memory buffer that did not actually create a
      // OneWriterSeqLock instance. It is never changed manually here.
      //
      // Use MockSensor.#BUFFER_OFFSET_TIMESTAMP and
      // MockSensor.#BUFFER_OFFSET_READINGS to access the other positions in
      // |this.buffer_| without having to hardcode magic numbers in the code.
      this.buffer_ = buffer;
      this.buffer_.fill(0);
      this.receiver_ = new SensorReceiver(this);
      this.receiver_.$.bindHandle(sensorRequest.handle);
      this.lastRawReading_ = null;
      this.lastRoundedReading_ = null;

      if (sensorType == SensorType.AMBIENT_LIGHT) {
        this.sensorTraits = AmbientLightSensorTraits;
      } else {
        this.sensorTraits = DefaultSensorTraits;
      }
    }

    // Returns default configuration.
    async getDefaultConfiguration() {
      return { frequency: DEFAULT_FREQUENCY };
    }

    // Adds configuration for the sensor and starts reporting fake data
    // through setSensorReading function.
    async addConfiguration(configuration) {
      this.requestedFrequencies_.push(configuration.frequency);
      // Sort using descending order.
      this.requestedFrequencies_.sort(
          (first, second) => { return second - first });

      if (!this.startShouldFail_ )
        this.startReading();

      return { success: !this.startShouldFail_ };
    }

    // Removes sensor configuration from the list of active configurations and
    // stops notification about sensor reading changes if
    // requestedFrequencies_ is empty.
    removeConfiguration(configuration) {
      const index = this.requestedFrequencies_.indexOf(configuration.frequency);
      if (index == -1)
        return;

      this.requestedFrequencies_.splice(index, 1);
      if (this.requestedFrequencies_.length === 0)
        this.stopReading();
    }

    // ConfigureReadingChangeNotifications(bool enabled)
    // Configures whether to report a reading change when in ON_CHANGE
    // reporting mode.
    configureReadingChangeNotifications(notifyOnReadingChange) {
      this.notifyOnReadingChange_ = notifyOnReadingChange;
    }

    resume() {
      this.startReading();
    }

    suspend() {
      this.stopReading();
    }

    // Mock functions

    // Resets mock Sensor state.
    reset() {
      this.stopReading();
      this.startShouldFail_ = false;
      this.requestedFrequencies_ = [];
      this.notifyOnReadingChange_ = true;
      this.readingData_ = null;
      this.buffer_.fill(0);
      this.receiver_.$.close();
      this.lastRawReading_ = null;
      this.lastRoundedReading_ = null;
    }

    // Sets fake data that is used to deliver sensor reading updates.
    setSensorReading(readingData) {
      this.readingData_ = new RingBuffer(readingData);
    }

    // This is a workaround to accommodate Blink's Device Orientation
    // implementation. In general, all tests should use setSensorReading()
    // instead.
    setSensorReadingImmediately(readingData) {
      this.setSensorReading(readingData);

      const reading = this.readingData_.value();
      this.buffer_.set(reading, MockSensor.#BUFFER_OFFSET_READINGS);
      this.buffer_[MockSensor.#BUFFER_OFFSET_TIMESTAMP] = this.timestamp_++;
    }

    // Sets flag that forces sensor to fail when addConfiguration is invoked.
    setStartShouldFail(shouldFail) {
      this.startShouldFail_ = shouldFail;
    }

    startReading() {
      if (this.readingData_ != null) {
        this.stopReading();
      }
      let maxFrequencyUsed = this.requestedFrequencies_[0];
      let timeout = (1 / maxFrequencyUsed) * 1000;
      this.sensorReadingTimerId_ = window.setInterval(() => {
        if (this.readingData_) {
          // |buffer_| is a TypedArray, so we need to make sure pass an
          // array to set().
          const reading = this.readingData_.next().value;
          if (!Array.isArray(reading)) {
            throw new TypeError("startReading(): The readings passed to " +
              "setSensorReading() must be arrays");
          }

          if (this.reportingMode_ == ReportingMode.ON_CHANGE &&
              this.lastRawReading_ !== null &&
              !this.sensorTraits.isSignificantlyDifferent(
                  this.lastRawReading_, reading)) {
            // In case new value is not significantly different compared to
            // old value, new value is not sent.
            return;
          }

          this.lastRawReading_ = reading.slice();
          const roundedReading = this.sensorTraits.roundToMultiple(reading);

          if (this.reportingMode_ == ReportingMode.ON_CHANGE &&
              this.lastRoundedReading_ !== null &&
              this.sensorTraits.areReadingsEqual(
                roundedReading, this.lastRoundedReading_)) {
            // In case new rounded value is not different compared to old
            // value, new value is not sent.
            return;
          }
          this.buffer_.set(roundedReading, MockSensor.#BUFFER_OFFSET_READINGS);
          this.lastRoundedReading_ = roundedReading;
        }

        // For all tests sensor reading should have monotonically
        // increasing timestamp.
        this.buffer_[MockSensor.#BUFFER_OFFSET_TIMESTAMP] = this.timestamp_++;

        if (this.reportingMode_ === ReportingMode.ON_CHANGE &&
            this.notifyOnReadingChange_) {
          this.client_.sensorReadingChanged();
        }
      }, timeout);
    }

    stopReading() {
      if (this.sensorReadingTimerId_ != null) {
        window.clearInterval(this.sensorReadingTimerId_);
        this.sensorReadingTimerId_ = null;
      }
    }

    getSamplingFrequency() {
      if (this.requestedFrequencies_.length == 0) {
        throw new Error("getSamplingFrequency(): No configured frequency");
      }
       return this.requestedFrequencies_[0];
    }

    isReadingData() {
      return this.sensorReadingTimerId_ != null;
    }
  }

  // Class that mocks SensorProvider interface defined in
  // https://cs.chromium.org/chromium/src/services/device/public/mojom/sensor_provider.mojom
  class MockSensorProvider {
    constructor() {
      this.readingSizeInBytes_ =
          Number(SensorInitParams_READ_BUFFER_SIZE_FOR_TESTS);
      this.sharedBufferSizeInBytes_ =
          this.readingSizeInBytes_ * (SensorType.MAX_VALUE + 1);
      let rv = Mojo.createSharedBuffer(this.sharedBufferSizeInBytes_);
      if (rv.result != Mojo.RESULT_OK) {
        throw new Error('MockSensorProvider: Failed to create shared buffer');
      }
      const handle = rv.handle;
      rv = handle.mapBuffer(0, this.sharedBufferSizeInBytes_);
      if (rv.result != Mojo.RESULT_OK) {
        throw new Error("MockSensorProvider: Failed to map shared buffer");
      }
      this.shmemArrayBuffer_ = rv.buffer;
      rv = handle.duplicateBufferHandle({readOnly: true});
      if (rv.result != Mojo.RESULT_OK) {
        throw new Error(
            'MockSensorProvider: failed to duplicate shared buffer');
      }
      this.readOnlySharedBufferHandle_ = rv.handle;
      this.activeSensors_ = new Map();
      this.resolveFuncs_ = new Map();
      this.getSensorShouldFail_ = new Map();
      this.permissionsDenied_ = new Map();
      this.maxFrequency_ = 60;
      this.minFrequency_ = 1;
      this.mojomSensorType_ = new Map([
        ['Accelerometer', SensorType.ACCELEROMETER],
        ['LinearAccelerationSensor', SensorType.LINEAR_ACCELERATION],
        ['GravitySensor', SensorType.GRAVITY],
        ['AmbientLightSensor', SensorType.AMBIENT_LIGHT],
        ['Gyroscope', SensorType.GYROSCOPE],
        ['Magnetometer', SensorType.MAGNETOMETER],
        ['AbsoluteOrientationSensor',
            SensorType.ABSOLUTE_ORIENTATION_QUATERNION],
        ['AbsoluteOrientationEulerAngles',
            SensorType.ABSOLUTE_ORIENTATION_EULER_ANGLES],
        ['RelativeOrientationSensor',
            SensorType.RELATIVE_ORIENTATION_QUATERNION],
        ['RelativeOrientationEulerAngles',
            SensorType.RELATIVE_ORIENTATION_EULER_ANGLES],
        ['ProximitySensor', SensorType.PROXIMITY]
      ]);
      this.receiver_ = new SensorProviderReceiver(this);

      this.interceptor_ =
          new MojoInterfaceInterceptor(SensorProvider.$interfaceName);
      this.interceptor_.oninterfacerequest = e => {
        this.bindToPipe(e.handle);
      };
      this.interceptor_.start();
    }

    // Returns initialized Sensor proxy to the client.
    async getSensor(type) {
      if (this.getSensorShouldFail_.get(type)) {
        return {result: SensorCreationResult.ERROR_NOT_AVAILABLE,
                initParams: null};
      }
      if (this.permissionsDenied_.get(type)) {
        return {result: SensorCreationResult.ERROR_NOT_ALLOWED,
                initParams: null};
      }

      const offset = type * this.readingSizeInBytes_;
      const reportingMode = ReportingMode.ON_CHANGE;

      const sensor = new SensorRemote();
      if (!this.activeSensors_.has(type)) {
        const shmemView = new Float64Array(
            this.shmemArrayBuffer_, offset,
            this.readingSizeInBytes_ / Float64Array.BYTES_PER_ELEMENT);
        const mockSensor = new MockSensor(
            sensor.$.bindNewPipeAndPassReceiver(), shmemView, reportingMode,
            type);
        this.activeSensors_.set(type, mockSensor);
        this.activeSensors_.get(type).client_ = new SensorClientRemote();
      }

      const rv = this.readOnlySharedBufferHandle_.duplicateBufferHandle(
          {readOnly: true});
      if (rv.result != Mojo.RESULT_OK) {
        throw new Error('getSensor(): failed to duplicate shared buffer');
      }

      const defaultConfig = { frequency: DEFAULT_FREQUENCY };
      // Consider sensor traits to meet assertions in C++ code (see
      // services/device/public/cpp/generic_sensor/sensor_traits.h)
      if (type == SensorType.AMBIENT_LIGHT || type == SensorType.MAGNETOMETER) {
        this.maxFrequency_ = Math.min(10, this.maxFrequency_);
      }

      const client = this.activeSensors_.get(type).client_;
      const initParams = {
        sensor,
        clientReceiver: client.$.bindNewPipeAndPassReceiver(),
        memory: {buffer: rv.handle},
        bufferOffset: BigInt(offset),
        mode: reportingMode,
        defaultConfiguration: defaultConfig,
        minimumFrequency: this.minFrequency_,
        maximumFrequency: this.maxFrequency_
      };

      if (this.resolveFuncs_.has(type)) {
        for (let resolveFunc of this.resolveFuncs_.get(type)) {
          resolveFunc(this.activeSensors_.get(type));
        }
        this.resolveFuncs_.delete(type);
      }

      return {result: SensorCreationResult.SUCCESS, initParams};
    }

    // Binds object to mojo message pipe
    bindToPipe(pipe) {
      this.receiver_.$.bindHandle(pipe);
    }

    // Mock functions

    // Resets state of mock SensorProvider between test runs.
    reset() {
      for (const sensor of this.activeSensors_.values()) {
        sensor.reset();
      }
      this.activeSensors_.clear();
      this.resolveFuncs_.clear();
      this.getSensorShouldFail_.clear();
      this.permissionsDenied_.clear();
      this.maxFrequency_ = 60;
      this.minFrequency_ = 1;
      this.receiver_.$.close();
      this.interceptor_.stop();
    }

    // Sets flag that forces mock SensorProvider to fail when getSensor() is
    // invoked.
    setGetSensorShouldFail(sensorType, shouldFail) {
      this.getSensorShouldFail_.set(this.mojomSensorType_.get(sensorType),
          shouldFail);
    }

    setPermissionsDenied(sensorType, permissionsDenied) {
      this.permissionsDenied_.set(this.mojomSensorType_.get(sensorType),
          permissionsDenied);
    }

    // Returns mock sensor that was created in getSensor to the layout test.
    getCreatedSensor(sensorType) {
      const type = this.mojomSensorType_.get(sensorType);
      if (typeof type != "number") {
        throw new TypeError(`getCreatedSensor(): Invalid sensor type ${sensorType}`);
      }

      if (this.activeSensors_.has(type)) {
        return Promise.resolve(this.activeSensors_.get(type));
      }

      return new Promise(resolve => {
        if (!this.resolveFuncs_.has(type)) {
          this.resolveFuncs_.set(type, []);
        }
        this.resolveFuncs_.get(type).push(resolve);
      });
    }

    // Sets the maximum frequency for a concrete sensor.
    setMaximumSupportedFrequency(frequency) {
      this.maxFrequency_ = frequency;
    }

    // Sets the minimum frequency for a concrete sensor.
    setMinimumSupportedFrequency(frequency) {
      this.minFrequency_ = frequency;
    }
  }

  let testInternal = {
    initialized: false,
    sensorProvider: null
  }

  class GenericSensorTestChromium {
    constructor() {
      Object.freeze(this); // Make it immutable.
    }

    async initialize() {
      if (testInternal.initialized)
        throw new Error('Call reset() before initialize().');

      // Grant sensor permissions for Chromium testdriver.
      // testdriver.js only works in the top-level browsing context, so do
      // nothing if we're in e.g. an iframe.
      if (window.parent === window) {
        for (const entry
                 of ['accelerometer', 'gyroscope', 'magnetometer',
                     'ambient-light-sensor']) {
          await test_driver.set_permission({name: entry}, 'granted');
        }
      }

      testInternal.sensorProvider = new MockSensorProvider;
      testInternal.initialized = true;
    }
    // Resets state of sensor mocks between test runs.
    async reset() {
      if (!testInternal.initialized)
        throw new Error('Call initialize() before reset().');
      testInternal.sensorProvider.reset();
      testInternal.sensorProvider = null;
      testInternal.initialized = false;

      // Wait for an event loop iteration to let any pending mojo commands in
      // the sensor provider finish.
      await new Promise(resolve => setTimeout(resolve, 0));
    }

    getSensorProvider() {
      return testInternal.sensorProvider;
    }
  }

  return GenericSensorTestChromium;
})();