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