| 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 "content/renderer/device_sensors/device_motion_event_pump.h" | 5 #include "content/renderer/device_sensors/device_motion_event_pump.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 motion_pump_.reset(new DeviceMotionEventPumpForTesting); | 95 motion_pump_.reset(new DeviceMotionEventPumpForTesting); |
| 96 shared_memory_ = mojo::SharedBufferHandle::Create( | 96 shared_memory_ = mojo::SharedBufferHandle::Create( |
| 97 sizeof(device::DeviceMotionHardwareBuffer)); | 97 sizeof(device::DeviceMotionHardwareBuffer)); |
| 98 mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer)); | 98 mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer)); |
| 99 ASSERT_TRUE(mapping_); | 99 ASSERT_TRUE(mapping_); |
| 100 memset(buffer(), 0, sizeof(device::DeviceMotionHardwareBuffer)); | 100 memset(buffer(), 0, sizeof(device::DeviceMotionHardwareBuffer)); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void InitBuffer(bool allAvailableSensorsActive) { | 103 void InitBuffer(bool allAvailableSensorsActive) { |
| 104 device::MotionData& data = buffer()->data; | 104 device::MotionData& data = buffer()->data; |
| 105 data.accelerationX = 1; | 105 data.acceleration_x = 1; |
| 106 data.hasAccelerationX = true; | 106 data.has_acceleration_x = true; |
| 107 data.accelerationY = 2; | 107 data.acceleration_y = 2; |
| 108 data.hasAccelerationY = true; | 108 data.has_acceleration_y = true; |
| 109 data.accelerationZ = 3; | 109 data.acceleration_z = 3; |
| 110 data.hasAccelerationZ = true; | 110 data.has_acceleration_z = true; |
| 111 data.allAvailableSensorsAreActive = allAvailableSensorsActive; | 111 data.all_available_sensors_are_active = allAvailableSensorsActive; |
| 112 } | 112 } |
| 113 | 113 |
| 114 MockDeviceMotionListener* listener() { return listener_.get(); } | 114 MockDeviceMotionListener* listener() { return listener_.get(); } |
| 115 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } | 115 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } |
| 116 mojo::ScopedSharedBufferHandle handle() { | 116 mojo::ScopedSharedBufferHandle handle() { |
| 117 return shared_memory_->Clone( | 117 return shared_memory_->Clone( |
| 118 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | 118 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| 119 } | 119 } |
| 120 device::DeviceMotionHardwareBuffer* buffer() { | 120 device::DeviceMotionHardwareBuffer* buffer() { |
| 121 return reinterpret_cast<device::DeviceMotionHardwareBuffer*>( | 121 return reinterpret_cast<device::DeviceMotionHardwareBuffer*>( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 135 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 135 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { |
| 136 InitBuffer(true); | 136 InitBuffer(true); |
| 137 | 137 |
| 138 motion_pump()->Start(listener()); | 138 motion_pump()->Start(listener()); |
| 139 motion_pump()->DidStart(handle()); | 139 motion_pump()->DidStart(handle()); |
| 140 | 140 |
| 141 base::RunLoop().Run(); | 141 base::RunLoop().Run(); |
| 142 | 142 |
| 143 const device::MotionData& received_data = listener()->data(); | 143 const device::MotionData& received_data = listener()->data(); |
| 144 EXPECT_TRUE(listener()->did_change_device_motion()); | 144 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 145 EXPECT_TRUE(received_data.hasAccelerationX); | 145 EXPECT_TRUE(received_data.has_acceleration_x); |
| 146 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); | 146 EXPECT_EQ(1, static_cast<double>(received_data.acceleration_x)); |
| 147 EXPECT_TRUE(received_data.hasAccelerationX); | 147 EXPECT_TRUE(received_data.has_acceleration_x); |
| 148 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); | 148 EXPECT_EQ(2, static_cast<double>(received_data.acceleration_y)); |
| 149 EXPECT_TRUE(received_data.hasAccelerationY); | 149 EXPECT_TRUE(received_data.has_acceleration_y); |
| 150 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); | 150 EXPECT_EQ(3, static_cast<double>(received_data.acceleration_z)); |
| 151 EXPECT_TRUE(received_data.hasAccelerationZ); | 151 EXPECT_TRUE(received_data.has_acceleration_z); |
| 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 152 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x); |
| 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 153 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y); |
| 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 154 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z); |
| 155 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 155 EXPECT_FALSE(received_data.has_rotation_rate_alpha); |
| 156 EXPECT_FALSE(received_data.hasRotationRateBeta); | 156 EXPECT_FALSE(received_data.has_rotation_rate_beta); |
| 157 EXPECT_FALSE(received_data.hasRotationRateGamma); | 157 EXPECT_FALSE(received_data.has_rotation_rate_gamma); |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { | 160 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { |
| 161 InitBuffer(false); | 161 InitBuffer(false); |
| 162 | 162 |
| 163 motion_pump()->Start(listener()); | 163 motion_pump()->Start(listener()); |
| 164 motion_pump()->DidStart(handle()); | 164 motion_pump()->DidStart(handle()); |
| 165 | 165 |
| 166 base::RunLoop().Run(); | 166 base::RunLoop().Run(); |
| 167 | 167 |
| 168 const device::MotionData& received_data = listener()->data(); | 168 const device::MotionData& received_data = listener()->data(); |
| 169 // No change in device motion because allAvailableSensorsAreActive is false. | 169 // No change in device motion because all_available_sensors_are_active is |
| 170 // false. |
| 170 EXPECT_FALSE(listener()->did_change_device_motion()); | 171 EXPECT_FALSE(listener()->did_change_device_motion()); |
| 171 EXPECT_FALSE(received_data.hasAccelerationX); | 172 EXPECT_FALSE(received_data.has_acceleration_x); |
| 172 EXPECT_FALSE(received_data.hasAccelerationX); | 173 EXPECT_FALSE(received_data.has_acceleration_x); |
| 173 EXPECT_FALSE(received_data.hasAccelerationY); | 174 EXPECT_FALSE(received_data.has_acceleration_y); |
| 174 EXPECT_FALSE(received_data.hasAccelerationZ); | 175 EXPECT_FALSE(received_data.has_acceleration_z); |
| 175 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 176 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x); |
| 176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 177 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y); |
| 177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 178 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z); |
| 178 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 179 EXPECT_FALSE(received_data.has_rotation_rate_alpha); |
| 179 EXPECT_FALSE(received_data.hasRotationRateBeta); | 180 EXPECT_FALSE(received_data.has_rotation_rate_beta); |
| 180 EXPECT_FALSE(received_data.hasRotationRateGamma); | 181 EXPECT_FALSE(received_data.has_rotation_rate_gamma); |
| 181 } | 182 } |
| 182 | 183 |
| 183 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate | 184 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate |
| 184 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) | 185 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) |
| 185 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { | 186 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { |
| 186 // Confirm that the delay for pumping events is 60 Hz. | 187 // Confirm that the delay for pumping events is 60 Hz. |
| 187 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / | 188 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / |
| 188 motion_pump()->pump_delay_microseconds()); | 189 motion_pump()->pump_delay_microseconds()); |
| 189 | 190 |
| 190 InitBuffer(true); | 191 InitBuffer(true); |
| 191 | 192 |
| 192 motion_pump()->set_stop_on_fire_event(false); | 193 motion_pump()->set_stop_on_fire_event(false); |
| 193 motion_pump()->Start(listener()); | 194 motion_pump()->Start(listener()); |
| 194 motion_pump()->DidStart(handle()); | 195 motion_pump()->DidStart(handle()); |
| 195 | 196 |
| 196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 197 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 197 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 198 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| 198 base::TimeDelta::FromMilliseconds(100)); | 199 base::TimeDelta::FromMilliseconds(100)); |
| 199 base::RunLoop().Run(); | 200 base::RunLoop().Run(); |
| 200 motion_pump()->Stop(); | 201 motion_pump()->Stop(); |
| 201 | 202 |
| 202 // Check that the blink::WebDeviceMotionListener does not receive excess | 203 // Check that the blink::WebDeviceMotionListener does not receive excess |
| 203 // events. | 204 // events. |
| 204 EXPECT_TRUE(listener()->did_change_device_motion()); | 205 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 205 EXPECT_GE(6, listener()->number_of_events()); | 206 EXPECT_GE(6, listener()->number_of_events()); |
| 206 } | 207 } |
| 207 | 208 |
| 208 } // namespace content | 209 } // namespace content |
| OLD | NEW |