OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device_motion_event_pump.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "content/common/device_motion_hardware_buffer.h" |
| 11 #include "content/public/test/test_utils.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class DeviceMotionEventPumpTest : public testing::Test { |
| 18 }; |
| 19 |
| 20 class MockDeviceMotionListener : public WebKit::WebDeviceMotionListener { |
| 21 public: |
| 22 MockDeviceMotionListener(); |
| 23 virtual ~MockDeviceMotionListener() { } |
| 24 virtual void didChangeDeviceMotion( |
| 25 const WebKit::WebDeviceMotionData&) OVERRIDE; |
| 26 bool did_change_device_motion_; |
| 27 WebKit::WebDeviceMotionData data_; |
| 28 }; |
| 29 |
| 30 MockDeviceMotionListener::MockDeviceMotionListener() |
| 31 : did_change_device_motion_(false) { |
| 32 memset(&data_, 0, sizeof(data_)); |
| 33 } |
| 34 |
| 35 void MockDeviceMotionListener::didChangeDeviceMotion( |
| 36 const WebKit::WebDeviceMotionData& data) { |
| 37 memcpy(&data_, &data, sizeof(data)); |
| 38 did_change_device_motion_ = true; |
| 39 } |
| 40 |
| 41 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
| 42 public: |
| 43 DeviceMotionEventPumpForTesting() { } |
| 44 virtual ~DeviceMotionEventPumpForTesting() { } |
| 45 void OnDidStartDeviceMotion(base::SharedMemoryHandle handle); |
| 46 bool SetListener(WebKit::WebDeviceMotionListener*); |
| 47 bool StartFetchingDeviceMotion(); |
| 48 bool StopFetchingDeviceMotion(); |
| 49 }; |
| 50 |
| 51 bool DeviceMotionEventPumpForTesting::StartFetchingDeviceMotion() { |
| 52 state_ = PENDING_START; |
| 53 return true; |
| 54 } |
| 55 |
| 56 bool DeviceMotionEventPumpForTesting::StopFetchingDeviceMotion() { |
| 57 if (timer_.IsRunning()) |
| 58 timer_.Stop(); |
| 59 state_ = STOPPED; |
| 60 return true; |
| 61 } |
| 62 |
| 63 bool DeviceMotionEventPumpForTesting::SetListener( |
| 64 WebKit::WebDeviceMotionListener* listener) { |
| 65 listener_ = listener; |
| 66 return (listener_) ? StartFetchingDeviceMotion() : StopFetchingDeviceMotion(); |
| 67 } |
| 68 |
| 69 void DeviceMotionEventPumpForTesting::OnDidStartDeviceMotion( |
| 70 base::SharedMemoryHandle handle) { |
| 71 DeviceMotionEventPump::OnDidStartDeviceMotion(handle); |
| 72 } |
| 73 |
| 74 // Always failing in the win try bot. See http://crbug.com/256782. |
| 75 #if defined(OS_WIN) |
| 76 #define MAYBE_DidStartPolling DISABLED_DidStartPolling |
| 77 #else |
| 78 #define MAYBE_DidStartPolling DidStartPolling |
| 79 #endif |
| 80 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPolling) { |
| 81 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 82 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); |
| 83 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump( |
| 84 new DeviceMotionEventPumpForTesting); |
| 85 |
| 86 base::SharedMemoryHandle handle; |
| 87 base::SharedMemory shared_memory; |
| 88 EXPECT_TRUE(shared_memory.CreateAndMapAnonymous( |
| 89 sizeof(DeviceMotionHardwareBuffer))); |
| 90 DeviceMotionHardwareBuffer* buffer = |
| 91 static_cast<DeviceMotionHardwareBuffer*>(shared_memory.memory()); |
| 92 memset(buffer, 0, sizeof(DeviceMotionHardwareBuffer)); |
| 93 shared_memory.ShareToProcess(base::kNullProcessHandle, &handle); |
| 94 |
| 95 WebKit::WebDeviceMotionData& data = buffer->data; |
| 96 data.accelerationX = 1; |
| 97 data.hasAccelerationX = true; |
| 98 data.accelerationY = 2; |
| 99 data.hasAccelerationY = true; |
| 100 data.accelerationZ = 3; |
| 101 data.hasAccelerationZ = true; |
| 102 data.allAvailableSensorsAreActive = true; |
| 103 |
| 104 motion_pump->SetListener(listener.get()); |
| 105 motion_pump->OnDidStartDeviceMotion(handle); |
| 106 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 107 DeviceMotionEventPump::GetDelayMillis() * 2)); |
| 108 RunAllPendingInMessageLoop(); |
| 109 motion_pump->SetListener(0); |
| 110 |
| 111 WebKit::WebDeviceMotionData& received_data = listener->data_; |
| 112 EXPECT_TRUE(listener->did_change_device_motion_); |
| 113 EXPECT_TRUE(received_data.hasAccelerationX); |
| 114 EXPECT_EQ(1, (double)received_data.accelerationX); |
| 115 EXPECT_TRUE(received_data.hasAccelerationX); |
| 116 EXPECT_EQ(2, (double)received_data.accelerationY); |
| 117 EXPECT_TRUE(received_data.hasAccelerationY); |
| 118 EXPECT_EQ(3, (double)received_data.accelerationZ); |
| 119 EXPECT_TRUE(received_data.hasAccelerationZ); |
| 120 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 121 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 122 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 123 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 124 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 125 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 126 } |
| 127 |
| 128 // Although this test passes on windows builds it is not certain if it does |
| 129 // so for the right reason. See http://crbug.com/256782. |
| 130 #if defined(OS_WIN) |
| 131 #define MAYBE_DidStartPollingNotAllSensorsActive \ |
| 132 DISABLED_DidStartPollingNotAllSensorsActive |
| 133 #else |
| 134 #define MAYBE_DidStartPollingNotAllSensorsActive \ |
| 135 DidStartPollingNotAllSensorsActive |
| 136 #endif |
| 137 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPollingNotAllSensorsActive) { |
| 138 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 139 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); |
| 140 scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump( |
| 141 new DeviceMotionEventPumpForTesting); |
| 142 |
| 143 base::SharedMemoryHandle handle; |
| 144 base::SharedMemory shared_memory; |
| 145 EXPECT_TRUE(shared_memory.CreateAndMapAnonymous( |
| 146 sizeof(DeviceMotionHardwareBuffer))); |
| 147 DeviceMotionHardwareBuffer* buffer = |
| 148 static_cast<DeviceMotionHardwareBuffer*>(shared_memory.memory()); |
| 149 memset(buffer, 0, sizeof(DeviceMotionHardwareBuffer)); |
| 150 shared_memory.ShareToProcess(base::kNullProcessHandle, &handle); |
| 151 |
| 152 WebKit::WebDeviceMotionData& data = buffer->data; |
| 153 data.accelerationX = 1; |
| 154 data.hasAccelerationX = true; |
| 155 data.accelerationY = 2; |
| 156 data.hasAccelerationY = true; |
| 157 data.accelerationZ = 3; |
| 158 data.hasAccelerationZ = true; |
| 159 data.allAvailableSensorsAreActive = false; |
| 160 |
| 161 motion_pump->SetListener(listener.get()); |
| 162 motion_pump->OnDidStartDeviceMotion(handle); |
| 163 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 164 DeviceMotionEventPump::GetDelayMillis() * 2)); |
| 165 RunAllPendingInMessageLoop(); |
| 166 motion_pump->SetListener(0); |
| 167 |
| 168 WebKit::WebDeviceMotionData& received_data = listener->data_; |
| 169 // No change in device motion because allAvailableSensorsAreActive is false. |
| 170 EXPECT_FALSE(listener->did_change_device_motion_); |
| 171 EXPECT_FALSE(received_data.hasAccelerationX); |
| 172 EXPECT_FALSE(received_data.hasAccelerationX); |
| 173 EXPECT_FALSE(received_data.hasAccelerationY); |
| 174 EXPECT_FALSE(received_data.hasAccelerationZ); |
| 175 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 178 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 179 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 180 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 181 } |
| 182 |
| 183 } // namespace content |
OLD | NEW |