| 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" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | 15 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" |
| 15 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" | 18 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 class MockDeviceOrientationListener | 22 class MockDeviceOrientationListener |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); | 124 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
| 124 }; | 125 }; |
| 125 | 126 |
| 126 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
| 127 base::MessageLoop loop; | 128 base::MessageLoop loop; |
| 128 | 129 |
| 129 InitBuffer(); | 130 InitBuffer(); |
| 130 orientation_pump()->Start(listener()); | 131 orientation_pump()->Start(listener()); |
| 131 orientation_pump()->OnDidStart(handle()); | 132 orientation_pump()->OnDidStart(handle()); |
| 132 | 133 |
| 133 base::MessageLoop::current()->Run(); | 134 base::RunLoop().Run(); |
| 134 | 135 |
| 135 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 136 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 136 EXPECT_TRUE(listener()->did_change_device_orientation()); | 137 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 137 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 138 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 138 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 139 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 139 EXPECT_TRUE(received_data.hasAlpha); | 140 EXPECT_TRUE(received_data.hasAlpha); |
| 140 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 141 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 141 EXPECT_TRUE(received_data.hasBeta); | 142 EXPECT_TRUE(received_data.hasBeta); |
| 142 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 143 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 143 EXPECT_TRUE(received_data.hasGamma); | 144 EXPECT_TRUE(received_data.hasGamma); |
| 144 } | 145 } |
| 145 | 146 |
| 146 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 147 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
| 147 base::MessageLoop loop; | 148 base::MessageLoop loop; |
| 148 | 149 |
| 149 InitBufferNoData(); | 150 InitBufferNoData(); |
| 150 orientation_pump()->Start(listener()); | 151 orientation_pump()->Start(listener()); |
| 151 orientation_pump()->OnDidStart(handle()); | 152 orientation_pump()->OnDidStart(handle()); |
| 152 | 153 |
| 153 base::MessageLoop::current()->Run(); | 154 base::RunLoop().Run(); |
| 154 | 155 |
| 155 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 156 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 156 EXPECT_TRUE(listener()->did_change_device_orientation()); | 157 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 157 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 158 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 158 EXPECT_FALSE(received_data.hasAlpha); | 159 EXPECT_FALSE(received_data.hasAlpha); |
| 159 EXPECT_FALSE(received_data.hasBeta); | 160 EXPECT_FALSE(received_data.hasBeta); |
| 160 EXPECT_FALSE(received_data.hasGamma); | 161 EXPECT_FALSE(received_data.hasGamma); |
| 161 } | 162 } |
| 162 | 163 |
| 163 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 164 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
| 164 base::MessageLoop loop; | 165 base::MessageLoop loop; |
| 165 | 166 |
| 166 InitBuffer(); | 167 InitBuffer(); |
| 167 orientation_pump()->Start(listener()); | 168 orientation_pump()->Start(listener()); |
| 168 orientation_pump()->OnDidStart(handle()); | 169 orientation_pump()->OnDidStart(handle()); |
| 169 | 170 |
| 170 base::MessageLoop::current()->Run(); | 171 base::RunLoop().Run(); |
| 171 | 172 |
| 172 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 173 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 173 EXPECT_TRUE(listener()->did_change_device_orientation()); | 174 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 174 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 175 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 175 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 176 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 176 EXPECT_TRUE(received_data.hasAlpha); | 177 EXPECT_TRUE(received_data.hasAlpha); |
| 177 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 178 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 178 EXPECT_TRUE(received_data.hasBeta); | 179 EXPECT_TRUE(received_data.hasBeta); |
| 179 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 180 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 180 EXPECT_TRUE(received_data.hasGamma); | 181 EXPECT_TRUE(received_data.hasGamma); |
| 181 | 182 |
| 182 buffer()->data.alpha = | 183 buffer()->data.alpha = |
| 183 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; | 184 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; |
| 184 listener()->set_did_change_device_orientation(false); | 185 listener()->set_did_change_device_orientation(false); |
| 185 | 186 |
| 186 // Reset the pump's listener. | 187 // Reset the pump's listener. |
| 187 orientation_pump()->Start(listener()); | 188 orientation_pump()->Start(listener()); |
| 188 | 189 |
| 189 base::ThreadTaskRunnerHandle::Get()->PostTask( | 190 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 190 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 191 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 191 base::Unretained(orientation_pump()))); | 192 base::Unretained(orientation_pump()))); |
| 192 base::MessageLoop::current()->Run(); | 193 base::RunLoop().Run(); |
| 193 | 194 |
| 194 EXPECT_FALSE(listener()->did_change_device_orientation()); | 195 EXPECT_FALSE(listener()->did_change_device_orientation()); |
| 195 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 196 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 196 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 197 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 197 EXPECT_TRUE(received_data.hasAlpha); | 198 EXPECT_TRUE(received_data.hasAlpha); |
| 198 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 199 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 199 EXPECT_TRUE(received_data.hasBeta); | 200 EXPECT_TRUE(received_data.hasBeta); |
| 200 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 201 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 201 EXPECT_TRUE(received_data.hasGamma); | 202 EXPECT_TRUE(received_data.hasGamma); |
| 202 | 203 |
| 203 buffer()->data.alpha = | 204 buffer()->data.alpha = |
| 204 1 + DeviceOrientationEventPump::kOrientationThreshold; | 205 1 + DeviceOrientationEventPump::kOrientationThreshold; |
| 205 listener()->set_did_change_device_orientation(false); | 206 listener()->set_did_change_device_orientation(false); |
| 206 | 207 |
| 207 // Reset the pump's listener. | 208 // Reset the pump's listener. |
| 208 orientation_pump()->Start(listener()); | 209 orientation_pump()->Start(listener()); |
| 209 | 210 |
| 210 base::ThreadTaskRunnerHandle::Get()->PostTask( | 211 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 211 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 212 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 212 base::Unretained(orientation_pump()))); | 213 base::Unretained(orientation_pump()))); |
| 213 base::MessageLoop::current()->Run(); | 214 base::RunLoop().Run(); |
| 214 | 215 |
| 215 EXPECT_TRUE(listener()->did_change_device_orientation()); | 216 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 216 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 217 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
| 217 static_cast<double>(received_data.alpha)); | 218 static_cast<double>(received_data.alpha)); |
| 218 } | 219 } |
| 219 | 220 |
| 220 } // namespace content | 221 } // namespace content |
| OLD | NEW |