- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / device_orientation / device_motion_event_pump_unittest.cc
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
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/message_loop.h"
10 #include "content/common/device_orientation/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
46   void OnDidStart(base::SharedMemoryHandle renderer_handle) {
47     DeviceMotionEventPump::OnDidStart(renderer_handle);
48   }
49   virtual bool SendStartMessage() OVERRIDE { return true; }
50   virtual bool SendStopMessage() OVERRIDE { return true; }
51 };
52
53 // Always failing in the win try bot. See http://crbug.com/256782.
54 #if defined(OS_WIN)
55 #define MAYBE_DidStartPolling DISABLED_DidStartPolling
56 #else
57 #define MAYBE_DidStartPolling DidStartPolling
58 #endif
59 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPolling) {
60   base::MessageLoop loop(base::MessageLoop::TYPE_UI);
61   scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener);
62   scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump(
63       new DeviceMotionEventPumpForTesting);
64
65   base::SharedMemoryHandle handle;
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
74   WebKit::WebDeviceMotionData& data = buffer->data;
75   data.accelerationX = 1;
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
83   motion_pump->SetListener(listener.get());
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
90   WebKit::WebDeviceMotionData& received_data = listener->data_;
91   EXPECT_TRUE(listener->did_change_device_motion_);
92   EXPECT_TRUE(received_data.hasAccelerationX);
93   EXPECT_EQ(1, (double)received_data.accelerationX);
94   EXPECT_TRUE(received_data.hasAccelerationX);
95   EXPECT_EQ(2, (double)received_data.accelerationY);
96   EXPECT_TRUE(received_data.hasAccelerationY);
97   EXPECT_EQ(3, (double)received_data.accelerationZ);
98   EXPECT_TRUE(received_data.hasAccelerationZ);
99   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
100   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
101   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
102   EXPECT_FALSE(received_data.hasRotationRateAlpha);
103   EXPECT_FALSE(received_data.hasRotationRateBeta);
104   EXPECT_FALSE(received_data.hasRotationRateGamma);
105 }
106
107 // 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.
109 #if defined(OS_WIN)
110 #define MAYBE_DidStartPollingNotAllSensorsActive \
111     DISABLED_DidStartPollingNotAllSensorsActive
112 #else
113 #define MAYBE_DidStartPollingNotAllSensorsActive \
114     DidStartPollingNotAllSensorsActive
115 #endif
116 TEST_F(DeviceMotionEventPumpTest, MAYBE_DidStartPollingNotAllSensorsActive) {
117   base::MessageLoop loop(base::MessageLoop::TYPE_UI);
118   scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener);
119   scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump(
120       new DeviceMotionEventPumpForTesting);
121
122   base::SharedMemoryHandle handle;
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
131   WebKit::WebDeviceMotionData& data = buffer->data;
132   data.accelerationX = 1;
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
140   motion_pump->SetListener(listener.get());
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
147   WebKit::WebDeviceMotionData& received_data = listener->data_;
148   // No change in device motion because allAvailableSensorsAreActive is false.
149   EXPECT_FALSE(listener->did_change_device_motion_);
150   EXPECT_FALSE(received_data.hasAccelerationX);
151   EXPECT_FALSE(received_data.hasAccelerationX);
152   EXPECT_FALSE(received_data.hasAccelerationY);
153   EXPECT_FALSE(received_data.hasAccelerationZ);
154   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
155   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
156   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
157   EXPECT_FALSE(received_data.hasRotationRateAlpha);
158   EXPECT_FALSE(received_data.hasRotationRateBeta);
159   EXPECT_FALSE(received_data.hasRotationRateGamma);
160 }
161
162 }  // namespace content