| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_orientation/device_motion_hardware_buffer.h" | 10 #include "content/common/device_orientation/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 DeviceMotionEventPumpTest : public testing::Test { | |
| 18 }; | |
| 19 | |
| 20 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
| 21 public: | 18 public: |
| 22 MockDeviceMotionListener(); | 19 MockDeviceMotionListener(); |
| 23 virtual ~MockDeviceMotionListener() { } | 20 virtual ~MockDeviceMotionListener() { } |
| 24 virtual void didChangeDeviceMotion( | 21 virtual void didChangeDeviceMotion( |
| 25 const blink::WebDeviceMotionData&) OVERRIDE; | 22 const blink::WebDeviceMotionData&) OVERRIDE; |
| 26 bool did_change_device_motion_; | 23 bool did_change_device_motion_; |
| 27 blink::WebDeviceMotionData data_; | 24 blink::WebDeviceMotionData data_; |
| 28 }; | 25 }; |
| 29 | 26 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 41 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { | 38 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
| 42 public: | 39 public: |
| 43 DeviceMotionEventPumpForTesting() { } | 40 DeviceMotionEventPumpForTesting() { } |
| 44 virtual ~DeviceMotionEventPumpForTesting() { } | 41 virtual ~DeviceMotionEventPumpForTesting() { } |
| 45 | 42 |
| 46 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 43 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 47 DeviceMotionEventPump::OnDidStart(renderer_handle); | 44 DeviceMotionEventPump::OnDidStart(renderer_handle); |
| 48 } | 45 } |
| 49 virtual bool SendStartMessage() OVERRIDE { return true; } | 46 virtual bool SendStartMessage() OVERRIDE { return true; } |
| 50 virtual bool SendStopMessage() OVERRIDE { return true; } | 47 virtual bool SendStopMessage() OVERRIDE { return true; } |
| 48 virtual void FireEvent() OVERRIDE { |
| 49 DeviceMotionEventPump::FireEvent(); |
| 50 Stop(); |
| 51 base::MessageLoop::current()->QuitWhenIdle(); |
| 52 } |
| 53 }; |
| 54 |
| 55 class DeviceMotionEventPumpTest : public testing::Test { |
| 56 public: |
| 57 DeviceMotionEventPumpTest() { |
| 58 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 59 sizeof(DeviceMotionHardwareBuffer))); |
| 60 } |
| 61 |
| 62 protected: |
| 63 virtual void SetUp() OVERRIDE { |
| 64 listener_.reset(new MockDeviceMotionListener); |
| 65 motion_pump_.reset(new DeviceMotionEventPumpForTesting); |
| 66 buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory()); |
| 67 memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer)); |
| 68 shared_memory_.ShareToProcess(base::kNullProcessHandle, &handle_); |
| 69 } |
| 70 |
| 71 void InitBuffer(bool allAvailableSensorsActive) { |
| 72 blink::WebDeviceMotionData& data = buffer_->data; |
| 73 data.accelerationX = 1; |
| 74 data.hasAccelerationX = true; |
| 75 data.accelerationY = 2; |
| 76 data.hasAccelerationY = true; |
| 77 data.accelerationZ = 3; |
| 78 data.hasAccelerationZ = true; |
| 79 data.allAvailableSensorsAreActive = allAvailableSensorsActive; |
| 80 } |
| 81 |
| 82 scoped_ptr<MockDeviceMotionListener> listener_; |
| 83 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump_; |
| 84 base::SharedMemoryHandle handle_; |
| 85 base::SharedMemory shared_memory_; |
| 86 DeviceMotionHardwareBuffer* buffer_; |
| 51 }; | 87 }; |
| 52 | 88 |
| 53 // Always failing in the win try bot. See http://crbug.com/256782. | 89 // Always failing in the win try bot. See http://crbug.com/256782. |
| 54 #if defined(OS_WIN) | 90 #if defined(OS_WIN) |
| 55 #define MAYBE_DidStartPolling DISABLED_DidStartPolling | 91 #define MAYBE_DidStartPolling DISABLED_DidStartPolling |
| 56 #else | 92 #else |
| 57 #define MAYBE_DidStartPolling DidStartPolling | 93 #define MAYBE_DidStartPolling DidStartPolling |
| 58 #endif | 94 #endif |
| 59 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPolling) { | 95 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPolling) { |
| 60 base::MessageLoopForUI loop; | 96 base::MessageLoopForUI loop; |
| 61 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); | |
| 62 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump( | |
| 63 new DeviceMotionEventPumpForTesting); | |
| 64 | 97 |
| 65 base::SharedMemoryHandle handle; | 98 InitBuffer(true); |
| 66 base::SharedMemory shared_memory; | |
| 67 EXPECT_TRUE(shared_memory.CreateAndMapAnonymous( | |
| 68 sizeof(DeviceMotionHardwareBuffer))); | |
| 69 DeviceMotionHardwareBuffer* buffer = | |
| 70 static_cast<DeviceMotionHardwareBuffer*>(shared_memory.memory()); | |
| 71 memset(buffer, 0, sizeof(DeviceMotionHardwareBuffer)); | |
| 72 shared_memory.ShareToProcess(base::kNullProcessHandle, &handle); | |
| 73 | 99 |
| 74 blink::WebDeviceMotionData& data = buffer->data; | 100 motion_pump_->SetListener(listener_.get()); |
| 75 data.accelerationX = 1; | 101 motion_pump_->OnDidStart(handle_); |
| 76 data.hasAccelerationX = true; | |
| 77 data.accelerationY = 2; | |
| 78 data.hasAccelerationY = true; | |
| 79 data.accelerationZ = 3; | |
| 80 data.hasAccelerationZ = true; | |
| 81 data.allAvailableSensorsAreActive = true; | |
| 82 | 102 |
| 83 motion_pump->SetListener(listener.get()); | 103 base::MessageLoop::current()->Run(); |
| 84 motion_pump->OnDidStart(handle); | |
| 85 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | |
| 86 motion_pump->GetDelayMillis() * 2)); | |
| 87 RunAllPendingInMessageLoop(); | |
| 88 motion_pump->SetListener(0); | |
| 89 | 104 |
| 90 blink::WebDeviceMotionData& received_data = listener->data_; | 105 blink::WebDeviceMotionData& received_data = listener_->data_; |
| 91 EXPECT_TRUE(listener->did_change_device_motion_); | 106 EXPECT_TRUE(listener_->did_change_device_motion_); |
| 92 EXPECT_TRUE(received_data.hasAccelerationX); | 107 EXPECT_TRUE(received_data.hasAccelerationX); |
| 93 EXPECT_EQ(1, (double)received_data.accelerationX); | 108 EXPECT_EQ(1, (double)received_data.accelerationX); |
| 94 EXPECT_TRUE(received_data.hasAccelerationX); | 109 EXPECT_TRUE(received_data.hasAccelerationX); |
| 95 EXPECT_EQ(2, (double)received_data.accelerationY); | 110 EXPECT_EQ(2, (double)received_data.accelerationY); |
| 96 EXPECT_TRUE(received_data.hasAccelerationY); | 111 EXPECT_TRUE(received_data.hasAccelerationY); |
| 97 EXPECT_EQ(3, (double)received_data.accelerationZ); | 112 EXPECT_EQ(3, (double)received_data.accelerationZ); |
| 98 EXPECT_TRUE(received_data.hasAccelerationZ); | 113 EXPECT_TRUE(received_data.hasAccelerationZ); |
| 99 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 114 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 100 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 115 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 101 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 116 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 102 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 117 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 103 EXPECT_FALSE(received_data.hasRotationRateBeta); | 118 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 104 EXPECT_FALSE(received_data.hasRotationRateGamma); | 119 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 105 } | 120 } |
| 106 | 121 |
| 107 // Although this test passes on windows builds it is not certain if it does | 122 // Although this test passes on windows builds it is not certain if it does |
| 108 // so for the right reason. See http://crbug.com/256782. | 123 // so for the right reason. See http://crbug.com/256782. |
| 109 #if defined(OS_WIN) | 124 #if defined(OS_WIN) |
| 110 #define MAYBE_DidStartPollingNotAllSensorsActive \ | 125 #define MAYBE_DidStartPollingNotAllSensorsActive \ |
| 111 DISABLED_DidStartPollingNotAllSensorsActive | 126 DISABLED_DidStartPollingNotAllSensorsActive |
| 112 #else | 127 #else |
| 113 #define MAYBE_DidStartPollingNotAllSensorsActive \ | 128 #define MAYBE_DidStartPollingNotAllSensorsActive \ |
| 114 DidStartPollingNotAllSensorsActive | 129 DidStartPollingNotAllSensorsActive |
| 115 #endif | 130 #endif |
| 116 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPollingNotAllSensorsActive) { | 131 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPollingNotAllSensorsActive) { |
| 117 base::MessageLoopForUI loop; | 132 base::MessageLoopForUI loop; |
| 118 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); | |
| 119 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump( | |
| 120 new DeviceMotionEventPumpForTesting); | |
| 121 | 133 |
| 122 base::SharedMemoryHandle handle; | 134 InitBuffer(false); |
| 123 base::SharedMemory shared_memory; | |
| 124 EXPECT_TRUE(shared_memory.CreateAndMapAnonymous( | |
| 125 sizeof(DeviceMotionHardwareBuffer))); | |
| 126 DeviceMotionHardwareBuffer* buffer = | |
| 127 static_cast<DeviceMotionHardwareBuffer*>(shared_memory.memory()); | |
| 128 memset(buffer, 0, sizeof(DeviceMotionHardwareBuffer)); | |
| 129 shared_memory.ShareToProcess(base::kNullProcessHandle, &handle); | |
| 130 | 135 |
| 131 blink::WebDeviceMotionData& data = buffer->data; | 136 motion_pump_->SetListener(listener_.get()); |
| 132 data.accelerationX = 1; | 137 motion_pump_->OnDidStart(handle_); |
| 133 data.hasAccelerationX = true; | |
| 134 data.accelerationY = 2; | |
| 135 data.hasAccelerationY = true; | |
| 136 data.accelerationZ = 3; | |
| 137 data.hasAccelerationZ = true; | |
| 138 data.allAvailableSensorsAreActive = false; | |
| 139 | 138 |
| 140 motion_pump->SetListener(listener.get()); | 139 base::MessageLoop::current()->Run(); |
| 141 motion_pump->OnDidStart(handle); | |
| 142 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | |
| 143 motion_pump->GetDelayMillis() * 2)); | |
| 144 RunAllPendingInMessageLoop(); | |
| 145 motion_pump->SetListener(0); | |
| 146 | 140 |
| 147 blink::WebDeviceMotionData& received_data = listener->data_; | 141 blink::WebDeviceMotionData& received_data = listener_->data_; |
| 148 // No change in device motion because allAvailableSensorsAreActive is false. | 142 // No change in device motion because allAvailableSensorsAreActive is false. |
| 149 EXPECT_FALSE(listener->did_change_device_motion_); | 143 EXPECT_FALSE(listener_->did_change_device_motion_); |
| 150 EXPECT_FALSE(received_data.hasAccelerationX); | 144 EXPECT_FALSE(received_data.hasAccelerationX); |
| 151 EXPECT_FALSE(received_data.hasAccelerationX); | 145 EXPECT_FALSE(received_data.hasAccelerationX); |
| 152 EXPECT_FALSE(received_data.hasAccelerationY); | 146 EXPECT_FALSE(received_data.hasAccelerationY); |
| 153 EXPECT_FALSE(received_data.hasAccelerationZ); | 147 EXPECT_FALSE(received_data.hasAccelerationZ); |
| 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 148 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 149 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 156 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 150 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 157 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 151 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 158 EXPECT_FALSE(received_data.hasRotationRateBeta); | 152 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 159 EXPECT_FALSE(received_data.hasRotationRateGamma); | 153 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 160 } | 154 } |
| 161 | 155 |
| 162 } // namespace content | 156 } // namespace content |
| OLD | NEW |