Use nullptr instead of NULL in device_sensors
[chromium-blink-merge.git] / content / renderer / device_sensors / device_orientation_event_pump_unittest.cc
blob8dd10cb1cdb5987c5f432d4c540fee6f96c36108
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_orientation_event_pump.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
10 #include "content/public/test/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
14 namespace content {
16 class MockDeviceOrientationListener
17 : public blink::WebDeviceOrientationListener {
18 public:
19 MockDeviceOrientationListener() : did_change_device_orientation_(false) {
20 memset(&data_, 0, sizeof(data_));
22 virtual ~MockDeviceOrientationListener() { }
24 virtual void didChangeDeviceOrientation(
25 const blink::WebDeviceOrientationData& data) override {
26 memcpy(&data_, &data, sizeof(data));
27 did_change_device_orientation_ = true;
30 bool did_change_device_orientation() const {
31 return did_change_device_orientation_;
33 void set_did_change_device_orientation(bool value) {
34 did_change_device_orientation_ = value;
36 const blink::WebDeviceOrientationData& data() const {
37 return data_;
40 private:
41 bool did_change_device_orientation_;
42 blink::WebDeviceOrientationData data_;
44 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener);
47 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump {
48 public:
49 DeviceOrientationEventPumpForTesting()
50 : DeviceOrientationEventPump(0) { }
51 ~DeviceOrientationEventPumpForTesting() override {}
53 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
54 DeviceOrientationEventPump::OnDidStart(renderer_handle);
56 void SendStartMessage() override {}
57 void SendStopMessage() override {}
58 void FireEvent() override {
59 DeviceOrientationEventPump::FireEvent();
60 Stop();
61 base::MessageLoop::current()->QuitWhenIdle();
64 private:
65 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting);
68 class DeviceOrientationEventPumpTest : public testing::Test {
69 public:
70 DeviceOrientationEventPumpTest() {
71 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
72 sizeof(DeviceOrientationHardwareBuffer)));
75 protected:
76 void SetUp() override {
77 const DeviceOrientationHardwareBuffer* null_buffer = nullptr;
78 listener_.reset(new MockDeviceOrientationListener);
79 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting);
80 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
81 shared_memory_.memory());
82 ASSERT_NE(null_buffer, buffer_);
83 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer));
84 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
85 &handle_));
88 void InitBuffer() {
89 blink::WebDeviceOrientationData& data = buffer_->data;
90 data.alpha = 1;
91 data.hasAlpha = true;
92 data.beta = 2;
93 data.hasBeta = true;
94 data.gamma = 3;
95 data.hasGamma = true;
96 data.allAvailableSensorsAreActive = true;
99 void InitBufferNoData() {
100 blink::WebDeviceOrientationData& data = buffer_->data;
101 data.allAvailableSensorsAreActive = true;
104 MockDeviceOrientationListener* listener() { return listener_.get(); }
105 DeviceOrientationEventPumpForTesting* orientation_pump() {
106 return orientation_pump_.get();
108 base::SharedMemoryHandle handle() { return handle_; }
109 DeviceOrientationHardwareBuffer* buffer() { return buffer_; }
111 private:
112 scoped_ptr<MockDeviceOrientationListener> listener_;
113 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_;
114 base::SharedMemoryHandle handle_;
115 base::SharedMemory shared_memory_;
116 DeviceOrientationHardwareBuffer* buffer_;
118 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest);
121 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) {
122 base::MessageLoop loop;
124 InitBuffer();
125 orientation_pump()->Start(listener());
126 orientation_pump()->OnDidStart(handle());
128 base::MessageLoop::current()->Run();
130 const blink::WebDeviceOrientationData& received_data = listener()->data();
131 EXPECT_TRUE(listener()->did_change_device_orientation());
132 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
133 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
134 EXPECT_TRUE(received_data.hasAlpha);
135 EXPECT_EQ(2, static_cast<double>(received_data.beta));
136 EXPECT_TRUE(received_data.hasBeta);
137 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
138 EXPECT_TRUE(received_data.hasGamma);
141 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) {
142 base::MessageLoop loop;
144 InitBufferNoData();
145 orientation_pump()->Start(listener());
146 orientation_pump()->OnDidStart(handle());
148 base::MessageLoop::current()->Run();
150 const blink::WebDeviceOrientationData& received_data = listener()->data();
151 EXPECT_TRUE(listener()->did_change_device_orientation());
152 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
153 EXPECT_FALSE(received_data.hasAlpha);
154 EXPECT_FALSE(received_data.hasBeta);
155 EXPECT_FALSE(received_data.hasGamma);
158 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) {
159 base::MessageLoop loop;
161 InitBuffer();
162 orientation_pump()->Start(listener());
163 orientation_pump()->OnDidStart(handle());
165 base::MessageLoop::current()->Run();
167 const blink::WebDeviceOrientationData& received_data = listener()->data();
168 EXPECT_TRUE(listener()->did_change_device_orientation());
169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
170 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
171 EXPECT_TRUE(received_data.hasAlpha);
172 EXPECT_EQ(2, static_cast<double>(received_data.beta));
173 EXPECT_TRUE(received_data.hasBeta);
174 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
175 EXPECT_TRUE(received_data.hasGamma);
177 buffer()->data.alpha =
178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0;
179 listener()->set_did_change_device_orientation(false);
181 // Reset the pump's listener.
182 orientation_pump()->Start(listener());
184 base::MessageLoop::current()->PostTask(FROM_HERE,
185 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
186 base::Unretained(orientation_pump())));
187 base::MessageLoop::current()->Run();
189 EXPECT_FALSE(listener()->did_change_device_orientation());
190 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
191 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
192 EXPECT_TRUE(received_data.hasAlpha);
193 EXPECT_EQ(2, static_cast<double>(received_data.beta));
194 EXPECT_TRUE(received_data.hasBeta);
195 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
196 EXPECT_TRUE(received_data.hasGamma);
198 buffer()->data.alpha =
199 1 + DeviceOrientationEventPump::kOrientationThreshold;
200 listener()->set_did_change_device_orientation(false);
202 // Reset the pump's listener.
203 orientation_pump()->Start(listener());
205 base::MessageLoop::current()->PostTask(FROM_HERE,
206 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
207 base::Unretained(orientation_pump())));
208 base::MessageLoop::current()->Run();
210 EXPECT_TRUE(listener()->did_change_device_orientation());
211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold,
212 static_cast<double>(received_data.alpha));
215 } // namespace content