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 "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 |
(...skipping 12 matching lines...) Expand all Loading... |
23 namespace content { | 23 namespace content { |
24 | 24 |
25 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { | 25 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { |
26 public: | 26 public: |
27 MockDeviceMotionListener() | 27 MockDeviceMotionListener() |
28 : did_change_device_motion_(false), number_of_events_(0) { | 28 : did_change_device_motion_(false), number_of_events_(0) { |
29 memset(&data_, 0, sizeof(data_)); | 29 memset(&data_, 0, sizeof(data_)); |
30 } | 30 } |
31 ~MockDeviceMotionListener() override {} | 31 ~MockDeviceMotionListener() override {} |
32 | 32 |
33 void didChangeDeviceMotion(const blink::WebDeviceMotionData& data) override { | 33 void didChangeDeviceMotion(const device::MotionData& data) override { |
34 memcpy(&data_, &data, sizeof(data)); | 34 memcpy(&data_, &data, sizeof(data)); |
35 did_change_device_motion_ = true; | 35 did_change_device_motion_ = true; |
36 ++number_of_events_; | 36 ++number_of_events_; |
37 } | 37 } |
38 | 38 |
39 bool did_change_device_motion() const { | 39 bool did_change_device_motion() const { |
40 return did_change_device_motion_; | 40 return did_change_device_motion_; |
41 } | 41 } |
42 | 42 |
43 int number_of_events() const { return number_of_events_; } | 43 int number_of_events() const { return number_of_events_; } |
44 | 44 |
45 const blink::WebDeviceMotionData& data() const { | 45 const device::MotionData& data() const { return data_; } |
46 return data_; | |
47 } | |
48 | 46 |
49 private: | 47 private: |
50 bool did_change_device_motion_; | 48 bool did_change_device_motion_; |
51 int number_of_events_; | 49 int number_of_events_; |
52 blink::WebDeviceMotionData data_; | 50 device::MotionData data_; |
53 | 51 |
54 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); | 52 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); |
55 }; | 53 }; |
56 | 54 |
57 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { | 55 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { |
58 public: | 56 public: |
59 DeviceMotionEventPumpForTesting() | 57 DeviceMotionEventPumpForTesting() |
60 : DeviceMotionEventPump(0), stop_on_fire_event_(true) {} | 58 : DeviceMotionEventPump(0), stop_on_fire_event_(true) {} |
61 ~DeviceMotionEventPumpForTesting() override {} | 59 ~DeviceMotionEventPumpForTesting() override {} |
62 | 60 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 listener_.reset(new MockDeviceMotionListener); | 94 listener_.reset(new MockDeviceMotionListener); |
97 motion_pump_.reset(new DeviceMotionEventPumpForTesting); | 95 motion_pump_.reset(new DeviceMotionEventPumpForTesting); |
98 shared_memory_ = | 96 shared_memory_ = |
99 mojo::SharedBufferHandle::Create(sizeof(DeviceMotionHardwareBuffer)); | 97 mojo::SharedBufferHandle::Create(sizeof(DeviceMotionHardwareBuffer)); |
100 mapping_ = shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer)); | 98 mapping_ = shared_memory_->Map(sizeof(DeviceMotionHardwareBuffer)); |
101 ASSERT_TRUE(mapping_); | 99 ASSERT_TRUE(mapping_); |
102 memset(buffer(), 0, sizeof(DeviceMotionHardwareBuffer)); | 100 memset(buffer(), 0, sizeof(DeviceMotionHardwareBuffer)); |
103 } | 101 } |
104 | 102 |
105 void InitBuffer(bool allAvailableSensorsActive) { | 103 void InitBuffer(bool allAvailableSensorsActive) { |
106 blink::WebDeviceMotionData& data = buffer()->data; | 104 device::MotionData& data = buffer()->data; |
107 data.accelerationX = 1; | 105 data.accelerationX = 1; |
108 data.hasAccelerationX = true; | 106 data.hasAccelerationX = true; |
109 data.accelerationY = 2; | 107 data.accelerationY = 2; |
110 data.hasAccelerationY = true; | 108 data.hasAccelerationY = true; |
111 data.accelerationZ = 3; | 109 data.accelerationZ = 3; |
112 data.hasAccelerationZ = true; | 110 data.hasAccelerationZ = true; |
113 data.allAvailableSensorsAreActive = allAvailableSensorsActive; | 111 data.allAvailableSensorsAreActive = allAvailableSensorsActive; |
114 } | 112 } |
115 | 113 |
116 MockDeviceMotionListener* listener() { return listener_.get(); } | 114 MockDeviceMotionListener* listener() { return listener_.get(); } |
(...skipping 17 matching lines...) Expand all Loading... |
134 }; | 132 }; |
135 | 133 |
136 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 134 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { |
137 InitBuffer(true); | 135 InitBuffer(true); |
138 | 136 |
139 motion_pump()->Start(listener()); | 137 motion_pump()->Start(listener()); |
140 motion_pump()->DidStart(handle()); | 138 motion_pump()->DidStart(handle()); |
141 | 139 |
142 base::RunLoop().Run(); | 140 base::RunLoop().Run(); |
143 | 141 |
144 const blink::WebDeviceMotionData& received_data = listener()->data(); | 142 const device::MotionData& received_data = listener()->data(); |
145 EXPECT_TRUE(listener()->did_change_device_motion()); | 143 EXPECT_TRUE(listener()->did_change_device_motion()); |
146 EXPECT_TRUE(received_data.hasAccelerationX); | 144 EXPECT_TRUE(received_data.hasAccelerationX); |
147 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); | 145 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); |
148 EXPECT_TRUE(received_data.hasAccelerationX); | 146 EXPECT_TRUE(received_data.hasAccelerationX); |
149 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); | 147 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); |
150 EXPECT_TRUE(received_data.hasAccelerationY); | 148 EXPECT_TRUE(received_data.hasAccelerationY); |
151 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); | 149 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); |
152 EXPECT_TRUE(received_data.hasAccelerationZ); | 150 EXPECT_TRUE(received_data.hasAccelerationZ); |
153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 151 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
156 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 154 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
157 EXPECT_FALSE(received_data.hasRotationRateBeta); | 155 EXPECT_FALSE(received_data.hasRotationRateBeta); |
158 EXPECT_FALSE(received_data.hasRotationRateGamma); | 156 EXPECT_FALSE(received_data.hasRotationRateGamma); |
159 } | 157 } |
160 | 158 |
161 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { | 159 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { |
162 InitBuffer(false); | 160 InitBuffer(false); |
163 | 161 |
164 motion_pump()->Start(listener()); | 162 motion_pump()->Start(listener()); |
165 motion_pump()->DidStart(handle()); | 163 motion_pump()->DidStart(handle()); |
166 | 164 |
167 base::RunLoop().Run(); | 165 base::RunLoop().Run(); |
168 | 166 |
169 const blink::WebDeviceMotionData& received_data = listener()->data(); | 167 const device::MotionData& received_data = listener()->data(); |
170 // No change in device motion because allAvailableSensorsAreActive is false. | 168 // No change in device motion because allAvailableSensorsAreActive is false. |
171 EXPECT_FALSE(listener()->did_change_device_motion()); | 169 EXPECT_FALSE(listener()->did_change_device_motion()); |
172 EXPECT_FALSE(received_data.hasAccelerationX); | 170 EXPECT_FALSE(received_data.hasAccelerationX); |
173 EXPECT_FALSE(received_data.hasAccelerationX); | 171 EXPECT_FALSE(received_data.hasAccelerationX); |
174 EXPECT_FALSE(received_data.hasAccelerationY); | 172 EXPECT_FALSE(received_data.hasAccelerationY); |
175 EXPECT_FALSE(received_data.hasAccelerationZ); | 173 EXPECT_FALSE(received_data.hasAccelerationZ); |
176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 174 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 175 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
178 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
179 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 177 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
(...skipping 20 matching lines...) Expand all Loading... |
200 base::RunLoop().Run(); | 198 base::RunLoop().Run(); |
201 motion_pump()->Stop(); | 199 motion_pump()->Stop(); |
202 | 200 |
203 // Check that the blink::WebDeviceMotionListener does not receive excess | 201 // Check that the blink::WebDeviceMotionListener does not receive excess |
204 // events. | 202 // events. |
205 EXPECT_TRUE(listener()->did_change_device_motion()); | 203 EXPECT_TRUE(listener()->did_change_device_motion()); |
206 EXPECT_GE(6, listener()->number_of_events()); | 204 EXPECT_GE(6, listener()->number_of_events()); |
207 } | 205 } |
208 | 206 |
209 } // namespace content | 207 } // namespace content |
OLD | NEW |