| 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 22 matching lines...) Expand all Loading... |
| 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(device::DeviceOrientationHardwareBuffer)); | 81 sizeof(device::DeviceOrientationHardwareBuffer)); |
| 84 mapping_ = | 82 mapping_ = |
| 85 shared_memory_->Map(sizeof(device::DeviceOrientationHardwareBuffer)); | 83 shared_memory_->Map(sizeof(device::DeviceOrientationHardwareBuffer)); |
| 86 ASSERT_TRUE(mapping_); | 84 ASSERT_TRUE(mapping_); |
| 87 memset(buffer(), 0, sizeof(device::DeviceOrientationHardwareBuffer)); | 85 memset(buffer(), 0, sizeof(device::DeviceOrientationHardwareBuffer)); |
| 88 } | 86 } |
| 89 | 87 |
| 90 void InitBuffer() { | 88 void InitBuffer() { |
| 91 blink::WebDeviceOrientationData& data = buffer()->data; | 89 device::OrientationData& data = buffer()->data; |
| 92 data.alpha = 1; | 90 data.alpha = 1; |
| 93 data.hasAlpha = true; | 91 data.hasAlpha = true; |
| 94 data.beta = 2; | 92 data.beta = 2; |
| 95 data.hasBeta = true; | 93 data.hasBeta = true; |
| 96 data.gamma = 3; | 94 data.gamma = 3; |
| 97 data.hasGamma = true; | 95 data.hasGamma = true; |
| 98 data.allAvailableSensorsAreActive = true; | 96 data.allAvailableSensorsAreActive = true; |
| 99 } | 97 } |
| 100 | 98 |
| 101 void InitBufferNoData() { | 99 void InitBufferNoData() { |
| 102 blink::WebDeviceOrientationData& data = buffer()->data; | 100 device::OrientationData& data = buffer()->data; |
| 103 data.allAvailableSensorsAreActive = true; | 101 data.allAvailableSensorsAreActive = true; |
| 104 } | 102 } |
| 105 | 103 |
| 106 MockDeviceOrientationListener* listener() { return listener_.get(); } | 104 MockDeviceOrientationListener* listener() { return listener_.get(); } |
| 107 DeviceOrientationEventPumpForTesting* orientation_pump() { | 105 DeviceOrientationEventPumpForTesting* orientation_pump() { |
| 108 return orientation_pump_.get(); | 106 return orientation_pump_.get(); |
| 109 } | 107 } |
| 110 mojo::ScopedSharedBufferHandle handle() { | 108 mojo::ScopedSharedBufferHandle handle() { |
| 111 return shared_memory_->Clone( | 109 return shared_memory_->Clone( |
| 112 mojo::SharedBufferHandle::AccessMode::READ_ONLY); | 110 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 126 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); | 124 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
| 127 }; | 125 }; |
| 128 | 126 |
| 129 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 127 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
| 130 InitBuffer(); | 128 InitBuffer(); |
| 131 orientation_pump()->Start(listener()); | 129 orientation_pump()->Start(listener()); |
| 132 orientation_pump()->DidStart(handle()); | 130 orientation_pump()->DidStart(handle()); |
| 133 | 131 |
| 134 base::RunLoop().Run(); | 132 base::RunLoop().Run(); |
| 135 | 133 |
| 136 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 134 const device::OrientationData& received_data = listener()->data(); |
| 137 EXPECT_TRUE(listener()->did_change_device_orientation()); | 135 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 138 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 136 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 139 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 137 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 140 EXPECT_TRUE(received_data.hasAlpha); | 138 EXPECT_TRUE(received_data.hasAlpha); |
| 141 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 139 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 142 EXPECT_TRUE(received_data.hasBeta); | 140 EXPECT_TRUE(received_data.hasBeta); |
| 143 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 141 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 144 EXPECT_TRUE(received_data.hasGamma); | 142 EXPECT_TRUE(received_data.hasGamma); |
| 145 } | 143 } |
| 146 | 144 |
| 147 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 145 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
| 148 InitBufferNoData(); | 146 InitBufferNoData(); |
| 149 orientation_pump()->Start(listener()); | 147 orientation_pump()->Start(listener()); |
| 150 orientation_pump()->DidStart(handle()); | 148 orientation_pump()->DidStart(handle()); |
| 151 | 149 |
| 152 base::RunLoop().Run(); | 150 base::RunLoop().Run(); |
| 153 | 151 |
| 154 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 152 const device::OrientationData& received_data = listener()->data(); |
| 155 EXPECT_TRUE(listener()->did_change_device_orientation()); | 153 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 156 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 154 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 157 EXPECT_FALSE(received_data.hasAlpha); | 155 EXPECT_FALSE(received_data.hasAlpha); |
| 158 EXPECT_FALSE(received_data.hasBeta); | 156 EXPECT_FALSE(received_data.hasBeta); |
| 159 EXPECT_FALSE(received_data.hasGamma); | 157 EXPECT_FALSE(received_data.hasGamma); |
| 160 } | 158 } |
| 161 | 159 |
| 162 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 160 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
| 163 InitBuffer(); | 161 InitBuffer(); |
| 164 orientation_pump()->Start(listener()); | 162 orientation_pump()->Start(listener()); |
| 165 orientation_pump()->DidStart(handle()); | 163 orientation_pump()->DidStart(handle()); |
| 166 | 164 |
| 167 base::RunLoop().Run(); | 165 base::RunLoop().Run(); |
| 168 | 166 |
| 169 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 167 const device::OrientationData& received_data = listener()->data(); |
| 170 EXPECT_TRUE(listener()->did_change_device_orientation()); | 168 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 171 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 172 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 173 EXPECT_TRUE(received_data.hasAlpha); | 171 EXPECT_TRUE(received_data.hasAlpha); |
| 174 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 175 EXPECT_TRUE(received_data.hasBeta); | 173 EXPECT_TRUE(received_data.hasBeta); |
| 176 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 177 EXPECT_TRUE(received_data.hasGamma); | 175 EXPECT_TRUE(received_data.hasGamma); |
| 178 | 176 |
| 179 buffer()->data.alpha = | 177 buffer()->data.alpha = |
| (...skipping 28 matching lines...) Expand all Loading... |
| 208 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 206 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 209 base::Unretained(orientation_pump()))); | 207 base::Unretained(orientation_pump()))); |
| 210 base::RunLoop().Run(); | 208 base::RunLoop().Run(); |
| 211 | 209 |
| 212 EXPECT_TRUE(listener()->did_change_device_orientation()); | 210 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 213 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
| 214 static_cast<double>(received_data.alpha)); | 212 static_cast<double>(received_data.alpha)); |
| 215 } | 213 } |
| 216 | 214 |
| 217 } // namespace content | 215 } // namespace content |
| OLD | NEW |