1 // Copyright 2013 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_orientation/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 WebKit::WebDeviceOrientationListener { 18 public: 19 MockDeviceOrientationListener(); 20 virtual ~MockDeviceOrientationListener() { } 21 virtual void didChangeDeviceOrientation( 22 const WebKit::WebDeviceOrientationData&) OVERRIDE; 23 void ResetDidChangeOrientation(); 24 bool did_change_device_orientation_; 25 WebKit::WebDeviceOrientationData data_; 26 }; 27 28 MockDeviceOrientationListener::MockDeviceOrientationListener() 29 : did_change_device_orientation_(false) { 30 memset(&data_, 0, sizeof(data_)); 31 } 32 33 void MockDeviceOrientationListener::didChangeDeviceOrientation( 34 const WebKit::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 { 44 public: 45 DeviceOrientationEventPumpForTesting() { } 46 virtual ~DeviceOrientationEventPumpForTesting() { } 47 48 void OnDidStart(base::SharedMemoryHandle renderer_handle) { 49 DeviceOrientationEventPump::OnDidStart(renderer_handle); 50 } 51 virtual bool SendStartMessage() OVERRIDE { return true; } 52 virtual bool SendStopMessage() OVERRIDE { return true; } 53 }; 54 55 class DeviceOrientationEventPumpTest : public testing::Test { 56 public: 57 DeviceOrientationEventPumpTest() { 58 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( 59 sizeof(DeviceOrientationHardwareBuffer))); 60 } 61 62 protected: 63 virtual void SetUp() OVERRIDE { 64 listener_.reset(new MockDeviceOrientationListener); 65 orientation_pump_.reset(new DeviceOrientationEventPumpForTesting); 66 buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( 67 shared_memory_.memory()); 68 memset(buffer_, 0, sizeof(DeviceOrientationHardwareBuffer)); 69 shared_memory_.ShareToProcess(base::kNullProcessHandle, &handle_); 70 } 71 72 void InitBuffer() { 73 WebKit::WebDeviceOrientationData& data = buffer_->data; 74 data.alpha = 1; 75 data.hasAlpha = true; 76 data.beta = 2; 77 data.hasBeta = true; 78 data.gamma = 3; 79 data.hasGamma = true; 80 data.allAvailableSensorsAreActive = true; 81 } 82 83 scoped_ptr<MockDeviceOrientationListener> listener_; 84 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; 85 base::SharedMemoryHandle handle_; 86 base::SharedMemory shared_memory_; 87 DeviceOrientationHardwareBuffer* buffer_; 88 }; 89 90 // Always failing in the win try bot. See http://crbug.com/256782. 91 #if defined(OS_WIN) 92 #define MAYBE_DidStartPolling DISABLED_DidStartPolling 93 #else 94 #define MAYBE_DidStartPolling DidStartPolling 95 #endif 96 TEST_F(DeviceOrientationEventPumpTest, MAYBE_DidStartPolling) { 97 base::MessageLoop loop(base::MessageLoop::TYPE_UI); 98 InitBuffer(); 99 100 orientation_pump_->SetListener(listener_.get()); 101 orientation_pump_->OnDidStart(handle_); 102 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( 103 orientation_pump_->GetDelayMillis() * 2)); 104 RunAllPendingInMessageLoop(); 105 orientation_pump_->SetListener(0); 106 107 WebKit::WebDeviceOrientationData& received_data = listener_->data_; 108 EXPECT_TRUE(listener_->did_change_device_orientation_); 109 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 110 EXPECT_EQ(1, (double)received_data.alpha); 111 EXPECT_TRUE(received_data.hasAlpha); 112 EXPECT_EQ(2, (double)received_data.beta); 113 EXPECT_TRUE(received_data.hasBeta); 114 EXPECT_EQ(3, (double)received_data.gamma); 115 EXPECT_TRUE(received_data.hasGamma); 116 } 117 118 // Always failing in the win try bot. See http://crbug.com/256782. 119 #if defined(OS_WIN) 120 #define MAYBE_UpdateRespectsOrientationThreshold \ 121 DISABLED_UpdateRespectsOrientationThreshold 122 #else 123 #define MAYBE_UpdateRespectsOrientationThreshold \ 124 UpdateRespectsOrientationThreshold 125 #endif 126 TEST_F(DeviceOrientationEventPumpTest, 127 MAYBE_UpdateRespectsOrientationThreshold) { 128 base::MessageLoop loop(base::MessageLoop::TYPE_UI); 129 InitBuffer(); 130 131 orientation_pump_->SetListener(listener_.get()); 132 orientation_pump_->OnDidStart(handle_); 133 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( 134 orientation_pump_->GetDelayMillis() * 2)); 135 RunAllPendingInMessageLoop(); 136 137 WebKit::WebDeviceOrientationData& received_data = listener_->data_; 138 EXPECT_TRUE(listener_->did_change_device_orientation_); 139 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 140 EXPECT_EQ(1, (double)received_data.alpha); 141 EXPECT_TRUE(received_data.hasAlpha); 142 EXPECT_EQ(2, (double)received_data.beta); 143 EXPECT_TRUE(received_data.hasBeta); 144 EXPECT_EQ(3, (double)received_data.gamma); 145 EXPECT_TRUE(received_data.hasGamma); 146 147 buffer_->data.alpha = 148 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; 149 listener_->ResetDidChangeOrientation(); 150 151 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( 152 orientation_pump_->GetDelayMillis() * 2)); 153 RunAllPendingInMessageLoop(); 154 155 EXPECT_FALSE(listener_->did_change_device_orientation_); 156 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); 157 EXPECT_EQ(1, (double)received_data.alpha); 158 EXPECT_TRUE(received_data.hasAlpha); 159 EXPECT_EQ(2, (double)received_data.beta); 160 EXPECT_TRUE(received_data.hasBeta); 161 EXPECT_EQ(3, (double)received_data.gamma); 162 EXPECT_TRUE(received_data.hasGamma); 163 164 buffer_->data.alpha = 165 1 + DeviceOrientationEventPump::kOrientationThreshold; 166 listener_->ResetDidChangeOrientation(); 167 168 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( 169 orientation_pump_->GetDelayMillis() * 2)); 170 RunAllPendingInMessageLoop(); 171 orientation_pump_->SetListener(0); 172 173 EXPECT_TRUE(listener_->did_change_device_orientation_); 174 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, 175 (double)received_data.alpha); 176 } 177 178 } // namespace content 179