| 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 mock 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/mock_location_provider.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 40 bool MockLocationProvider::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 MockLocationProvider::StopProvider() { |
| 46 state_ = STOPPED; | 46 state_ = STOPPED; |
| 47 } | 47 } |
| 48 | 48 |
| 49 void MockLocationProvider::GetPosition(Geoposition* position) { | 49 const Geoposition& MockLocationProvider::GetPosition() { |
| 50 *position = position_; | 50 return position_; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void MockLocationProvider::OnPermissionGranted() { | 53 void MockLocationProvider::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 | |
| 58 // once, when StartProvider or OnPermissionGranted is called. Use | |
| 59 // |requires_permission_to_start| to select which event triggers the callback. | |
| 60 class AutoMockLocationProvider : public MockLocationProvider { | |
| 61 public: | |
| 62 AutoMockLocationProvider(bool has_valid_location, | |
| 63 bool requires_permission_to_start) | |
| 64 : requires_permission_to_start_(requires_permission_to_start), | |
| 65 listeners_updated_(false) { | |
| 66 if (has_valid_location) { | |
| 67 position_.accuracy = 3; | |
| 68 position_.latitude = 4.3; | |
| 69 position_.longitude = -7.8; | |
| 70 // Webkit compares the timestamp to wall clock time, so we need it to be | |
| 71 // contemporary. | |
| 72 position_.timestamp = base::Time::Now(); | |
| 73 } else { | |
| 74 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 75 } | |
| 76 } | |
| 77 bool StartProvider(bool high_accuracy) override { | |
| 78 MockLocationProvider::StartProvider(high_accuracy); | |
| 79 if (!requires_permission_to_start_) { | |
| 80 UpdateListenersIfNeeded(); | |
| 81 } | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 void OnPermissionGranted() override { | |
| 86 MockLocationProvider::OnPermissionGranted(); | |
| 87 if (requires_permission_to_start_) { | |
| 88 UpdateListenersIfNeeded(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void UpdateListenersIfNeeded() { | |
| 93 if (!listeners_updated_) { | |
| 94 listeners_updated_ = true; | |
| 95 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 96 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | |
| 97 base::Unretained(this), position_)); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 const bool requires_permission_to_start_; | |
| 103 bool listeners_updated_; | |
| 104 | |
| 105 DISALLOW_COPY_AND_ASSIGN(AutoMockLocationProvider); | |
| 106 }; | |
| 107 | |
| 108 LocationProvider* NewMockLocationProvider() { | |
| 109 return new MockLocationProvider; | |
| 110 } | |
| 111 | |
| 112 LocationProvider* NewAutoSuccessMockLocationProvider() { | |
| 113 return new AutoMockLocationProvider(true, false); | |
| 114 } | |
| 115 | |
| 116 LocationProvider* NewAutoFailMockLocationProvider() { | |
| 117 return new AutoMockLocationProvider(false, false); | |
| 118 } | |
| 119 | |
| 120 LocationProvider* NewAutoSuccessMockNetworkLocationProvider() { | |
| 121 return new AutoMockLocationProvider(true, true); | |
| 122 } | |
| 123 | |
| 124 } // namespace device | 57 } // namespace device |
| OLD | NEW |