OLD | NEW |
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 "device_motion_event_pump.h" | 5 #include "device_motion_event_pump.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/common/device_sensors/device_motion_hardware_buffer.h" | 10 #include "content/common/device_sensors/device_motion_hardware_buffer.h" |
11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" | 13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
18 public: | 18 public: |
19 MockDeviceMotionListener() : did_change_device_motion_(false) { | 19 MockDeviceMotionListener() |
| 20 : did_change_device_motion_(false), number_of_events_(0) { |
20 memset(&data_, 0, sizeof(data_)); | 21 memset(&data_, 0, sizeof(data_)); |
21 } | 22 } |
22 virtual ~MockDeviceMotionListener() { } | 23 virtual ~MockDeviceMotionListener() { } |
23 | 24 |
24 virtual void didChangeDeviceMotion( | 25 virtual void didChangeDeviceMotion( |
25 const blink::WebDeviceMotionData& data) override { | 26 const blink::WebDeviceMotionData& data) override { |
26 memcpy(&data_, &data, sizeof(data)); | 27 memcpy(&data_, &data, sizeof(data)); |
27 did_change_device_motion_ = true; | 28 did_change_device_motion_ = true; |
| 29 ++number_of_events_; |
28 } | 30 } |
29 | 31 |
30 bool did_change_device_motion() const { | 32 bool did_change_device_motion() const { |
31 return did_change_device_motion_; | 33 return did_change_device_motion_; |
32 } | 34 } |
| 35 |
| 36 int number_of_events() const { return number_of_events_; } |
| 37 |
33 const blink::WebDeviceMotionData& data() const { | 38 const blink::WebDeviceMotionData& data() const { |
34 return data_; | 39 return data_; |
35 } | 40 } |
36 | 41 |
37 private: | 42 private: |
38 bool did_change_device_motion_; | 43 bool did_change_device_motion_; |
| 44 int number_of_events_; |
39 blink::WebDeviceMotionData data_; | 45 blink::WebDeviceMotionData data_; |
40 | 46 |
41 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); | 47 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); |
42 }; | 48 }; |
43 | 49 |
44 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { | 50 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
45 public: | 51 public: |
46 DeviceMotionEventPumpForTesting() | 52 DeviceMotionEventPumpForTesting() |
47 : DeviceMotionEventPump(0) { } | 53 : DeviceMotionEventPump(0), stop_on_fire_event_(true) {} |
48 ~DeviceMotionEventPumpForTesting() override {} | 54 ~DeviceMotionEventPumpForTesting() override {} |
49 | 55 |
| 56 void set_stop_on_fire_event(bool stop_on_fire_event) { |
| 57 stop_on_fire_event_ = stop_on_fire_event; |
| 58 } |
| 59 |
| 60 bool stop_on_fire_event() { return stop_on_fire_event_; } |
| 61 |
| 62 int pump_delay_microseconds() const { return pump_delay_microseconds_; } |
| 63 |
50 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 64 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
51 DeviceMotionEventPump::OnDidStart(renderer_handle); | 65 DeviceMotionEventPump::OnDidStart(renderer_handle); |
52 } | 66 } |
53 void SendStartMessage() override {} | 67 void SendStartMessage() override {} |
54 void SendStopMessage() override {} | 68 void SendStopMessage() override {} |
55 void FireEvent() override { | 69 void FireEvent() override { |
56 DeviceMotionEventPump::FireEvent(); | 70 DeviceMotionEventPump::FireEvent(); |
57 Stop(); | 71 if (stop_on_fire_event_) { |
58 base::MessageLoop::current()->QuitWhenIdle(); | 72 Stop(); |
| 73 base::MessageLoop::current()->QuitWhenIdle(); |
| 74 } |
59 } | 75 } |
60 | 76 |
61 private: | 77 private: |
| 78 bool stop_on_fire_event_; |
| 79 |
62 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); | 80 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); |
63 }; | 81 }; |
64 | 82 |
65 class DeviceMotionEventPumpTest : public testing::Test { | 83 class DeviceMotionEventPumpTest : public testing::Test { |
66 public: | 84 public: |
67 DeviceMotionEventPumpTest() { | 85 DeviceMotionEventPumpTest() { |
68 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | 86 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
69 sizeof(DeviceMotionHardwareBuffer))); | 87 sizeof(DeviceMotionHardwareBuffer))); |
70 } | 88 } |
71 | 89 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 EXPECT_FALSE(received_data.hasAccelerationY); | 169 EXPECT_FALSE(received_data.hasAccelerationY); |
152 EXPECT_FALSE(received_data.hasAccelerationZ); | 170 EXPECT_FALSE(received_data.hasAccelerationZ); |
153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 171 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 172 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 173 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
156 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 174 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
157 EXPECT_FALSE(received_data.hasRotationRateBeta); | 175 EXPECT_FALSE(received_data.hasRotationRateBeta); |
158 EXPECT_FALSE(received_data.hasRotationRateGamma); | 176 EXPECT_FALSE(received_data.hasRotationRateGamma); |
159 } | 177 } |
160 | 178 |
| 179 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate |
| 180 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) |
| 181 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { |
| 182 // Confirm that the delay for pumping events is 60 Hz. |
| 183 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / |
| 184 motion_pump()->pump_delay_microseconds()); |
| 185 |
| 186 base::MessageLoopForUI loop; |
| 187 |
| 188 InitBuffer(true); |
| 189 |
| 190 motion_pump()->set_stop_on_fire_event(false); |
| 191 motion_pump()->Start(listener()); |
| 192 motion_pump()->OnDidStart(handle()); |
| 193 |
| 194 base::MessageLoop::current()->PostDelayedTask( |
| 195 FROM_HERE, base::MessageLoop::QuitClosure(), |
| 196 base::TimeDelta::FromMilliseconds(100)); |
| 197 base::MessageLoop::current()->Run(); |
| 198 motion_pump()->Stop(); |
| 199 |
| 200 // Check that the blink::WebDeviceMotionListener does not receive excess |
| 201 // events. |
| 202 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 203 EXPECT_GE(6, listener()->number_of_events()); |
| 204 } |
| 205 |
161 } // namespace content | 206 } // namespace content |
OLD | NEW |