| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "base/bind.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "content/browser/geolocation/win7_location_api_win.h" | |
| 8 #include "content/browser/geolocation/win7_location_provider_win.h" | |
| 9 #include "content/public/common/geoposition.h" | |
| 10 #include "testing/gmock/include/gmock/gmock.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using testing::_; | |
| 14 using testing::AtLeast; | |
| 15 using testing::DoDefault; | |
| 16 using testing::Invoke; | |
| 17 using testing::Return; | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 class MockWin7LocationApi : public Win7LocationApi { | |
| 22 public: | |
| 23 static MockWin7LocationApi* CreateMock() { | |
| 24 return new MockWin7LocationApi(); | |
| 25 } | |
| 26 | |
| 27 // Used to signal when the destructor is called. | |
| 28 MOCK_METHOD0(Die, void()); | |
| 29 // Win7LocationApi | |
| 30 MOCK_METHOD1(GetPosition, void(Geoposition*)); | |
| 31 MOCK_METHOD1(SetHighAccuracy, bool(bool)); | |
| 32 | |
| 33 virtual ~MockWin7LocationApi() { | |
| 34 Die(); | |
| 35 } | |
| 36 | |
| 37 void GetPositionValid(Geoposition* position) { | |
| 38 position->latitude = 4.5; | |
| 39 position->longitude = -34.1; | |
| 40 position->accuracy = 0.5; | |
| 41 position->timestamp = base::Time::FromDoubleT(200); | |
| 42 position->error_code = Geoposition::ERROR_CODE_NONE; | |
| 43 } | |
| 44 void GetPositionInvalid(Geoposition* position) { | |
| 45 position->latitude = 4.5; | |
| 46 position->longitude = -340000.1; | |
| 47 position->accuracy = 0.5; | |
| 48 position->timestamp = base::Time::FromDoubleT(200); | |
| 49 position->error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 MockWin7LocationApi() { | |
| 54 ON_CALL(*this, GetPosition(_)) | |
| 55 .WillByDefault(Invoke(this, | |
| 56 &MockWin7LocationApi::GetPositionValid)); | |
| 57 ON_CALL(*this, SetHighAccuracy(true)) | |
| 58 .WillByDefault(Return(true)); | |
| 59 ON_CALL(*this, SetHighAccuracy(false)) | |
| 60 .WillByDefault(Return(false)); | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 class LocationProviderListenerLoopQuitter { | |
| 65 public: | |
| 66 explicit LocationProviderListenerLoopQuitter(base::MessageLoop* message_loop) | |
| 67 : message_loop_to_quit_(message_loop) { | |
| 68 CHECK(message_loop_to_quit_ != NULL); | |
| 69 } | |
| 70 | |
| 71 void LocationUpdateAvailable(const LocationProvider* provider, | |
| 72 const Geoposition& position) { | |
| 73 EXPECT_EQ(base::MessageLoop::current(), message_loop_to_quit_); | |
| 74 provider_ = provider; | |
| 75 message_loop_to_quit_->Quit(); | |
| 76 } | |
| 77 | |
| 78 base::MessageLoop* message_loop_to_quit_; | |
| 79 const LocationProvider* provider_; | |
| 80 }; | |
| 81 | |
| 82 class GeolocationProviderWin7Tests : public testing::Test { | |
| 83 public: | |
| 84 GeolocationProviderWin7Tests(): location_listener_(&main_message_loop_) { | |
| 85 } | |
| 86 | |
| 87 virtual void SetUp() { | |
| 88 api_ = MockWin7LocationApi::CreateMock(); | |
| 89 provider_ = new Win7LocationProvider(api_); | |
| 90 provider_->SetUpdateCallback(base::Bind( | |
| 91 &LocationProviderListenerLoopQuitter::LocationUpdateAvailable, | |
| 92 base::Unretained(&location_listener_))); | |
| 93 } | |
| 94 virtual void TearDown() { | |
| 95 provider_->StopProvider(); | |
| 96 delete provider_; | |
| 97 main_message_loop_.RunUntilIdle(); | |
| 98 } | |
| 99 | |
| 100 protected: | |
| 101 Win7LocationProvider* provider() { | |
| 102 return static_cast<Win7LocationProvider*>(provider_); | |
| 103 } | |
| 104 | |
| 105 MockWin7LocationApi* api_; | |
| 106 LocationProviderListenerLoopQuitter location_listener_; | |
| 107 base::MessageLoop main_message_loop_; | |
| 108 LocationProvider* provider_; | |
| 109 }; | |
| 110 | |
| 111 TEST_F(GeolocationProviderWin7Tests, StartStop) { | |
| 112 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 113 .WillOnce(Return(true)); | |
| 114 EXPECT_TRUE(provider()->StartProvider(true)); | |
| 115 provider()->StopProvider(); | |
| 116 EXPECT_CALL(*api_, SetHighAccuracy(false)) | |
| 117 .WillOnce(Return(true)); | |
| 118 EXPECT_TRUE(provider()->StartProvider(false)); | |
| 119 } | |
| 120 | |
| 121 TEST_F(GeolocationProviderWin7Tests, GetValidPosition) { | |
| 122 EXPECT_CALL(*api_, GetPosition(_)) | |
| 123 .Times(AtLeast(1)); | |
| 124 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 125 .WillOnce(Return(true)); | |
| 126 EXPECT_TRUE(provider()->StartProvider(true)); | |
| 127 main_message_loop_.Run(); | |
| 128 Geoposition position; | |
| 129 provider()->GetPosition(&position); | |
| 130 EXPECT_TRUE(position.Validate()); | |
| 131 } | |
| 132 | |
| 133 TEST_F(GeolocationProviderWin7Tests, GetInvalidPosition) { | |
| 134 EXPECT_CALL(*api_, GetPosition(_)) | |
| 135 .Times(AtLeast(1)) | |
| 136 .WillRepeatedly(Invoke(api_, | |
| 137 &MockWin7LocationApi::GetPositionInvalid)); | |
| 138 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 139 .WillOnce(Return(true)); | |
| 140 EXPECT_TRUE(provider()->StartProvider(true)); | |
| 141 main_message_loop_.Run(); | |
| 142 Geoposition position; | |
| 143 provider()->GetPosition(&position); | |
| 144 EXPECT_FALSE(position.Validate()); | |
| 145 } | |
| 146 | |
| 147 } // namespace content | |
| OLD | NEW |