Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: content/renderer/device_orientation/device_orientation_event_pump_unittest.cc

Issue 20707002: Implement Device Orientation using shared memory in content/renderer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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_orientation_event_pump.h"
6
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/common/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"
13
14 namespace content {
15
16 class MockDeviceOrientationListener
17 : public WebKit::WebDeviceOrientationListener {
18 public:
19 MockDeviceOrientationListener();
20 virtual ~MockDeviceOrientationListener() { }
21 virtual void didChangeDeviceOrientation(
22 const WebKit::WebDeviceOrientationData&) OVERRIDE;
23 void ResetDidChangeOrientation();
24 bool did_change_device_orientation_;
25 WebKit::WebDeviceOrientationData data_;
26 };
27
28 MockDeviceOrientationListener::MockDeviceOrientationListener()
29 : did_change_device_orientation_(false) {
30 memset(&data_, 0, sizeof(data_));
31 }
32
33 void MockDeviceOrientationListener::didChangeDeviceOrientation(
34 const WebKit::WebDeviceOrientationData& data) {
35 memcpy(&data_, &data, sizeof(data));
36 did_change_device_orientation_ = true;
37 }
38
39 void MockDeviceOrientationListener::ResetDidChangeOrientation() {
40 did_change_device_orientation_ = false;
41 }
42
43 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump {
44 public:
45 DeviceOrientationEventPumpForTesting() { }
46 virtual ~DeviceOrientationEventPumpForTesting() { }
47
48 void OnDidStart(base::SharedMemoryHandle renderer_handle) {
49 DeviceOrientationEventPump::OnDidStart(renderer_handle);
50 }
51 virtual bool SendStartMessage() OVERRIDE { return true; }
52 virtual bool SendStopMessage() OVERRIDE { return true; }
53 };
54
55 class DeviceOrientationEventPumpTest : public testing::Test {
56 public:
57 DeviceOrientationEventPumpTest() {
58 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
59 sizeof(DeviceOrientationHardwareBuffer)));
60 }
61
62 protected:
63 virtual void SetUp() OVERRIDE {
64 listener_.reset(new MockDeviceOrientationListener);
65 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting);
66 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>(
67 shared_memory_.memory());
68 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer));
69 shared_memory_.ShareToProcess(base::kNullProcessHandle, &handle_);
70 }
71
72 void InitBuffer() {
73 WebKit::WebDeviceOrientationData& data = buffer_->data;
74 data.alpha = 1;
75 data.hasAlpha = true;
76 data.beta = 2;
77 data.hasBeta = true;
78 data.gamma = 3;
79 data.hasGamma = true;
80 data.allAvailableSensorsAreActive = true;
81 }
82
83 scoped_ptr<MockDeviceOrientationListener> listener_;
84 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_;
85 base::SharedMemoryHandle handle_;
86 base::SharedMemory shared_memory_;
87 DeviceOrientationHardwareBuffer* buffer_;
88 };
89
90
91 TEST_F(DeviceOrientationEventPumpTest, DoesPolling) {
92 base::MessageLoop loop(base::MessageLoop::TYPE_UI);
93 InitBuffer();
94
95 orientation_pump_->SetListener(listener_.get());
96 orientation_pump_->OnDidStart(handle_);
97 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
98 orientation_pump_->GetDelayMillis() * 2));
99 RunAllPendingInMessageLoop();
100 orientation_pump_->SetListener(0);
101
102 WebKit::WebDeviceOrientationData& received_data = listener_->data_;
103 EXPECT_TRUE(listener_->did_change_device_orientation_);
104 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
105 EXPECT_EQ(1, (double)received_data.alpha);
106 EXPECT_TRUE(received_data.hasAlpha);
107 EXPECT_EQ(2, (double)received_data.beta);
108 EXPECT_TRUE(received_data.hasBeta);
109 EXPECT_EQ(3, (double)received_data.gamma);
110 EXPECT_TRUE(received_data.hasGamma);
111 }
112
113 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) {
114 base::MessageLoop loop(base::MessageLoop::TYPE_UI);
115 InitBuffer();
116
117 orientation_pump_->SetListener(listener_.get());
118 orientation_pump_->OnDidStart(handle_);
119 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
120 orientation_pump_->GetDelayMillis() * 2));
121 RunAllPendingInMessageLoop();
122
123 WebKit::WebDeviceOrientationData& received_data = listener_->data_;
124 EXPECT_TRUE(listener_->did_change_device_orientation_);
125 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
126 EXPECT_EQ(1, (double)received_data.alpha);
127 EXPECT_TRUE(received_data.hasAlpha);
128 EXPECT_EQ(2, (double)received_data.beta);
129 EXPECT_TRUE(received_data.hasBeta);
130 EXPECT_EQ(3, (double)received_data.gamma);
131 EXPECT_TRUE(received_data.hasGamma);
132
133 buffer_->data.alpha =
134 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0;
135 listener_->ResetDidChangeOrientation();
136
137 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
138 orientation_pump_->GetDelayMillis() * 2));
139 RunAllPendingInMessageLoop();
140
141 EXPECT_FALSE(listener_->did_change_device_orientation_);
142 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
143 EXPECT_EQ(1, (double)received_data.alpha);
144 EXPECT_TRUE(received_data.hasAlpha);
145 EXPECT_EQ(2, (double)received_data.beta);
146 EXPECT_TRUE(received_data.hasBeta);
147 EXPECT_EQ(3, (double)received_data.gamma);
148 EXPECT_TRUE(received_data.hasGamma);
149
150 buffer_->data.alpha =
151 1 + DeviceOrientationEventPump::kOrientationThreshold;
152 listener_->ResetDidChangeOrientation();
153
154 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
155 orientation_pump_->GetDelayMillis() * 2));
156 RunAllPendingInMessageLoop();
157 orientation_pump_->SetListener(0);
158
159 EXPECT_TRUE(listener_->did_change_device_orientation_);
160 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold,
161 (double)received_data.alpha);
162 }
163
164 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698