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/public/platform/WebDeviceMotionData.h" |
| 14 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.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 virtual void didChangeDeviceMotion( |
| 26 const WebKit::WebDeviceMotionData&) OVERRIDE; |
| 27 bool did_change_device_motion_; |
| 28 WebKit::WebDeviceMotionData data_; |
| 29 }; |
| 30 |
| 31 MockDeviceMotionListener::MockDeviceMotionListener() |
| 32 : did_change_device_motion_(false) { |
| 33 memset(&data_, 0, sizeof(data_)); |
| 34 } |
| 35 |
| 36 void MockDeviceMotionListener::didChangeDeviceMotion( |
| 37 const WebKit::WebDeviceMotionData& data) { |
| 38 memcpy(&data_, &data, sizeof(data)); |
| 39 did_change_device_motion_ = true; |
| 40 } |
| 41 |
| 42 class MockDeviceMotionSharedMemoryReader |
| 43 : public DeviceMotionSharedMemoryReader { |
| 44 public: |
| 45 MockDeviceMotionSharedMemoryReader(); |
| 46 ~MockDeviceMotionSharedMemoryReader() { } |
| 47 virtual bool GetLatestDeviceMotionData( |
| 48 WebKit::WebDeviceMotionData& data) OVERRIDE; |
| 49 virtual void StartUpdating() OVERRIDE; |
| 50 virtual void StopUpdating() OVERRIDE; |
| 51 int count_start_updating_; |
| 52 int count_stop_updating_; |
| 53 int count_latest_data_; |
| 54 }; |
| 55 |
| 56 MockDeviceMotionSharedMemoryReader::MockDeviceMotionSharedMemoryReader() |
| 57 : count_start_updating_(0), |
| 58 count_stop_updating_(0), |
| 59 count_latest_data_(0) { |
| 60 } |
| 61 |
| 62 bool MockDeviceMotionSharedMemoryReader::GetLatestDeviceMotionData( |
| 63 WebKit::WebDeviceMotionData& data) { |
| 64 data.accelerationX = 1; |
| 65 data.hasAccelerationX = true; |
| 66 data.accelerationY = 2; |
| 67 data.hasAccelerationY = true; |
| 68 data.accelerationZ = 3; |
| 69 data.hasAccelerationZ = true; |
| 70 count_latest_data_++; |
| 71 return true; |
| 72 } |
| 73 |
| 74 void MockDeviceMotionSharedMemoryReader::StartUpdating() { |
| 75 count_start_updating_++; |
| 76 } |
| 77 |
| 78 void MockDeviceMotionSharedMemoryReader::StopUpdating() { |
| 79 count_stop_updating_++; |
| 80 } |
| 81 |
| 82 TEST_F(DeviceMotionEventPumpTest, StartUpdateStop) { |
| 83 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 84 scoped_ptr<MockDeviceMotionListener> listener(new MockDeviceMotionListener); |
| 85 MockDeviceMotionSharedMemoryReader* reader = |
| 86 new MockDeviceMotionSharedMemoryReader; |
| 87 scoped_ptr<DeviceMotionEventPump> motion_pump(new DeviceMotionEventPump); |
| 88 motion_pump->SetDeviceMotionReader(reader); |
| 89 motion_pump->SetListener(listener.get()); |
| 90 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); |
| 91 RunAllPendingInMessageLoop(); |
| 92 motion_pump->SetListener(0); |
| 93 EXPECT_EQ(1, reader->count_start_updating_); |
| 94 EXPECT_EQ(1, reader->count_latest_data_); |
| 95 EXPECT_EQ(1, reader->count_stop_updating_); |
| 96 EXPECT_TRUE(listener->did_change_device_motion_); |
| 97 EXPECT_EQ(1, listener->data_.accelerationX); |
| 98 EXPECT_TRUE(listener->data_.hasAccelerationX); |
| 99 EXPECT_EQ(2, listener->data_.accelerationY); |
| 100 EXPECT_TRUE(listener->data_.hasAccelerationY); |
| 101 EXPECT_EQ(3, listener->data_.accelerationZ); |
| 102 EXPECT_TRUE(listener->data_.hasAccelerationZ); |
| 103 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityX); |
| 104 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityY); |
| 105 EXPECT_FALSE(listener->data_.hasAccelerationIncludingGravityZ); |
| 106 EXPECT_FALSE(listener->data_.hasRotationRateAlpha); |
| 107 EXPECT_FALSE(listener->data_.hasRotationRateBeta); |
| 108 EXPECT_FALSE(listener->data_.hasRotationRateGamma); |
| 109 } |
| 110 |
| 111 } // namespace content |
OLD | NEW |