| 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 "device_motion_event_pump.h" | 5 #include "device_motion_event_pump.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/common/device_sensors/device_motion_hardware_buffer.h" | 10 #include "content/common/device_sensors/device_motion_hardware_buffer.h" |
| 11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" | 13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
| 18 public: | 18 public: |
| 19 MockDeviceMotionListener(); | 19 MockDeviceMotionListener() : did_change_device_motion_(false) { |
| 20 memset(&data_, 0, sizeof(data_)); |
| 21 } |
| 20 virtual ~MockDeviceMotionListener() { } | 22 virtual ~MockDeviceMotionListener() { } |
| 23 |
| 21 virtual void didChangeDeviceMotion( | 24 virtual void didChangeDeviceMotion( |
| 22 const blink::WebDeviceMotionData&) OVERRIDE; | 25 const blink::WebDeviceMotionData& data) OVERRIDE { |
| 26 memcpy(&data_, &data, sizeof(data)); |
| 27 did_change_device_motion_ = true; |
| 28 } |
| 29 |
| 30 bool did_change_device_motion() const { |
| 31 return did_change_device_motion_; |
| 32 } |
| 33 const blink::WebDeviceMotionData& data() const { |
| 34 return data_; |
| 35 } |
| 36 |
| 37 private: |
| 23 bool did_change_device_motion_; | 38 bool did_change_device_motion_; |
| 24 blink::WebDeviceMotionData data_; | 39 blink::WebDeviceMotionData data_; |
| 40 |
| 41 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); |
| 25 }; | 42 }; |
| 26 | 43 |
| 27 MockDeviceMotionListener::MockDeviceMotionListener() | |
| 28 : did_change_device_motion_(false) { | |
| 29 memset(&data_, 0, sizeof(data_)); | |
| 30 } | |
| 31 | |
| 32 void MockDeviceMotionListener::didChangeDeviceMotion( | |
| 33 const blink::WebDeviceMotionData& data) { | |
| 34 memcpy(&data_, &data, sizeof(data)); | |
| 35 did_change_device_motion_ = true; | |
| 36 } | |
| 37 | |
| 38 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { | 44 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
| 39 public: | 45 public: |
| 40 DeviceMotionEventPumpForTesting() { } | 46 DeviceMotionEventPumpForTesting() { } |
| 41 virtual ~DeviceMotionEventPumpForTesting() { } | 47 virtual ~DeviceMotionEventPumpForTesting() { } |
| 42 | 48 |
| 43 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 49 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 44 DeviceMotionEventPump::OnDidStart(renderer_handle); | 50 DeviceMotionEventPump::OnDidStart(renderer_handle); |
| 45 } | 51 } |
| 46 virtual bool SendStartMessage() OVERRIDE { return true; } | 52 virtual bool SendStartMessage() OVERRIDE { return true; } |
| 47 virtual bool SendStopMessage() OVERRIDE { return true; } | 53 virtual bool SendStopMessage() OVERRIDE { return true; } |
| 48 virtual void FireEvent() OVERRIDE { | 54 virtual void FireEvent() OVERRIDE { |
| 49 DeviceMotionEventPump::FireEvent(); | 55 DeviceMotionEventPump::FireEvent(); |
| 50 Stop(); | 56 Stop(); |
| 51 base::MessageLoop::current()->QuitWhenIdle(); | 57 base::MessageLoop::current()->QuitWhenIdle(); |
| 52 } | 58 } |
| 59 |
| 60 private: |
| 61 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); |
| 53 }; | 62 }; |
| 54 | 63 |
| 55 class DeviceMotionEventPumpTest : public testing::Test { | 64 class DeviceMotionEventPumpTest : public testing::Test { |
| 56 public: | 65 public: |
| 57 DeviceMotionEventPumpTest() { | 66 DeviceMotionEventPumpTest() { |
| 58 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | 67 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 59 sizeof(DeviceMotionHardwareBuffer))); | 68 sizeof(DeviceMotionHardwareBuffer))); |
| 60 } | 69 } |
| 61 | 70 |
| 62 protected: | 71 protected: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 75 blink::WebDeviceMotionData& data = buffer_->data; | 84 blink::WebDeviceMotionData& data = buffer_->data; |
| 76 data.accelerationX = 1; | 85 data.accelerationX = 1; |
| 77 data.hasAccelerationX = true; | 86 data.hasAccelerationX = true; |
| 78 data.accelerationY = 2; | 87 data.accelerationY = 2; |
| 79 data.hasAccelerationY = true; | 88 data.hasAccelerationY = true; |
| 80 data.accelerationZ = 3; | 89 data.accelerationZ = 3; |
| 81 data.hasAccelerationZ = true; | 90 data.hasAccelerationZ = true; |
| 82 data.allAvailableSensorsAreActive = allAvailableSensorsActive; | 91 data.allAvailableSensorsAreActive = allAvailableSensorsActive; |
| 83 } | 92 } |
| 84 | 93 |
| 94 MockDeviceMotionListener* listener() { return listener_.get(); } |
| 95 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } |
| 96 base::SharedMemoryHandle handle() { return handle_; } |
| 97 |
| 98 private: |
| 85 scoped_ptr<MockDeviceMotionListener> listener_; | 99 scoped_ptr<MockDeviceMotionListener> listener_; |
| 86 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump_; | 100 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump_; |
| 87 base::SharedMemoryHandle handle_; | 101 base::SharedMemoryHandle handle_; |
| 88 base::SharedMemory shared_memory_; | 102 base::SharedMemory shared_memory_; |
| 89 DeviceMotionHardwareBuffer* buffer_; | 103 DeviceMotionHardwareBuffer* buffer_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); |
| 90 }; | 106 }; |
| 91 | 107 |
| 92 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 108 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { |
| 93 base::MessageLoopForUI loop; | 109 base::MessageLoopForUI loop; |
| 94 | 110 |
| 95 InitBuffer(true); | 111 InitBuffer(true); |
| 96 | 112 |
| 97 motion_pump_->SetListener(listener_.get()); | 113 motion_pump()->SetListener(listener()); |
| 98 motion_pump_->OnDidStart(handle_); | 114 motion_pump()->OnDidStart(handle()); |
| 99 | 115 |
| 100 base::MessageLoop::current()->Run(); | 116 base::MessageLoop::current()->Run(); |
| 101 | 117 |
| 102 blink::WebDeviceMotionData& received_data = listener_->data_; | 118 const blink::WebDeviceMotionData& received_data = listener()->data(); |
| 103 EXPECT_TRUE(listener_->did_change_device_motion_); | 119 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 104 EXPECT_TRUE(received_data.hasAccelerationX); | 120 EXPECT_TRUE(received_data.hasAccelerationX); |
| 105 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); | 121 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); |
| 106 EXPECT_TRUE(received_data.hasAccelerationX); | 122 EXPECT_TRUE(received_data.hasAccelerationX); |
| 107 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); | 123 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); |
| 108 EXPECT_TRUE(received_data.hasAccelerationY); | 124 EXPECT_TRUE(received_data.hasAccelerationY); |
| 109 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); | 125 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); |
| 110 EXPECT_TRUE(received_data.hasAccelerationZ); | 126 EXPECT_TRUE(received_data.hasAccelerationZ); |
| 111 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 127 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 112 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 128 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 113 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 129 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 114 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 130 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 115 EXPECT_FALSE(received_data.hasRotationRateBeta); | 131 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 116 EXPECT_FALSE(received_data.hasRotationRateGamma); | 132 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 117 } | 133 } |
| 118 | 134 |
| 119 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { | 135 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { |
| 120 base::MessageLoopForUI loop; | 136 base::MessageLoopForUI loop; |
| 121 | 137 |
| 122 InitBuffer(false); | 138 InitBuffer(false); |
| 123 | 139 |
| 124 motion_pump_->SetListener(listener_.get()); | 140 motion_pump()->SetListener(listener()); |
| 125 motion_pump_->OnDidStart(handle_); | 141 motion_pump()->OnDidStart(handle()); |
| 126 | 142 |
| 127 base::MessageLoop::current()->Run(); | 143 base::MessageLoop::current()->Run(); |
| 128 | 144 |
| 129 blink::WebDeviceMotionData& received_data = listener_->data_; | 145 const blink::WebDeviceMotionData& received_data = listener()->data(); |
| 130 // No change in device motion because allAvailableSensorsAreActive is false. | 146 // No change in device motion because allAvailableSensorsAreActive is false. |
| 131 EXPECT_FALSE(listener_->did_change_device_motion_); | 147 EXPECT_FALSE(listener()->did_change_device_motion()); |
| 132 EXPECT_FALSE(received_data.hasAccelerationX); | 148 EXPECT_FALSE(received_data.hasAccelerationX); |
| 133 EXPECT_FALSE(received_data.hasAccelerationX); | 149 EXPECT_FALSE(received_data.hasAccelerationX); |
| 134 EXPECT_FALSE(received_data.hasAccelerationY); | 150 EXPECT_FALSE(received_data.hasAccelerationY); |
| 135 EXPECT_FALSE(received_data.hasAccelerationZ); | 151 EXPECT_FALSE(received_data.hasAccelerationZ); |
| 136 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 137 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 138 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 139 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 155 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 140 EXPECT_FALSE(received_data.hasRotationRateBeta); | 156 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 141 EXPECT_FALSE(received_data.hasRotationRateGamma); | 157 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 142 } | 158 } |
| 143 | 159 |
| 144 } // namespace content | 160 } // namespace content |
| OLD | NEW |