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
3 // found in the LICENSE file.
5 #include "device_motion_event_pump.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/common/device_sensors/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"
17 class MockDeviceMotionListener
: public blink::WebDeviceMotionListener
{
19 MockDeviceMotionListener() : did_change_device_motion_(false) {
20 memset(&data_
, 0, sizeof(data_
));
22 virtual ~MockDeviceMotionListener() { }
24 virtual void didChangeDeviceMotion(
25 const blink::WebDeviceMotionData
& data
) OVERRIDE
{
26 memcpy(&data_
, &data
, sizeof(data
));
27 did_change_device_motion_
= true;
30 bool did_change_device_motion() const {
31 return did_change_device_motion_
;
33 const blink::WebDeviceMotionData
& data() const {
38 bool did_change_device_motion_
;
39 blink::WebDeviceMotionData data_
;
41 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener
);
44 class DeviceMotionEventPumpForTesting
: public DeviceMotionEventPump
{
46 DeviceMotionEventPumpForTesting()
47 : DeviceMotionEventPump(0) { }
48 virtual ~DeviceMotionEventPumpForTesting() { }
50 void OnDidStart(base::SharedMemoryHandle renderer_handle
) {
51 DeviceMotionEventPump::OnDidStart(renderer_handle
);
53 virtual void SendStartMessage() OVERRIDE
{ }
54 virtual void SendStopMessage() OVERRIDE
{ }
55 virtual void FireEvent() OVERRIDE
{
56 DeviceMotionEventPump::FireEvent();
58 base::MessageLoop::current()->QuitWhenIdle();
62 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting
);
65 class DeviceMotionEventPumpTest
: public testing::Test
{
67 DeviceMotionEventPumpTest() {
68 EXPECT_TRUE(shared_memory_
.CreateAndMapAnonymous(
69 sizeof(DeviceMotionHardwareBuffer
)));
73 virtual void SetUp() OVERRIDE
{
74 const DeviceMotionHardwareBuffer
* null_buffer
= NULL
;
75 listener_
.reset(new MockDeviceMotionListener
);
76 motion_pump_
.reset(new DeviceMotionEventPumpForTesting
);
77 buffer_
= static_cast<DeviceMotionHardwareBuffer
*>(shared_memory_
.memory());
78 ASSERT_NE(null_buffer
, buffer_
);
79 memset(buffer_
, 0, sizeof(DeviceMotionHardwareBuffer
));
80 ASSERT_TRUE(shared_memory_
.ShareToProcess(base::GetCurrentProcessHandle(),
84 void InitBuffer(bool allAvailableSensorsActive
) {
85 blink::WebDeviceMotionData
& data
= buffer_
->data
;
86 data
.accelerationX
= 1;
87 data
.hasAccelerationX
= true;
88 data
.accelerationY
= 2;
89 data
.hasAccelerationY
= true;
90 data
.accelerationZ
= 3;
91 data
.hasAccelerationZ
= true;
92 data
.allAvailableSensorsAreActive
= allAvailableSensorsActive
;
95 MockDeviceMotionListener
* listener() { return listener_
.get(); }
96 DeviceMotionEventPumpForTesting
* motion_pump() { return motion_pump_
.get(); }
97 base::SharedMemoryHandle
handle() { return handle_
; }
100 scoped_ptr
<MockDeviceMotionListener
> listener_
;
101 scoped_ptr
<DeviceMotionEventPumpForTesting
> motion_pump_
;
102 base::SharedMemoryHandle handle_
;
103 base::SharedMemory shared_memory_
;
104 DeviceMotionHardwareBuffer
* buffer_
;
106 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest
);
109 TEST_F(DeviceMotionEventPumpTest
, DidStartPolling
) {
110 base::MessageLoopForUI loop
;
114 motion_pump()->Start(listener());
115 motion_pump()->OnDidStart(handle());
117 base::MessageLoop::current()->Run();
119 const blink::WebDeviceMotionData
& received_data
= listener()->data();
120 EXPECT_TRUE(listener()->did_change_device_motion());
121 EXPECT_TRUE(received_data
.hasAccelerationX
);
122 EXPECT_EQ(1, static_cast<double>(received_data
.accelerationX
));
123 EXPECT_TRUE(received_data
.hasAccelerationX
);
124 EXPECT_EQ(2, static_cast<double>(received_data
.accelerationY
));
125 EXPECT_TRUE(received_data
.hasAccelerationY
);
126 EXPECT_EQ(3, static_cast<double>(received_data
.accelerationZ
));
127 EXPECT_TRUE(received_data
.hasAccelerationZ
);
128 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityX
);
129 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityY
);
130 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityZ
);
131 EXPECT_FALSE(received_data
.hasRotationRateAlpha
);
132 EXPECT_FALSE(received_data
.hasRotationRateBeta
);
133 EXPECT_FALSE(received_data
.hasRotationRateGamma
);
136 TEST_F(DeviceMotionEventPumpTest
, DidStartPollingNotAllSensorsActive
) {
137 base::MessageLoopForUI loop
;
141 motion_pump()->Start(listener());
142 motion_pump()->OnDidStart(handle());
144 base::MessageLoop::current()->Run();
146 const blink::WebDeviceMotionData
& received_data
= listener()->data();
147 // No change in device motion because allAvailableSensorsAreActive is false.
148 EXPECT_FALSE(listener()->did_change_device_motion());
149 EXPECT_FALSE(received_data
.hasAccelerationX
);
150 EXPECT_FALSE(received_data
.hasAccelerationX
);
151 EXPECT_FALSE(received_data
.hasAccelerationY
);
152 EXPECT_FALSE(received_data
.hasAccelerationZ
);
153 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityX
);
154 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityY
);
155 EXPECT_FALSE(received_data
.hasAccelerationIncludingGravityZ
);
156 EXPECT_FALSE(received_data
.hasRotationRateAlpha
);
157 EXPECT_FALSE(received_data
.hasRotationRateBeta
);
158 EXPECT_FALSE(received_data
.hasRotationRateGamma
);
161 } // namespace content