OLD | NEW |
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 // Wraps callback and calls reject_func if callback throws an error. | 3 // Wraps callback and calls reject_func if callback throws an error. |
4 class CallbackWrapper { | 4 class CallbackWrapper { |
5 constructor(callback, reject_func) { | 5 constructor(callback, reject_func) { |
6 this.wrapper_func_ = (args) => { | 6 this.wrapper_func_ = (args) => { |
7 try { | 7 try { |
8 callback(args); | 8 callback(args); |
9 } catch(e) { | 9 } catch(e) { |
10 reject_func(); | 10 reject_func(); |
(...skipping 18 matching lines...) Expand all Loading... |
29 // Helper function that returns resolved promise with result. | 29 // Helper function that returns resolved promise with result. |
30 function sensorResponse(success) { | 30 function sensorResponse(success) { |
31 return Promise.resolve({success}); | 31 return Promise.resolve({success}); |
32 } | 32 } |
33 | 33 |
34 // Class that mocks Sensor interface defined in sensor.mojom | 34 // Class that mocks Sensor interface defined in sensor.mojom |
35 class MockSensor { | 35 class MockSensor { |
36 constructor(stub, handle, offset, size, reportingMode) { | 36 constructor(stub, handle, offset, size, reportingMode) { |
37 this.client_ = null; | 37 this.client_ = null; |
38 this.stub_ = stub; | 38 this.stub_ = stub; |
| 39 this.expects_modified_reading_ = false; |
39 this.start_should_fail_ = false; | 40 this.start_should_fail_ = false; |
40 this.reporting_mode_ = reportingMode; | 41 this.reporting_mode_ = reportingMode; |
41 this.sensor_reading_timer_id_ = null; | 42 this.sensor_reading_timer_id_ = null; |
42 this.update_reading_function_ = null; | 43 this.update_reading_function_ = null; |
43 this.suspend_called_ = null; | 44 this.suspend_called_ = null; |
44 this.resume_called_ = null; | 45 this.resume_called_ = null; |
45 this.add_configuration_called_ = null; | 46 this.add_configuration_called_ = null; |
46 this.remove_configuration_called_ = null; | 47 this.remove_configuration_called_ = null; |
47 this.active_sensor_configurations_ = []; | 48 this.active_sensor_configurations_ = []; |
48 let rv = core.mapBuffer(handle, offset, size, | 49 let rv = core.mapBuffer(handle, offset, size, |
(...skipping 11 matching lines...) Expand all Loading... |
60 // Returns default configuration. | 61 // Returns default configuration. |
61 getDefaultConfiguration() { | 62 getDefaultConfiguration() { |
62 return Promise.resolve({frequency: 5}); | 63 return Promise.resolve({frequency: 5}); |
63 } | 64 } |
64 | 65 |
65 // Adds configuration for the sensor and starts reporting fake data | 66 // Adds configuration for the sensor and starts reporting fake data |
66 // through update_reading_function_ callback. | 67 // through update_reading_function_ callback. |
67 addConfiguration(configuration) { | 68 addConfiguration(configuration) { |
68 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 69 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
69 | 70 |
70 if (!this.start_should_fail_ && this.update_reading_function_ != null) { | 71 this.active_sensor_configurations_.push(configuration); |
71 let timeout = (1 / configuration.frequency) * 1000; | 72 // Sort using descending order. |
72 this.sensor_reading_timer_id_ = window.setTimeout(() => { | 73 this.active_sensor_configurations_.sort( |
73 if (this.update_reading_function_) | 74 (first, second) => { return second.frequency - first.frequency }); |
74 this.update_reading_function_(this.buffer_); | |
75 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | |
76 this.client_.sensorReadingChanged(); | |
77 } | |
78 }, timeout); | |
79 } | |
80 | 75 |
81 this.active_sensor_configurations_.push(configuration); | 76 if (!this.start_should_fail_ ) |
| 77 this.startReading(); |
82 | 78 |
83 if (this.add_configuration_called_ != null) | 79 if (this.add_configuration_called_ != null) |
84 this.add_configuration_called_(this); | 80 this.add_configuration_called_(this); |
85 | 81 |
86 return sensorResponse(!this.start_should_fail_); | 82 return sensorResponse(!this.start_should_fail_); |
87 } | 83 } |
88 | 84 |
89 // Removes sensor configuration from the list of active configurations and | 85 // Removes sensor configuration from the list of active configurations and |
90 // stops notification about sensor reading changes if | 86 // stops notification about sensor reading changes if |
91 // active_sensor_configurations_ is empty. | 87 // active_sensor_configurations_ is empty. |
92 removeConfiguration(configuration) { | 88 removeConfiguration(configuration) { |
93 if (this.remove_configuration_called_ != null) { | 89 if (this.remove_configuration_called_ != null) { |
94 this.remove_configuration_called_(this); | 90 this.remove_configuration_called_(this); |
95 } | 91 } |
96 | 92 |
97 let index = this.active_sensor_configurations_.indexOf(configuration); | 93 let index = this.active_sensor_configurations_.indexOf(configuration); |
98 if (index !== -1) { | 94 if (index !== -1) { |
99 this.active_sensor_configurations_.splice(index, 1); | 95 this.active_sensor_configurations_.splice(index, 1); |
100 } else { | 96 } else { |
101 return sensorResponse(false); | 97 return sensorResponse(false); |
102 } | 98 } |
103 | 99 |
104 if (this.sensor_reading_timer_id_ != null | 100 if (this.active_sensor_configurations_.length === 0) |
105 && this.active_sensor_configurations_.length === 0) { | 101 this.stopReading(); |
106 window.clearTimeout(this.sensor_reading_timer_id_); | |
107 this.sensor_reading_timer_id_ = null; | |
108 } | |
109 | 102 |
110 return sensorResponse(true); | 103 return sensorResponse(true); |
111 } | 104 } |
112 | 105 |
113 // Suspends sensor. | 106 // Suspends sensor. |
114 suspend() { | 107 suspend() { |
| 108 this.stopReading(); |
115 if (this.suspend_called_ != null) { | 109 if (this.suspend_called_ != null) { |
116 this.suspend_called_(this); | 110 this.suspend_called_(this); |
117 } | 111 } |
118 } | 112 } |
119 | 113 |
120 // Resumes sensor. | 114 // Resumes sensor. |
121 resume() { | 115 resume() { |
| 116 assert_equals(this.sensor_reading_timer_id_, null); |
| 117 this.startReading(); |
122 if (this.resume_called_ != null) { | 118 if (this.resume_called_ != null) { |
123 this.resume_called_(this); | 119 this.resume_called_(this); |
124 } | 120 } |
125 } | 121 } |
126 | 122 |
127 // Mock functions | 123 // Mock functions |
128 | 124 |
129 // Resets mock Sensor state. | 125 // Resets mock Sensor state. |
130 reset() { | 126 reset() { |
131 if (this.sensor_reading_timer_id_) { | 127 this.stopReading(); |
132 window.clearTimeout(this.sensor_reading_timer_id_); | |
133 this.sensor_reading_timer_id_ = null; | |
134 } | |
135 | 128 |
| 129 this.expects_modified_reading_ = false; |
136 this.start_should_fail_ = false; | 130 this.start_should_fail_ = false; |
137 this.update_reading_function_ = null; | 131 this.update_reading_function_ = null; |
138 this.active_sensor_configurations_ = []; | 132 this.active_sensor_configurations_ = []; |
139 this.suspend_called_ = null; | 133 this.suspend_called_ = null; |
140 this.resume_called_ = null; | 134 this.resume_called_ = null; |
141 this.add_configuration_called_ = null; | 135 this.add_configuration_called_ = null; |
142 this.remove_configuration_called_ = null; | 136 this.remove_configuration_called_ = null; |
143 this.resetBuffer(); | 137 this.resetBuffer(); |
144 core.unmapBuffer(this.buffer_array_); | 138 core.unmapBuffer(this.buffer_array_); |
145 this.buffer_array_ = null; | 139 this.buffer_array_ = null; |
(...skipping 11 matching lines...) Expand all Loading... |
157 setUpdateSensorReadingFunction(update_reading_function) { | 151 setUpdateSensorReadingFunction(update_reading_function) { |
158 this.update_reading_function_ = update_reading_function; | 152 this.update_reading_function_ = update_reading_function; |
159 return Promise.resolve(this); | 153 return Promise.resolve(this); |
160 } | 154 } |
161 | 155 |
162 // Sets flag that forces sensor to fail when addConfiguration is invoked. | 156 // Sets flag that forces sensor to fail when addConfiguration is invoked. |
163 setStartShouldFail(should_fail) { | 157 setStartShouldFail(should_fail) { |
164 this.start_should_fail_ = should_fail; | 158 this.start_should_fail_ = should_fail; |
165 } | 159 } |
166 | 160 |
| 161 // Sets flags that asks for a modified reading values at each iteration |
| 162 // to initiate 'onchange' event broadcasting. |
| 163 setExpectsModifiedReading(expects_modified_reading) { |
| 164 this.expects_modified_reading_ = expects_modified_reading; |
| 165 } |
| 166 |
167 // Returns resolved promise if suspend() was called, rejected otherwise. | 167 // Returns resolved promise if suspend() was called, rejected otherwise. |
168 suspendCalled() { | 168 suspendCalled() { |
169 return new Promise((resolve, reject) => { | 169 return new Promise((resolve, reject) => { |
170 this.suspend_called_ = resolve; | 170 this.suspend_called_ = resolve; |
171 }); | 171 }); |
172 } | 172 } |
173 | 173 |
174 // Returns resolved promise if resume() was called, rejected otherwise. | 174 // Returns resolved promise if resume() was called, rejected otherwise. |
175 resumeCalled() { | 175 resumeCalled() { |
176 return new Promise((resolve, reject) => { | 176 return new Promise((resolve, reject) => { |
177 this.resume_called_ = resolve; | 177 this.resume_called_ = resolve; |
178 }); | 178 }); |
179 } | 179 } |
180 | 180 |
181 // Resolves promise when addConfiguration() is called. | 181 // Resolves promise when addConfiguration() is called. |
182 addConfigurationCalled() { | 182 addConfigurationCalled() { |
183 return new Promise((resolve, reject) => { | 183 return new Promise((resolve, reject) => { |
184 this.add_configuration_called_ = resolve; | 184 this.add_configuration_called_ = resolve; |
185 }); | 185 }); |
186 } | 186 } |
187 | 187 |
188 // Resolves promise when removeConfiguration() is called. | 188 // Resolves promise when removeConfiguration() is called. |
189 removeConfigurationCalled() { | 189 removeConfigurationCalled() { |
190 return new Promise((resolve, reject) => { | 190 return new Promise((resolve, reject) => { |
191 this.remove_configuration_called_ = resolve; | 191 this.remove_configuration_called_ = resolve; |
192 }); | 192 }); |
193 } | 193 } |
194 | 194 |
| 195 startReading() { |
| 196 if (this.update_reading_function_ != null) { |
| 197 let max_frequency_used = |
| 198 this.active_sensor_configurations_[0].frequency; |
| 199 let timeout = (1 / max_frequency_used) * 1000; |
| 200 this.sensor_reading_timer_id_ = window.setInterval(() => { |
| 201 if (this.update_reading_function_) |
| 202 this.update_reading_function_(this.buffer_, |
| 203 this.expects_modified_reading_); |
| 204 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
| 205 this.client_.sensorReadingChanged(); |
| 206 } |
| 207 }, timeout); |
| 208 } |
| 209 } |
| 210 |
| 211 stopReading() { |
| 212 if (this.sensor_reading_timer_id_ != null) { |
| 213 window.clearInterval(this.sensor_reading_timer_id_); |
| 214 this.sensor_reading_timer_id_ = null; |
| 215 } |
| 216 } |
| 217 |
195 } | 218 } |
196 | 219 |
197 // Helper function that returns resolved promise for getSensor() function. | 220 // Helper function that returns resolved promise for getSensor() function. |
198 function getSensorResponse(init_params, client_request) { | 221 function getSensorResponse(init_params, client_request) { |
199 return Promise.resolve({init_params, client_request}); | 222 return Promise.resolve({init_params, client_request}); |
200 } | 223 } |
201 | 224 |
202 // Class that mocks SensorProvider interface defined in | 225 // Class that mocks SensorProvider interface defined in |
203 // sensor_provider.mojom | 226 // sensor_provider.mojom |
204 class MockSensorProvider { | 227 class MockSensorProvider { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // Resets state of mock SensorProvider between test runs. | 302 // Resets state of mock SensorProvider between test runs. |
280 reset() { | 303 reset() { |
281 if (this.active_sensor_ != null) { | 304 if (this.active_sensor_ != null) { |
282 this.active_sensor_.reset(); | 305 this.active_sensor_.reset(); |
283 this.active_sensor_ = null; | 306 this.active_sensor_ = null; |
284 } | 307 } |
285 | 308 |
286 this.get_sensor_should_fail_ = false; | 309 this.get_sensor_should_fail_ = false; |
287 this.resolve_func_ = null; | 310 this.resolve_func_ = null; |
288 this.max_frequency_ = 60; | 311 this.max_frequency_ = 60; |
| 312 this.is_continuous_ = false; |
289 } | 313 } |
290 | 314 |
291 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 315 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
292 // invoked. | 316 // invoked. |
293 setGetSensorShouldFail(should_fail) { | 317 setGetSensorShouldFail(should_fail) { |
294 this.get_sensor_should_fail_ = should_fail; | 318 this.get_sensor_should_fail_ = should_fail; |
295 } | 319 } |
296 | 320 |
297 // Returns mock sensor that was created in getSensor to the layout test. | 321 // Returns mock sensor that was created in getSensor to the layout test. |
298 getCreatedSensor() { | 322 getCreatedSensor() { |
299 if (this.active_sensor_ != null) { | 323 if (this.active_sensor_ != null) { |
300 return Promise.resolve(this.active_sensor_); | 324 return Promise.resolve(this.active_sensor_); |
301 } | 325 } |
302 | 326 |
303 return new Promise((resolve, reject) => { | 327 return new Promise((resolve, reject) => { |
304 this.resolve_func_ = resolve; | 328 this.resolve_func_ = resolve; |
305 }); | 329 }); |
306 } | 330 } |
307 | 331 |
308 // Forces sensor to use |reporting_mode| as an update mode. | 332 // Forces sensor to use |reporting_mode| as an update mode. |
309 setContinuousReportingMode(reporting_mode) { | 333 setContinuousReportingMode() { |
310 this.is_continuous_ = reporting_mode; | 334 this.is_continuous_ = true; |
311 } | 335 } |
312 | 336 |
313 // Sets the maximum frequency for a concrete sensor. | 337 // Sets the maximum frequency for a concrete sensor. |
314 setMaximumSupportedFrequency(frequency) { | 338 setMaximumSupportedFrequency(frequency) { |
315 this.max_frequency_ = frequency; | 339 this.max_frequency_ = frequency; |
316 } | 340 } |
317 } | 341 } |
318 | 342 |
319 let mockSensorProvider = new MockSensorProvider; | 343 let mockSensorProvider = new MockSensorProvider; |
320 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 344 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
(...skipping 19 matching lines...) Expand all Loading... |
340 }; | 364 }; |
341 | 365 |
342 let onFailure = () => { | 366 let onFailure = () => { |
343 sensor.mockSensorProvider.reset(); | 367 sensor.mockSensorProvider.reset(); |
344 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 368 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
345 }; | 369 }; |
346 | 370 |
347 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 371 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
348 }), name, properties); | 372 }), name, properties); |
349 } | 373 } |
OLD | NEW |