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

Side by Side Diff: content/renderer/device_sensors/device_motion_event_pump_unittest.cc

Issue 2037513002: Convert device_sensors to use mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@conversion--mime-registry
Patch Set: Created 4 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/device_sensors/device_motion_event_pump.h" 5 #include "content/renderer/device_sensors/device_motion_event_pump.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "content/common/device_sensors/device_motion_hardware_buffer.h" 16 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
17 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
18 #include "mojo/public/cpp/system/buffer.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h" 20 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h"
20 21
21 namespace content { 22 namespace content {
22 23
23 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { 24 class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
24 public: 25 public:
25 MockDeviceMotionListener() 26 MockDeviceMotionListener()
26 : did_change_device_motion_(false), number_of_events_(0) { 27 : did_change_device_motion_(false), number_of_events_(0) {
27 memset(&data_, 0, sizeof(data_)); 28 memset(&data_, 0, sizeof(data_));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 ~DeviceMotionEventPumpForTesting() override {} 60 ~DeviceMotionEventPumpForTesting() override {}
60 61
61 void set_stop_on_fire_event(bool stop_on_fire_event) { 62 void set_stop_on_fire_event(bool stop_on_fire_event) {
62 stop_on_fire_event_ = stop_on_fire_event; 63 stop_on_fire_event_ = stop_on_fire_event;
63 } 64 }
64 65
65 bool stop_on_fire_event() { return stop_on_fire_event_; } 66 bool stop_on_fire_event() { return stop_on_fire_event_; }
66 67
67 int pump_delay_microseconds() const { return pump_delay_microseconds_; } 68 int pump_delay_microseconds() const { return pump_delay_microseconds_; }
68 69
69 void OnDidStart(base::SharedMemoryHandle renderer_handle) { 70 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) {
70 DeviceMotionEventPump::OnDidStart(renderer_handle); 71 DeviceMotionEventPump::DidStart(std::move(renderer_handle));
71 } 72 }
72 void SendStartMessage() override {} 73 void SendStartMessage() override {}
73 void SendStopMessage() override {} 74 void SendStopMessage() override {}
74 void FireEvent() override { 75 void FireEvent() override {
75 DeviceMotionEventPump::FireEvent(); 76 DeviceMotionEventPump::FireEvent();
76 if (stop_on_fire_event_) { 77 if (stop_on_fire_event_) {
77 Stop(); 78 Stop();
78 base::MessageLoop::current()->QuitWhenIdle(); 79 base::MessageLoop::current()->QuitWhenIdle();
79 } 80 }
80 } 81 }
81 82
82 private: 83 private:
83 bool stop_on_fire_event_; 84 bool stop_on_fire_event_;
84 85
85 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); 86 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting);
86 }; 87 };
87 88
88 class DeviceMotionEventPumpTest : public testing::Test { 89 class DeviceMotionEventPumpTest : public testing::Test {
89 public: 90 public:
90 DeviceMotionEventPumpTest() { 91 DeviceMotionEventPumpTest() = default;
91 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous(
92 sizeof(DeviceMotionHardwareBuffer)));
93 }
94 92
95 protected: 93 protected:
96 void SetUp() override { 94 void SetUp() override {
97 const DeviceMotionHardwareBuffer* null_buffer = nullptr;
98 listener_.reset(new MockDeviceMotionListener); 95 listener_.reset(new MockDeviceMotionListener);
99 motion_pump_.reset(new DeviceMotionEventPumpForTesting); 96 motion_pump_.reset(new DeviceMotionEventPumpForTesting);
100 buffer_ = static_cast<DeviceMotionHardwareBuffer*>(shared_memory_.memory()); 97 shared_memory_ =
101 ASSERT_NE(null_buffer, buffer_); 98 mojo::SharedBufferHandle::Create(sizeof(DeviceMotionHardwareBuffer));
102 memset(buffer_, 0, sizeof(DeviceMotionHardwareBuffer)); 99 mapping_ = shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer));
103 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), 100 ASSERT_TRUE(mapping_);
104 &handle_)); 101 memset(buffer(), 0, sizeof(DeviceMotionHardwareBuffer));
105 } 102 }
106 103
107 void InitBuffer(bool allAvailableSensorsActive) { 104 void InitBuffer(bool allAvailableSensorsActive) {
108 blink::WebDeviceMotionData& data = buffer_->data; 105 blink::WebDeviceMotionData& data = buffer()->data;
109 data.accelerationX = 1; 106 data.accelerationX = 1;
110 data.hasAccelerationX = true; 107 data.hasAccelerationX = true;
111 data.accelerationY = 2; 108 data.accelerationY = 2;
112 data.hasAccelerationY = true; 109 data.hasAccelerationY = true;
113 data.accelerationZ = 3; 110 data.accelerationZ = 3;
114 data.hasAccelerationZ = true; 111 data.hasAccelerationZ = true;
115 data.allAvailableSensorsAreActive = allAvailableSensorsActive; 112 data.allAvailableSensorsAreActive = allAvailableSensorsActive;
116 } 113 }
117 114
118 MockDeviceMotionListener* listener() { return listener_.get(); } 115 MockDeviceMotionListener* listener() { return listener_.get(); }
119 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } 116 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); }
120 base::SharedMemoryHandle handle() { return handle_; } 117 mojo::ScopedSharedBufferHandle handle() {
118 return shared_memory_->Clone(
119 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
120 }
121 DeviceMotionHardwareBuffer* buffer() {
122 return reinterpret_cast<DeviceMotionHardwareBuffer*>(mapping_.get());
123 }
121 124
122 private: 125 private:
126 base::MessageLoop loop_;
123 std::unique_ptr<MockDeviceMotionListener> listener_; 127 std::unique_ptr<MockDeviceMotionListener> listener_;
124 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; 128 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_;
125 base::SharedMemoryHandle handle_; 129 mojo::ScopedSharedBufferHandle shared_memory_;
126 base::SharedMemory shared_memory_; 130 mojo::ScopedSharedBufferMapping mapping_;
127 DeviceMotionHardwareBuffer* buffer_;
128 131
129 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); 132 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest);
130 }; 133 };
131 134
132 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { 135 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
133 base::MessageLoopForUI loop;
134
135 InitBuffer(true); 136 InitBuffer(true);
136 137
137 motion_pump()->Start(listener()); 138 motion_pump()->Start(listener());
138 motion_pump()->OnDidStart(handle()); 139 motion_pump()->DidStart(handle());
139 140
140 base::MessageLoop::current()->Run(); 141 base::MessageLoop::current()->Run();
141 142
142 const blink::WebDeviceMotionData& received_data = listener()->data(); 143 const blink::WebDeviceMotionData& received_data = listener()->data();
143 EXPECT_TRUE(listener()->did_change_device_motion()); 144 EXPECT_TRUE(listener()->did_change_device_motion());
144 EXPECT_TRUE(received_data.hasAccelerationX); 145 EXPECT_TRUE(received_data.hasAccelerationX);
145 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); 146 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX));
146 EXPECT_TRUE(received_data.hasAccelerationX); 147 EXPECT_TRUE(received_data.hasAccelerationX);
147 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); 148 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY));
148 EXPECT_TRUE(received_data.hasAccelerationY); 149 EXPECT_TRUE(received_data.hasAccelerationY);
149 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); 150 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ));
150 EXPECT_TRUE(received_data.hasAccelerationZ); 151 EXPECT_TRUE(received_data.hasAccelerationZ);
151 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
154 EXPECT_FALSE(received_data.hasRotationRateAlpha); 155 EXPECT_FALSE(received_data.hasRotationRateAlpha);
155 EXPECT_FALSE(received_data.hasRotationRateBeta); 156 EXPECT_FALSE(received_data.hasRotationRateBeta);
156 EXPECT_FALSE(received_data.hasRotationRateGamma); 157 EXPECT_FALSE(received_data.hasRotationRateGamma);
157 } 158 }
158 159
159 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { 160 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
160 base::MessageLoopForUI loop;
161
162 InitBuffer(false); 161 InitBuffer(false);
163 162
164 motion_pump()->Start(listener()); 163 motion_pump()->Start(listener());
165 motion_pump()->OnDidStart(handle()); 164 motion_pump()->DidStart(handle());
166 165
167 base::MessageLoop::current()->Run(); 166 base::MessageLoop::current()->Run();
168 167
169 const blink::WebDeviceMotionData& received_data = listener()->data(); 168 const blink::WebDeviceMotionData& received_data = listener()->data();
170 // No change in device motion because allAvailableSensorsAreActive is false. 169 // No change in device motion because allAvailableSensorsAreActive is false.
171 EXPECT_FALSE(listener()->did_change_device_motion()); 170 EXPECT_FALSE(listener()->did_change_device_motion());
172 EXPECT_FALSE(received_data.hasAccelerationX); 171 EXPECT_FALSE(received_data.hasAccelerationX);
173 EXPECT_FALSE(received_data.hasAccelerationX); 172 EXPECT_FALSE(received_data.hasAccelerationX);
174 EXPECT_FALSE(received_data.hasAccelerationY); 173 EXPECT_FALSE(received_data.hasAccelerationY);
175 EXPECT_FALSE(received_data.hasAccelerationZ); 174 EXPECT_FALSE(received_data.hasAccelerationZ);
176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); 175 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX);
177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); 176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY);
178 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); 177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ);
179 EXPECT_FALSE(received_data.hasRotationRateAlpha); 178 EXPECT_FALSE(received_data.hasRotationRateAlpha);
180 EXPECT_FALSE(received_data.hasRotationRateBeta); 179 EXPECT_FALSE(received_data.hasRotationRateBeta);
181 EXPECT_FALSE(received_data.hasRotationRateGamma); 180 EXPECT_FALSE(received_data.hasRotationRateGamma);
182 } 181 }
183 182
184 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate 183 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate
185 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) 184 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691)
186 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { 185 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) {
187 // Confirm that the delay for pumping events is 60 Hz. 186 // Confirm that the delay for pumping events is 60 Hz.
188 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / 187 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond /
189 motion_pump()->pump_delay_microseconds()); 188 motion_pump()->pump_delay_microseconds());
190 189
191 base::MessageLoopForUI loop;
192
193 InitBuffer(true); 190 InitBuffer(true);
194 191
195 motion_pump()->set_stop_on_fire_event(false); 192 motion_pump()->set_stop_on_fire_event(false);
196 motion_pump()->Start(listener()); 193 motion_pump()->Start(listener());
197 motion_pump()->OnDidStart(handle()); 194 motion_pump()->DidStart(handle());
198 195
199 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 196 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
200 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 197 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
201 base::TimeDelta::FromMilliseconds(100)); 198 base::TimeDelta::FromMilliseconds(100));
202 base::MessageLoop::current()->Run(); 199 base::MessageLoop::current()->Run();
203 motion_pump()->Stop(); 200 motion_pump()->Stop();
204 201
205 // Check that the blink::WebDeviceMotionListener does not receive excess 202 // Check that the blink::WebDeviceMotionListener does not receive excess
206 // events. 203 // events.
207 EXPECT_TRUE(listener()->did_change_device_motion()); 204 EXPECT_TRUE(listener()->did_change_device_motion());
208 EXPECT_GE(6, listener()->number_of_events()); 205 EXPECT_GE(6, listener()->number_of_events());
209 } 206 }
210 207
211 } // namespace content 208 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698