Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js

Issue 2051083002: WIP : Generic Sensor API implementation Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 'use strict';
2
3 function sensor_mocks(mojo) {
4 return define('Generic Sensor API mocks', [
5 'mojo/public/js/core',
6 'mojo/public/js/bindings',
7 'mojo/public/js/connection',
8 'device/sensors/sensor_factory.mojom',
9 'device/sensors/sensor.mojom',
10 ], (core, bindings, connection, sensor_factory, sensor) => {
11
12 function sensorResponse(result) {
13 return Promise.resolve({result});
14 }
15
16 class MockSensor {
17 constructor(type, stub, handle, offset, size) {
18 this.type_ = type;
19 this.stub_ = stub;
20 this.client_ = null;
21 this.start_should_fail_ = false;
22 this.sensor_reading_timer_id_ = null;
23 this.update_reading_function_ = null;
24 this.active_sensor_configurations_ = [];
25 let rv = core.mapBuffer(handle, offset, size, core.MAP_BUFFER_FLAG_NONE) ;
26 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer");
27 this.buffer_ = new Float64Array(rv.buffer);
28 bindings.StubBindings(this.stub_).delegate = this;
29 bindings.StubBindings(this.stub_).connectionErrorHandler = () => {
30 reset();
31 };
32 }
33
34 setClient(client) {
35 this.client_ = client;
36 }
37
38 start(configuration) {
39 assert_not_equals(configuration, null, "Invalid sensor configuration.");
40
41 if (!this.start_should_fail_ && this.update_reading_function_ != null) {
42 let timeout = (1 / configuration.frequency) * 1000;
43 this.sensor_reading_timer_id_ = window.setTimeout(() => {
44 this.update_reading_function_(this.buffer_)
45 this.client_.onSensorReadingChanged();
46 }, timeout);
47
48 this.active_sensor_configurations_.push(configuration);
49 }
50
51 return sensorResponse(!this.start_should_fail_);
52 }
53
54 stop(configuration) {
55 let index = this.active_sensor_configurations_.findIndex(configuration);
56 if (index !== -1) {
57 this.active_sensor_configurations_.splice(index, 1);
58 } else {
59 return sensorResponse(false);
60 }
61
62 if (this.sensor_reading_timer_id_ != null
63 && this.active_sensor_configurations_.length === 0) {
64 window.clearTimeout(this.sensor_reading_timer_id_);
65 this.sensor_reading_timer_id_ = null;
66 }
67
68 return sensorResponse(true);
69 }
70
71 // Mock functions
72 reset() {
73 if (this.sensor_reading_timer_id_) {
74 window.clearTimeout(this.sensor_reading_timer_id_);
75 }
76
77 bindings.StubBindings(this.stub_).close();
78 // TODO(shalamov): remove |this| from factory.
79 this.type_ = null;
80 this.stub_ = null;
81 this.client_ = null;
82 this.start_should_fail_ = false;
83 this.sensor_reading_timer_id_ = null;
84 this.sensor_reading_function_ = null;
85 this.active_sensor_configurations_ = [];
86 }
87
88 setUpdateSensorReadingFunction(update_reading_function) {
89 this.update_reading_function_ = update_reading_function;
90 }
91
92 setStartShouldFail(should_fail) {
93 this.create_sensor_should_fail_ = should_fail;
94 }
95 }
96
97 function createSensorResponse(result, shmem, offset, size, mode) {
98 return Promise.resolve({result, shmem, offset, size, mode});
99 }
100
101 class MockSensorFactory {
102 constructor() {
103 this.resding_size_in_bytes_ = sensor.kSensorReadingFieldSize *
104 sensor.kSensorReadingFieldsCount;
105 this.shared_buffer_size_in_bytes_ = this.resding_size_in_bytes_ *
106 sensor.SensorType.LAST;
107 let rv =
108 core.createSharedBuffer(
109 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE,
110 this.shared_buffer_size_in_bytes_);
111 assert_equals(rv.result, core.RESULT_OK, "Failed to create shared buffer ");
112 this.shared_buffer_handle_ = rv.handle;
113 this.active_sensors_ = {};
114 this.create_sensor_should_fail_ = false;
115 this.resolve_func_ = null;
116 this.is_continuous_ = false;
117 }
118
119 createSensor(type, stub) {
120 if (this.create_sensor_should_fail_) {
121 return createSensorResponse(sensor_factory.Result.ERROR, this.shared_b uffer_handle_, 0, 0, 0);
122 }
123
124 let offset =
125 (type - sensor.SensorType.FIRST) * this.resding_size_in_bytes_;
126
127 if (!(type in this.active_sensors_)) {
128 let mockSensor = new MockSensor(type, stub, this.shared_buffer_handle_ ,
129 offset, this.resding_size_in_bytes_);
130 this.active_sensors_[type] = mockSensor;
131 }
132
133 let rv =
134 core.duplicateSharedBufferHandle(
135 this.shared_buffer_handle_,
136 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE);
137
138 assert_equals(rv.result, core.RESULT_OK);
139
140 let reporting_mode = sensor_factory.ReportingMode.DISCRETE;
141 if (this.is_continuous_) {
142 reporting_mode = sensor_factory.ReportingMode.CONTINUOUS;
143 }
144
145 if (this.resolve_func_ !== null)
146 this.resolve_func_(this.active_sensors_[type]);
147 return createSensorResponse(sensor_factory.Result.SUCCESS, rv.handle,
148 offset, this.resding_size_in_bytes_, reporting_mode);
149 }
150
151 // Binds object to mojo message pipe
152 bindToPipe(pipe) {
153 this.stub_ = connection.bindHandleToStub(
154 pipe, sensor_factory.SensorFactory);
155 bindings.StubBindings(this.stub_).delegate = this;
156 }
157
158 // Mock functions
159 reset() {
160 for (let id in this.active_sensors_) {
161 this.active_sensors_[id].reset();
162 }
163
164 // core.close(this.shared_buffer_handle_);
165 // bindings.StubBindings(this.stub_).close();
166 this.active_sensors_ = {};
167 this.create_sensor_should_fail_ = false;
168 this.resolve_func_ = null;
169 this.sensor_reading_function_ = null;
170 }
171
172 setCreateSensorShouldFail(should_fail) {
173 this.create_sensor_should_fail_ = should_fail;
174 }
175
176 getCreatedSensor() {
177 return new Promise((resolve, reject) => {
178 this.resolve_func_ = resolve;
179 });
180 }
181
182 setContinuousReportingMode(reporting_mode) {
183 this.is_continuous_ = reporting_mode;
184 }
185 }
186
187 let mockSensorFactory = new MockSensorFactory;
188 mojo.frameServiceRegistry.addServiceOverrideForTesting(
189 sensor_factory.SensorFactory.name,
190 pipe => {
191 mockSensorFactory.bindToPipe(pipe);
192 });
193
194 return Promise.resolve({
195 mockSensorFactory: mockSensorFactory,
196 });
197 });
198 }
199
200 function sensor_test(func, name, properties) {
201 mojo_test(mojo => sensor_mocks(mojo).then(sensor => {
202 let result = Promise.resolve(func(sensor));
203 let cleanUp = () => sensor.mockSensorFactory.reset();
204 result.then(cleanUp, cleanUp);
205 return result;
206 }), name, properties);
207 }
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/sensor/pressure-sensor.html ('k') | third_party/WebKit/Source/modules/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698