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_orientation_event_pump.h" | 5 #include "device_orientation_event_pump.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 class MockDeviceOrientationListener | 22 class MockDeviceOrientationListener |
23 : public blink::WebDeviceOrientationListener { | 23 : public blink::WebDeviceOrientationListener { |
24 public: | 24 public: |
25 MockDeviceOrientationListener() : did_change_device_orientation_(false) { | 25 MockDeviceOrientationListener() : did_change_device_orientation_(false) { |
26 memset(&data_, 0, sizeof(data_)); | 26 memset(&data_, 0, sizeof(data_)); |
27 } | 27 } |
28 ~MockDeviceOrientationListener() override {} | 28 ~MockDeviceOrientationListener() override {} |
29 | 29 |
30 void didChangeDeviceOrientation( | 30 void didChangeDeviceOrientation( |
31 const blink::WebDeviceOrientationData& data) override { | 31 const device::OrientationData& data) override { |
32 memcpy(&data_, &data, sizeof(data)); | 32 memcpy(&data_, &data, sizeof(data)); |
33 did_change_device_orientation_ = true; | 33 did_change_device_orientation_ = true; |
34 } | 34 } |
35 | 35 |
36 bool did_change_device_orientation() const { | 36 bool did_change_device_orientation() const { |
37 return did_change_device_orientation_; | 37 return did_change_device_orientation_; |
38 } | 38 } |
39 void set_did_change_device_orientation(bool value) { | 39 void set_did_change_device_orientation(bool value) { |
40 did_change_device_orientation_ = value; | 40 did_change_device_orientation_ = value; |
41 } | 41 } |
42 const blink::WebDeviceOrientationData& data() const { | 42 const device::OrientationData& data() const { return data_; } |
43 return data_; | |
44 } | |
45 | 43 |
46 private: | 44 private: |
47 bool did_change_device_orientation_; | 45 bool did_change_device_orientation_; |
48 blink::WebDeviceOrientationData data_; | 46 device::OrientationData data_; |
49 | 47 |
50 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); | 48 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); |
51 }; | 49 }; |
52 | 50 |
53 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { | 51 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { |
54 public: | 52 public: |
55 DeviceOrientationEventPumpForTesting() | 53 DeviceOrientationEventPumpForTesting() |
56 : DeviceOrientationEventPump(nullptr) {} | 54 : DeviceOrientationEventPump(nullptr) {} |
57 ~DeviceOrientationEventPumpForTesting() override {} | 55 ~DeviceOrientationEventPumpForTesting() override {} |
58 | 56 |
(...skipping 21 matching lines...) Expand all Loading... |
80 listener_.reset(new MockDeviceOrientationListener); | 78 listener_.reset(new MockDeviceOrientationListener); |
81 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); | 79 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); |
82 shared_memory_ = mojo::SharedBufferHandle::Create( | 80 shared_memory_ = mojo::SharedBufferHandle::Create( |
83 sizeof(DeviceOrientationHardwareBuffer)); | 81 sizeof(DeviceOrientationHardwareBuffer)); |
84 mapping_ = shared_memory_->Map(sizeof(DeviceOrientationHardwareBuffer)); | 82 mapping_ = shared_memory_->Map(sizeof(DeviceOrientationHardwareBuffer)); |
85 ASSERT_TRUE(mapping_); | 83 ASSERT_TRUE(mapping_); |
86 memset(buffer(), 0, sizeof(DeviceOrientationHardwareBuffer)); | 84 memset(buffer(), 0, sizeof(DeviceOrientationHardwareBuffer)); |
87 } | 85 } |
88 | 86 |
89 void InitBuffer() { | 87 void InitBuffer() { |
90 blink::WebDeviceOrientationData& data = buffer()->data; | 88 device::OrientationData& data = buffer()->data; |
91 data.alpha = 1; | 89 data.alpha = 1; |
92 data.hasAlpha = true; | 90 data.hasAlpha = true; |
93 data.beta = 2; | 91 data.beta = 2; |
94 data.hasBeta = true; | 92 data.hasBeta = true; |
95 data.gamma = 3; | 93 data.gamma = 3; |
96 data.hasGamma = true; | 94 data.hasGamma = true; |
97 data.allAvailableSensorsAreActive = true; | 95 data.allAvailableSensorsAreActive = true; |
98 } | 96 } |
99 | 97 |
100 void InitBufferNoData() { | 98 void InitBufferNoData() { |
101 blink::WebDeviceOrientationData& data = buffer()->data; | 99 device::OrientationData& data = buffer()->data; |
102 data.allAvailableSensorsAreActive = true; | 100 data.allAvailableSensorsAreActive = true; |
103 } | 101 } |
104 | 102 |
105 MockDeviceOrientationListener* listener() { return listener_.get(); } | 103 MockDeviceOrientationListener* listener() { return listener_.get(); } |
106 DeviceOrientationEventPumpForTesting* orientation_pump() { | 104 DeviceOrientationEventPumpForTesting* orientation_pump() { |
107 return orientation_pump_.get(); | 105 return orientation_pump_.get(); |
108 } | 106 } |
109 mojo::ScopedSharedBufferHandle handle() { | 107 mojo::ScopedSharedBufferHandle handle() { |
110 return shared_memory_->Clone( | 108 return shared_memory_->Clone( |
111 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | 109 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
(...skipping 12 matching lines...) Expand all Loading... |
124 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); | 122 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
125 }; | 123 }; |
126 | 124 |
127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 125 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
128 InitBuffer(); | 126 InitBuffer(); |
129 orientation_pump()->Start(listener()); | 127 orientation_pump()->Start(listener()); |
130 orientation_pump()->DidStart(handle()); | 128 orientation_pump()->DidStart(handle()); |
131 | 129 |
132 base::RunLoop().Run(); | 130 base::RunLoop().Run(); |
133 | 131 |
134 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 132 const device::OrientationData& received_data = listener()->data(); |
135 EXPECT_TRUE(listener()->did_change_device_orientation()); | 133 EXPECT_TRUE(listener()->did_change_device_orientation()); |
136 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 134 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
137 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 135 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
138 EXPECT_TRUE(received_data.hasAlpha); | 136 EXPECT_TRUE(received_data.hasAlpha); |
139 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 137 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
140 EXPECT_TRUE(received_data.hasBeta); | 138 EXPECT_TRUE(received_data.hasBeta); |
141 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 139 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
142 EXPECT_TRUE(received_data.hasGamma); | 140 EXPECT_TRUE(received_data.hasGamma); |
143 } | 141 } |
144 | 142 |
145 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 143 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
146 InitBufferNoData(); | 144 InitBufferNoData(); |
147 orientation_pump()->Start(listener()); | 145 orientation_pump()->Start(listener()); |
148 orientation_pump()->DidStart(handle()); | 146 orientation_pump()->DidStart(handle()); |
149 | 147 |
150 base::RunLoop().Run(); | 148 base::RunLoop().Run(); |
151 | 149 |
152 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 150 const device::OrientationData& received_data = listener()->data(); |
153 EXPECT_TRUE(listener()->did_change_device_orientation()); | 151 EXPECT_TRUE(listener()->did_change_device_orientation()); |
154 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 152 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
155 EXPECT_FALSE(received_data.hasAlpha); | 153 EXPECT_FALSE(received_data.hasAlpha); |
156 EXPECT_FALSE(received_data.hasBeta); | 154 EXPECT_FALSE(received_data.hasBeta); |
157 EXPECT_FALSE(received_data.hasGamma); | 155 EXPECT_FALSE(received_data.hasGamma); |
158 } | 156 } |
159 | 157 |
160 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 158 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
161 InitBuffer(); | 159 InitBuffer(); |
162 orientation_pump()->Start(listener()); | 160 orientation_pump()->Start(listener()); |
163 orientation_pump()->DidStart(handle()); | 161 orientation_pump()->DidStart(handle()); |
164 | 162 |
165 base::RunLoop().Run(); | 163 base::RunLoop().Run(); |
166 | 164 |
167 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 165 const device::OrientationData& received_data = listener()->data(); |
168 EXPECT_TRUE(listener()->did_change_device_orientation()); | 166 EXPECT_TRUE(listener()->did_change_device_orientation()); |
169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 167 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 168 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
171 EXPECT_TRUE(received_data.hasAlpha); | 169 EXPECT_TRUE(received_data.hasAlpha); |
172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 170 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
173 EXPECT_TRUE(received_data.hasBeta); | 171 EXPECT_TRUE(received_data.hasBeta); |
174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 172 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
175 EXPECT_TRUE(received_data.hasGamma); | 173 EXPECT_TRUE(received_data.hasGamma); |
176 | 174 |
177 buffer()->data.alpha = | 175 buffer()->data.alpha = |
(...skipping 28 matching lines...) Expand all Loading... |
206 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 204 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
207 base::Unretained(orientation_pump()))); | 205 base::Unretained(orientation_pump()))); |
208 base::RunLoop().Run(); | 206 base::RunLoop().Run(); |
209 | 207 |
210 EXPECT_TRUE(listener()->did_change_device_orientation()); | 208 EXPECT_TRUE(listener()->did_change_device_orientation()); |
211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 209 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
212 static_cast<double>(received_data.alpha)); | 210 static_cast<double>(received_data.alpha)); |
213 } | 211 } |
214 | 212 |
215 } // namespace content | 213 } // namespace content |
OLD | NEW |