| OLD | NEW |
| 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" | |
| 22 #include "device/sensors/data_fetcher_shared_memory.h" | 20 #include "device/sensors/data_fetcher_shared_memory.h" |
| 23 #include "device/sensors/device_sensor_service.h" | 21 #include "device/sensors/device_sensor_service.h" |
| 24 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | 22 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" |
| 25 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | 23 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
| 26 | 24 |
| 27 namespace content { | 25 namespace content { |
| 28 | 26 |
| 29 namespace { | 27 namespace { |
| 30 | 28 |
| 31 class FakeDataFetcher : public device::DataFetcherSharedMemory { | 29 class FakeDataFetcher : public device::DataFetcherSharedMemory { |
| 32 public: | 30 public: |
| 33 FakeDataFetcher() : sensor_data_available_(true) {} | 31 FakeDataFetcher() : sensor_data_available_(true) {} |
| 34 ~FakeDataFetcher() override {} | 32 ~FakeDataFetcher() override {} |
| 35 | 33 |
| 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 |
| 36 void SetOrientationStartedCallback( | 42 void SetOrientationStartedCallback( |
| 37 base::Closure orientation_started_callback) { | 43 base::Closure orientation_started_callback) { |
| 38 orientation_started_callback_ = orientation_started_callback; | 44 orientation_started_callback_ = orientation_started_callback; |
| 39 } | 45 } |
| 40 | 46 |
| 41 void SetOrientationStoppedCallback( | 47 void SetOrientationStoppedCallback( |
| 42 base::Closure orientation_stopped_callback) { | 48 base::Closure orientation_stopped_callback) { |
| 43 orientation_stopped_callback_ = orientation_stopped_callback; | 49 orientation_stopped_callback_ = orientation_stopped_callback; |
| 44 } | 50 } |
| 45 | 51 |
| 46 void SetOrientationAbsoluteStartedCallback( | 52 void SetOrientationAbsoluteStartedCallback( |
| 47 base::Closure orientation_absolute_started_callback) { | 53 base::Closure orientation_absolute_started_callback) { |
| 48 orientation_absolute_started_callback_ = | 54 orientation_absolute_started_callback_ = |
| 49 orientation_absolute_started_callback; | 55 orientation_absolute_started_callback; |
| 50 } | 56 } |
| 51 | 57 |
| 52 void SetOrientationAbsoluteStoppedCallback( | 58 void SetOrientationAbsoluteStoppedCallback( |
| 53 base::Closure orientation_absolute_stopped_callback) { | 59 base::Closure orientation_absolute_stopped_callback) { |
| 54 orientation_absolute_stopped_callback_ = | 60 orientation_absolute_stopped_callback_ = |
| 55 orientation_absolute_stopped_callback; | 61 orientation_absolute_stopped_callback; |
| 56 } | 62 } |
| 57 | 63 |
| 58 bool Start(device::ConsumerType consumer_type, void* buffer) override { | 64 bool Start(device::ConsumerType consumer_type, void* buffer) override { |
| 59 EXPECT_TRUE(buffer); | 65 EXPECT_TRUE(buffer); |
| 60 | 66 |
| 61 switch (consumer_type) { | 67 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; |
| 62 case device::CONSUMER_TYPE_ORIENTATION: { | 77 case device::CONSUMER_TYPE_ORIENTATION: { |
| 63 device::DeviceOrientationHardwareBuffer* orientation_buffer = | 78 device::DeviceOrientationHardwareBuffer* orientation_buffer = |
| 64 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | 79 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); |
| 65 if (sensor_data_available_) | 80 if (sensor_data_available_) |
| 66 UpdateOrientation(orientation_buffer); | 81 UpdateOrientation(orientation_buffer); |
| 67 SetOrientationBufferReady(orientation_buffer); | 82 SetOrientationBufferReady(orientation_buffer); |
| 68 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 83 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 69 orientation_started_callback_); | 84 orientation_started_callback_); |
| 70 } break; | 85 } break; |
| 71 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { | 86 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { |
| 72 device::DeviceOrientationHardwareBuffer* orientation_buffer = | 87 device::DeviceOrientationHardwareBuffer* orientation_buffer = |
| 73 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | 88 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); |
| 74 if (sensor_data_available_) | 89 if (sensor_data_available_) |
| 75 UpdateOrientationAbsolute(orientation_buffer); | 90 UpdateOrientationAbsolute(orientation_buffer); |
| 76 SetOrientationBufferReady(orientation_buffer); | 91 SetOrientationBufferReady(orientation_buffer); |
| 77 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 92 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 78 orientation_absolute_started_callback_); | 93 orientation_absolute_started_callback_); |
| 79 } break; | 94 } break; |
| 80 default: | 95 default: |
| 81 return false; | 96 return false; |
| 82 } | 97 } |
| 83 return true; | 98 return true; |
| 84 } | 99 } |
| 85 | 100 |
| 86 bool Stop(device::ConsumerType consumer_type) override { | 101 bool Stop(device::ConsumerType consumer_type) override { |
| 87 switch (consumer_type) { | 102 switch (consumer_type) { |
| 103 case device::CONSUMER_TYPE_MOTION: |
| 104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 105 motion_stopped_callback_); |
| 106 break; |
| 88 case device::CONSUMER_TYPE_ORIENTATION: | 107 case device::CONSUMER_TYPE_ORIENTATION: |
| 89 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 90 orientation_stopped_callback_); | 109 orientation_stopped_callback_); |
| 91 break; | 110 break; |
| 92 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | 111 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: |
| 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 94 orientation_absolute_stopped_callback_); | 113 orientation_absolute_stopped_callback_); |
| 95 break; | 114 break; |
| 96 default: | 115 default: |
| 97 return false; | 116 return false; |
| 98 } | 117 } |
| 99 return true; | 118 return true; |
| 100 } | 119 } |
| 101 | 120 |
| 102 void Fetch(unsigned consumer_bitmask) override { | 121 void Fetch(unsigned consumer_bitmask) override { |
| 103 FAIL() << "fetch should not be called"; | 122 FAIL() << "fetch should not be called"; |
| 104 } | 123 } |
| 105 | 124 |
| 106 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } | 125 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } |
| 107 | 126 |
| 108 void SetSensorDataAvailable(bool available) { | 127 void SetSensorDataAvailable(bool available) { |
| 109 sensor_data_available_ = available; | 128 sensor_data_available_ = available; |
| 110 } | 129 } |
| 111 | 130 |
| 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 |
| 112 void SetOrientationBufferReady( | 137 void SetOrientationBufferReady( |
| 113 device::DeviceOrientationHardwareBuffer* buffer) { | 138 device::DeviceOrientationHardwareBuffer* buffer) { |
| 114 buffer->seqlock.WriteBegin(); | 139 buffer->seqlock.WriteBegin(); |
| 115 buffer->data.all_available_sensors_are_active = true; | 140 buffer->data.all_available_sensors_are_active = true; |
| 116 buffer->seqlock.WriteEnd(); | 141 buffer->seqlock.WriteEnd(); |
| 117 } | 142 } |
| 143 |
| 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 } |
| 118 | 171 |
| 119 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) { | 172 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) { |
| 120 buffer->seqlock.WriteBegin(); | 173 buffer->seqlock.WriteBegin(); |
| 121 buffer->data.alpha = 1; | 174 buffer->data.alpha = 1; |
| 122 buffer->data.has_alpha = true; | 175 buffer->data.has_alpha = true; |
| 123 buffer->data.beta = 2; | 176 buffer->data.beta = 2; |
| 124 buffer->data.has_beta = true; | 177 buffer->data.has_beta = true; |
| 125 buffer->data.gamma = 3; | 178 buffer->data.gamma = 3; |
| 126 buffer->data.has_gamma = true; | 179 buffer->data.has_gamma = true; |
| 127 buffer->data.all_available_sensors_are_active = true; | 180 buffer->data.all_available_sensors_are_active = true; |
| 128 buffer->seqlock.WriteEnd(); | 181 buffer->seqlock.WriteEnd(); |
| 129 } | 182 } |
| 130 | 183 |
| 131 void UpdateOrientationAbsolute( | 184 void UpdateOrientationAbsolute( |
| 132 device::DeviceOrientationHardwareBuffer* buffer) { | 185 device::DeviceOrientationHardwareBuffer* buffer) { |
| 133 buffer->seqlock.WriteBegin(); | 186 buffer->seqlock.WriteBegin(); |
| 134 buffer->data.alpha = 4; | 187 buffer->data.alpha = 4; |
| 135 buffer->data.has_alpha = true; | 188 buffer->data.has_alpha = true; |
| 136 buffer->data.beta = 5; | 189 buffer->data.beta = 5; |
| 137 buffer->data.has_beta = true; | 190 buffer->data.has_beta = true; |
| 138 buffer->data.gamma = 6; | 191 buffer->data.gamma = 6; |
| 139 buffer->data.has_gamma = true; | 192 buffer->data.has_gamma = true; |
| 140 buffer->data.absolute = true; | 193 buffer->data.absolute = true; |
| 141 buffer->data.all_available_sensors_are_active = true; | 194 buffer->data.all_available_sensors_are_active = true; |
| 142 buffer->seqlock.WriteEnd(); | 195 buffer->seqlock.WriteEnd(); |
| 143 } | 196 } |
| 144 | 197 |
| 145 // The below callbacks should be run on the UI thread. | 198 // The below callbacks should be run on the UI thread. |
| 199 base::Closure motion_started_callback_; |
| 146 base::Closure orientation_started_callback_; | 200 base::Closure orientation_started_callback_; |
| 147 base::Closure orientation_absolute_started_callback_; | 201 base::Closure orientation_absolute_started_callback_; |
| 202 base::Closure motion_stopped_callback_; |
| 148 base::Closure orientation_stopped_callback_; | 203 base::Closure orientation_stopped_callback_; |
| 149 base::Closure orientation_absolute_stopped_callback_; | 204 base::Closure orientation_absolute_stopped_callback_; |
| 150 bool sensor_data_available_; | 205 bool sensor_data_available_; |
| 151 | 206 |
| 152 private: | 207 private: |
| 153 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); | 208 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); |
| 154 }; | 209 }; |
| 155 | 210 |
| 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 | |
| 337 class DeviceSensorBrowserTest : public ContentBrowserTest { | 211 class DeviceSensorBrowserTest : public ContentBrowserTest { |
| 338 public: | 212 public: |
| 339 DeviceSensorBrowserTest() | 213 DeviceSensorBrowserTest() |
| 340 : fetcher_(nullptr), | 214 : fetcher_(nullptr), |
| 341 io_loop_finished_event_( | 215 io_loop_finished_event_( |
| 342 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 216 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 343 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 217 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 344 | 218 |
| 345 void SetUpOnMainThread() override { | 219 void SetUpOnMainThread() override { |
| 346 // Initialize the RunLoops now that the main thread has been created. | 220 // 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()); |
| 347 orientation_started_runloop_.reset(new base::RunLoop()); | 223 orientation_started_runloop_.reset(new base::RunLoop()); |
| 348 orientation_stopped_runloop_.reset(new base::RunLoop()); | 224 orientation_stopped_runloop_.reset(new base::RunLoop()); |
| 349 orientation_absolute_started_runloop_.reset(new base::RunLoop()); | 225 orientation_absolute_started_runloop_.reset(new base::RunLoop()); |
| 350 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); | 226 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); |
| 351 #if defined(OS_ANDROID) | 227 #if defined(OS_ANDROID) |
| 352 // On Android, the DeviceSensorService lives on the UI thread. | 228 // On Android, the DeviceSensorService lives on the UI thread. |
| 353 SetUpFetcher(); | 229 SetUpFetcher(); |
| 354 #endif // defined(OS_ANDROID) | 230 #else |
| 231 // On all other platforms, the DeviceSensorService lives on the IO thread. |
| 355 BrowserThread::PostTask( | 232 BrowserThread::PostTask( |
| 356 BrowserThread::IO, FROM_HERE, | 233 BrowserThread::IO, FROM_HERE, |
| 357 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, | 234 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, |
| 358 base::Unretained(this))); | 235 base::Unretained(this))); |
| 359 io_loop_finished_event_.Wait(); | 236 io_loop_finished_event_.Wait(); |
| 237 #endif |
| 360 } | 238 } |
| 361 | 239 |
| 362 void SetUpFetcher() { | 240 void SetUpFetcher() { |
| 363 fetcher_ = new FakeDataFetcher(); | 241 fetcher_ = new FakeDataFetcher(); |
| 242 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure()); |
| 243 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure()); |
| 364 fetcher_->SetOrientationStartedCallback( | 244 fetcher_->SetOrientationStartedCallback( |
| 365 orientation_started_runloop_->QuitClosure()); | 245 orientation_started_runloop_->QuitClosure()); |
| 366 fetcher_->SetOrientationStoppedCallback( | 246 fetcher_->SetOrientationStoppedCallback( |
| 367 orientation_stopped_runloop_->QuitClosure()); | 247 orientation_stopped_runloop_->QuitClosure()); |
| 368 fetcher_->SetOrientationAbsoluteStartedCallback( | 248 fetcher_->SetOrientationAbsoluteStartedCallback( |
| 369 orientation_absolute_started_runloop_->QuitClosure()); | 249 orientation_absolute_started_runloop_->QuitClosure()); |
| 370 fetcher_->SetOrientationAbsoluteStoppedCallback( | 250 fetcher_->SetOrientationAbsoluteStoppedCallback( |
| 371 orientation_absolute_stopped_runloop_->QuitClosure()); | 251 orientation_absolute_stopped_runloop_->QuitClosure()); |
| 372 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( | 252 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( |
| 373 fetcher_); | 253 fetcher_); |
| 374 } | 254 } |
| 375 | 255 |
| 376 void SetUpOnIOThread() { | 256 void SetUpOnIOThread() { |
| 377 #if !defined(OS_ANDROID) | |
| 378 // On non-Android platforms, the DeviceSensorService lives on the IO thread. | |
| 379 SetUpFetcher(); | 257 SetUpFetcher(); |
| 380 #endif // !defined(OS_ANDROID) | |
| 381 sensor_provider_ = FakeSensorProvider::GetInstance(); | |
| 382 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_); | |
| 383 io_loop_finished_event_.Signal(); | 258 io_loop_finished_event_.Signal(); |
| 384 } | 259 } |
| 385 | 260 |
| 386 void TearDown() override { | |
| 387 device::PlatformSensorProvider::SetProviderForTesting(nullptr); | |
| 388 } | |
| 389 | |
| 390 void DelayAndQuit(base::TimeDelta delay) { | 261 void DelayAndQuit(base::TimeDelta delay) { |
| 391 base::PlatformThread::Sleep(delay); | 262 base::PlatformThread::Sleep(delay); |
| 392 base::MessageLoop::current()->QuitWhenIdle(); | 263 base::MessageLoop::current()->QuitWhenIdle(); |
| 393 } | 264 } |
| 394 | 265 |
| 395 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { | 266 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { |
| 396 ShellJavaScriptDialogManager* dialog_manager = | 267 ShellJavaScriptDialogManager* dialog_manager = |
| 397 static_cast<ShellJavaScriptDialogManager*>( | 268 static_cast<ShellJavaScriptDialogManager*>( |
| 398 shell()->GetJavaScriptDialogManager(shell()->web_contents())); | 269 shell()->GetJavaScriptDialogManager(shell()->web_contents())); |
| 399 | 270 |
| 400 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); | 271 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); |
| 401 dialog_manager->set_dialog_request_callback( | 272 dialog_manager->set_dialog_request_callback( |
| 402 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, | 273 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, |
| 403 base::Unretained(this), delay)); | 274 base::Unretained(this), delay)); |
| 404 runner->Run(); | 275 runner->Run(); |
| 405 } | 276 } |
| 406 | 277 |
| 407 FakeDataFetcher* fetcher_; | 278 FakeDataFetcher* fetcher_; |
| 408 FakeSensorProvider* sensor_provider_; | |
| 409 | 279 |
| 410 // NOTE: These can only be initialized once the main thread has been created | 280 // NOTE: These can only be initialized once the main thread has been created |
| 411 // and so must be pointers instead of plain objects. | 281 // 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_; |
| 412 std::unique_ptr<base::RunLoop> orientation_started_runloop_; | 284 std::unique_ptr<base::RunLoop> orientation_started_runloop_; |
| 413 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; | 285 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; |
| 414 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; | 286 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; |
| 415 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; | 287 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; |
| 416 | 288 |
| 417 private: | 289 private: |
| 418 base::WaitableEvent io_loop_finished_event_; | 290 base::WaitableEvent io_loop_finished_event_; |
| 419 }; | 291 }; |
| 420 | 292 |
| 421 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { | 293 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 444 } | 316 } |
| 445 | 317 |
| 446 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { | 318 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { |
| 447 // The test page will register an event handler for motion events, | 319 // The test page will register an event handler for motion events, |
| 448 // expects to get an event with fake values, then removes the event | 320 // expects to get an event with fake values, then removes the event |
| 449 // handler and navigates to #pass. | 321 // handler and navigates to #pass. |
| 450 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); | 322 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); |
| 451 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 323 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 452 | 324 |
| 453 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 325 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 326 motion_started_runloop_->Run(); |
| 327 motion_stopped_runloop_->Run(); |
| 454 } | 328 } |
| 455 | 329 |
| 456 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { | 330 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { |
| 457 // The test page registers an event handler for orientation events and | 331 // The test page registers an event handler for orientation events and |
| 458 // expects to get an event with null values, because no sensor data can be | 332 // expects to get an event with null values, because no sensor data can be |
| 459 // provided. | 333 // provided. |
| 460 fetcher_->SetSensorDataAvailable(false); | 334 fetcher_->SetSensorDataAvailable(false); |
| 461 GURL test_url = | 335 GURL test_url = |
| 462 GetTestUrl("device_sensors", "device_orientation_null_test.html"); | 336 GetTestUrl("device_sensors", "device_orientation_null_test.html"); |
| 463 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 337 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 478 | 352 |
| 479 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 480 orientation_absolute_started_runloop_->Run(); | 354 orientation_absolute_started_runloop_->Run(); |
| 481 orientation_absolute_stopped_runloop_->Run(); | 355 orientation_absolute_stopped_runloop_->Run(); |
| 482 } | 356 } |
| 483 | 357 |
| 484 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { | 358 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { |
| 485 // The test page registers an event handler for motion events and | 359 // The test page registers an event handler for motion events and |
| 486 // expects to get an event with null values, because no sensor data can be | 360 // expects to get an event with null values, because no sensor data can be |
| 487 // provided. | 361 // provided. |
| 488 sensor_provider_->set_accelerometer_is_available(false); | 362 fetcher_->SetSensorDataAvailable(false); |
| 489 sensor_provider_->set_linear_acceleration_sensor_is_available(false); | |
| 490 sensor_provider_->set_gyroscope_is_available(false); | |
| 491 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); | 363 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); |
| 492 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 364 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 493 | 365 |
| 494 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 366 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 495 } | 367 motion_started_runloop_->Run(); |
| 496 | 368 motion_stopped_runloop_->Run(); |
| 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 the gyroscope and linear acceleration | |
| 501 // sensor values, because no accelerometer values 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()); | |
| 509 } | 369 } |
| 510 | 370 |
| 511 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { | 371 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { |
| 512 // The test page registers an event handlers for motion/orientation events and | 372 // The test page registers an event handlers for motion/orientation events and |
| 513 // expects to get events with null values. The test raises a modal alert | 373 // expects to get events with null values. The test raises a modal alert |
| 514 // dialog with a delay to test that the one-off null-events still propagate to | 374 // dialog with a delay to test that the one-off null-events still propagate to |
| 515 // window after the alert is dismissed and the callbacks are invoked which | 375 // window after the alert is dismissed and the callbacks are invoked which |
| 516 // eventually navigate to #pass. | 376 // eventually navigate to #pass. |
| 517 fetcher_->SetSensorDataAvailable(false); | 377 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); | |
| 521 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | 378 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| 522 | 379 |
| 523 GURL test_url = | 380 GURL test_url = |
| 524 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); | 381 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); |
| 525 shell()->LoadURL(test_url); | 382 shell()->LoadURL(test_url); |
| 526 | 383 |
| 527 // TODO(timvolodine): investigate if it is possible to test this without | 384 // TODO(timvolodine): investigate if it is possible to test this without |
| 528 // delay, crbug.com/360044. | 385 // delay, crbug.com/360044. |
| 529 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); | 386 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); |
| 530 | 387 |
| 388 motion_started_runloop_->Run(); |
| 389 motion_stopped_runloop_->Run(); |
| 531 orientation_started_runloop_->Run(); | 390 orientation_started_runloop_->Run(); |
| 532 orientation_stopped_runloop_->Run(); | 391 orientation_stopped_runloop_->Run(); |
| 533 same_tab_observer.Wait(); | 392 same_tab_observer.Wait(); |
| 534 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 393 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 535 } | 394 } |
| 536 | 395 |
| 537 } // namespace | 396 } // namespace |
| 538 | 397 |
| 539 } // namespace content | 398 } // namespace content |
| OLD | NEW |