Upstream version 5.34.104.0
[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 MockDeviceMotionListener : public blink::WebDeviceMotionListener {
18  public:
19   MockDeviceMotionListener();
20   virtual ~MockDeviceMotionListener() { }
21   virtual void didChangeDeviceMotion(
22       const blink::WebDeviceMotionData&) OVERRIDE;
23   bool did_change_device_motion_;
24   blink::WebDeviceMotionData data_;
25 };
26
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 {
39  public:
40   DeviceMotionEventPumpForTesting() { }
41   virtual ~DeviceMotionEventPumpForTesting() { }
42
43   void OnDidStart(base::SharedMemoryHandle renderer_handle) {
44     DeviceMotionEventPump::OnDidStart(renderer_handle);
45   }
46   virtual bool SendStartMessage() 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     const DeviceMotionHardwareBuffer* null_buffer = NULL;
65     listener_.reset(new MockDeviceMotionListener);
66     motion_pump_.reset(new DeviceMotionEventPumpForTesting);
67     buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory());
68     ASSERT_NE(null_buffer, buffer_);
69     memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer));
70     ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
71         &handle_));
72   }
73
74   void InitBuffer(bool allAvailableSensorsActive) {
75     blink::WebDeviceMotionData& data = buffer_->data;
76     data.accelerationX = 1;
77     data.hasAccelerationX = true;
78     data.accelerationY = 2;
79     data.hasAccelerationY = true;
80     data.accelerationZ = 3;
81     data.hasAccelerationZ = true;
82     data.allAvailableSensorsAreActive = allAvailableSensorsActive;
83   }
84
85   scoped_ptr<MockDeviceMotionListener> listener_;
86   scoped_ptr<DeviceMotionEventPumpForTesting> motion_pump_;
87   base::SharedMemoryHandle handle_;
88   base::SharedMemory shared_memory_;
89   DeviceMotionHardwareBuffer* buffer_;
90 };
91
92 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
93   base::MessageLoopForUI loop;
94
95   InitBuffer(true);
96
97   motion_pump_->SetListener(listener_.get());
98   motion_pump_->OnDidStart(handle_);
99
100   base::MessageLoop::current()->Run();
101
102   blink::WebDeviceMotionData& received_data = listener_->data_;
103   EXPECT_TRUE(listener_->did_change_device_motion_);
104   EXPECT_TRUE(received_data.hasAccelerationX);
105   EXPECT_EQ(1, (double)received_data.accelerationX);
106   EXPECT_TRUE(received_data.hasAccelerationX);
107   EXPECT_EQ(2, (double)received_data.accelerationY);
108   EXPECT_TRUE(received_data.hasAccelerationY);
109   EXPECT_EQ(3, (double)received_data.accelerationZ);
110   EXPECT_TRUE(received_data.hasAccelerationZ);
111   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
112   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
113   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
114   EXPECT_FALSE(received_data.hasRotationRateAlpha);
115   EXPECT_FALSE(received_data.hasRotationRateBeta);
116   EXPECT_FALSE(received_data.hasRotationRateGamma);
117 }
118
119 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
120   base::MessageLoopForUI loop;
121
122   InitBuffer(false);
123
124   motion_pump_->SetListener(listener_.get());
125   motion_pump_->OnDidStart(handle_);
126
127   base::MessageLoop::current()->Run();
128
129   blink::WebDeviceMotionData& received_data = listener_->data_;
130   // No change in device motion because allAvailableSensorsAreActive is false.
131   EXPECT_FALSE(listener_->did_change_device_motion_);
132   EXPECT_FALSE(received_data.hasAccelerationX);
133   EXPECT_FALSE(received_data.hasAccelerationX);
134   EXPECT_FALSE(received_data.hasAccelerationY);
135   EXPECT_FALSE(received_data.hasAccelerationZ);
136   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
137   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
138   EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
139   EXPECT_FALSE(received_data.hasRotationRateAlpha);
140   EXPECT_FALSE(received_data.hasRotationRateBeta);
141   EXPECT_FALSE(received_data.hasRotationRateGamma);
142 }
143
144 }  // namespace content