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

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: address more comments 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 FakeAccelerometer : public device::PlatformSensor {
157 public:
158 FakeAccelerometer(mojo::ScopedSharedBufferMapping mapping,
159 device::PlatformSensorProvider* provider)
160 : PlatformSensor(device::mojom::SensorType::ACCELEROMETER,
161 std::move(mapping),
162 provider) {}
163
164 device::mojom::ReportingMode GetReportingMode() override {
165 return device::mojom::ReportingMode::ON_CHANGE;
166 }
167
168 bool StartSensor(
169 const device::PlatformSensorConfiguration& configuration) override {
170 device::SensorReading reading;
171 reading.timestamp =
172 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
173 reading.values[0] = 4;
174 reading.values[1] = 5;
175 reading.values[2] = 6;
176 UpdateSensorReading(reading, true);
177 return true;
178 }
179
180 void StopSensor() override {}
181
182 protected:
183 ~FakeAccelerometer() override = default;
184
185 bool CheckSensorConfiguration(
186 const device::PlatformSensorConfiguration& configuration) override {
187 return true;
188 }
189
190 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
191 return device::PlatformSensorConfiguration(60 /* frequency */);
192 }
193
194 private:
195 DISALLOW_COPY_AND_ASSIGN(FakeAccelerometer);
196 };
197
198 class FakeLinearAccelerationSensor : public device::PlatformSensor {
199 public:
200 FakeLinearAccelerationSensor(mojo::ScopedSharedBufferMapping mapping,
201 device::PlatformSensorProvider* provider)
202 : PlatformSensor(device::mojom::SensorType::LINEAR_ACCELERATION,
203 std::move(mapping),
204 provider) {}
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 return device::PlatformSensorConfiguration(60 /* frequency */);
234 }
235
236 private:
237 DISALLOW_COPY_AND_ASSIGN(FakeLinearAccelerationSensor);
238 };
239
240 class FakeGyroscope : public device::PlatformSensor {
241 public:
242 FakeGyroscope(mojo::ScopedSharedBufferMapping mapping,
243 device::PlatformSensorProvider* provider)
244 : PlatformSensor(device::mojom::SensorType::GYROSCOPE,
245 std::move(mapping),
246 provider) {}
247
248 device::mojom::ReportingMode GetReportingMode() override {
249 return device::mojom::ReportingMode::ON_CHANGE;
250 }
251
252 bool StartSensor(
253 const device::PlatformSensorConfiguration& configuration) override {
254 device::SensorReading reading;
255 reading.timestamp =
256 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
257 reading.values[0] = 7;
258 reading.values[1] = 8;
259 reading.values[2] = 9;
260 UpdateSensorReading(reading, true);
261 return true;
262 }
263
264 void StopSensor() override {}
265
266 protected:
267 ~FakeGyroscope() override = default;
268
269 bool CheckSensorConfiguration(
270 const device::PlatformSensorConfiguration& configuration) override {
271 return true;
272 }
273
274 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
275 return device::PlatformSensorConfiguration(60 /* frequency */);
276 }
277
278 private:
279 DISALLOW_COPY_AND_ASSIGN(FakeGyroscope);
280 };
281
282 class FakeSensorProvider : public device::PlatformSensorProvider {
283 public:
284 static FakeSensorProvider* GetInstance() {
285 return base::Singleton<FakeSensorProvider, base::LeakySingletonTraits<
286 FakeSensorProvider>>::get();
287 }
288 FakeSensorProvider() {}
289 ~FakeSensorProvider() override = default;
290
291 void set_accelerometer_is_available(bool accelerometer_is_available) {
292 accelerometer_is_available_ = accelerometer_is_available;
293 }
294
295 void set_linear_acceleration_sensor_is_available(
296 bool linear_acceleration_sensor_is_available) {
297 linear_acceleration_sensor_is_available_ =
298 linear_acceleration_sensor_is_available;
299 }
300
301 void set_gyroscope_is_available(bool gyroscope_is_available) {
302 gyroscope_is_available_ = gyroscope_is_available;
303 }
304
305 protected:
306 void CreateSensorInternal(device::mojom::SensorType type,
307 mojo::ScopedSharedBufferMapping mapping,
308 const CreateSensorCallback& callback) override {
309 // Create Sensors here.
310 scoped_refptr<device::PlatformSensor> sensor;
311
312 switch (type) {
313 case device::mojom::SensorType::ACCELEROMETER:
314 if (accelerometer_is_available_)
315 sensor = new FakeAccelerometer(std::move(mapping), this);
316 break;
317 case device::mojom::SensorType::LINEAR_ACCELERATION:
318 if (linear_acceleration_sensor_is_available_)
319 sensor = new FakeLinearAccelerationSensor(std::move(mapping), this);
320 break;
321 case device::mojom::SensorType::GYROSCOPE:
322 if (gyroscope_is_available_)
323 sensor = new FakeGyroscope(std::move(mapping), this);
324 break;
325 default:
326 NOTIMPLEMENTED();
327 }
328
329 callback.Run(std::move(sensor));
330 }
331
332 bool accelerometer_is_available_ = true;
333 bool linear_acceleration_sensor_is_available_ = true;
334 bool gyroscope_is_available_ = true;
335 };
336
211 class DeviceSensorBrowserTest : public ContentBrowserTest { 337 class DeviceSensorBrowserTest : public ContentBrowserTest {
212 public: 338 public:
213 DeviceSensorBrowserTest() 339 DeviceSensorBrowserTest()
214 : fetcher_(nullptr), 340 : fetcher_(nullptr),
215 io_loop_finished_event_( 341 io_loop_finished_event_(
216 base::WaitableEvent::ResetPolicy::AUTOMATIC, 342 base::WaitableEvent::ResetPolicy::AUTOMATIC,
217 base::WaitableEvent::InitialState::NOT_SIGNALED) {} 343 base::WaitableEvent::InitialState::NOT_SIGNALED) {}
218 344
219 void SetUpOnMainThread() override { 345 void SetUpOnMainThread() override {
220 // Initialize the RunLoops now that the main thread has been created. 346 // 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()); 347 orientation_started_runloop_.reset(new base::RunLoop());
224 orientation_stopped_runloop_.reset(new base::RunLoop()); 348 orientation_stopped_runloop_.reset(new base::RunLoop());
225 orientation_absolute_started_runloop_.reset(new base::RunLoop()); 349 orientation_absolute_started_runloop_.reset(new base::RunLoop());
226 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); 350 orientation_absolute_stopped_runloop_.reset(new base::RunLoop());
227 #if defined(OS_ANDROID) 351 #if defined(OS_ANDROID)
228 // On Android, the DeviceSensorService lives on the UI thread. 352 // On Android, the DeviceSensorService lives on the UI thread.
229 SetUpFetcher(); 353 SetUpFetcher();
230 #else 354 #endif // defined(OS_ANDROID)
timvolodine 2017/06/07 20:37:35 same question here, no #else?
juncai 2017/06/07 23:05:07 As the comment above and inside SetUpOnIOThread()
231 // On all other platforms, the DeviceSensorService lives on the IO thread.
232 BrowserThread::PostTask( 355 BrowserThread::PostTask(
233 BrowserThread::IO, FROM_HERE, 356 BrowserThread::IO, FROM_HERE,
234 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, 357 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread,
235 base::Unretained(this))); 358 base::Unretained(this)));
236 io_loop_finished_event_.Wait(); 359 io_loop_finished_event_.Wait();
237 #endif
238 } 360 }
239 361
240 void SetUpFetcher() { 362 void SetUpFetcher() {
241 fetcher_ = new FakeDataFetcher(); 363 fetcher_ = new FakeDataFetcher();
242 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure());
243 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure());
244 fetcher_->SetOrientationStartedCallback( 364 fetcher_->SetOrientationStartedCallback(
245 orientation_started_runloop_->QuitClosure()); 365 orientation_started_runloop_->QuitClosure());
246 fetcher_->SetOrientationStoppedCallback( 366 fetcher_->SetOrientationStoppedCallback(
247 orientation_stopped_runloop_->QuitClosure()); 367 orientation_stopped_runloop_->QuitClosure());
248 fetcher_->SetOrientationAbsoluteStartedCallback( 368 fetcher_->SetOrientationAbsoluteStartedCallback(
249 orientation_absolute_started_runloop_->QuitClosure()); 369 orientation_absolute_started_runloop_->QuitClosure());
250 fetcher_->SetOrientationAbsoluteStoppedCallback( 370 fetcher_->SetOrientationAbsoluteStoppedCallback(
251 orientation_absolute_stopped_runloop_->QuitClosure()); 371 orientation_absolute_stopped_runloop_->QuitClosure());
252 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( 372 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting(
253 fetcher_); 373 fetcher_);
254 } 374 }
255 375
256 void SetUpOnIOThread() { 376 void SetUpOnIOThread() {
377 #if !defined(OS_ANDROID)
378 // On non-Android platforms, the DeviceSensorService lives on the IO thread.
257 SetUpFetcher(); 379 SetUpFetcher();
timvolodine 2017/06/07 20:37:35 why do we need to set-up the fetcher only on non-a
juncai 2017/06/07 23:05:07 On Android, SetUpFetcher() is called at the above
timvolodine 2017/06/08 20:26:51 oh I see, it's because you need to do extra stuff
juncai 2017/06/08 23:29:43 Yes.
380 #endif // !defined(OS_ANDROID)
381 sensor_provider_ = FakeSensorProvider::GetInstance();
382 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_);
258 io_loop_finished_event_.Signal(); 383 io_loop_finished_event_.Signal();
259 } 384 }
260 385
386 void TearDown() override {
387 device::PlatformSensorProvider::SetProviderForTesting(nullptr);
388 }
389
261 void DelayAndQuit(base::TimeDelta delay) { 390 void DelayAndQuit(base::TimeDelta delay) {
262 base::PlatformThread::Sleep(delay); 391 base::PlatformThread::Sleep(delay);
263 base::MessageLoop::current()->QuitWhenIdle(); 392 base::MessageLoop::current()->QuitWhenIdle();
264 } 393 }
265 394
266 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { 395 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) {
267 ShellJavaScriptDialogManager* dialog_manager = 396 ShellJavaScriptDialogManager* dialog_manager =
268 static_cast<ShellJavaScriptDialogManager*>( 397 static_cast<ShellJavaScriptDialogManager*>(
269 shell()->GetJavaScriptDialogManager(shell()->web_contents())); 398 shell()->GetJavaScriptDialogManager(shell()->web_contents()));
270 399
271 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); 400 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
272 dialog_manager->set_dialog_request_callback( 401 dialog_manager->set_dialog_request_callback(
273 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, 402 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit,
274 base::Unretained(this), delay)); 403 base::Unretained(this), delay));
275 runner->Run(); 404 runner->Run();
276 } 405 }
277 406
278 FakeDataFetcher* fetcher_; 407 FakeDataFetcher* fetcher_;
408 FakeSensorProvider* sensor_provider_;
279 409
280 // NOTE: These can only be initialized once the main thread has been created 410 // NOTE: These can only be initialized once the main thread has been created
281 // and so must be pointers instead of plain objects. 411 // 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_; 412 std::unique_ptr<base::RunLoop> orientation_started_runloop_;
285 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; 413 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_;
286 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; 414 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_;
287 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; 415 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_;
288 416
289 private: 417 private:
290 base::WaitableEvent io_loop_finished_event_; 418 base::WaitableEvent io_loop_finished_event_;
291 }; 419 };
292 420
293 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { 421 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) {
(...skipping 22 matching lines...) Expand all
316 } 444 }
317 445
318 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { 446 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) {
319 // The test page will register an event handler for motion events, 447 // The test page will register an event handler for motion events,
320 // expects to get an event with fake values, then removes the event 448 // expects to get an event with fake values, then removes the event
321 // handler and navigates to #pass. 449 // handler and navigates to #pass.
322 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); 450 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html");
323 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 451 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
324 452
325 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 453 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
326 motion_started_runloop_->Run();
327 motion_stopped_runloop_->Run();
328 } 454 }
329 455
330 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { 456 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) {
331 // The test page registers an event handler for orientation events and 457 // 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 458 // expects to get an event with null values, because no sensor data can be
333 // provided. 459 // provided.
334 fetcher_->SetSensorDataAvailable(false); 460 fetcher_->SetSensorDataAvailable(false);
335 GURL test_url = 461 GURL test_url =
336 GetTestUrl("device_sensors", "device_orientation_null_test.html"); 462 GetTestUrl("device_sensors", "device_orientation_null_test.html");
337 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 463 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
(...skipping 14 matching lines...) Expand all
352 478
353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 479 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
354 orientation_absolute_started_runloop_->Run(); 480 orientation_absolute_started_runloop_->Run();
355 orientation_absolute_stopped_runloop_->Run(); 481 orientation_absolute_stopped_runloop_->Run();
356 } 482 }
357 483
358 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { 484 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) {
359 // The test page registers an event handler for motion events and 485 // 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 486 // expects to get an event with null values, because no sensor data can be
361 // provided. 487 // provided.
362 fetcher_->SetSensorDataAvailable(false); 488 sensor_provider_->set_accelerometer_is_available(false);
489 sensor_provider_->set_linear_acceleration_sensor_is_available(false);
490 sensor_provider_->set_gyroscope_is_available(false);
363 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); 491 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html");
364 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); 492 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
365 493
366 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 494 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
367 motion_started_runloop_->Run(); 495 }
368 motion_stopped_runloop_->Run(); 496
497 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest,
498 MotionOnlySomeSensorsAreAvailableTest) {
499 // The test page registers an event handler for motion events and
500 // expects to get an event with only some fake values, because only
timvolodine 2017/06/07 20:37:35 nit: maybe be more specific "..expects to get an e
juncai 2017/06/07 23:05:07 Done.
501 // some sensor data can be provided.
502 sensor_provider_->set_accelerometer_is_available(false);
503 GURL test_url =
504 GetTestUrl("device_sensors",
505 "device_motion_only_some_sensors_are_available_test.html");
506 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
507
508 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
369 } 509 }
370 510
371 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { 511 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) {
372 // The test page registers an event handlers for motion/orientation events and 512 // 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 513 // 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 514 // 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 515 // window after the alert is dismissed and the callbacks are invoked which
376 // eventually navigate to #pass. 516 // eventually navigate to #pass.
377 fetcher_->SetSensorDataAvailable(false); 517 fetcher_->SetSensorDataAvailable(false);
518 sensor_provider_->set_accelerometer_is_available(false);
519 sensor_provider_->set_linear_acceleration_sensor_is_available(false);
520 sensor_provider_->set_gyroscope_is_available(false);
378 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); 521 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
379 522
380 GURL test_url = 523 GURL test_url =
381 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); 524 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html");
382 shell()->LoadURL(test_url); 525 shell()->LoadURL(test_url);
383 526
384 // TODO(timvolodine): investigate if it is possible to test this without 527 // TODO(timvolodine): investigate if it is possible to test this without
385 // delay, crbug.com/360044. 528 // delay, crbug.com/360044.
386 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); 529 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500));
387 530
388 motion_started_runloop_->Run();
389 motion_stopped_runloop_->Run();
390 orientation_started_runloop_->Run(); 531 orientation_started_runloop_->Run();
391 orientation_stopped_runloop_->Run(); 532 orientation_stopped_runloop_->Run();
392 same_tab_observer.Wait(); 533 same_tab_observer.Wait();
393 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); 534 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
394 } 535 }
395 536
396 } // namespace 537 } // namespace
397 538
398 } // namespace content 539 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/renderer/BUILD.gn » ('j') | content/renderer/device_sensors/device_motion_event_pump.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698