| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | 9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" |
| 10 #include "content/public/test/test_utils.h" | 10 #include "content/public/test/test_utils.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h" | 12 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 class MockDeviceOrientationListener | 16 class MockDeviceOrientationListener |
| 17 : public blink::WebDeviceOrientationListener { | 17 : public blink::WebDeviceOrientationListener { |
| 18 public: | 18 public: |
| 19 MockDeviceOrientationListener(); | 19 MockDeviceOrientationListener() : did_change_device_orientation_(false) { |
| 20 memset(&data_, 0, sizeof(data_)); |
| 21 } |
| 20 virtual ~MockDeviceOrientationListener() { } | 22 virtual ~MockDeviceOrientationListener() { } |
| 23 |
| 21 virtual void didChangeDeviceOrientation( | 24 virtual void didChangeDeviceOrientation( |
| 22 const blink::WebDeviceOrientationData&) OVERRIDE; | 25 const blink::WebDeviceOrientationData& data) OVERRIDE { |
| 23 void ResetDidChangeOrientation(); | 26 memcpy(&data_, &data, sizeof(data)); |
| 27 did_change_device_orientation_ = true; |
| 28 } |
| 29 |
| 30 bool did_change_device_orientation() const { |
| 31 return did_change_device_orientation_; |
| 32 } |
| 33 void set_did_change_device_orientation(bool value) { |
| 34 did_change_device_orientation_ = value; |
| 35 } |
| 36 const blink::WebDeviceOrientationData& data() const { |
| 37 return data_; |
| 38 } |
| 39 |
| 40 private: |
| 24 bool did_change_device_orientation_; | 41 bool did_change_device_orientation_; |
| 25 blink::WebDeviceOrientationData data_; | 42 blink::WebDeviceOrientationData data_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); |
| 26 }; | 45 }; |
| 27 | 46 |
| 28 MockDeviceOrientationListener::MockDeviceOrientationListener() | |
| 29 : did_change_device_orientation_(false) { | |
| 30 memset(&data_, 0, sizeof(data_)); | |
| 31 } | |
| 32 | |
| 33 void MockDeviceOrientationListener::didChangeDeviceOrientation( | |
| 34 const blink::WebDeviceOrientationData& data) { | |
| 35 memcpy(&data_, &data, sizeof(data)); | |
| 36 did_change_device_orientation_ = true; | |
| 37 } | |
| 38 | |
| 39 void MockDeviceOrientationListener::ResetDidChangeOrientation() { | |
| 40 did_change_device_orientation_ = false; | |
| 41 } | |
| 42 | |
| 43 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { | 47 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { |
| 44 public: | 48 public: |
| 45 DeviceOrientationEventPumpForTesting() { } | 49 DeviceOrientationEventPumpForTesting() { } |
| 46 virtual ~DeviceOrientationEventPumpForTesting() { } | 50 virtual ~DeviceOrientationEventPumpForTesting() { } |
| 47 | 51 |
| 48 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 52 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
| 49 DeviceOrientationEventPump::OnDidStart(renderer_handle); | 53 DeviceOrientationEventPump::OnDidStart(renderer_handle); |
| 50 } | 54 } |
| 51 virtual bool SendStartMessage() OVERRIDE { return true; } | 55 virtual bool SendStartMessage() OVERRIDE { return true; } |
| 52 virtual bool SendStopMessage() OVERRIDE { return true; } | 56 virtual bool SendStopMessage() OVERRIDE { return true; } |
| 53 virtual void FireEvent() OVERRIDE { | 57 virtual void FireEvent() OVERRIDE { |
| 54 DeviceOrientationEventPump::FireEvent(); | 58 DeviceOrientationEventPump::FireEvent(); |
| 55 Stop(); | 59 Stop(); |
| 56 base::MessageLoop::current()->QuitWhenIdle(); | 60 base::MessageLoop::current()->QuitWhenIdle(); |
| 57 } | 61 } |
| 62 |
| 63 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); |
| 58 }; | 65 }; |
| 59 | 66 |
| 60 class DeviceOrientationEventPumpTest : public testing::Test { | 67 class DeviceOrientationEventPumpTest : public testing::Test { |
| 61 public: | 68 public: |
| 62 DeviceOrientationEventPumpTest() { | 69 DeviceOrientationEventPumpTest() { |
| 63 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | 70 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( |
| 64 sizeof(DeviceOrientationHardwareBuffer))); | 71 sizeof(DeviceOrientationHardwareBuffer))); |
| 65 } | 72 } |
| 66 | 73 |
| 67 protected: | 74 protected: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 83 data.gamma = 3; | 90 data.gamma = 3; |
| 84 data.hasGamma = true; | 91 data.hasGamma = true; |
| 85 data.allAvailableSensorsAreActive = true; | 92 data.allAvailableSensorsAreActive = true; |
| 86 } | 93 } |
| 87 | 94 |
| 88 void InitBufferNoData() { | 95 void InitBufferNoData() { |
| 89 blink::WebDeviceOrientationData& data = buffer_->data; | 96 blink::WebDeviceOrientationData& data = buffer_->data; |
| 90 data.allAvailableSensorsAreActive = true; | 97 data.allAvailableSensorsAreActive = true; |
| 91 } | 98 } |
| 92 | 99 |
| 100 MockDeviceOrientationListener* listener() { return listener_.get(); } |
| 101 DeviceOrientationEventPumpForTesting* orientation_pump() { |
| 102 return orientation_pump_.get(); |
| 103 } |
| 104 base::SharedMemoryHandle handle() { return handle_; } |
| 105 DeviceOrientationHardwareBuffer* buffer() { return buffer_; } |
| 106 |
| 107 private: |
| 93 scoped_ptr<MockDeviceOrientationListener> listener_; | 108 scoped_ptr<MockDeviceOrientationListener> listener_; |
| 94 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; | 109 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; |
| 95 base::SharedMemoryHandle handle_; | 110 base::SharedMemoryHandle handle_; |
| 96 base::SharedMemory shared_memory_; | 111 base::SharedMemory shared_memory_; |
| 97 DeviceOrientationHardwareBuffer* buffer_; | 112 DeviceOrientationHardwareBuffer* buffer_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
| 98 }; | 115 }; |
| 99 | 116 |
| 100 // Always failing in the win try bot. See http://crbug.com/256782. | 117 // Always failing in the win try bot. See http://crbug.com/256782. |
| 101 #if defined(OS_WIN) | 118 #if defined(OS_WIN) |
| 102 #define MAYBE_DidStartPolling DISABLED_DidStartPolling | 119 #define MAYBE_DidStartPolling DISABLED_DidStartPolling |
| 103 #else | 120 #else |
| 104 #define MAYBE_DidStartPolling DidStartPolling | 121 #define MAYBE_DidStartPolling DidStartPolling |
| 105 #endif | 122 #endif |
| 106 TEST_F(DeviceOrientationEventPumpTest, MAYBE_DidStartPolling) { | 123 TEST_F(DeviceOrientationEventPumpTest, MAYBE_DidStartPolling) { |
| 107 base::MessageLoop loop; | 124 base::MessageLoop loop; |
| 108 | 125 |
| 109 InitBuffer(); | 126 InitBuffer(); |
| 110 orientation_pump_->SetListener(listener_.get()); | 127 orientation_pump()->SetListener(listener()); |
| 111 orientation_pump_->OnDidStart(handle_); | 128 orientation_pump()->OnDidStart(handle()); |
| 112 | 129 |
| 113 base::MessageLoop::current()->Run(); | 130 base::MessageLoop::current()->Run(); |
| 114 | 131 |
| 115 blink::WebDeviceOrientationData& received_data = listener_->data_; | 132 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 116 EXPECT_TRUE(listener_->did_change_device_orientation_); | 133 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 117 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 134 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 118 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 135 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 119 EXPECT_TRUE(received_data.hasAlpha); | 136 EXPECT_TRUE(received_data.hasAlpha); |
| 120 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 137 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 121 EXPECT_TRUE(received_data.hasBeta); | 138 EXPECT_TRUE(received_data.hasBeta); |
| 122 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 139 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 123 EXPECT_TRUE(received_data.hasGamma); | 140 EXPECT_TRUE(received_data.hasGamma); |
| 124 } | 141 } |
| 125 | 142 |
| 126 // Always failing in the win try bot. See http://crbug.com/256782. | 143 // Always failing in the win try bot. See http://crbug.com/256782. |
| 127 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 128 #define MAYBE_FireAllNullEvent DISABLED_FireAllNullEvent | 145 #define MAYBE_FireAllNullEvent DISABLED_FireAllNullEvent |
| 129 #else | 146 #else |
| 130 #define MAYBE_FireAllNullEvent FireAllNullEvent | 147 #define MAYBE_FireAllNullEvent FireAllNullEvent |
| 131 #endif | 148 #endif |
| 132 TEST_F(DeviceOrientationEventPumpTest, MAYBE_FireAllNullEvent) { | 149 TEST_F(DeviceOrientationEventPumpTest, MAYBE_FireAllNullEvent) { |
| 133 base::MessageLoop loop; | 150 base::MessageLoop loop; |
| 134 | 151 |
| 135 InitBufferNoData(); | 152 InitBufferNoData(); |
| 136 orientation_pump_->SetListener(listener_.get()); | 153 orientation_pump()->SetListener(listener()); |
| 137 orientation_pump_->OnDidStart(handle_); | 154 orientation_pump()->OnDidStart(handle()); |
| 138 | 155 |
| 139 base::MessageLoop::current()->Run(); | 156 base::MessageLoop::current()->Run(); |
| 140 | 157 |
| 141 blink::WebDeviceOrientationData& received_data = listener_->data_; | 158 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 142 EXPECT_TRUE(listener_->did_change_device_orientation_); | 159 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 143 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 160 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 144 EXPECT_FALSE(received_data.hasAlpha); | 161 EXPECT_FALSE(received_data.hasAlpha); |
| 145 EXPECT_FALSE(received_data.hasBeta); | 162 EXPECT_FALSE(received_data.hasBeta); |
| 146 EXPECT_FALSE(received_data.hasGamma); | 163 EXPECT_FALSE(received_data.hasGamma); |
| 147 } | 164 } |
| 148 | 165 |
| 149 // Always failing in the win try bot. See http://crbug.com/256782. | 166 // Always failing in the win try bot. See http://crbug.com/256782. |
| 150 #if defined(OS_WIN) | 167 #if defined(OS_WIN) |
| 151 #define MAYBE_UpdateRespectsOrientationThreshold \ | 168 #define MAYBE_UpdateRespectsOrientationThreshold \ |
| 152 DISABLED_UpdateRespectsOrientationThreshold | 169 DISABLED_UpdateRespectsOrientationThreshold |
| 153 #else | 170 #else |
| 154 #define MAYBE_UpdateRespectsOrientationThreshold \ | 171 #define MAYBE_UpdateRespectsOrientationThreshold \ |
| 155 UpdateRespectsOrientationThreshold | 172 UpdateRespectsOrientationThreshold |
| 156 #endif | 173 #endif |
| 157 TEST_F(DeviceOrientationEventPumpTest, | 174 TEST_F(DeviceOrientationEventPumpTest, |
| 158 MAYBE_UpdateRespectsOrientationThreshold) { | 175 MAYBE_UpdateRespectsOrientationThreshold) { |
| 159 base::MessageLoop loop; | 176 base::MessageLoop loop; |
| 160 | 177 |
| 161 InitBuffer(); | 178 InitBuffer(); |
| 162 orientation_pump_->SetListener(listener_.get()); | 179 orientation_pump()->SetListener(listener()); |
| 163 orientation_pump_->OnDidStart(handle_); | 180 orientation_pump()->OnDidStart(handle()); |
| 164 | 181 |
| 165 base::MessageLoop::current()->Run(); | 182 base::MessageLoop::current()->Run(); |
| 166 | 183 |
| 167 blink::WebDeviceOrientationData& received_data = listener_->data_; | 184 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
| 168 EXPECT_TRUE(listener_->did_change_device_orientation_); | 185 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 186 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 187 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 171 EXPECT_TRUE(received_data.hasAlpha); | 188 EXPECT_TRUE(received_data.hasAlpha); |
| 172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 189 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 173 EXPECT_TRUE(received_data.hasBeta); | 190 EXPECT_TRUE(received_data.hasBeta); |
| 174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 191 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 175 EXPECT_TRUE(received_data.hasGamma); | 192 EXPECT_TRUE(received_data.hasGamma); |
| 176 | 193 |
| 177 buffer_->data.alpha = | 194 buffer()->data.alpha = |
| 178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; | 195 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; |
| 179 listener_->ResetDidChangeOrientation(); | 196 listener()->set_did_change_device_orientation(false); |
| 180 | 197 |
| 181 base::MessageLoop::current()->PostTask(FROM_HERE, | 198 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 182 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 199 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 183 base::Unretained(orientation_pump_.get()))); | 200 base::Unretained(orientation_pump()))); |
| 184 base::MessageLoop::current()->Run(); | 201 base::MessageLoop::current()->Run(); |
| 185 | 202 |
| 186 EXPECT_FALSE(listener_->did_change_device_orientation_); | 203 EXPECT_FALSE(listener()->did_change_device_orientation()); |
| 187 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 204 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
| 188 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 205 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
| 189 EXPECT_TRUE(received_data.hasAlpha); | 206 EXPECT_TRUE(received_data.hasAlpha); |
| 190 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 207 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
| 191 EXPECT_TRUE(received_data.hasBeta); | 208 EXPECT_TRUE(received_data.hasBeta); |
| 192 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 209 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
| 193 EXPECT_TRUE(received_data.hasGamma); | 210 EXPECT_TRUE(received_data.hasGamma); |
| 194 | 211 |
| 195 buffer_->data.alpha = | 212 buffer()->data.alpha = |
| 196 1 + DeviceOrientationEventPump::kOrientationThreshold; | 213 1 + DeviceOrientationEventPump::kOrientationThreshold; |
| 197 listener_->ResetDidChangeOrientation(); | 214 listener()->set_did_change_device_orientation(false); |
| 198 | 215 |
| 199 base::MessageLoop::current()->PostTask(FROM_HERE, | 216 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 200 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 217 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
| 201 base::Unretained(orientation_pump_.get()))); | 218 base::Unretained(orientation_pump()))); |
| 202 base::MessageLoop::current()->Run(); | 219 base::MessageLoop::current()->Run(); |
| 203 | 220 |
| 204 EXPECT_TRUE(listener_->did_change_device_orientation_); | 221 EXPECT_TRUE(listener()->did_change_device_orientation()); |
| 205 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 222 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
| 206 static_cast<double>(received_data.alpha)); | 223 static_cast<double>(received_data.alpha)); |
| 207 } | 224 } |
| 208 | 225 |
| 209 } // namespace content | 226 } // namespace content |
| OLD | NEW |