Chromium Code Reviews| 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 "content/renderer/device_sensors/device_sensor_event_pump.h" | |
|
timvolodine
2015/02/04 13:30:18
is this needed?
jonross
2015/02/04 14:52:55
Nope, forgot to remove that after testing
| |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" | 14 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 18 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
| 18 public: | 19 public: |
| 19 MockDeviceMotionListener() : did_change_device_motion_(false) { | 20 MockDeviceMotionListener() |
| 21 : did_change_device_motion_(false), number_of_events_(0) { | |
| 20 memset(&data_, 0, sizeof(data_)); | 22 memset(&data_, 0, sizeof(data_)); |
| 21 } | 23 } |
| 22 virtual ~MockDeviceMotionListener() { } | 24 virtual ~MockDeviceMotionListener() { } |
| 23 | 25 |
| 24 virtual void didChangeDeviceMotion( | 26 virtual void didChangeDeviceMotion( |
| 25 const blink::WebDeviceMotionData& data) override { | 27 const blink::WebDeviceMotionData& data) override { |
| 26 memcpy(&data_, &data, sizeof(data)); | 28 memcpy(&data_, &data, sizeof(data)); |
| 27 did_change_device_motion_ = true; | 29 did_change_device_motion_ = true; |
| 30 ++number_of_events_; | |
| 28 } | 31 } |
| 29 | 32 |
| 30 bool did_change_device_motion() const { | 33 bool did_change_device_motion() const { |
| 31 return did_change_device_motion_; | 34 return did_change_device_motion_; |
| 32 } | 35 } |
| 36 | |
| 37 int number_of_events() const { return number_of_events_; } | |
| 38 | |
| 33 const blink::WebDeviceMotionData& data() const { | 39 const blink::WebDeviceMotionData& data() const { |
| 34 return data_; | 40 return data_; |
| 35 } | 41 } |
| 36 | 42 |
| 37 private: | 43 private: |
| 38 bool did_change_device_motion_; | 44 bool did_change_device_motion_; |
| 45 int number_of_events_; | |
| 39 blink::WebDeviceMotionData data_; | 46 blink::WebDeviceMotionData data_; |
| 40 | 47 |
| 41 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); | 48 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); |
| 42 }; | 49 }; |
| 43 | 50 |
| 44 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { | 51 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
| 45 public: | 52 public: |
| 46 DeviceMotionEventPumpForTesting() | 53 DeviceMotionEventPumpForTesting() |
| 47 : DeviceMotionEventPump(0) { } | 54 : DeviceMotionEventPump(0), stop_on_fire_event_(true) {} |
| 48 ~DeviceMotionEventPumpForTesting() override {} | 55 ~DeviceMotionEventPumpForTesting() override {} |
| 49 | 56 |
| 57 void set_stop_on_fire_event(bool stop_on_fire_event) { | |
| 58 stop_on_fire_event_ = stop_on_fire_event; | |
| 59 } | |
| 60 | |
| 61 bool stop_on_fire_event() { return stop_on_fire_event_; } | |
| 62 | |
| 63 int pump_delay_microseconds() const { return pump_delay_microseconds_; } | |
| 64 | |
| 50 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 65 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 51 DeviceMotionEventPump::OnDidStart(renderer_handle); | 66 DeviceMotionEventPump::OnDidStart(renderer_handle); |
| 52 } | 67 } |
| 53 void SendStartMessage() override {} | 68 void SendStartMessage() override {} |
| 54 void SendStopMessage() override {} | 69 void SendStopMessage() override {} |
| 55 void FireEvent() override { | 70 void FireEvent() override { |
| 56 DeviceMotionEventPump::FireEvent(); | 71 DeviceMotionEventPump::FireEvent(); |
| 57 Stop(); | 72 if (stop_on_fire_event_) { |
| 58 base::MessageLoop::current()->QuitWhenIdle(); | 73 Stop(); |
| 74 base::MessageLoop::current()->QuitWhenIdle(); | |
| 75 } | |
| 59 } | 76 } |
| 60 | 77 |
| 61 private: | 78 private: |
| 79 bool stop_on_fire_event_; | |
| 80 | |
| 62 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); | 81 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); |
| 63 }; | 82 }; |
| 64 | 83 |
| 65 class DeviceMotionEventPumpTest : public testing::Test { | 84 class DeviceMotionEventPumpTest : public testing::Test { |
| 66 public: | 85 public: |
| 67 DeviceMotionEventPumpTest() { | 86 DeviceMotionEventPumpTest() { |
| 68 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | 87 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 69 sizeof(DeviceMotionHardwareBuffer))); | 88 sizeof(DeviceMotionHardwareBuffer))); |
| 70 } | 89 } |
| 71 | 90 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 EXPECT_FALSE(received_data.hasAccelerationY); | 170 EXPECT_FALSE(received_data.hasAccelerationY); |
| 152 EXPECT_FALSE(received_data.hasAccelerationZ); | 171 EXPECT_FALSE(received_data.hasAccelerationZ); |
| 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 172 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
| 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 173 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
| 155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 174 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
| 156 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 175 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
| 157 EXPECT_FALSE(received_data.hasRotationRateBeta); | 176 EXPECT_FALSE(received_data.hasRotationRateBeta); |
| 158 EXPECT_FALSE(received_data.hasRotationRateGamma); | 177 EXPECT_FALSE(received_data.hasRotationRateGamma); |
| 159 } | 178 } |
| 160 | 179 |
| 180 // Tests that the pump is not firing events at a rate faster than 60Hz. | |
| 181 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { | |
| 182 // Confirm that the delay for pumping events is 60 Hz. | |
| 183 EXPECT_EQ(60, base::Time::kMicrosecondsPerSecond / | |
|
timvolodine
2015/02/04 13:30:18
should this better be EXPECT_GE?
jonross
2015/02/04 14:52:55
Done.
| |
| 184 motion_pump()->pump_delay_microseconds()); | |
|
timvolodine
2015/02/04 13:30:17
indentation
jonross
2015/02/04 14:52:55
Done.
| |
| 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 |