| 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 "chrome/browser/geolocation/gps_location_provider_linux.h" | |
| 6 | |
| 7 #include "chrome/browser/browser_thread.h" | |
| 8 #include "chrome/browser/geolocation/libgps_wrapper_linux.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 struct gps_data_t { | |
| 12 }; | |
| 13 | |
| 14 namespace { | |
| 15 class MockLibGps : public LibGps { | |
| 16 public: | |
| 17 MockLibGps(); | |
| 18 ~MockLibGps(); | |
| 19 | |
| 20 virtual bool StartStreaming() { | |
| 21 ++start_streaming_calls_; | |
| 22 return start_streaming_ret_; | |
| 23 } | |
| 24 virtual bool DataWaiting() { | |
| 25 EXPECT_GT(start_streaming_calls_, 0); | |
| 26 ++data_waiting_calls_; | |
| 27 // Toggle the return value, so the poll loop will exit once per test step. | |
| 28 return (data_waiting_calls_ & 1) != 0; | |
| 29 } | |
| 30 virtual bool GetPositionIfFixed(Geoposition* position) { | |
| 31 CHECK(position); | |
| 32 EXPECT_GT(start_streaming_calls_, 0); | |
| 33 EXPECT_GT(data_waiting_calls_, 0); | |
| 34 ++get_position_calls_; | |
| 35 *position = get_position_; | |
| 36 return get_position_ret_; | |
| 37 | |
| 38 } | |
| 39 int start_streaming_calls_; | |
| 40 bool start_streaming_ret_; | |
| 41 int data_waiting_calls_; | |
| 42 int get_position_calls_; | |
| 43 Geoposition get_position_; | |
| 44 bool get_position_ret_; | |
| 45 static MockLibGps* g_instance_; | |
| 46 }; | |
| 47 | |
| 48 class LocaionProviderListenerLoopQuitter | |
| 49 : public LocationProviderBase::ListenerInterface { | |
| 50 // LocationProviderBase::ListenerInterface | |
| 51 virtual void LocationUpdateAvailable(LocationProviderBase* provider) { | |
| 52 MessageLoop::current()->Quit(); | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 class GeolocationGpsProviderLinuxTests : public testing::Test { | |
| 57 public: | |
| 58 GeolocationGpsProviderLinuxTests(); | |
| 59 ~GeolocationGpsProviderLinuxTests(); | |
| 60 | |
| 61 static LibGps* NewMockLibGps() { | |
| 62 return new MockLibGps; | |
| 63 } | |
| 64 static LibGps* NoLibGpsFactory() { | |
| 65 return NULL; | |
| 66 } | |
| 67 | |
| 68 protected: | |
| 69 MessageLoop message_loop_; | |
| 70 BrowserThread ui_thread_; | |
| 71 LocaionProviderListenerLoopQuitter location_listener_; | |
| 72 scoped_ptr<GpsLocationProviderLinux> provider_; | |
| 73 }; | |
| 74 | |
| 75 gps_data_t* gps_open_stub(const char*, const char*) { | |
| 76 // Need to return a non-NULL value here to indicate success, however we don't | |
| 77 // need (or want) a valid pointer as it should never be dereferenced. | |
| 78 return static_cast<gps_data_t*>(NULL) + 1; | |
| 79 } | |
| 80 int gps_close_stub(gps_data_t*) { | |
| 81 return 0; | |
| 82 } | |
| 83 int gps_poll_stub(gps_data_t*) { | |
| 84 return 0; | |
| 85 } | |
| 86 // v2.34 only | |
| 87 int gps_query_stub(gps_data_t*, const char*, ...) { | |
| 88 return 0; | |
| 89 } | |
| 90 // v2.90+ | |
| 91 int gps_stream_stub(gps_data_t*, unsigned int, void*) { | |
| 92 return 0; | |
| 93 } | |
| 94 bool gps_waiting_stub(gps_data_t*) { | |
| 95 return 0; | |
| 96 } | |
| 97 | |
| 98 void CheckValidPosition(const Geoposition& expected, | |
| 99 const Geoposition& actual) { | |
| 100 EXPECT_TRUE(actual.IsValidFix()); | |
| 101 EXPECT_DOUBLE_EQ(expected.latitude, actual.latitude); | |
| 102 EXPECT_DOUBLE_EQ(expected.longitude, actual.longitude); | |
| 103 EXPECT_DOUBLE_EQ(expected.accuracy, actual.accuracy); | |
| 104 } | |
| 105 | |
| 106 MockLibGps* MockLibGps::g_instance_ = NULL; | |
| 107 | |
| 108 MockLibGps::MockLibGps() | |
| 109 : LibGps(new LibGpsLibraryWrapper(NULL, | |
| 110 gps_open_stub, | |
| 111 gps_close_stub, | |
| 112 gps_poll_stub, | |
| 113 gps_query_stub, | |
| 114 gps_stream_stub, | |
| 115 gps_waiting_stub)), | |
| 116 start_streaming_calls_(0), | |
| 117 start_streaming_ret_(true), | |
| 118 data_waiting_calls_(0), | |
| 119 get_position_calls_(0), | |
| 120 get_position_ret_(true) { | |
| 121 get_position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 122 EXPECT_FALSE(g_instance_); | |
| 123 g_instance_ = this; | |
| 124 } | |
| 125 | |
| 126 MockLibGps::~MockLibGps() { | |
| 127 EXPECT_EQ(this, g_instance_); | |
| 128 g_instance_ = NULL; | |
| 129 } | |
| 130 | |
| 131 GeolocationGpsProviderLinuxTests::GeolocationGpsProviderLinuxTests() | |
| 132 : ui_thread_(BrowserThread::IO, &message_loop_), | |
| 133 provider_(new GpsLocationProviderLinux(NewMockLibGps)) { | |
| 134 provider_->RegisterListener(&location_listener_); | |
| 135 } | |
| 136 | |
| 137 GeolocationGpsProviderLinuxTests::~GeolocationGpsProviderLinuxTests() { | |
| 138 provider_->UnregisterListener(&location_listener_); | |
| 139 } | |
| 140 | |
| 141 TEST_F(GeolocationGpsProviderLinuxTests, NoLibGpsInstalled) { | |
| 142 provider_.reset(new GpsLocationProviderLinux(NoLibGpsFactory)); | |
| 143 ASSERT_TRUE(provider_.get()); | |
| 144 const bool ok = provider_->StartProvider(true); | |
| 145 EXPECT_FALSE(ok); | |
| 146 Geoposition position; | |
| 147 provider_->GetPosition(&position); | |
| 148 EXPECT_TRUE(position.IsInitialized()); | |
| 149 EXPECT_FALSE(position.IsValidFix()); | |
| 150 EXPECT_EQ(Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, position.error_code); | |
| 151 } | |
| 152 | |
| 153 TEST_F(GeolocationGpsProviderLinuxTests, GetPosition) { | |
| 154 ASSERT_TRUE(provider_.get()); | |
| 155 const bool ok = provider_->StartProvider(true); | |
| 156 EXPECT_TRUE(ok); | |
| 157 ASSERT_TRUE(MockLibGps::g_instance_); | |
| 158 EXPECT_EQ(0, MockLibGps::g_instance_->start_streaming_calls_); | |
| 159 EXPECT_EQ(0, MockLibGps::g_instance_->data_waiting_calls_); | |
| 160 EXPECT_EQ(0, MockLibGps::g_instance_->get_position_calls_); | |
| 161 Geoposition position; | |
| 162 provider_->GetPosition(&position); | |
| 163 EXPECT_TRUE(position.IsInitialized()); | |
| 164 EXPECT_FALSE(position.IsValidFix()); | |
| 165 EXPECT_EQ(Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, position.error_code); | |
| 166 MockLibGps::g_instance_->get_position_.error_code = | |
| 167 Geoposition::ERROR_CODE_NONE; | |
| 168 MockLibGps::g_instance_->get_position_.latitude = 4.5; | |
| 169 MockLibGps::g_instance_->get_position_.longitude = -34.1; | |
| 170 MockLibGps::g_instance_->get_position_.accuracy = 345; | |
| 171 MockLibGps::g_instance_->get_position_.timestamp = | |
| 172 base::Time::FromDoubleT(200); | |
| 173 EXPECT_TRUE(MockLibGps::g_instance_->get_position_.IsValidFix()); | |
| 174 | |
| 175 MessageLoop::current()->Run(); | |
| 176 EXPECT_GT(MockLibGps::g_instance_->start_streaming_calls_, 0); | |
| 177 EXPECT_GT(MockLibGps::g_instance_->data_waiting_calls_, 0); | |
| 178 EXPECT_EQ(1, MockLibGps::g_instance_->get_position_calls_); | |
| 179 provider_->GetPosition(&position); | |
| 180 CheckValidPosition(MockLibGps::g_instance_->get_position_, position); | |
| 181 | |
| 182 // Movement. This will block for up to half a second. | |
| 183 MockLibGps::g_instance_->get_position_.latitude += 0.01; | |
| 184 MessageLoop::current()->Run(); | |
| 185 provider_->GetPosition(&position); | |
| 186 EXPECT_EQ(2, MockLibGps::g_instance_->get_position_calls_); | |
| 187 CheckValidPosition(MockLibGps::g_instance_->get_position_, position); | |
| 188 } | |
| 189 | |
| 190 // TODO(joth): Add a test for LibGps::Start() returning false (i.e. gpsd not | |
| 191 // running). Need to work around the 10s reconnect delay (either by injecting | |
| 192 // a shorter retry interval, or adapt MessageLoop / Time::Now to be more test | |
| 193 // friendly). | |
| 194 | |
| 195 } // namespace | |
| OLD | NEW |