| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 sizeof(device::DeviceOrientationHardwareBuffer)); | 81 sizeof(device::DeviceOrientationHardwareBuffer)); |
| 82 mapping_ = | 82 mapping_ = |
| 83 shared_memory_->Map(sizeof(device::DeviceOrientationHardwareBuffer)); | 83 shared_memory_->Map(sizeof(device::DeviceOrientationHardwareBuffer)); |
| 84 ASSERT_TRUE(mapping_); | 84 ASSERT_TRUE(mapping_); |
| 85 memset(buffer(), 0, sizeof(device::DeviceOrientationHardwareBuffer)); | 85 memset(buffer(), 0, sizeof(device::DeviceOrientationHardwareBuffer)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void InitBuffer() { | 88 void InitBuffer() { |
| 89 device::OrientationData& data = buffer()->data; | 89 device::OrientationData& data = buffer()->data; |
| 90 data.alpha = 1; | 90 data.alpha = 1; |
| 91 data.hasAlpha = true; | 91 data.has_alpha = true; |
| 92 data.beta = 2; | 92 data.beta = 2; |
| 93 data.hasBeta = true; | 93 data.has_beta = true; |
| 94 data.gamma = 3; | 94 data.gamma = 3; |
| 95 data.hasGamma = true; | 95 data.has_gamma = true; |
| 96 data.allAvailableSensorsAreActive = true; | 96 data.all_available_sensors_are_active = true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 void InitBufferNoData() { | 99 void InitBufferNoData() { |
| 100 device::OrientationData& data = buffer()->data; | 100 device::OrientationData& data = buffer()->data; |
| 101 data.allAvailableSensorsAreActive = true; | 101 data.all_available_sensors_are_active = true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 MockDeviceOrientationListener* listener() { return listener_.get(); } | 104 MockDeviceOrientationListener* listener() { return listener_.get(); } |
| 105 DeviceOrientationEventPumpForTesting* orientation_pump() { | 105 DeviceOrientationEventPumpForTesting* orientation_pump() { |
| 106 return orientation_pump_.get(); | 106 return orientation_pump_.get(); |
| 107 } | 107 } |
| 108 mojo::ScopedSharedBufferHandle handle() { | 108 mojo::ScopedSharedBufferHandle handle() { |
| 109 return shared_memory_->Clone( | 109 return shared_memory_->Clone( |
| 110 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | 110 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| 111 } | 111 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 126 | 126 |
| 127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
| 128 InitBuffer(); | 128 InitBuffer(); |
| 129 orientation_pump()->Start(listener()); | 129 orientation_pump()->Start(listener()); |
| 130 orientation_pump()->DidStart(handle()); | 130 orientation_pump()->DidStart(handle()); |
| 131 | 131 |
| 132 base::RunLoop().Run(); | 132 base::RunLoop().Run(); |
| 133 | 133 |
| 134 const device::OrientationData& received_data = listener()->data(); | 134 const device::OrientationData& received_data = listener()->data(); |
| 135 EXPECT_TRUE(listener()->did_change_device_orientation()); | 135 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 136 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 136 EXPECT_TRUE(received_data.all_available_sensors_are_active); |
| 137 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 137 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 138 EXPECT_TRUE(received_data.hasAlpha); | 138 EXPECT_TRUE(received_data.has_alpha); |
| 139 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 139 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 140 EXPECT_TRUE(received_data.hasBeta); | 140 EXPECT_TRUE(received_data.has_beta); |
| 141 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 141 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 142 EXPECT_TRUE(received_data.hasGamma); | 142 EXPECT_TRUE(received_data.has_gamma); |
| 143 } | 143 } |
| 144 | 144 |
| 145 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 145 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
| 146 InitBufferNoData(); | 146 InitBufferNoData(); |
| 147 orientation_pump()->Start(listener()); | 147 orientation_pump()->Start(listener()); |
| 148 orientation_pump()->DidStart(handle()); | 148 orientation_pump()->DidStart(handle()); |
| 149 | 149 |
| 150 base::RunLoop().Run(); | 150 base::RunLoop().Run(); |
| 151 | 151 |
| 152 const device::OrientationData& received_data = listener()->data(); | 152 const device::OrientationData& received_data = listener()->data(); |
| 153 EXPECT_TRUE(listener()->did_change_device_orientation()); | 153 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 154 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 154 EXPECT_TRUE(received_data.all_available_sensors_are_active); |
| 155 EXPECT_FALSE(received_data.hasAlpha); | 155 EXPECT_FALSE(received_data.has_alpha); |
| 156 EXPECT_FALSE(received_data.hasBeta); | 156 EXPECT_FALSE(received_data.has_beta); |
| 157 EXPECT_FALSE(received_data.hasGamma); | 157 EXPECT_FALSE(received_data.has_gamma); |
| 158 } | 158 } |
| 159 | 159 |
| 160 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 160 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
| 161 InitBuffer(); | 161 InitBuffer(); |
| 162 orientation_pump()->Start(listener()); | 162 orientation_pump()->Start(listener()); |
| 163 orientation_pump()->DidStart(handle()); | 163 orientation_pump()->DidStart(handle()); |
| 164 | 164 |
| 165 base::RunLoop().Run(); | 165 base::RunLoop().Run(); |
| 166 | 166 |
| 167 const device::OrientationData& received_data = listener()->data(); | 167 const device::OrientationData& received_data = listener()->data(); |
| 168 EXPECT_TRUE(listener()->did_change_device_orientation()); | 168 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 169 EXPECT_TRUE(received_data.all_available_sensors_are_active); |
| 170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 171 EXPECT_TRUE(received_data.hasAlpha); | 171 EXPECT_TRUE(received_data.has_alpha); |
| 172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 173 EXPECT_TRUE(received_data.hasBeta); | 173 EXPECT_TRUE(received_data.has_beta); |
| 174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 175 EXPECT_TRUE(received_data.hasGamma); | 175 EXPECT_TRUE(received_data.has_gamma); |
| 176 | 176 |
| 177 buffer()->data.alpha = | 177 buffer()->data.alpha = |
| 178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; | 178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; |
| 179 listener()->set_did_change_device_orientation(false); | 179 listener()->set_did_change_device_orientation(false); |
| 180 | 180 |
| 181 // Reset the pump's listener. | 181 // Reset the pump's listener. |
| 182 orientation_pump()->Start(listener()); | 182 orientation_pump()->Start(listener()); |
| 183 | 183 |
| 184 base::ThreadTaskRunnerHandle::Get()->PostTask( | 184 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 185 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 185 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 186 base::Unretained(orientation_pump()))); | 186 base::Unretained(orientation_pump()))); |
| 187 base::RunLoop().Run(); | 187 base::RunLoop().Run(); |
| 188 | 188 |
| 189 EXPECT_FALSE(listener()->did_change_device_orientation()); | 189 EXPECT_FALSE(listener()->did_change_device_orientation()); |
| 190 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 190 EXPECT_TRUE(received_data.all_available_sensors_are_active); |
| 191 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 191 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 192 EXPECT_TRUE(received_data.hasAlpha); | 192 EXPECT_TRUE(received_data.has_alpha); |
| 193 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 193 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 194 EXPECT_TRUE(received_data.hasBeta); | 194 EXPECT_TRUE(received_data.has_beta); |
| 195 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 195 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 196 EXPECT_TRUE(received_data.hasGamma); | 196 EXPECT_TRUE(received_data.has_gamma); |
| 197 | 197 |
| 198 buffer()->data.alpha = | 198 buffer()->data.alpha = |
| 199 1 + DeviceOrientationEventPump::kOrientationThreshold; | 199 1 + DeviceOrientationEventPump::kOrientationThreshold; |
| 200 listener()->set_did_change_device_orientation(false); | 200 listener()->set_did_change_device_orientation(false); |
| 201 | 201 |
| 202 // Reset the pump's listener. | 202 // Reset the pump's listener. |
| 203 orientation_pump()->Start(listener()); | 203 orientation_pump()->Start(listener()); |
| 204 | 204 |
| 205 base::ThreadTaskRunnerHandle::Get()->PostTask( | 205 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 206 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 206 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 207 base::Unretained(orientation_pump()))); | 207 base::Unretained(orientation_pump()))); |
| 208 base::RunLoop().Run(); | 208 base::RunLoop().Run(); |
| 209 | 209 |
| 210 EXPECT_TRUE(listener()->did_change_device_orientation()); | 210 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
| 212 static_cast<double>(received_data.alpha)); | 212 static_cast<double>(received_data.alpha)); |
| 213 } | 213 } |
| 214 | 214 |
| 215 } // namespace content | 215 } // namespace content |
| OLD | NEW |