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

Side by Side Diff: content/browser/device_sensors/device_sensor_browsertest.cc

Issue 2896583005: Reland: Refactor DeviceMotionEventPump to use //device/generic_sensor instead of //device/sensors (Closed)
Patch Set: updated device sensor browsertest Created 3 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "base/threading/platform_thread.h" 9 #include "base/threading/platform_thread.h"
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h" 12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/content_switches.h" 13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_browser_test.h" 14 #include "content/public/test/content_browser_test.h"
15 #include "content/public/test/content_browser_test_utils.h" 15 #include "content/public/test/content_browser_test_utils.h"
16 #include "content/public/test/test_navigation_observer.h" 16 #include "content/public/test/test_navigation_observer.h"
17 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
18 #include "content/shell/browser/shell.h" 18 #include "content/shell/browser/shell.h"
19 #include "content/shell/browser/shell_javascript_dialog_manager.h" 19 #include "content/shell/browser/shell_javascript_dialog_manager.h"
20 #include "device/generic_sensor/platform_sensor.h"
21 #include "device/generic_sensor/platform_sensor_provider.h"
20 #include "device/sensors/data_fetcher_shared_memory.h" 22 #include "device/sensors/data_fetcher_shared_memory.h"
21 #include "device/sensors/device_sensor_service.h" 23 #include "device/sensors/device_sensor_service.h"
22 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" 24 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h"
23 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" 25 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h"
24 26
25 namespace content { 27 namespace content {
26 28
27 namespace { 29 namespace {
28 30
29 class FakeDataFetcher : public device::DataFetcherSharedMemory { 31 class FakeDataFetcher : public device::DataFetcherSharedMemory {
30 public: 32 public:
31 FakeDataFetcher() : sensor_data_available_(true) {} 33 FakeDataFetcher() : sensor_data_available_(true) {}
32 ~FakeDataFetcher() override {} 34 ~FakeDataFetcher() override {}
33 35
34 void SetMotionStartedCallback(base::Closure motion_started_callback) {
35 motion_started_callback_ = motion_started_callback;
36 }
37
38 void SetMotionStoppedCallback(base::Closure motion_stopped_callback) {
39 motion_stopped_callback_ = motion_stopped_callback;
40 }
41
42 void SetOrientationStartedCallback( 36 void SetOrientationStartedCallback(
43 base::Closure orientation_started_callback) { 37 base::Closure orientation_started_callback) {
44 orientation_started_callback_ = orientation_started_callback; 38 orientation_started_callback_ = orientation_started_callback;
45 } 39 }
46 40
47 void SetOrientationStoppedCallback( 41 void SetOrientationStoppedCallback(
48 base::Closure orientation_stopped_callback) { 42 base::Closure orientation_stopped_callback) {
49 orientation_stopped_callback_ = orientation_stopped_callback; 43 orientation_stopped_callback_ = orientation_stopped_callback;
50 } 44 }
51 45
52 void SetOrientationAbsoluteStartedCallback( 46 void SetOrientationAbsoluteStartedCallback(
53 base::Closure orientation_absolute_started_callback) { 47 base::Closure orientation_absolute_started_callback) {
54 orientation_absolute_started_callback_ = 48 orientation_absolute_started_callback_ =
55 orientation_absolute_started_callback; 49 orientation_absolute_started_callback;
56 } 50 }
57 51
58 void SetOrientationAbsoluteStoppedCallback( 52 void SetOrientationAbsoluteStoppedCallback(
59 base::Closure orientation_absolute_stopped_callback) { 53 base::Closure orientation_absolute_stopped_callback) {
60 orientation_absolute_stopped_callback_ = 54 orientation_absolute_stopped_callback_ =
61 orientation_absolute_stopped_callback; 55 orientation_absolute_stopped_callback;
62 } 56 }
63 57
64 bool Start(device::ConsumerType consumer_type, void* buffer) override { 58 bool Start(device::ConsumerType consumer_type, void* buffer) override {
65 EXPECT_TRUE(buffer); 59 EXPECT_TRUE(buffer);
66 60
67 switch (consumer_type) { 61 switch (consumer_type) {
68 case device::CONSUMER_TYPE_MOTION: {
69 device::DeviceMotionHardwareBuffer* motion_buffer =
70 static_cast<device::DeviceMotionHardwareBuffer*>(buffer);
71 if (sensor_data_available_)
72 UpdateMotion(motion_buffer);
73 SetMotionBufferReady(motion_buffer);
74 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
75 motion_started_callback_);
76 } break;
77 case device::CONSUMER_TYPE_ORIENTATION: { 62 case device::CONSUMER_TYPE_ORIENTATION: {
78 device::DeviceOrientationHardwareBuffer* orientation_buffer = 63 device::DeviceOrientationHardwareBuffer* orientation_buffer =
79 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); 64 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer);
80 if (sensor_data_available_) 65 if (sensor_data_available_)
81 UpdateOrientation(orientation_buffer); 66 UpdateOrientation(orientation_buffer);
82 SetOrientationBufferReady(orientation_buffer); 67 SetOrientationBufferReady(orientation_buffer);
83 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 68 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
84 orientation_started_callback_); 69 orientation_started_callback_);
85 } break; 70 } break;
86 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { 71 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: {
87 device::DeviceOrientationHardwareBuffer* orientation_buffer = 72 device::DeviceOrientationHardwareBuffer* orientation_buffer =
88 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); 73 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer);
89 if (sensor_data_available_) 74 if (sensor_data_available_)
90 UpdateOrientationAbsolute(orientation_buffer); 75 UpdateOrientationAbsolute(orientation_buffer);
91 SetOrientationBufferReady(orientation_buffer); 76 SetOrientationBufferReady(orientation_buffer);
92 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 77 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
93 orientation_absolute_started_callback_); 78 orientation_absolute_started_callback_);
94 } break; 79 } break;
95 default: 80 default:
96 return false; 81 return false;
97 } 82 }
98 return true; 83 return true;
99 } 84 }
100 85
101 bool Stop(device::ConsumerType consumer_type) override { 86 bool Stop(device::ConsumerType consumer_type) override {
102 switch (consumer_type) { 87 switch (consumer_type) {
103 case device::CONSUMER_TYPE_MOTION:
104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
105 motion_stopped_callback_);
106 break;
107 case device::CONSUMER_TYPE_ORIENTATION: 88 case device::CONSUMER_TYPE_ORIENTATION:
108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 89 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
109 orientation_stopped_callback_); 90 orientation_stopped_callback_);
110 break; 91 break;
111 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: 92 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE:
112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
113 orientation_absolute_stopped_callback_); 94 orientation_absolute_stopped_callback_);
114 break; 95 break;
115 default: 96 default:
116 return false; 97 return false;
117 } 98 }
118 return true; 99 return true;
119 } 100 }
120 101
121 void Fetch(unsigned consumer_bitmask) override { 102 void Fetch(unsigned consumer_bitmask) override {
122 FAIL() << "fetch should not be called"; 103 FAIL() << "fetch should not be called";
123 } 104 }
124 105
125 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } 106 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; }
126 107
127 void SetSensorDataAvailable(bool available) { 108 void SetSensorDataAvailable(bool available) {
128 sensor_data_available_ = available; 109 sensor_data_available_ = available;
129 } 110 }
130 111
131 void SetMotionBufferReady(device::DeviceMotionHardwareBuffer* buffer) {
132 buffer->seqlock.WriteBegin();
133 buffer->data.all_available_sensors_are_active = true;
134 buffer->seqlock.WriteEnd();
135 }
136
137 void SetOrientationBufferReady( 112 void SetOrientationBufferReady(
138 device::DeviceOrientationHardwareBuffer* buffer) { 113 device::DeviceOrientationHardwareBuffer* buffer) {
139 buffer->seqlock.WriteBegin(); 114 buffer->seqlock.WriteBegin();
140 buffer->data.all_available_sensors_are_active = true; 115 buffer->data.all_available_sensors_are_active = true;
141 buffer->seqlock.WriteEnd(); 116 buffer->seqlock.WriteEnd();
142 } 117 }
143 118
144 void UpdateMotion(device::DeviceMotionHardwareBuffer* buffer) {
145 buffer->seqlock.WriteBegin();
146 buffer->data.acceleration_x = 1;
147 buffer->data.has_acceleration_x = true;
148 buffer->data.acceleration_y = 2;
149 buffer->data.has_acceleration_y = true;
150 buffer->data.acceleration_z = 3;
151 buffer->data.has_acceleration_z = true;
152
153 buffer->data.acceleration_including_gravity_x = 4;
154 buffer->data.has_acceleration_including_gravity_x = true;
155 buffer->data.acceleration_including_gravity_y = 5;
156 buffer->data.has_acceleration_including_gravity_y = true;
157 buffer->data.acceleration_including_gravity_z = 6;
158 buffer->data.has_acceleration_including_gravity_z = true;
159
160 buffer->data.rotation_rate_alpha = 7;
161 buffer->data.has_rotation_rate_alpha = true;
162 buffer->data.rotation_rate_beta = 8;
163 buffer->data.has_rotation_rate_beta = true;
164 buffer->data.rotation_rate_gamma = 9;
165 buffer->data.has_rotation_rate_gamma = true;
166
167 buffer->data.interval = 100;
168 buffer->data.all_available_sensors_are_active = true;
169 buffer->seqlock.WriteEnd();
170 }
171
172 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) { 119 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) {
173 buffer->seqlock.WriteBegin(); 120 buffer->seqlock.WriteBegin();
174 buffer->data.alpha = 1; 121 buffer->data.alpha = 1;
175 buffer->data.has_alpha = true; 122 buffer->data.has_alpha = true;
176 buffer->data.beta = 2; 123 buffer->data.beta = 2;
177 buffer->data.has_beta = true; 124 buffer->data.has_beta = true;
178 buffer->data.gamma = 3; 125 buffer->data.gamma = 3;
179 buffer->data.has_gamma = true; 126 buffer->data.has_gamma = true;
180 buffer->data.all_available_sensors_are_active = true; 127 buffer->data.all_available_sensors_are_active = true;
181 buffer->seqlock.WriteEnd(); 128 buffer->seqlock.WriteEnd();
182 } 129 }
183 130
184 void UpdateOrientationAbsolute( 131 void UpdateOrientationAbsolute(
185 device::DeviceOrientationHardwareBuffer* buffer) { 132 device::DeviceOrientationHardwareBuffer* buffer) {
186 buffer->seqlock.WriteBegin(); 133 buffer->seqlock.WriteBegin();
187 buffer->data.alpha = 4; 134 buffer->data.alpha = 4;
188 buffer->data.has_alpha = true; 135 buffer->data.has_alpha = true;
189 buffer->data.beta = 5; 136 buffer->data.beta = 5;
190 buffer->data.has_beta = true; 137 buffer->data.has_beta = true;
191 buffer->data.gamma = 6; 138 buffer->data.gamma = 6;
192 buffer->data.has_gamma = true; 139 buffer->data.has_gamma = true;
193 buffer->data.absolute = true; 140 buffer->data.absolute = true;
194 buffer->data.all_available_sensors_are_active = true; 141 buffer->data.all_available_sensors_are_active = true;
195 buffer->seqlock.WriteEnd(); 142 buffer->seqlock.WriteEnd();
196 } 143 }
197 144
198 // The below callbacks should be run on the UI thread. 145 // The below callbacks should be run on the UI thread.
199 base::Closure motion_started_callback_;
200 base::Closure orientation_started_callback_; 146 base::Closure orientation_started_callback_;
201 base::Closure orientation_absolute_started_callback_; 147 base::Closure orientation_absolute_started_callback_;
202 base::Closure motion_stopped_callback_;
203 base::Closure orientation_stopped_callback_; 148 base::Closure orientation_stopped_callback_;
204 base::Closure orientation_absolute_stopped_callback_; 149 base::Closure orientation_absolute_stopped_callback_;
205 bool sensor_data_available_; 150 bool sensor_data_available_;
206 151
207 private: 152 private:
208 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); 153 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher);
209 }; 154 };
210 155
156 class FakeAccelerometerSensor : public device::PlatformSensor {
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 "meter" means "sensor", just call it a FakeAcceler
juncai 2017/05/30 22:26:56 Done.
157 public:
158 FakeAccelerometerSensor(device::mojom::SensorType type,
159 mojo::ScopedSharedBufferMapping mapping,
160 device::PlatformSensorProvider* provider)
161 : PlatformSensor(type, std::move(mapping), provider) {}
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 type -> device::mojom::SensorType::ACCELEROMETER
juncai 2017/05/30 22:26:57 Done.
162
163 device::mojom::ReportingMode GetReportingMode() override {
164 return device::mojom::ReportingMode::ON_CHANGE;
165 }
166
167 bool StartSensor(
168 const device::PlatformSensorConfiguration& configuration) override {
169 device::SensorReading reading;
170 reading.timestamp =
171 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
172 reading.values[0] = 4;
173 reading.values[1] = 5;
174 reading.values[2] = 6;
175 UpdateSensorReading(reading, true);
176 return true;
177 }
178
179 void StopSensor() override{};
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 nit: space before {
juncai 2017/05/30 22:26:56 There was a space between {, but it's odd that "gi
180
181 protected:
182 ~FakeAccelerometerSensor() override = default;
183
184 bool CheckSensorConfiguration(
185 const device::PlatformSensorConfiguration& configuration) override {
186 return true;
187 }
188
189 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
190 device::PlatformSensorConfiguration default_configuration;
191 default_configuration.set_frequency(60);
192 return default_configuration;
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 return device::PlatformSensorConfiguration(60 /* f
juncai 2017/05/30 22:26:56 Done.
193 }
194
195 private:
196 DISALLOW_COPY_AND_ASSIGN(FakeAccelerometerSensor);
197 };
198
199 class FakeLinearAccelerationSensor : public device::PlatformSensor {
200 public:
201 FakeLinearAccelerationSensor(device::mojom::SensorType type,
202 mojo::ScopedSharedBufferMapping mapping,
203 device::PlatformSensorProvider* provider)
204 : PlatformSensor(type, std::move(mapping), provider) {}
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 type -> device::mojom::SensorType::LINEAR_ACCELERA
juncai 2017/05/30 22:26:56 Done.
205
206 device::mojom::ReportingMode GetReportingMode() override {
207 return device::mojom::ReportingMode::CONTINUOUS;
208 }
209
210 bool StartSensor(
211 const device::PlatformSensorConfiguration& configuration) override {
212 device::SensorReading reading;
213 reading.timestamp =
214 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
215 reading.values[0] = 1;
216 reading.values[1] = 2;
217 reading.values[2] = 3;
218 UpdateSensorReading(reading, true);
219 return true;
220 }
221
222 void StopSensor() override{};
223
224 protected:
225 ~FakeLinearAccelerationSensor() override = default;
226
227 bool CheckSensorConfiguration(
228 const device::PlatformSensorConfiguration& configuration) override {
229 return true;
230 }
231
232 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
233 device::PlatformSensorConfiguration default_configuration;
234 default_configuration.set_frequency(60);
235 return default_configuration;
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 return device::PlatformSensorConfiguration(60 /* f
juncai 2017/05/30 22:26:57 Done.
236 }
237
238 private:
239 DISALLOW_COPY_AND_ASSIGN(FakeLinearAccelerationSensor);
240 };
241
242 class FakeGyroscopeSensor : public device::PlatformSensor {
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 "scope" also means "sensor", so just call it a Fak
juncai 2017/05/30 22:26:56 Done.
243 public:
244 FakeGyroscopeSensor(device::mojom::SensorType type,
245 mojo::ScopedSharedBufferMapping mapping,
246 device::PlatformSensorProvider* provider)
247 : PlatformSensor(type, std::move(mapping), provider) {}
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 type -> device::mojom::SensorType::GYROSCOPE
juncai 2017/05/30 22:26:56 Done.
248
249 device::mojom::ReportingMode GetReportingMode() override {
250 return device::mojom::ReportingMode::ON_CHANGE;
251 }
252
253 bool StartSensor(
254 const device::PlatformSensorConfiguration& configuration) override {
255 device::SensorReading reading;
256 reading.timestamp =
257 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
258 reading.values[0] = 7;
259 reading.values[1] = 8;
260 reading.values[2] = 9;
261 UpdateSensorReading(reading, true);
262 return true;
263 }
264
265 void StopSensor() override{};
266
267 protected:
268 ~FakeGyroscopeSensor() override = default;
269
270 bool CheckSensorConfiguration(
271 const device::PlatformSensorConfiguration& configuration) override {
272 return true;
273 }
274
275 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
276 device::PlatformSensorConfiguration default_configuration;
277 default_configuration.set_frequency(60);
278 return default_configuration;
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 return device::PlatformSensorConfiguration(60 /* f
juncai 2017/05/30 22:26:56 Done.
279 }
280
281 private:
282 DISALLOW_COPY_AND_ASSIGN(FakeGyroscopeSensor);
283 };
284
285 class FakeSensorProvider : public device::PlatformSensorProvider {
286 public:
287 static FakeSensorProvider* GetInstance() {
288 return base::Singleton<FakeSensorProvider, base::LeakySingletonTraits<
289 FakeSensorProvider>>::get();
290 }
291 FakeSensorProvider()
292 : accelerometer_sensor_is_available_(true),
293 linear_acceleration_sensor_is_available_(true),
294 gyroscope_sensor_is_available_(true) {}
295 ~FakeSensorProvider() override = default;
296
297 void set_accelerometer_sensor_is_available(
298 bool accelerometer_sensor_is_available) {
299 accelerometer_sensor_is_available_ = accelerometer_sensor_is_available;
300 }
301
302 void set_linear_acceleration_sensor_is_available(
303 bool linear_acceleration_sensor_is_available) {
304 linear_acceleration_sensor_is_available_ =
305 linear_acceleration_sensor_is_available;
306 }
307
308 void set_gyroscope_sensor_is_available(bool gyroscope_sensor_is_available) {
309 gyroscope_sensor_is_available_ = gyroscope_sensor_is_available;
310 }
311
312 protected:
313 void CreateSensorInternal(device::mojom::SensorType type,
314 mojo::ScopedSharedBufferMapping mapping,
315 const CreateSensorCallback& callback) override {
316 // Create Sensors here.
317 switch (type) {
318 case device::mojom::SensorType::ACCELEROMETER: {
319 if (accelerometer_sensor_is_available_) {
320 scoped_refptr<device::PlatformSensor> sensor =
321 new FakeAccelerometerSensor(type, std::move(mapping), this);
322 callback.Run(std::move(sensor));
323 } else {
324 callback.Run(nullptr);
325 }
326 break;
327 }
328 case device::mojom::SensorType::LINEAR_ACCELERATION: {
329 if (linear_acceleration_sensor_is_available_) {
330 scoped_refptr<device::PlatformSensor> sensor =
331 new FakeLinearAccelerationSensor(type, std::move(mapping), this);
332 callback.Run(std::move(sensor));
333 } else {
334 callback.Run(nullptr);
335 }
336 break;
337 }
338 case device::mojom::SensorType::GYROSCOPE: {
339 if (gyroscope_sensor_is_available_) {
340 scoped_refptr<device::PlatformSensor> sensor =
341 new FakeGyroscopeSensor(type, std::move(mapping), this);
342 callback.Run(std::move(sensor));
343 } else {
344 callback.Run(nullptr);
345 }
346 break;
347 }
348 default:
349 NOTIMPLEMENTED();
350 callback.Run(nullptr);
351 }
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 scoped_refptr<device::PlatformSensor> sensor; swi
juncai 2017/05/30 22:26:56 Done.
352 }
353
354 bool accelerometer_sensor_is_available_;
355 bool linear_acceleration_sensor_is_available_;
356 bool gyroscope_sensor_is_available_;
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 bool accelerometer_is_available_; bool linear_acce
juncai 2017/05/30 22:26:57 Done.
357 };
358
211 class DeviceSensorBrowserTest : public ContentBrowserTest { 359 class DeviceSensorBrowserTest : public ContentBrowserTest {
212 public: 360 public:
213 DeviceSensorBrowserTest() 361 DeviceSensorBrowserTest()
214 : fetcher_(nullptr), 362 : fetcher_(nullptr),
215 io_loop_finished_event_( 363 io_loop_finished_event_(
216 base::WaitableEvent::ResetPolicy::AUTOMATIC, 364 base::WaitableEvent::ResetPolicy::AUTOMATIC,
217 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 365 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
218 366
219 void SetUpOnMainThread() override { 367 void SetUpOnMainThread() override {
220 // Initialize the RunLoops now that the main thread has been created. 368 // Initialize the RunLoops now that the main thread has been created.
221 motion_started_runloop_.reset(new base::RunLoop());
222 motion_stopped_runloop_.reset(new base::RunLoop());
223 orientation_started_runloop_.reset(new base::RunLoop()); 369 orientation_started_runloop_.reset(new base::RunLoop());
224 orientation_stopped_runloop_.reset(new base::RunLoop()); 370 orientation_stopped_runloop_.reset(new base::RunLoop());
225 orientation_absolute_started_runloop_.reset(new base::RunLoop()); 371 orientation_absolute_started_runloop_.reset(new base::RunLoop());
226 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); 372 orientation_absolute_stopped_runloop_.reset(new base::RunLoop());
227 #if defined(OS_ANDROID) 373 #if defined(OS_ANDROID)
228 // On Android, the DeviceSensorService lives on the UI thread. 374 // On Android, the DeviceSensorService lives on the UI thread.
229 SetUpFetcher(); 375 SetUpFetcher();
376 BrowserThread::PostTask(
377 BrowserThread::IO, FROM_HERE,
378 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThreadForAndroid,
379 base::Unretained(this)));
380 io_loop_finished_event_.Wait();
230 #else 381 #else
231 // On all other platforms, the DeviceSensorService lives on the IO thread. 382 // On all other platforms, the DeviceSensorService lives on the IO thread.
232 BrowserThread::PostTask( 383 BrowserThread::PostTask(
233 BrowserThread::IO, FROM_HERE, 384 BrowserThread::IO, FROM_HERE,
234 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, 385 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread,
235 base::Unretained(this))); 386 base::Unretained(this)));
236 io_loop_finished_event_.Wait(); 387 io_loop_finished_event_.Wait();
237 #endif 388 #endif
238 } 389 }
239 390
240 void SetUpFetcher() { 391 void SetUpFetcher() {
241 fetcher_ = new FakeDataFetcher(); 392 fetcher_ = new FakeDataFetcher();
242 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure());
243 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure());
244 fetcher_->SetOrientationStartedCallback( 393 fetcher_->SetOrientationStartedCallback(
245 orientation_started_runloop_->QuitClosure()); 394 orientation_started_runloop_->QuitClosure());
246 fetcher_->SetOrientationStoppedCallback( 395 fetcher_->SetOrientationStoppedCallback(
247 orientation_stopped_runloop_->QuitClosure()); 396 orientation_stopped_runloop_->QuitClosure());
248 fetcher_->SetOrientationAbsoluteStartedCallback( 397 fetcher_->SetOrientationAbsoluteStartedCallback(
249 orientation_absolute_started_runloop_->QuitClosure()); 398 orientation_absolute_started_runloop_->QuitClosure());
250 fetcher_->SetOrientationAbsoluteStoppedCallback( 399 fetcher_->SetOrientationAbsoluteStoppedCallback(
251 orientation_absolute_stopped_runloop_->QuitClosure()); 400 orientation_absolute_stopped_runloop_->QuitClosure());
252 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( 401 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting(
253 fetcher_); 402 fetcher_);
254 } 403 }
255 404
405 void SetUpOnIOThreadForAndroid() {
Reilly Grant (use Gerrit) 2017/05/27 03:17:50 This is the same as SetUpOnIOThread().
juncai 2017/05/30 22:26:57 It doesn't have the SetUpFetcher().
Reilly Grant (use Gerrit) 2017/06/06 20:58:55 It would be easier to read if you put the call to
juncai 2017/06/06 23:36:25 Done.
406 sensor_provider_ = FakeSensorProvider::GetInstance();
407 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_);
408 io_loop_finished_event_.Signal();
409 }
410
256 void SetUpOnIOThread() { 411 void SetUpOnIOThread() {
257 SetUpFetcher(); 412 SetUpFetcher();
413 sensor_provider_ = FakeSensorProvider::GetInstance();
414 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_);
258 io_loop_finished_event_.Signal(); 415 io_loop_finished_event_.Signal();
259 } 416 }
260 417
418 void TearDown() override {
419 device::PlatformSensorProvider::SetProviderForTesting(nullptr);
420 }
421
261 void DelayAndQuit(base::TimeDelta delay) { 422 void DelayAndQuit(base::TimeDelta delay) {
262 base::PlatformThread::Sleep(delay); 423 base::PlatformThread::Sleep(delay);
263 base::MessageLoop::current()->QuitWhenIdle(); 424 base::MessageLoop::current()->QuitWhenIdle();
264 } 425 }
265 426
266 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { 427 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) {
267 ShellJavaScriptDialogManager* dialog_manager = 428 ShellJavaScriptDialogManager* dialog_manager =
268 static_cast<ShellJavaScriptDialogManager*>( 429 static_cast<ShellJavaScriptDialogManager*>(
269 shell()->GetJavaScriptDialogManager(shell()->web_contents())); 430 shell()->GetJavaScriptDialogManager(shell()->web_contents()));
270 431
271 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); 432 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
272 dialog_manager->set_dialog_request_callback( 433 dialog_manager->set_dialog_request_callback(
273 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, 434 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit,
274 base::Unretained(this), delay)); 435 base::Unretained(this), delay));
275 runner->Run(); 436 runner->Run();
276 } 437 }
277 438
278 FakeDataFetcher* fetcher_; 439 FakeDataFetcher* fetcher_;
440 FakeSensorProvider* sensor_provider_;
279 441
280 // NOTE: These can only be initialized once the main thread has been created 442 // NOTE: These can only be initialized once the main thread has been created
281 // and so must be pointers instead of plain objects. 443 // and so must be pointers instead of plain objects.
282 std::unique_ptr<base::RunLoop> motion_started_runloop_;
283 std::unique_ptr<base::RunLoop> motion_stopped_runloop_;
284 std::unique_ptr<base::RunLoop> orientation_started_runloop_; 444 std::unique_ptr<base::RunLoop> orientation_started_runloop_;
285 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; 445 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_;
286 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; 446 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_;
287 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; 447 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_;
288 448
289 private: 449 private:
290 base::WaitableEvent io_loop_finished_event_; 450 base::WaitableEvent io_loop_finished_event_;
291 }; 451 };
292 452
293 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { 453 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) {
(...skipping 22 matching lines...) Expand all
316 } 476 }
317 477
318 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { 478 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) {
319 // The test page will register an event handler for motion events, 479 // The test page will register an event handler for motion events,
320 // expects to get an event with fake values, then removes the event 480 // expects to get an event with fake values, then removes the event
321 // handler and navigates to #pass. 481 // handler and navigates to #pass.
322 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); 482 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html");
323 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 483 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
324 484
325 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 485 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
326 motion_started_runloop_->Run();
327 motion_stopped_runloop_->Run();
328 } 486 }
329 487
330 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { 488 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) {
331 // The test page registers an event handler for orientation events and 489 // The test page registers an event handler for orientation events and
332 // expects to get an event with null values, because no sensor data can be 490 // expects to get an event with null values, because no sensor data can be
333 // provided. 491 // provided.
334 fetcher_->SetSensorDataAvailable(false); 492 fetcher_->SetSensorDataAvailable(false);
335 GURL test_url = 493 GURL test_url =
336 GetTestUrl("device_sensors", "device_orientation_null_test.html"); 494 GetTestUrl("device_sensors", "device_orientation_null_test.html");
337 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 495 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
(...skipping 14 matching lines...) Expand all
352 510
353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 511 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
354 orientation_absolute_started_runloop_->Run(); 512 orientation_absolute_started_runloop_->Run();
355 orientation_absolute_stopped_runloop_->Run(); 513 orientation_absolute_stopped_runloop_->Run();
356 } 514 }
357 515
358 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { 516 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) {
359 // The test page registers an event handler for motion events and 517 // The test page registers an event handler for motion events and
360 // expects to get an event with null values, because no sensor data can be 518 // expects to get an event with null values, because no sensor data can be
361 // provided. 519 // provided.
362 fetcher_->SetSensorDataAvailable(false); 520 sensor_provider_->set_accelerometer_sensor_is_available(false);
521 sensor_provider_->set_linear_acceleration_sensor_is_available(false);
522 sensor_provider_->set_gyroscope_sensor_is_available(false);
363 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); 523 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html");
364 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 524 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
365 525
366 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 526 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
367 motion_started_runloop_->Run(); 527 }
368 motion_stopped_runloop_->Run(); 528
529 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest,
530 MotionOnlySomeSensorsAreAvailableTest) {
531 // The test page registers an event handler for motion events and
532 // expects to get an event with only some fake values, because only
533 // some sensor data can be provided.
534 sensor_provider_->set_accelerometer_sensor_is_available(false);
535 GURL test_url =
536 GetTestUrl("device_sensors",
537 "device_motion_only_some_sensors_are_available_test.html");
538 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
539
540 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
369 } 541 }
370 542
371 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { 543 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) {
372 // The test page registers an event handlers for motion/orientation events and 544 // The test page registers an event handlers for motion/orientation events and
373 // expects to get events with null values. The test raises a modal alert 545 // expects to get events with null values. The test raises a modal alert
374 // dialog with a delay to test that the one-off null-events still propagate to 546 // dialog with a delay to test that the one-off null-events still propagate to
375 // window after the alert is dismissed and the callbacks are invoked which 547 // window after the alert is dismissed and the callbacks are invoked which
376 // eventually navigate to #pass. 548 // eventually navigate to #pass.
377 fetcher_->SetSensorDataAvailable(false); 549 fetcher_->SetSensorDataAvailable(false);
550 sensor_provider_->set_accelerometer_sensor_is_available(false);
551 sensor_provider_->set_linear_acceleration_sensor_is_available(false);
552 sensor_provider_->set_gyroscope_sensor_is_available(false);
378 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); 553 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
379 554
380 GURL test_url = 555 GURL test_url =
381 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); 556 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html");
382 shell()->LoadURL(test_url); 557 shell()->LoadURL(test_url);
383 558
384 // TODO(timvolodine): investigate if it is possible to test this without 559 // TODO(timvolodine): investigate if it is possible to test this without
385 // delay, crbug.com/360044. 560 // delay, crbug.com/360044.
386 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); 561 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500));
387 562
388 motion_started_runloop_->Run();
389 motion_stopped_runloop_->Run();
390 orientation_started_runloop_->Run(); 563 orientation_started_runloop_->Run();
391 orientation_stopped_runloop_->Run(); 564 orientation_stopped_runloop_->Run();
392 same_tab_observer.Wait(); 565 same_tab_observer.Wait();
393 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 566 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
394 } 567 }
395 568
396 } // namespace 569 } // namespace
397 570
398 } // namespace content 571 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/public/test/layouttest_support.h » ('j') | content/renderer/device_sensors/device_motion_event_pump.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698