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_ = mojo::SharedBufferHandle::Create( | 96 shared_memory_ = mojo::SharedBufferHandle::Create( |
99 sizeof(device::DeviceMotionHardwareBuffer)); | 97 sizeof(device::DeviceMotionHardwareBuffer)); |
100 mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer)); | 98 mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer)); |
101 ASSERT_TRUE(mapping_); | 99 ASSERT_TRUE(mapping_); |
102 memset(buffer(), 0, sizeof(device::DeviceMotionHardwareBuffer)); | 100 memset(buffer(), 0, sizeof(device::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 18 matching lines...) Expand all Loading... |
135 }; | 133 }; |
136 | 134 |
137 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 135 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { |
138 InitBuffer(true); | 136 InitBuffer(true); |
139 | 137 |
140 motion_pump()->Start(listener()); | 138 motion_pump()->Start(listener()); |
141 motion_pump()->DidStart(handle()); | 139 motion_pump()->DidStart(handle()); |
142 | 140 |
143 base::RunLoop().Run(); | 141 base::RunLoop().Run(); |
144 | 142 |
145 const blink::WebDeviceMotionData& received_data = listener()->data(); | 143 const device::MotionData& received_data = listener()->data(); |
146 EXPECT_TRUE(listener()->did_change_device_motion()); | 144 EXPECT_TRUE(listener()->did_change_device_motion()); |
147 EXPECT_TRUE(received_data.hasAccelerationX); | 145 EXPECT_TRUE(received_data.hasAccelerationX); |
148 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); | 146 EXPECT_EQ(1, static_cast<double>(received_data.accelerationX)); |
149 EXPECT_TRUE(received_data.hasAccelerationX); | 147 EXPECT_TRUE(received_data.hasAccelerationX); |
150 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); | 148 EXPECT_EQ(2, static_cast<double>(received_data.accelerationY)); |
151 EXPECT_TRUE(received_data.hasAccelerationY); | 149 EXPECT_TRUE(received_data.hasAccelerationY); |
152 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); | 150 EXPECT_EQ(3, static_cast<double>(received_data.accelerationZ)); |
153 EXPECT_TRUE(received_data.hasAccelerationZ); | 151 EXPECT_TRUE(received_data.hasAccelerationZ); |
154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 152 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
155 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 153 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
156 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 154 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
157 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 155 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
158 EXPECT_FALSE(received_data.hasRotationRateBeta); | 156 EXPECT_FALSE(received_data.hasRotationRateBeta); |
159 EXPECT_FALSE(received_data.hasRotationRateGamma); | 157 EXPECT_FALSE(received_data.hasRotationRateGamma); |
160 } | 158 } |
161 | 159 |
162 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { | 160 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) { |
163 InitBuffer(false); | 161 InitBuffer(false); |
164 | 162 |
165 motion_pump()->Start(listener()); | 163 motion_pump()->Start(listener()); |
166 motion_pump()->DidStart(handle()); | 164 motion_pump()->DidStart(handle()); |
167 | 165 |
168 base::RunLoop().Run(); | 166 base::RunLoop().Run(); |
169 | 167 |
170 const blink::WebDeviceMotionData& received_data = listener()->data(); | 168 const device::MotionData& received_data = listener()->data(); |
171 // No change in device motion because allAvailableSensorsAreActive is false. | 169 // No change in device motion because allAvailableSensorsAreActive is false. |
172 EXPECT_FALSE(listener()->did_change_device_motion()); | 170 EXPECT_FALSE(listener()->did_change_device_motion()); |
173 EXPECT_FALSE(received_data.hasAccelerationX); | 171 EXPECT_FALSE(received_data.hasAccelerationX); |
174 EXPECT_FALSE(received_data.hasAccelerationX); | 172 EXPECT_FALSE(received_data.hasAccelerationX); |
175 EXPECT_FALSE(received_data.hasAccelerationY); | 173 EXPECT_FALSE(received_data.hasAccelerationY); |
176 EXPECT_FALSE(received_data.hasAccelerationZ); | 174 EXPECT_FALSE(received_data.hasAccelerationZ); |
177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); | 175 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityX); |
178 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); | 176 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityY); |
179 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); | 177 EXPECT_FALSE(received_data.hasAccelerationIncludingGravityZ); |
180 EXPECT_FALSE(received_data.hasRotationRateAlpha); | 178 EXPECT_FALSE(received_data.hasRotationRateAlpha); |
(...skipping 20 matching lines...) Expand all Loading... |
201 base::RunLoop().Run(); | 199 base::RunLoop().Run(); |
202 motion_pump()->Stop(); | 200 motion_pump()->Stop(); |
203 | 201 |
204 // Check that the blink::WebDeviceMotionListener does not receive excess | 202 // Check that the blink::WebDeviceMotionListener does not receive excess |
205 // events. | 203 // events. |
206 EXPECT_TRUE(listener()->did_change_device_motion()); | 204 EXPECT_TRUE(listener()->did_change_device_motion()); |
207 EXPECT_GE(6, listener()->number_of_events()); | 205 EXPECT_GE(6, listener()->number_of_events()); |
208 } | 206 } |
209 | 207 |
210 } // namespace content | 208 } // namespace content |
OLD | NEW |