| 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 |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "content/common/device_sensors/device_motion_hardware_buffer.h" | 16 #include "content/common/device_sensors/device_motion_hardware_buffer.h" |
| 17 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
| 18 #include "mojo/public/cpp/system/buffer.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionListener.h" | 20 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eMotionListener.h" |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 24 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
| 24 public: | 25 public: |
| 25 MockDeviceMotionListener() | 26 MockDeviceMotionListener() |
| 26 : did_change_device_motion_(false), number_of_events_(0) { | 27 : did_change_device_motion_(false), number_of_events_(0) { |
| 27 memset(&data_, 0, sizeof(data_)); | 28 memset(&data_, 0, sizeof(data_)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 ~DeviceMotionEventPumpForTesting() override {} | 60 ~DeviceMotionEventPumpForTesting() override {} |
| 60 | 61 |
| 61 void set_stop_on_fire_event(bool stop_on_fire_event) { | 62 void set_stop_on_fire_event(bool stop_on_fire_event) { |
| 62 stop_on_fire_event_ = stop_on_fire_event; | 63 stop_on_fire_event_ = stop_on_fire_event; |
| 63 } | 64 } |
| 64 | 65 |
| 65 bool stop_on_fire_event() { return stop_on_fire_event_; } | 66 bool stop_on_fire_event() { return stop_on_fire_event_; } |
| 66 | 67 |
| 67 int pump_delay_microseconds() const { return pump_delay_microseconds_; } | 68 int pump_delay_microseconds() const { return pump_delay_microseconds_; } |
| 68 | 69 |
| 69 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 70 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) { |
| 70 DeviceMotionEventPump::OnDidStart(renderer_handle); | 71 DeviceMotionEventPump::DidStart(std::move(renderer_handle)); |
| 71 } | 72 } |
| 72 void SendStartMessage() override {} | 73 void SendStartMessage() override {} |
| 73 void SendStopMessage() override {} | 74 void SendStopMessage() override {} |
| 74 void FireEvent() override { | 75 void FireEvent() override { |
| 75 DeviceMotionEventPump::FireEvent(); | 76 DeviceMotionEventPump::FireEvent(); |
| 76 if (stop_on_fire_event_) { | 77 if (stop_on_fire_event_) { |
| 77 Stop(); | 78 Stop(); |
| 78 base::MessageLoop::current()->QuitWhenIdle(); | 79 base::MessageLoop::current()->QuitWhenIdle(); |
| 79 } | 80 } |
| 80 } | 81 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 93 } | 94 } |
| 94 | 95 |
| 95 protected: | 96 protected: |
| 96 void SetUp() override { | 97 void SetUp() override { |
| 97 const DeviceMotionHardwareBuffer* null_buffer = nullptr; | 98 const DeviceMotionHardwareBuffer* null_buffer = nullptr; |
| 98 listener_.reset(new MockDeviceMotionListener); | 99 listener_.reset(new MockDeviceMotionListener); |
| 99 motion_pump_.reset(new DeviceMotionEventPumpForTesting); | 100 motion_pump_.reset(new DeviceMotionEventPumpForTesting); |
| 100 buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory()); | 101 buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory()); |
| 101 ASSERT_NE(null_buffer, buffer_); | 102 ASSERT_NE(null_buffer, buffer_); |
| 102 memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer)); | 103 memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer)); |
| 103 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), | |
| 104 &handle_)); | |
| 105 } | 104 } |
| 106 | 105 |
| 107 void InitBuffer(bool allAvailableSensorsActive) { | 106 void InitBuffer(bool allAvailableSensorsActive) { |
| 108 blink::WebDeviceMotionData& data = buffer_->data; | 107 blink::WebDeviceMotionData& data = buffer_->data; |
| 109 data.accelerationX = 1; | 108 data.accelerationX = 1; |
| 110 data.hasAccelerationX = true; | 109 data.hasAccelerationX = true; |
| 111 data.accelerationY = 2; | 110 data.accelerationY = 2; |
| 112 data.hasAccelerationY = true; | 111 data.hasAccelerationY = true; |
| 113 data.accelerationZ = 3; | 112 data.accelerationZ = 3; |
| 114 data.hasAccelerationZ = true; | 113 data.hasAccelerationZ = true; |
| 115 data.allAvailableSensorsAreActive = allAvailableSensorsActive; | 114 data.allAvailableSensorsAreActive = allAvailableSensorsActive; |
| 116 } | 115 } |
| 117 | 116 |
| 118 MockDeviceMotionListener* listener() { return listener_.get(); } | 117 MockDeviceMotionListener* listener() { return listener_.get(); } |
| 119 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } | 118 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } |
| 120 base::SharedMemoryHandle handle() { return handle_; } | 119 mojo::ScopedSharedBufferHandle handle() { |
| 120 return mojo::WrapSharedMemoryHandle( |
| 121 base::SharedMemory::DuplicateHandle(shared_memory_.handle()), |
| 122 sizeof(DeviceMotionHardwareBuffer), false); |
| 123 } |
| 121 | 124 |
| 122 private: | 125 private: |
| 123 std::unique_ptr<MockDeviceMotionListener> listener_; | 126 std::unique_ptr<MockDeviceMotionListener> listener_; |
| 124 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; | 127 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; |
| 125 base::SharedMemoryHandle handle_; | |
| 126 base::SharedMemory shared_memory_; | 128 base::SharedMemory shared_memory_; |
| 127 DeviceMotionHardwareBuffer* buffer_; | 129 DeviceMotionHardwareBuffer* buffer_; |
| 128 | 130 |
| 129 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); | 131 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); |
| 130 }; | 132 }; |
| 131 | 133 |
| 132 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 134 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { |
| 133 base::MessageLoopForUI loop; | 135 base::MessageLoopForUI loop; |
| 134 | 136 |
| 135 InitBuffer(true); | 137 InitBuffer(true); |
| 136 | 138 |
| 137 motion_pump()->Start(listener()); | 139 motion_pump()->Start(listener()); |
| 138 motion_pump()->OnDidStart(handle()); | 140 motion_pump()->DidStart(handle()); |
| 139 | 141 |
| 140 base::MessageLoop::current()->Run(); | 142 base::MessageLoop::current()->Run(); |
| 141 | 143 |
| 142 const blink::WebDeviceMotionData& received_data = listener()->data(); | 144 const blink::WebDeviceMotionData& received_data = listener()->data(); |
| 143 EXPECT_TRUE(listener()->did_change_device_motion()); | 145 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 144 EXPECT_TRUE(received_data.hasAccelerationX); | 146 EXPECT_TRUE(received_data.hasAccelerationX); |
| 145 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); | 147 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); |
| 146 EXPECT_TRUE(received_data.hasAccelerationX); | 148 EXPECT_TRUE(received_data.hasAccelerationX); |
| 147 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); | 149 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); |
| 148 EXPECT_TRUE(received_data.hasAccelerationY); | 150 EXPECT_TRUE(received_data.hasAccelerationY); |
| 149 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); | 151 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); |
| 150 EXPECT_TRUE(received_data.hasAccelerationZ); | 152 EXPECT_TRUE(received_data.hasAccelerationZ); |
| 151 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 154 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 156 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 155 EXPECT_FALSE(received_data.hasRotationRateBeta); | 157 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 156 EXPECT_FALSE(received_data.hasRotationRateGamma); | 158 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 157 } | 159 } |
| 158 | 160 |
| 159 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { | 161 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { |
| 160 base::MessageLoopForUI loop; | 162 base::MessageLoopForUI loop; |
| 161 | 163 |
| 162 InitBuffer(false); | 164 InitBuffer(false); |
| 163 | 165 |
| 164 motion_pump()->Start(listener()); | 166 motion_pump()->Start(listener()); |
| 165 motion_pump()->OnDidStart(handle()); | 167 motion_pump()->DidStart(handle()); |
| 166 | 168 |
| 167 base::MessageLoop::current()->Run(); | 169 base::MessageLoop::current()->Run(); |
| 168 | 170 |
| 169 const blink::WebDeviceMotionData& received_data = listener()->data(); | 171 const blink::WebDeviceMotionData& received_data = listener()->data(); |
| 170 // No change in device motion because allAvailableSensorsAreActive is false. | 172 // No change in device motion because allAvailableSensorsAreActive is false. |
| 171 EXPECT_FALSE(listener()->did_change_device_motion()); | 173 EXPECT_FALSE(listener()->did_change_device_motion()); |
| 172 EXPECT_FALSE(received_data.hasAccelerationX); | 174 EXPECT_FALSE(received_data.hasAccelerationX); |
| 173 EXPECT_FALSE(received_data.hasAccelerationX); | 175 EXPECT_FALSE(received_data.hasAccelerationX); |
| 174 EXPECT_FALSE(received_data.hasAccelerationY); | 176 EXPECT_FALSE(received_data.hasAccelerationY); |
| 175 EXPECT_FALSE(received_data.hasAccelerationZ); | 177 EXPECT_FALSE(received_data.hasAccelerationZ); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 187 // Confirm that the delay for pumping events is 60 Hz. | 189 // Confirm that the delay for pumping events is 60 Hz. |
| 188 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / | 190 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / |
| 189 motion_pump()->pump_delay_microseconds()); | 191 motion_pump()->pump_delay_microseconds()); |
| 190 | 192 |
| 191 base::MessageLoopForUI loop; | 193 base::MessageLoopForUI loop; |
| 192 | 194 |
| 193 InitBuffer(true); | 195 InitBuffer(true); |
| 194 | 196 |
| 195 motion_pump()->set_stop_on_fire_event(false); | 197 motion_pump()->set_stop_on_fire_event(false); |
| 196 motion_pump()->Start(listener()); | 198 motion_pump()->Start(listener()); |
| 197 motion_pump()->OnDidStart(handle()); | 199 motion_pump()->DidStart(handle()); |
| 198 | 200 |
| 199 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 201 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 200 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 202 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
| 201 base::TimeDelta::FromMilliseconds(100)); | 203 base::TimeDelta::FromMilliseconds(100)); |
| 202 base::MessageLoop::current()->Run(); | 204 base::MessageLoop::current()->Run(); |
| 203 motion_pump()->Stop(); | 205 motion_pump()->Stop(); |
| 204 | 206 |
| 205 // Check that the blink::WebDeviceMotionListener does not receive excess | 207 // Check that the blink::WebDeviceMotionListener does not receive excess |
| 206 // events. | 208 // events. |
| 207 EXPECT_TRUE(listener()->did_change_device_motion()); | 209 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 208 EXPECT_GE(6, listener()->number_of_events()); | 210 EXPECT_GE(6, listener()->number_of_events()); |
| 209 } | 211 } |
| 210 | 212 |
| 211 } // namespace content | 213 } // namespace content |
| OLD | NEW |