| 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 <Objbase.h> | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <cmath> | |
| 9 | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop.h" | |
| 13 #include "base/scoped_ptr.h" | |
| 14 #include "base/time.h" | |
| 15 #include "chrome/common/geoposition.h" | |
| 16 #include "chrome/browser/geolocation/win7_location_api_win.h" | |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using testing::_; | |
| 21 using testing::AtLeast; | |
| 22 using testing::DoDefault; | |
| 23 using testing::Invoke; | |
| 24 using testing::Return; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class MockLatLongReport : public ILatLongReport { | |
| 29 public: | |
| 30 MockLatLongReport() : ref_count_(1) { | |
| 31 ON_CALL(*this, GetAltitude(_)) | |
| 32 .WillByDefault(Invoke(this, &MockLatLongReport::GetAltitudeValid)); | |
| 33 ON_CALL(*this, GetAltitudeError(_)) | |
| 34 .WillByDefault(Invoke(this, | |
| 35 &MockLatLongReport::GetAltitudeErrorValid)); | |
| 36 ON_CALL(*this, GetErrorRadius(_)) | |
| 37 .WillByDefault(Invoke(this, &MockLatLongReport::GetErrorRadiusValid)); | |
| 38 ON_CALL(*this, GetLatitude(_)) | |
| 39 .WillByDefault(Invoke(this, &MockLatLongReport::GetLatitudeValid)); | |
| 40 ON_CALL(*this, GetLongitude(_)) | |
| 41 .WillByDefault(Invoke(this, &MockLatLongReport::GetLongitudeValid)); | |
| 42 ON_CALL(*this, GetValue(_, _)) | |
| 43 .WillByDefault(Invoke(this, &MockLatLongReport::GetValueValid)); | |
| 44 ON_CALL(*this, Release()) | |
| 45 .WillByDefault(Invoke(this, &MockLatLongReport::ReleaseInternal)); | |
| 46 ON_CALL(*this, AddRef()) | |
| 47 .WillByDefault(Invoke(this, &MockLatLongReport::AddRefInternal)); | |
| 48 } | |
| 49 | |
| 50 // ILatLongReport | |
| 51 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 52 GetAltitude, | |
| 53 HRESULT(DOUBLE*)); | |
| 54 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 55 GetAltitudeError, | |
| 56 HRESULT(DOUBLE*)); | |
| 57 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 58 GetErrorRadius, | |
| 59 HRESULT(DOUBLE*)); | |
| 60 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 61 GetLatitude, | |
| 62 HRESULT(DOUBLE*)); | |
| 63 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 64 GetLongitude, | |
| 65 HRESULT(DOUBLE*)); | |
| 66 // ILocationReport | |
| 67 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 68 GetSensorID, | |
| 69 HRESULT(SENSOR_ID*)); | |
| 70 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 71 GetTimestamp, | |
| 72 HRESULT(SYSTEMTIME*)); | |
| 73 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 74 GetValue, | |
| 75 HRESULT(REFPROPERTYKEY, PROPVARIANT*)); | |
| 76 // IUnknown | |
| 77 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 78 QueryInterface, | |
| 79 HRESULT(REFIID, void**)); | |
| 80 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 81 AddRef, | |
| 82 ULONG()); | |
| 83 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 84 Release, | |
| 85 ULONG()); | |
| 86 | |
| 87 HRESULT GetAltitudeValid(DOUBLE* altitude) { | |
| 88 *altitude = 20.5; | |
| 89 return S_OK; | |
| 90 } | |
| 91 HRESULT GetAltitudeErrorValid(DOUBLE* altitude_error) { | |
| 92 *altitude_error = 10.0; | |
| 93 return S_OK; | |
| 94 } | |
| 95 HRESULT GetErrorRadiusValid(DOUBLE* error) { | |
| 96 *error = 5.0; | |
| 97 return S_OK; | |
| 98 } | |
| 99 HRESULT GetLatitudeValid(DOUBLE* latitude) { | |
| 100 *latitude = 51.0; | |
| 101 return S_OK; | |
| 102 } | |
| 103 HRESULT GetLongitudeValid(DOUBLE* longitude) { | |
| 104 *longitude = -0.1; | |
| 105 return S_OK; | |
| 106 } | |
| 107 HRESULT GetValueValid(REFPROPERTYKEY prop_key, PROPVARIANT* prop) { | |
| 108 prop->dblVal = 10.0; | |
| 109 return S_OK; | |
| 110 } | |
| 111 | |
| 112 private: | |
| 113 ~MockLatLongReport() {} | |
| 114 | |
| 115 ULONG AddRefInternal() { | |
| 116 return InterlockedIncrement(&ref_count_); | |
| 117 } | |
| 118 ULONG ReleaseInternal() { | |
| 119 LONG new_ref_count = InterlockedDecrement(&ref_count_); | |
| 120 if (0 == new_ref_count) | |
| 121 delete this; | |
| 122 return new_ref_count; | |
| 123 } | |
| 124 | |
| 125 LONG ref_count_; | |
| 126 }; | |
| 127 | |
| 128 class MockReport : public ILocationReport { | |
| 129 public: | |
| 130 MockReport() : ref_count_(1) { | |
| 131 mock_lat_long_report_ = | |
| 132 new MockLatLongReport(); | |
| 133 ON_CALL(*this, QueryInterface(_, _)) | |
| 134 .WillByDefault(Invoke(this, &MockReport::QueryInterfaceValid)); | |
| 135 ON_CALL(*this, Release()) | |
| 136 .WillByDefault(Invoke(this, &MockReport::ReleaseInternal)); | |
| 137 ON_CALL(*this, AddRef()) | |
| 138 .WillByDefault(Invoke(this, &MockReport::AddRefInternal)); | |
| 139 } | |
| 140 | |
| 141 // ILocationReport | |
| 142 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 143 GetSensorID, | |
| 144 HRESULT(SENSOR_ID*)); | |
| 145 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 146 GetTimestamp, | |
| 147 HRESULT(SYSTEMTIME*)); | |
| 148 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 149 GetValue, | |
| 150 HRESULT(REFPROPERTYKEY, PROPVARIANT*)); | |
| 151 // IUnknown | |
| 152 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 153 QueryInterface, | |
| 154 HRESULT(REFIID, void**)); | |
| 155 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 156 AddRef, | |
| 157 ULONG()); | |
| 158 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 159 Release, | |
| 160 ULONG()); | |
| 161 | |
| 162 MockLatLongReport* mock_lat_long_report_; | |
| 163 | |
| 164 private: | |
| 165 ~MockReport() { | |
| 166 mock_lat_long_report_->Release(); | |
| 167 } | |
| 168 | |
| 169 ULONG AddRefInternal() { | |
| 170 return InterlockedIncrement(&ref_count_); | |
| 171 } | |
| 172 ULONG ReleaseInternal() { | |
| 173 LONG new_ref_count = InterlockedDecrement(&ref_count_); | |
| 174 if (0 == new_ref_count) | |
| 175 delete this; | |
| 176 return new_ref_count; | |
| 177 } | |
| 178 HRESULT QueryInterfaceValid(REFIID id, void** report) { | |
| 179 EXPECT_TRUE(id == IID_ILatLongReport); | |
| 180 *report = reinterpret_cast<ILatLongReport*>(mock_lat_long_report_); | |
| 181 mock_lat_long_report_->AddRef(); | |
| 182 return S_OK; | |
| 183 } | |
| 184 | |
| 185 LONG ref_count_; | |
| 186 }; | |
| 187 | |
| 188 class MockLocation : public ILocation { | |
| 189 public: | |
| 190 MockLocation() : ref_count_(1) { | |
| 191 mock_report_ = new MockReport(); | |
| 192 ON_CALL(*this, SetDesiredAccuracy(_, _)) | |
| 193 .WillByDefault(Return(S_OK)); | |
| 194 ON_CALL(*this, GetReport(_, _)) | |
| 195 .WillByDefault(Invoke(this, &MockLocation::GetReportValid)); | |
| 196 ON_CALL(*this, RequestPermissions(_, _, _, _)) | |
| 197 .WillByDefault(Return(S_OK)); | |
| 198 ON_CALL(*this, AddRef()) | |
| 199 .WillByDefault(Invoke(this, &MockLocation::AddRefInternal)); | |
| 200 ON_CALL(*this, Release()) | |
| 201 .WillByDefault(Invoke(this, &MockLocation::ReleaseInternal)); | |
| 202 } | |
| 203 | |
| 204 // ILocation | |
| 205 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 206 GetDesiredAccuracy, | |
| 207 HRESULT(REFIID, LOCATION_DESIRED_ACCURACY*)); | |
| 208 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 209 GetReport, | |
| 210 HRESULT(REFIID, ILocationReport**)); | |
| 211 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 212 GetReportInterval, | |
| 213 HRESULT(REFIID, DWORD*)); | |
| 214 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 215 GetReportStatus, | |
| 216 HRESULT(REFIID, LOCATION_REPORT_STATUS*)); | |
| 217 MOCK_METHOD4_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 218 RequestPermissions, | |
| 219 HRESULT(HWND, IID*, ULONG, BOOL)); | |
| 220 MOCK_METHOD3_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 221 RegisterForReport, | |
| 222 HRESULT(ILocationEvents*, REFIID, DWORD)); | |
| 223 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 224 SetDesiredAccuracy, | |
| 225 HRESULT(REFIID, LOCATION_DESIRED_ACCURACY)); | |
| 226 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 227 SetReportInterval, | |
| 228 HRESULT(REFIID, DWORD)); | |
| 229 MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 230 UnregisterForReport, | |
| 231 HRESULT(REFIID)); | |
| 232 // IUnknown | |
| 233 MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 234 QueryInterface, | |
| 235 HRESULT(REFIID, void**)); | |
| 236 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 237 AddRef, | |
| 238 ULONG()); | |
| 239 MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, | |
| 240 Release, | |
| 241 ULONG()); | |
| 242 | |
| 243 MockReport* mock_report_; | |
| 244 | |
| 245 private: | |
| 246 ~MockLocation() { | |
| 247 mock_report_->Release(); | |
| 248 } | |
| 249 | |
| 250 HRESULT GetReportValid(REFIID report_type, | |
| 251 ILocationReport** location_report) { | |
| 252 *location_report = reinterpret_cast<ILocationReport*>(mock_report_); | |
| 253 mock_report_->AddRef(); | |
| 254 return S_OK; | |
| 255 } | |
| 256 ULONG AddRefInternal() { | |
| 257 return InterlockedIncrement(&ref_count_); | |
| 258 } | |
| 259 ULONG ReleaseInternal() { | |
| 260 LONG new_ref_count = InterlockedDecrement(&ref_count_); | |
| 261 if (0 == new_ref_count) | |
| 262 delete this; | |
| 263 return new_ref_count; | |
| 264 } | |
| 265 | |
| 266 LONG ref_count_; | |
| 267 }; | |
| 268 | |
| 269 | |
| 270 HRESULT __stdcall MockPropVariantToDoubleFunction(REFPROPVARIANT propvarIn, | |
| 271 DOUBLE *pdblRet) { | |
| 272 CHECK_EQ(10.0, propvarIn.dblVal); | |
| 273 *pdblRet = 10.0; | |
| 274 return S_OK; | |
| 275 } | |
| 276 | |
| 277 // TODO(allanwoj): Either make mock classes into NiceMock classes | |
| 278 // or check every mock method call. | |
| 279 class GeolocationApiWin7Tests : public testing::Test { | |
| 280 public: | |
| 281 GeolocationApiWin7Tests() { | |
| 282 } | |
| 283 virtual void SetUp() { | |
| 284 api_.reset(CreateMock()); | |
| 285 report_ = locator_->mock_report_; | |
| 286 lat_long_report_ = report_->mock_lat_long_report_; | |
| 287 } | |
| 288 virtual void TearDown() { | |
| 289 locator_->Release(); | |
| 290 api_.reset(); | |
| 291 } | |
| 292 ~GeolocationApiWin7Tests() { | |
| 293 } | |
| 294 protected: | |
| 295 Win7LocationApi* CreateMock() { | |
| 296 MockLocation* locator = new MockLocation(); | |
| 297 locator_ = locator; | |
| 298 return Win7LocationApi::CreateForTesting(&MockPropVariantToDoubleFunction, | |
| 299 locator); | |
| 300 } | |
| 301 | |
| 302 scoped_ptr<Win7LocationApi> api_; | |
| 303 MockLatLongReport* lat_long_report_; | |
| 304 MockLocation* locator_; | |
| 305 MockReport* report_; | |
| 306 }; | |
| 307 | |
| 308 TEST_F(GeolocationApiWin7Tests, PermissionDenied) { | |
| 309 EXPECT_CALL(*locator_, GetReport(_, _)) | |
| 310 .Times(AtLeast(1)) | |
| 311 .WillRepeatedly(Return(E_ACCESSDENIED)); | |
| 312 Geoposition position; | |
| 313 api_->GetPosition(&position); | |
| 314 EXPECT_EQ(Geoposition::ERROR_CODE_PERMISSION_DENIED, | |
| 315 position.error_code); | |
| 316 } | |
| 317 | |
| 318 TEST_F(GeolocationApiWin7Tests, GetValidPosition) { | |
| 319 EXPECT_CALL(*locator_, GetReport(_, _)) | |
| 320 .Times(AtLeast(1)); | |
| 321 Geoposition position; | |
| 322 api_->GetPosition(&position); | |
| 323 EXPECT_TRUE(position.IsValidFix()); | |
| 324 } | |
| 325 | |
| 326 TEST_F(GeolocationApiWin7Tests, GetInvalidPosition) { | |
| 327 EXPECT_CALL(*lat_long_report_, GetLatitude(_)) | |
| 328 .Times(AtLeast(1)) | |
| 329 .WillRepeatedly(Return(HRESULT_FROM_WIN32(ERROR_NO_DATA))); | |
| 330 EXPECT_CALL(*locator_, GetReport(_, _)) | |
| 331 .Times(AtLeast(1)); | |
| 332 Geoposition position; | |
| 333 api_->GetPosition(&position); | |
| 334 EXPECT_FALSE(position.IsValidFix()); | |
| 335 } | |
| 336 | |
| 337 } // namespace | |
| OLD | NEW |