| Index: third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js | 
| diff --git a/third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js b/third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..401eb23665d9168b0e6ea85903bbbaa886db2965 | 
| --- /dev/null | 
| +++ b/third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js | 
| @@ -0,0 +1,207 @@ | 
| +'use strict'; | 
| + | 
| +function sensor_mocks(mojo) { | 
| +  return define('Generic Sensor API mocks', [ | 
| +    'mojo/public/js/core', | 
| +    'mojo/public/js/bindings', | 
| +    'mojo/public/js/connection', | 
| +    'device/sensors/sensor_factory.mojom', | 
| +    'device/sensors/sensor.mojom', | 
| +  ], (core, bindings, connection, sensor_factory, sensor) => { | 
| + | 
| +    function sensorResponse(result) { | 
| +      return Promise.resolve({result}); | 
| +    } | 
| + | 
| +    class MockSensor { | 
| +      constructor(type, stub, handle, offset, size) { | 
| +        this.type_ = type; | 
| +        this.stub_ = stub; | 
| +        this.client_ = null; | 
| +        this.start_should_fail_ = false; | 
| +        this.sensor_reading_timer_id_ = null; | 
| +        this.update_reading_function_ = null; | 
| +        this.active_sensor_configurations_ = []; | 
| +        let rv = core.mapBuffer(handle, offset, size, core.MAP_BUFFER_FLAG_NONE); | 
| +        assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | 
| +        this.buffer_ = new Float64Array(rv.buffer); | 
| +        bindings.StubBindings(this.stub_).delegate = this; | 
| +        bindings.StubBindings(this.stub_).connectionErrorHandler = () => { | 
| +          reset(); | 
| +        }; | 
| +      } | 
| + | 
| +      setClient(client) { | 
| +        this.client_ = client; | 
| +      } | 
| + | 
| +      start(configuration) { | 
| +        assert_not_equals(configuration, null, "Invalid sensor configuration."); | 
| + | 
| +        if (!this.start_should_fail_ && this.update_reading_function_ != null) { | 
| +          let timeout = (1 / configuration.frequency) * 1000; | 
| +          this.sensor_reading_timer_id_ = window.setTimeout(() => { | 
| +            this.update_reading_function_(this.buffer_) | 
| +            this.client_.onSensorReadingChanged(); | 
| +          }, timeout); | 
| + | 
| +          this.active_sensor_configurations_.push(configuration); | 
| +        } | 
| + | 
| +        return sensorResponse(!this.start_should_fail_); | 
| +      } | 
| + | 
| +      stop(configuration) { | 
| +        let index = this.active_sensor_configurations_.findIndex(configuration); | 
| +        if (index !== -1) { | 
| +          this.active_sensor_configurations_.splice(index, 1); | 
| +        } else { | 
| +          return sensorResponse(false); | 
| +        } | 
| + | 
| +        if (this.sensor_reading_timer_id_ != null | 
| +            && this.active_sensor_configurations_.length === 0) { | 
| +          window.clearTimeout(this.sensor_reading_timer_id_); | 
| +          this.sensor_reading_timer_id_ = null; | 
| +        } | 
| + | 
| +        return sensorResponse(true); | 
| +      } | 
| + | 
| +      // Mock functions | 
| +      reset() { | 
| +        if (this.sensor_reading_timer_id_) { | 
| +          window.clearTimeout(this.sensor_reading_timer_id_); | 
| +        } | 
| + | 
| +        bindings.StubBindings(this.stub_).close(); | 
| +        // TODO(shalamov): remove |this| from factory. | 
| +        this.type_ = null; | 
| +        this.stub_ = null; | 
| +        this.client_ = null; | 
| +        this.start_should_fail_ = false; | 
| +        this.sensor_reading_timer_id_ = null; | 
| +        this.sensor_reading_function_ = null; | 
| +        this.active_sensor_configurations_ = []; | 
| +      } | 
| + | 
| +      setUpdateSensorReadingFunction(update_reading_function) { | 
| +        this.update_reading_function_ = update_reading_function; | 
| +      } | 
| + | 
| +      setStartShouldFail(should_fail) { | 
| +        this.create_sensor_should_fail_ = should_fail; | 
| +      } | 
| +    } | 
| + | 
| +    function createSensorResponse(result, shmem, offset, size, mode) { | 
| +      return Promise.resolve({result, shmem, offset, size, mode}); | 
| +    } | 
| + | 
| +    class MockSensorFactory { | 
| +      constructor() { | 
| +        this.resding_size_in_bytes_ = sensor.kSensorReadingFieldSize * | 
| +                sensor.kSensorReadingFieldsCount; | 
| +        this.shared_buffer_size_in_bytes_ = this.resding_size_in_bytes_ * | 
| +                sensor.SensorType.LAST; | 
| +        let rv = | 
| +                core.createSharedBuffer( | 
| +                        core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE, | 
| +                        this.shared_buffer_size_in_bytes_); | 
| +        assert_equals(rv.result, core.RESULT_OK, "Failed to create shared buffer"); | 
| +        this.shared_buffer_handle_ = rv.handle; | 
| +        this.active_sensors_ = {}; | 
| +        this.create_sensor_should_fail_ = false; | 
| +        this.resolve_func_ = null; | 
| +        this.is_continuous_ = false; | 
| +      } | 
| + | 
| +      createSensor(type, stub) { | 
| +        if (this.create_sensor_should_fail_) { | 
| +          return createSensorResponse(sensor_factory.Result.ERROR, this.shared_buffer_handle_, 0, 0, 0); | 
| +        } | 
| + | 
| +        let offset = | 
| +                (type - sensor.SensorType.FIRST) * this.resding_size_in_bytes_; | 
| + | 
| +        if (!(type in this.active_sensors_)) { | 
| +          let mockSensor = new MockSensor(type, stub, this.shared_buffer_handle_, | 
| +                  offset, this.resding_size_in_bytes_); | 
| +          this.active_sensors_[type] = mockSensor; | 
| +        } | 
| + | 
| +        let rv = | 
| +                core.duplicateSharedBufferHandle( | 
| +                        this.shared_buffer_handle_, | 
| +                        core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); | 
| + | 
| +        assert_equals(rv.result, core.RESULT_OK); | 
| + | 
| +        let reporting_mode = sensor_factory.ReportingMode.DISCRETE; | 
| +        if (this.is_continuous_) { | 
| +            reporting_mode = sensor_factory.ReportingMode.CONTINUOUS; | 
| +        } | 
| + | 
| +        if (this.resolve_func_ !== null) | 
| +          this.resolve_func_(this.active_sensors_[type]); | 
| +        return createSensorResponse(sensor_factory.Result.SUCCESS, rv.handle, | 
| +                offset, this.resding_size_in_bytes_, reporting_mode); | 
| +      } | 
| + | 
| +      // Binds object to mojo message pipe | 
| +      bindToPipe(pipe) { | 
| +        this.stub_ = connection.bindHandleToStub( | 
| +            pipe, sensor_factory.SensorFactory); | 
| +        bindings.StubBindings(this.stub_).delegate = this; | 
| +      } | 
| + | 
| +      // Mock functions | 
| +      reset() { | 
| +        for (let id in this.active_sensors_) { | 
| +          this.active_sensors_[id].reset(); | 
| +        } | 
| + | 
| +//        core.close(this.shared_buffer_handle_); | 
| +//        bindings.StubBindings(this.stub_).close(); | 
| +        this.active_sensors_ = {}; | 
| +        this.create_sensor_should_fail_ = false; | 
| +        this.resolve_func_ = null; | 
| +        this.sensor_reading_function_ = null; | 
| +      } | 
| + | 
| +      setCreateSensorShouldFail(should_fail) { | 
| +        this.create_sensor_should_fail_ = should_fail; | 
| +      } | 
| + | 
| +      getCreatedSensor() { | 
| +        return new Promise((resolve, reject) => { | 
| +          this.resolve_func_ = resolve; | 
| +         }); | 
| +      } | 
| + | 
| +      setContinuousReportingMode(reporting_mode) { | 
| +          this.is_continuous_ = reporting_mode; | 
| +      } | 
| +    } | 
| + | 
| +    let mockSensorFactory = new MockSensorFactory; | 
| +    mojo.frameServiceRegistry.addServiceOverrideForTesting( | 
| +        sensor_factory.SensorFactory.name, | 
| +        pipe => { | 
| +          mockSensorFactory.bindToPipe(pipe); | 
| +        }); | 
| + | 
| +    return Promise.resolve({ | 
| +      mockSensorFactory: mockSensorFactory, | 
| +    }); | 
| +  }); | 
| +} | 
| + | 
| +function sensor_test(func, name, properties) { | 
| +  mojo_test(mojo => sensor_mocks(mojo).then(sensor => { | 
| +    let result = Promise.resolve(func(sensor)); | 
| +    let cleanUp = () => sensor.mockSensorFactory.reset(); | 
| +    result.then(cleanUp, cleanUp); | 
| +    return result; | 
| +  }), name, properties); | 
| +} | 
|  |