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