Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file implements a mock location provider and the factory functions for | 5 // This file implements a fake location provider and the factory functions for |
| 6 // various ways of creating it. | 6 // various ways of creating it. |
| 7 | 7 |
| 8 #include "device/geolocation/mock_location_provider.h" | 8 #include "device/geolocation/fake_location_provider.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 | 17 |
| 18 namespace device { | 18 namespace device { |
| 19 | 19 |
| 20 MockLocationProvider::MockLocationProvider() | 20 FakeLocationProvider::FakeLocationProvider() |
| 21 : state_(STOPPED), | 21 : state_(STOPPED), |
| 22 is_permission_granted_(false), | 22 is_permission_granted_(false), |
| 23 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | 23 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| 24 | 24 |
| 25 MockLocationProvider::~MockLocationProvider() {} | 25 FakeLocationProvider::~FakeLocationProvider() {} |
| 26 | 26 |
| 27 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { | 27 void FakeLocationProvider::HandlePositionChanged(const Geoposition& position) { |
| 28 if (provider_task_runner_->BelongsToCurrentThread()) { | 28 if (provider_task_runner_->BelongsToCurrentThread()) { |
| 29 // The location arbitrator unit tests rely on this method running | 29 // The location arbitrator unit tests rely on this method running |
| 30 // synchronously. | 30 // synchronously. |
| 31 position_ = position; | 31 position_ = position; |
| 32 NotifyCallback(position_); | 32 NotifyCallback(position_); |
| 33 } else { | 33 } else { |
| 34 provider_task_runner_->PostTask( | 34 provider_task_runner_->PostTask( |
| 35 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 35 FROM_HERE, base::Bind(&FakeLocationProvider::HandlePositionChanged, |
| 36 base::Unretained(this), position)); | 36 base::Unretained(this), position)); |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 40 bool FakeLocationProvider::StartProvider(bool high_accuracy) { |
| 41 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | 41 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 void MockLocationProvider::StopProvider() { | 45 void FakeLocationProvider::StopProvider() { |
| 46 state_ = STOPPED; | 46 state_ = STOPPED; |
| 47 } | 47 } |
| 48 | 48 |
| 49 void MockLocationProvider::GetPosition(Geoposition* position) { | 49 void FakeLocationProvider::GetPosition(Geoposition* position) { |
| 50 *position = position_; | 50 *position = position_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void MockLocationProvider::OnPermissionGranted() { | 53 void FakeLocationProvider::OnPermissionGranted() { |
| 54 is_permission_granted_ = true; | 54 is_permission_granted_ = true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Mock location provider that automatically calls back its client at most | 57 // Fake location provider that automatically calls back its client at most |
|
Kevin M
2016/08/15 20:35:40
Is this class used anywhere in Chromium?
CJ
2016/08/15 21:59:32
It is used in LocationArbitratorImplTest.
| |
| 58 // once, when StartProvider or OnPermissionGranted is called. Use | 58 // once, when StartProvider or OnPermissionGranted is called. Use |
| 59 // |requires_permission_to_start| to select which event triggers the callback. | 59 // |requires_permission_to_start| to select which event triggers the callback. |
| 60 class AutoMockLocationProvider : public MockLocationProvider { | 60 class AutoFakeLocationProvider : public FakeLocationProvider { |
| 61 public: | 61 public: |
| 62 AutoMockLocationProvider(bool has_valid_location, | 62 AutoFakeLocationProvider(bool has_valid_location, |
| 63 bool requires_permission_to_start) | 63 bool requires_permission_to_start) |
| 64 : requires_permission_to_start_(requires_permission_to_start), | 64 : requires_permission_to_start_(requires_permission_to_start), |
| 65 listeners_updated_(false) { | 65 listeners_updated_(false) { |
| 66 if (has_valid_location) { | 66 if (has_valid_location) { |
| 67 position_.accuracy = 3; | 67 position_.accuracy = 3; |
| 68 position_.latitude = 4.3; | 68 position_.latitude = 4.3; |
| 69 position_.longitude = -7.8; | 69 position_.longitude = -7.8; |
| 70 // Webkit compares the timestamp to wall clock time, so we need it to be | 70 // Webkit compares the timestamp to wall clock time, so we need it to be |
| 71 // contemporary. | 71 // contemporary. |
| 72 position_.timestamp = base::Time::Now(); | 72 position_.timestamp = base::Time::Now(); |
| 73 } else { | 73 } else { |
| 74 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 74 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 bool StartProvider(bool high_accuracy) override { | 77 bool StartProvider(bool high_accuracy) override { |
| 78 MockLocationProvider::StartProvider(high_accuracy); | 78 FakeLocationProvider::StartProvider(high_accuracy); |
| 79 if (!requires_permission_to_start_) { | 79 if (!requires_permission_to_start_) { |
| 80 UpdateListenersIfNeeded(); | 80 UpdateListenersIfNeeded(); |
| 81 } | 81 } |
| 82 return true; | 82 return true; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void OnPermissionGranted() override { | 85 void OnPermissionGranted() override { |
| 86 MockLocationProvider::OnPermissionGranted(); | 86 FakeLocationProvider::OnPermissionGranted(); |
| 87 if (requires_permission_to_start_) { | 87 if (requires_permission_to_start_) { |
| 88 UpdateListenersIfNeeded(); | 88 UpdateListenersIfNeeded(); |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 | 91 |
| 92 void UpdateListenersIfNeeded() { | 92 void UpdateListenersIfNeeded() { |
| 93 if (!listeners_updated_) { | 93 if (!listeners_updated_) { |
| 94 listeners_updated_ = true; | 94 listeners_updated_ = true; |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | 95 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 96 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 96 FROM_HERE, base::Bind(&FakeLocationProvider::HandlePositionChanged, |
| 97 base::Unretained(this), position_)); | 97 base::Unretained(this), position_)); |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 private: | 101 private: |
| 102 const bool requires_permission_to_start_; | 102 const bool requires_permission_to_start_; |
| 103 bool listeners_updated_; | 103 bool listeners_updated_; |
| 104 | 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(AutoMockLocationProvider); | 105 DISALLOW_COPY_AND_ASSIGN(AutoFakeLocationProvider); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 LocationProvider* NewMockLocationProvider() { | 108 LocationProvider* NewFakeLocationProvider() { |
|
Kevin M
2016/08/15 20:35:40
1. These helper methods are very unusual. Generall
CJ
2016/08/15 21:59:32
Done.
Kevin M
2016/08/17 00:55:02
They're still here... :)
CJ
2016/08/18 00:58:49
Still there? Are you saying you want the class Aut
| |
| 109 return new MockLocationProvider; | 109 return new FakeLocationProvider; |
| 110 } | 110 } |
| 111 | 111 |
| 112 LocationProvider* NewAutoSuccessMockLocationProvider() { | 112 LocationProvider* NewAutoSuccessFakeLocationProvider() { |
| 113 return new AutoMockLocationProvider(true, false); | 113 return new AutoFakeLocationProvider(true, false); |
| 114 } | 114 } |
| 115 | 115 |
| 116 LocationProvider* NewAutoFailMockLocationProvider() { | 116 LocationProvider* NewAutoFailFakeLocationProvider() { |
| 117 return new AutoMockLocationProvider(false, false); | 117 return new AutoFakeLocationProvider(false, false); |
| 118 } | 118 } |
| 119 | 119 |
| 120 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { | 120 LocationProvider* NewAutoSuccessFakeNetworkLocationProvider() { |
| 121 return new AutoMockLocationProvider(true, true); | 121 return new AutoFakeLocationProvider(true, true); |
| 122 } | 122 } |
| 123 | 123 |
| 124 } // namespace device | 124 } // namespace device |
| OLD | NEW |