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

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

Issue 2604483004: [Sensors] Reland: Reland: Align sensor reading updates and 'onchange' notification with rAF. (Closed)
Patch Set: Patch with fix Created 3 years, 12 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
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 23 matching lines...) Expand all
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.expects_modified_reading_ = false;
40 this.start_should_fail_ = false; 40 this.start_should_fail_ = false;
41 this.reporting_mode_ = reportingMode; 41 this.reporting_mode_ = reportingMode;
42 this.sensor_reading_timer_id_ = null; 42 this.sensor_reading_timer_id_ = null;
43 this.update_reading_function_ = null; 43 this.update_reading_function_ = null;
44 this.reading_updates_count_ = 0;
44 this.suspend_called_ = null; 45 this.suspend_called_ = null;
45 this.resume_called_ = null; 46 this.resume_called_ = null;
46 this.add_configuration_called_ = null; 47 this.add_configuration_called_ = null;
47 this.remove_configuration_called_ = null; 48 this.remove_configuration_called_ = null;
48 this.active_sensor_configurations_ = []; 49 this.active_sensor_configurations_ = [];
49 let rv = core.mapBuffer(handle, offset, size, 50 let rv = core.mapBuffer(handle, offset, size,
50 core.MAP_BUFFER_FLAG_NONE); 51 core.MAP_BUFFER_FLAG_NONE);
51 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); 52 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer");
52 this.buffer_array_ = rv.buffer; 53 this.buffer_array_ = rv.buffer;
53 this.buffer_ = new Float64Array(this.buffer_array_); 54 this.buffer_ = new Float64Array(this.buffer_array_);
54 this.resetBuffer(); 55 this.resetBuffer();
55 bindings.StubBindings(this.stub_).delegate = this; 56 bindings.StubBindings(this.stub_).delegate = this;
56 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { 57 bindings.StubBindings(this.stub_).connectionErrorHandler = () => {
57 this.reset(); 58 this.reset();
58 }; 59 };
59 } 60 }
60 61
61 // Returns default configuration. 62 // Returns default configuration.
62 getDefaultConfiguration() { 63 getDefaultConfiguration() {
63 return Promise.resolve({frequency: 5}); 64 return Promise.resolve({frequency: 5});
64 } 65 }
65 66
67 reading_updates_count() {
68 return this.reading_updates_count_;
69 }
66 // Adds configuration for the sensor and starts reporting fake data 70 // Adds configuration for the sensor and starts reporting fake data
67 // through update_reading_function_ callback. 71 // through update_reading_function_ callback.
68 addConfiguration(configuration) { 72 addConfiguration(configuration) {
69 assert_not_equals(configuration, null, "Invalid sensor configuration."); 73 assert_not_equals(configuration, null, "Invalid sensor configuration.");
70 74
71 this.active_sensor_configurations_.push(configuration); 75 this.active_sensor_configurations_.push(configuration);
72 // Sort using descending order. 76 // Sort using descending order.
73 this.active_sensor_configurations_.sort( 77 this.active_sensor_configurations_.sort(
74 (first, second) => { return second.frequency - first.frequency }); 78 (first, second) => { return second.frequency - first.frequency });
75 79
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 124 }
121 } 125 }
122 126
123 // Mock functions 127 // Mock functions
124 128
125 // Resets mock Sensor state. 129 // Resets mock Sensor state.
126 reset() { 130 reset() {
127 this.stopReading(); 131 this.stopReading();
128 132
129 this.expects_modified_reading_ = false; 133 this.expects_modified_reading_ = false;
134 this.reading_updates_count_ = 0;
130 this.start_should_fail_ = false; 135 this.start_should_fail_ = false;
131 this.update_reading_function_ = null; 136 this.update_reading_function_ = null;
132 this.active_sensor_configurations_ = []; 137 this.active_sensor_configurations_ = [];
133 this.suspend_called_ = null; 138 this.suspend_called_ = null;
134 this.resume_called_ = null; 139 this.resume_called_ = null;
135 this.add_configuration_called_ = null; 140 this.add_configuration_called_ = null;
136 this.remove_configuration_called_ = null; 141 this.remove_configuration_called_ = null;
137 this.resetBuffer(); 142 this.resetBuffer();
138 core.unmapBuffer(this.buffer_array_); 143 core.unmapBuffer(this.buffer_array_);
139 this.buffer_array_ = null; 144 this.buffer_array_ = null;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 192
188 // Resolves promise when removeConfiguration() is called. 193 // Resolves promise when removeConfiguration() is called.
189 removeConfigurationCalled() { 194 removeConfigurationCalled() {
190 return new Promise((resolve, reject) => { 195 return new Promise((resolve, reject) => {
191 this.remove_configuration_called_ = resolve; 196 this.remove_configuration_called_ = resolve;
192 }); 197 });
193 } 198 }
194 199
195 startReading() { 200 startReading() {
196 if (this.update_reading_function_ != null) { 201 if (this.update_reading_function_ != null) {
202 this.stopReading();
197 let max_frequency_used = 203 let max_frequency_used =
198 this.active_sensor_configurations_[0].frequency; 204 this.active_sensor_configurations_[0].frequency;
199 let timeout = (1 / max_frequency_used) * 1000; 205 let timeout = (1 / max_frequency_used) * 1000;
200 this.sensor_reading_timer_id_ = window.setInterval(() => { 206 this.sensor_reading_timer_id_ = window.setInterval(() => {
201 if (this.update_reading_function_) 207 if (this.update_reading_function_) {
202 this.update_reading_function_(this.buffer_, 208 this.update_reading_function_(this.buffer_,
203 this.expects_modified_reading_); 209 this.expects_modified_reading_,
210 this.reading_updates_count_);
211 this.reading_updates_count_++;
212 }
204 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { 213 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) {
205 this.client_.sensorReadingChanged(); 214 this.client_.sensorReadingChanged();
206 } 215 }
207 }, timeout); 216 }, timeout);
208 } 217 }
209 } 218 }
210 219
211 stopReading() { 220 stopReading() {
212 if (this.sensor_reading_timer_id_ != null) { 221 if (this.sensor_reading_timer_id_ != null) {
213 window.clearInterval(this.sensor_reading_timer_id_); 222 window.clearInterval(this.sensor_reading_timer_id_);
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 }; 380 };
372 381
373 let onFailure = () => { 382 let onFailure = () => {
374 sensor.mockSensorProvider.reset(); 383 sensor.mockSensorProvider.reset();
375 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); 384 return new Promise((resolve, reject) => { setTimeout(reject, 0); });
376 }; 385 };
377 386
378 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); 387 return Promise.resolve(func(sensor)).then(onSuccess, onFailure);
379 }), name, properties); 388 }), name, properties);
380 } 389 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698