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 "content/browser/device_orientation/device_motion_provider.h" 6 7 #include "base/logging.h" 8 #include "base/memory/scoped_ptr.h" 9 #include "base/synchronization/waitable_event.h" 10 #include "content/browser/device_orientation/data_fetcher_shared_memory.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace content { 14 15 namespace { 16 17 const int kPeriodInMilliseconds = 100; 18 19 class FakeDataFetcherSharedMemory : public DataFetcherSharedMemory { 20 public: 21 FakeDataFetcherSharedMemory() 22 : start_fetching_data_(false, false), 23 stop_fetching_data_(false, false), 24 fetched_data_(false, false) { 25 } 26 virtual ~FakeDataFetcherSharedMemory() { } 27 28 virtual bool NeedsPolling() OVERRIDE { 29 return true; 30 } 31 32 virtual bool FetchDeviceMotionDataIntoBuffer() OVERRIDE { 33 buffer_->seqlock.WriteBegin(); 34 buffer_->data.interval = kPeriodInMilliseconds; 35 buffer_->seqlock.WriteEnd(); 36 fetched_data_.Signal(); 37 return true; 38 } 39 40 virtual bool StartFetchingDeviceMotionData( 41 DeviceMotionHardwareBuffer* buffer) OVERRIDE { 42 buffer_ = buffer; 43 start_fetching_data_.Signal(); 44 return true; 45 } 46 47 virtual void StopFetchingDeviceMotionData() OVERRIDE { 48 stop_fetching_data_.Signal(); 49 } 50 51 void WaitForStart() { 52 start_fetching_data_.Wait(); 53 } 54 55 void WaitForStop() { 56 stop_fetching_data_.Wait(); 57 } 58 59 void WaitForDataFetch() { 60 fetched_data_.Wait(); 61 } 62 63 DeviceMotionHardwareBuffer* GetBuffer() { 64 return buffer_; 65 } 66 67 private: 68 base::WaitableEvent start_fetching_data_; 69 base::WaitableEvent stop_fetching_data_; 70 base::WaitableEvent fetched_data_; 71 DeviceMotionHardwareBuffer* buffer_; 72 73 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcherSharedMemory); 74 }; 75 76 77 TEST(DeviceMotionProviderTest, DoesPolling) { 78 FakeDataFetcherSharedMemory* mock_data_fetcher = 79 new FakeDataFetcherSharedMemory(); 80 EXPECT_TRUE(mock_data_fetcher->NeedsPolling()); 81 82 scoped_ptr<DeviceMotionProvider> provider(new DeviceMotionProvider( 83 scoped_ptr<DataFetcherSharedMemory>(mock_data_fetcher))); 84 85 provider->StartFetchingDeviceMotionData(); 86 mock_data_fetcher->WaitForStart(); 87 mock_data_fetcher->WaitForDataFetch(); 88 89 EXPECT_EQ(kPeriodInMilliseconds, 90 mock_data_fetcher->GetBuffer()->data.interval); 91 92 provider->StopFetchingDeviceMotionData(); 93 mock_data_fetcher->WaitForStop(); 94 } 95 96 } // namespace 97 98 } // namespace content 99