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