OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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_motion_event_pump.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "content/public/test/test_utils.h" |
| 11 #include "device_motion_shared_memory_reader.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData
.h" |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionList
ener.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 class DeviceMotionEventPumpTest : public testing::Test { |
| 19 }; |
| 20 |
| 21 class MockDeviceMotionListener : public WebKit::WebDeviceMotionListener { |
| 22 public: |
| 23 MockDeviceMotionListener(); |
| 24 virtual ~MockDeviceMotionListener() { } |
| 25 |
| 26 virtual void didChangeDeviceMotion( |
| 27 const WebKit::WebDeviceMotionData&) OVERRIDE; |
| 28 virtual void setListener(WebKit::WebDeviceMotionListener*) OVERRIDE { } |
| 29 bool did_change_device_motion_; |
| 30 WebKit::WebDeviceMotionData data_; |
| 31 }; |
| 32 |
| 33 MockDeviceMotionListener::MockDeviceMotionListener() |
| 34 : did_change_device_motion_(false) { |
| 35 memset(&data_, 0, sizeof(data_)); |
| 36 } |
| 37 |
| 38 void MockDeviceMotionListener::didChangeDeviceMotion( |
| 39 const WebKit::WebDeviceMotionData& data) { |
| 40 memcpy(&data_, &data, sizeof(data)); |
| 41 did_change_device_motion_ = true; |
| 42 } |
| 43 |
| 44 class MockDeviceMotionSharedMemoryReader |
| 45 : public DeviceMotionSharedMemoryReader { |
| 46 public: |
| 47 MockDeviceMotionSharedMemoryReader(); |
| 48 ~MockDeviceMotionSharedMemoryReader() { } |
| 49 |
| 50 virtual bool latestDeviceMotionData( |
| 51 WebKit::WebDeviceMotionData& data) OVERRIDE; |
| 52 virtual void startUpdating() OVERRIDE; |
| 53 virtual void stopUpdating() OVERRIDE; |
| 54 |
| 55 int count_start_updating_; |
| 56 int count_stop_updating_; |
| 57 int count_latest_data_; |
| 58 }; |
| 59 |
| 60 MockDeviceMotionSharedMemoryReader::MockDeviceMotionSharedMemoryReader() |
| 61 : count_start_updating_(0), |
| 62 count_stop_updating_(0), |
| 63 count_latest_data_(0) { |
| 64 } |
| 65 |
| 66 bool MockDeviceMotionSharedMemoryReader::latestDeviceMotionData( |
| 67 WebKit::WebDeviceMotionData& data) { |
| 68 data.accelerationX = 1; |
| 69 data.hasAccelerationX = true; |
| 70 data.accelerationY = 2; |
| 71 data.hasAccelerationY = true; |
| 72 data.accelerationZ = 3; |
| 73 data.hasAccelerationZ = true; |
| 74 count_latest_data_++; |
| 75 return true; |
| 76 } |
| 77 |
| 78 void MockDeviceMotionSharedMemoryReader::startUpdating() { |
| 79 count_start_updating_++; |
| 80 } |
| 81 |
| 82 void MockDeviceMotionSharedMemoryReader::stopUpdating() { |
| 83 count_stop_updating_++; |
| 84 } |
| 85 |
| 86 TEST_F(DeviceMotionEventPumpTest, StartUpdateStop) { |
| 87 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 88 |
| 89 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); |
| 90 MockDeviceMotionSharedMemoryReader* reader = |
| 91 new MockDeviceMotionSharedMemoryReader; |
| 92 scoped_ptr<DeviceMotionEventPump> motion_pump(new DeviceMotionEventPump); |
| 93 |
| 94 motion_pump->setDeviceMotionReader(reader); |
| 95 motion_pump->setListener(listener.get()); |
| 96 |
| 97 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 98 RunAllPendingInMessageLoop(); |
| 99 motion_pump->setListener(0); |
| 100 |
| 101 EXPECT_EQ(1, reader->count_start_updating_); |
| 102 EXPECT_EQ(1, reader->count_latest_data_); |
| 103 EXPECT_EQ(1, reader->count_stop_updating_); |
| 104 |
| 105 EXPECT_TRUE(listener->did_change_device_motion_); |
| 106 EXPECT_EQ(1, listener->data_.accelerationX); |
| 107 EXPECT_TRUE(listener->data_.hasAccelerationX); |
| 108 EXPECT_EQ(2, listener->data_.accelerationY); |
| 109 EXPECT_TRUE(listener->data_.hasAccelerationY); |
| 110 EXPECT_EQ(3, listener->data_.accelerationZ); |
| 111 EXPECT_TRUE(listener->data_.hasAccelerationZ); |
| 112 |
| 113 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityX); |
| 114 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityY); |
| 115 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityZ); |
| 116 |
| 117 EXPECT_FALSE(listener->data_.hasRotationRateAlpha); |
| 118 EXPECT_FALSE(listener->data_.hasRotationRateBeta); |
| 119 EXPECT_FALSE(listener->data_.hasRotationRateGamma); |
| 120 } |
| 121 |
| 122 } // namespace content |
OLD | NEW |