| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 private: | 45 private: |
| 46 bool did_change_device_orientation_; | 46 bool did_change_device_orientation_; |
| 47 blink::WebDeviceOrientationData data_; | 47 blink::WebDeviceOrientationData data_; |
| 48 | 48 |
| 49 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); | 49 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { | 52 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { |
| 53 public: | 53 public: |
| 54 DeviceOrientationEventPumpForTesting() | 54 DeviceOrientationEventPumpForTesting() |
| 55 : DeviceOrientationEventPump(0) { } | 55 : DeviceOrientationEventPump(nullptr) {} |
| 56 ~DeviceOrientationEventPumpForTesting() override {} | 56 ~DeviceOrientationEventPumpForTesting() override {} |
| 57 | 57 |
| 58 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 58 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) { |
| 59 DeviceOrientationEventPump::OnDidStart(renderer_handle); | 59 DeviceOrientationEventPump::DidStart(std::move(renderer_handle)); |
| 60 } | 60 } |
| 61 void SendStartMessage() override {} | 61 void SendStartMessage() override {} |
| 62 void SendStopMessage() override {} | 62 void SendStopMessage() override {} |
| 63 void FireEvent() override { | 63 void FireEvent() override { |
| 64 DeviceOrientationEventPump::FireEvent(); | 64 DeviceOrientationEventPump::FireEvent(); |
| 65 Stop(); | 65 Stop(); |
| 66 base::MessageLoop::current()->QuitWhenIdle(); | 66 base::MessageLoop::current()->QuitWhenIdle(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); | 70 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 class DeviceOrientationEventPumpTest : public testing::Test { | 73 class DeviceOrientationEventPumpTest : public testing::Test { |
| 74 public: | 74 public: |
| 75 DeviceOrientationEventPumpTest() { | 75 DeviceOrientationEventPumpTest() = default; |
| 76 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | |
| 77 sizeof(DeviceOrientationHardwareBuffer))); | |
| 78 } | |
| 79 | 76 |
| 80 protected: | 77 protected: |
| 81 void SetUp() override { | 78 void SetUp() override { |
| 82 const DeviceOrientationHardwareBuffer* null_buffer = nullptr; | |
| 83 listener_.reset(new MockDeviceOrientationListener); | 79 listener_.reset(new MockDeviceOrientationListener); |
| 84 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); | 80 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); |
| 85 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( | 81 shared_memory_ = mojo::SharedBufferHandle::Create( |
| 86 shared_memory_.memory()); | 82 sizeof(DeviceOrientationHardwareBuffer)); |
| 87 ASSERT_NE(null_buffer, buffer_); | 83 mapping_ = shared_memory_->Map(sizeof(DeviceOrientationHardwareBuffer)); |
| 88 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer)); | 84 ASSERT_TRUE(mapping_); |
| 89 ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), | 85 memset(buffer(), 0, sizeof(DeviceOrientationHardwareBuffer)); |
| 90 &handle_)); | |
| 91 } | 86 } |
| 92 | 87 |
| 93 void InitBuffer() { | 88 void InitBuffer() { |
| 94 blink::WebDeviceOrientationData& data = buffer_->data; | 89 blink::WebDeviceOrientationData& data = buffer()->data; |
| 95 data.alpha = 1; | 90 data.alpha = 1; |
| 96 data.hasAlpha = true; | 91 data.hasAlpha = true; |
| 97 data.beta = 2; | 92 data.beta = 2; |
| 98 data.hasBeta = true; | 93 data.hasBeta = true; |
| 99 data.gamma = 3; | 94 data.gamma = 3; |
| 100 data.hasGamma = true; | 95 data.hasGamma = true; |
| 101 data.allAvailableSensorsAreActive = true; | 96 data.allAvailableSensorsAreActive = true; |
| 102 } | 97 } |
| 103 | 98 |
| 104 void InitBufferNoData() { | 99 void InitBufferNoData() { |
| 105 blink::WebDeviceOrientationData& data = buffer_->data; | 100 blink::WebDeviceOrientationData& data = buffer()->data; |
| 106 data.allAvailableSensorsAreActive = true; | 101 data.allAvailableSensorsAreActive = true; |
| 107 } | 102 } |
| 108 | 103 |
| 109 MockDeviceOrientationListener* listener() { return listener_.get(); } | 104 MockDeviceOrientationListener* listener() { return listener_.get(); } |
| 110 DeviceOrientationEventPumpForTesting* orientation_pump() { | 105 DeviceOrientationEventPumpForTesting* orientation_pump() { |
| 111 return orientation_pump_.get(); | 106 return orientation_pump_.get(); |
| 112 } | 107 } |
| 113 base::SharedMemoryHandle handle() { return handle_; } | 108 mojo::ScopedSharedBufferHandle handle() { |
| 114 DeviceOrientationHardwareBuffer* buffer() { return buffer_; } | 109 return shared_memory_->Clone( |
| 110 mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| 111 } |
| 112 DeviceOrientationHardwareBuffer* buffer() { |
| 113 return reinterpret_cast<DeviceOrientationHardwareBuffer*>(mapping_.get()); |
| 114 } |
| 115 | 115 |
| 116 private: | 116 private: |
| 117 base::MessageLoop loop_; |
| 117 std::unique_ptr<MockDeviceOrientationListener> listener_; | 118 std::unique_ptr<MockDeviceOrientationListener> listener_; |
| 118 std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; | 119 std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; |
| 119 base::SharedMemoryHandle handle_; | 120 mojo::ScopedSharedBufferHandle shared_memory_; |
| 120 base::SharedMemory shared_memory_; | 121 mojo::ScopedSharedBufferMapping mapping_; |
| 121 DeviceOrientationHardwareBuffer* buffer_; | |
| 122 | 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); | 123 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
| 124 }; | 124 }; |
| 125 | 125 |
| 126 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 126 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
| 127 base::MessageLoop loop; | |
| 128 | |
| 129 InitBuffer(); | 127 InitBuffer(); |
| 130 orientation_pump()->Start(listener()); | 128 orientation_pump()->Start(listener()); |
| 131 orientation_pump()->OnDidStart(handle()); | 129 orientation_pump()->DidStart(handle()); |
| 132 | 130 |
| 133 base::MessageLoop::current()->Run(); | 131 base::MessageLoop::current()->Run(); |
| 134 | 132 |
| 135 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 133 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 136 EXPECT_TRUE(listener()->did_change_device_orientation()); | 134 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 137 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 135 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 138 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 136 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 139 EXPECT_TRUE(received_data.hasAlpha); | 137 EXPECT_TRUE(received_data.hasAlpha); |
| 140 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 138 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 141 EXPECT_TRUE(received_data.hasBeta); | 139 EXPECT_TRUE(received_data.hasBeta); |
| 142 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 140 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 143 EXPECT_TRUE(received_data.hasGamma); | 141 EXPECT_TRUE(received_data.hasGamma); |
| 144 } | 142 } |
| 145 | 143 |
| 146 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 144 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
| 147 base::MessageLoop loop; | |
| 148 | |
| 149 InitBufferNoData(); | 145 InitBufferNoData(); |
| 150 orientation_pump()->Start(listener()); | 146 orientation_pump()->Start(listener()); |
| 151 orientation_pump()->OnDidStart(handle()); | 147 orientation_pump()->DidStart(handle()); |
| 152 | 148 |
| 153 base::MessageLoop::current()->Run(); | 149 base::MessageLoop::current()->Run(); |
| 154 | 150 |
| 155 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 151 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 156 EXPECT_TRUE(listener()->did_change_device_orientation()); | 152 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 157 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 153 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 158 EXPECT_FALSE(received_data.hasAlpha); | 154 EXPECT_FALSE(received_data.hasAlpha); |
| 159 EXPECT_FALSE(received_data.hasBeta); | 155 EXPECT_FALSE(received_data.hasBeta); |
| 160 EXPECT_FALSE(received_data.hasGamma); | 156 EXPECT_FALSE(received_data.hasGamma); |
| 161 } | 157 } |
| 162 | 158 |
| 163 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 159 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
| 164 base::MessageLoop loop; | |
| 165 | |
| 166 InitBuffer(); | 160 InitBuffer(); |
| 167 orientation_pump()->Start(listener()); | 161 orientation_pump()->Start(listener()); |
| 168 orientation_pump()->OnDidStart(handle()); | 162 orientation_pump()->DidStart(handle()); |
| 169 | 163 |
| 170 base::MessageLoop::current()->Run(); | 164 base::MessageLoop::current()->Run(); |
| 171 | 165 |
| 172 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 166 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 173 EXPECT_TRUE(listener()->did_change_device_orientation()); | 167 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 174 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 168 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 175 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 169 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 176 EXPECT_TRUE(received_data.hasAlpha); | 170 EXPECT_TRUE(received_data.hasAlpha); |
| 177 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 171 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 178 EXPECT_TRUE(received_data.hasBeta); | 172 EXPECT_TRUE(received_data.hasBeta); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 205 FROM_HERE, base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 212 base::Unretained(orientation_pump()))); | 206 base::Unretained(orientation_pump()))); |
| 213 base::MessageLoop::current()->Run(); | 207 base::MessageLoop::current()->Run(); |
| 214 | 208 |
| 215 EXPECT_TRUE(listener()->did_change_device_orientation()); | 209 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 216 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 210 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
| 217 static_cast<double>(received_data.alpha)); | 211 static_cast<double>(received_data.alpha)); |
| 218 } | 212 } |
| 219 | 213 |
| 220 } // namespace content | 214 } // namespace content |
| OLD | NEW |