Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / device_sensors / device_orientation_event_pump_unittest.cc
blob3787121753c206d74e3a07d314ef5eec4cd50dfa
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/location.h"
8 #include "base/logging.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
12 #include "content/public/test/test_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationListener.h"
16 namespace content {
18 class MockDeviceOrientationListener
19 : public blink::WebDeviceOrientationListener {
20 public:
21 MockDeviceOrientationListener() : did_change_device_orientation_(false) {
22 memset(&data_, 0, sizeof(data_));
24 virtual ~MockDeviceOrientationListener() { }
26 virtual void didChangeDeviceOrientation(
27 const blink::WebDeviceOrientationData& data) override {
28 memcpy(&data_, &data, sizeof(data));
29 did_change_device_orientation_ = true;
32 bool did_change_device_orientation() const {
33 return did_change_device_orientation_;
35 void set_did_change_device_orientation(bool value) {
36 did_change_device_orientation_ = value;
38 const blink::WebDeviceOrientationData& data() const {
39 return data_;
42 private:
43 bool did_change_device_orientation_;
44 blink::WebDeviceOrientationData data_;
46 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener);
49 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump {
50 public:
51 DeviceOrientationEventPumpForTesting()
52 : DeviceOrientationEventPump(0) { }
53 ~DeviceOrientationEventPumpForTesting() override {}
55 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
56 DeviceOrientationEventPump::OnDidStart(renderer_handle);
58 void SendStartMessage() override {}
59 void SendStopMessage() override {}
60 void FireEvent() override {
61 DeviceOrientationEventPump::FireEvent();
62 Stop();
63 base::MessageLoop::current()->QuitWhenIdle();
66 private:
67 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting);
70 class DeviceOrientationEventPumpTest : public testing::Test {
71 public:
72 DeviceOrientationEventPumpTest() {
73 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
74 sizeof(DeviceOrientationHardwareBuffer)));
77 protected:
78 void SetUp() override {
79 const DeviceOrientationHardwareBuffer* null_buffer = nullptr;
80 listener_.reset(new MockDeviceOrientationListener);
81 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting);
82 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
83 shared_memory_.memory());
84 ASSERT_NE(null_buffer, buffer_);
85 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer));
86 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(),
87 &handle_));
90 void InitBuffer() {
91 blink::WebDeviceOrientationData& data = buffer_->data;
92 data.alpha = 1;
93 data.hasAlpha = true;
94 data.beta = 2;
95 data.hasBeta = true;
96 data.gamma = 3;
97 data.hasGamma = true;
98 data.allAvailableSensorsAreActive = true;
101 void InitBufferNoData() {
102 blink::WebDeviceOrientationData& data = buffer_->data;
103 data.allAvailableSensorsAreActive = true;
106 MockDeviceOrientationListener* listener() { return listener_.get(); }
107 DeviceOrientationEventPumpForTesting* orientation_pump() {
108 return orientation_pump_.get();
110 base::SharedMemoryHandle handle() { return handle_; }
111 DeviceOrientationHardwareBuffer* buffer() { return buffer_; }
113 private:
114 scoped_ptr<MockDeviceOrientationListener> listener_;
115 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_;
116 base::SharedMemoryHandle handle_;
117 base::SharedMemory shared_memory_;
118 DeviceOrientationHardwareBuffer* buffer_;
120 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest);
123 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) {
124 base::MessageLoop loop;
126 InitBuffer();
127 orientation_pump()->Start(listener());
128 orientation_pump()->OnDidStart(handle());
130 base::MessageLoop::current()->Run();
132 const blink::WebDeviceOrientationData& received_data = listener()->data();
133 EXPECT_TRUE(listener()->did_change_device_orientation());
134 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
135 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
136 EXPECT_TRUE(received_data.hasAlpha);
137 EXPECT_EQ(2, static_cast<double>(received_data.beta));
138 EXPECT_TRUE(received_data.hasBeta);
139 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
140 EXPECT_TRUE(received_data.hasGamma);
143 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) {
144 base::MessageLoop loop;
146 InitBufferNoData();
147 orientation_pump()->Start(listener());
148 orientation_pump()->OnDidStart(handle());
150 base::MessageLoop::current()->Run();
152 const blink::WebDeviceOrientationData& received_data = listener()->data();
153 EXPECT_TRUE(listener()->did_change_device_orientation());
154 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
155 EXPECT_FALSE(received_data.hasAlpha);
156 EXPECT_FALSE(received_data.hasBeta);
157 EXPECT_FALSE(received_data.hasGamma);
160 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) {
161 base::MessageLoop loop;
163 InitBuffer();
164 orientation_pump()->Start(listener());
165 orientation_pump()->OnDidStart(handle());
167 base::MessageLoop::current()->Run();
169 const blink::WebDeviceOrientationData& received_data = listener()->data();
170 EXPECT_TRUE(listener()->did_change_device_orientation());
171 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
172 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
173 EXPECT_TRUE(received_data.hasAlpha);
174 EXPECT_EQ(2, static_cast<double>(received_data.beta));
175 EXPECT_TRUE(received_data.hasBeta);
176 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
177 EXPECT_TRUE(received_data.hasGamma);
179 buffer()->data.alpha =
180 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0;
181 listener()->set_did_change_device_orientation(false);
183 // Reset the pump's listener.
184 orientation_pump()->Start(listener());
186 base::ThreadTaskRunnerHandle::Get()->PostTask(
187 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
188 base::Unretained(orientation_pump())));
189 base::MessageLoop::current()->Run();
191 EXPECT_FALSE(listener()->did_change_device_orientation());
192 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
193 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
194 EXPECT_TRUE(received_data.hasAlpha);
195 EXPECT_EQ(2, static_cast<double>(received_data.beta));
196 EXPECT_TRUE(received_data.hasBeta);
197 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
198 EXPECT_TRUE(received_data.hasGamma);
200 buffer()->data.alpha =
201 1 + DeviceOrientationEventPump::kOrientationThreshold;
202 listener()->set_did_change_device_orientation(false);
204 // Reset the pump's listener.
205 orientation_pump()->Start(listener());
207 base::ThreadTaskRunnerHandle::Get()->PostTask(
208 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
209 base::Unretained(orientation_pump())));
210 base::MessageLoop::current()->Run();
212 EXPECT_TRUE(listener()->did_change_device_orientation());
213 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold,
214 static_cast<double>(received_data.alpha));
217 } // namespace content