| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/message_loop.h" | |
| 6 #include "content/browser/geolocation/win7_location_provider_win.h" | |
| 7 #include "content/browser/geolocation/win7_location_api_win.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 struct Geoposition; | |
| 12 | |
| 13 using testing::_; | |
| 14 using testing::AtLeast; | |
| 15 using testing::DoDefault; | |
| 16 using testing::Invoke; | |
| 17 using testing::Return; | |
| 18 | |
| 19 namespace { | |
| 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 LocationProviderBase::ListenerInterface { | |
| 66 public: | |
| 67 explicit LocationProviderListenerLoopQuitter(MessageLoop* message_loop) | |
| 68 : message_loop_to_quit_(message_loop) { | |
| 69 CHECK(message_loop_to_quit_ != NULL); | |
| 70 } | |
| 71 virtual void LocationUpdateAvailable(LocationProviderBase* provider) { | |
| 72 EXPECT_EQ(MessageLoop::current(), message_loop_to_quit_); | |
| 73 provider_ = provider; | |
| 74 message_loop_to_quit_->Quit(); | |
| 75 } | |
| 76 | |
| 77 MessageLoop* message_loop_to_quit_; | |
| 78 LocationProviderBase* provider_; | |
| 79 }; | |
| 80 | |
| 81 class GeolocationProviderWin7Tests : public testing::Test { | |
| 82 public: | |
| 83 GeolocationProviderWin7Tests(): location_listener_(&main_message_loop_) { | |
| 84 } | |
| 85 | |
| 86 virtual void SetUp() { | |
| 87 api_ = MockWin7LocationApi::CreateMock(); | |
| 88 provider_ = new Win7LocationProvider(api_); | |
| 89 provider_->RegisterListener(&location_listener_); | |
| 90 } | |
| 91 virtual void TearDown() { | |
| 92 provider_->UnregisterListener(&location_listener_); | |
| 93 provider_->StopProvider(); | |
| 94 delete provider_; | |
| 95 main_message_loop_.RunAllPending(); | |
| 96 } | |
| 97 | |
| 98 protected: | |
| 99 MockWin7LocationApi* api_; | |
| 100 LocationProviderListenerLoopQuitter location_listener_; | |
| 101 MessageLoop main_message_loop_; | |
| 102 Win7LocationProvider* provider_; | |
| 103 }; | |
| 104 | |
| 105 TEST_F(GeolocationProviderWin7Tests, StartStop) { | |
| 106 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 107 .WillOnce(Return(true)); | |
| 108 EXPECT_TRUE(provider_->StartProvider(true)); | |
| 109 provider_->StopProvider(); | |
| 110 EXPECT_CALL(*api_, SetHighAccuracy(false)) | |
| 111 .WillOnce(Return(true)); | |
| 112 EXPECT_TRUE(provider_->StartProvider(false)); | |
| 113 } | |
| 114 | |
| 115 TEST_F(GeolocationProviderWin7Tests, GetValidPosition) { | |
| 116 EXPECT_CALL(*api_, GetPosition(_)) | |
| 117 .Times(AtLeast(1)); | |
| 118 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 119 .WillOnce(Return(true)); | |
| 120 EXPECT_TRUE(provider_->StartProvider(true)); | |
| 121 main_message_loop_.Run(); | |
| 122 Geoposition position; | |
| 123 provider_->GetPosition(&position); | |
| 124 EXPECT_TRUE(position.IsValidFix()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(GeolocationProviderWin7Tests, GetInvalidPosition) { | |
| 128 EXPECT_CALL(*api_, GetPosition(_)) | |
| 129 .Times(AtLeast(1)) | |
| 130 .WillRepeatedly(Invoke(api_, | |
| 131 &MockWin7LocationApi::GetPositionInvalid)); | |
| 132 EXPECT_CALL(*api_, SetHighAccuracy(true)) | |
| 133 .WillOnce(Return(true)); | |
| 134 EXPECT_TRUE(provider_->StartProvider(true)); | |
| 135 main_message_loop_.Run(); | |
| 136 Geoposition position; | |
| 137 provider_->GetPosition(&position); | |
| 138 EXPECT_FALSE(position.IsValidFix()); | |
| 139 } | |
| 140 | |
| 141 } // namespace | |
| OLD | NEW |