| 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 "content/browser/geolocation/mock_location_provider.h" | 8 #include "content/browser/geolocation/mock_location_provider.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 CHECK(self_ref_); | 25 CHECK(self_ref_); |
| 26 CHECK(*self_ref_ == NULL); | 26 CHECK(*self_ref_ == NULL); |
| 27 *self_ref_ = this; | 27 *self_ref_ = this; |
| 28 } | 28 } |
| 29 | 29 |
| 30 MockLocationProvider::~MockLocationProvider() { | 30 MockLocationProvider::~MockLocationProvider() { |
| 31 CHECK(*self_ref_ == this); | 31 CHECK(*self_ref_ == this); |
| 32 *self_ref_ = NULL; | 32 *self_ref_ = NULL; |
| 33 } | 33 } |
| 34 | 34 |
| 35 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { | 35 void MockLocationProvider::HandlePositionChanged( |
| 36 const content::Geoposition& position) { |
| 36 if (provider_loop_->BelongsToCurrentThread()) { | 37 if (provider_loop_->BelongsToCurrentThread()) { |
| 37 // The location arbitrator unit tests rely on this method running | 38 // The location arbitrator unit tests rely on this method running |
| 38 // synchronously. | 39 // synchronously. |
| 39 position_ = position; | 40 position_ = position; |
| 40 UpdateListeners(); | 41 UpdateListeners(); |
| 41 } else { | 42 } else { |
| 42 provider_loop_->PostTask( | 43 provider_loop_->PostTask( |
| 43 FROM_HERE, | 44 FROM_HERE, |
| 44 base::Bind(&MockLocationProvider::HandlePositionChanged, | 45 base::Bind(&MockLocationProvider::HandlePositionChanged, |
| 45 base::Unretained(this), position)); | 46 base::Unretained(this), position)); |
| 46 } | 47 } |
| 47 } | 48 } |
| 48 | 49 |
| 49 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 50 bool MockLocationProvider::StartProvider(bool high_accuracy) { |
| 50 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | 51 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; |
| 51 return true; | 52 return true; |
| 52 } | 53 } |
| 53 | 54 |
| 54 void MockLocationProvider::StopProvider() { | 55 void MockLocationProvider::StopProvider() { |
| 55 state_ = STOPPED; | 56 state_ = STOPPED; |
| 56 } | 57 } |
| 57 | 58 |
| 58 void MockLocationProvider::GetPosition(Geoposition* position) { | 59 void MockLocationProvider::GetPosition(content::Geoposition* position) { |
| 59 *position = position_; | 60 *position = position_; |
| 60 } | 61 } |
| 61 | 62 |
| 62 void MockLocationProvider::OnPermissionGranted() { | 63 void MockLocationProvider::OnPermissionGranted() { |
| 63 is_permission_granted_ = true; | 64 is_permission_granted_ = true; |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Mock location provider that automatically calls back its client at most | 67 // Mock location provider that automatically calls back its client at most |
| 67 // once, when StartProvider or OnPermissionGranted is called. Use | 68 // once, when StartProvider or OnPermissionGranted is called. Use |
| 68 // |requires_permission_to_start| to select which event triggers the callback. | 69 // |requires_permission_to_start| to select which event triggers the callback. |
| 69 class AutoMockLocationProvider : public MockLocationProvider { | 70 class AutoMockLocationProvider : public MockLocationProvider { |
| 70 public: | 71 public: |
| 71 AutoMockLocationProvider(bool has_valid_location, | 72 AutoMockLocationProvider(bool has_valid_location, |
| 72 bool requires_permission_to_start) | 73 bool requires_permission_to_start) |
| 73 : MockLocationProvider(&instance_), | 74 : MockLocationProvider(&instance_), |
| 74 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 75 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 75 requires_permission_to_start_(requires_permission_to_start), | 76 requires_permission_to_start_(requires_permission_to_start), |
| 76 listeners_updated_(false) { | 77 listeners_updated_(false) { |
| 77 if (has_valid_location) { | 78 if (has_valid_location) { |
| 78 position_.accuracy = 3; | 79 position_.accuracy = 3; |
| 79 position_.latitude = 4.3; | 80 position_.latitude = 4.3; |
| 80 position_.longitude = -7.8; | 81 position_.longitude = -7.8; |
| 81 // Webkit compares the timestamp to wall clock time, so we need it to be | 82 // Webkit compares the timestamp to wall clock time, so we need it to be |
| 82 // contemporary. | 83 // contemporary. |
| 83 position_.timestamp = base::Time::Now(); | 84 position_.timestamp = base::Time::Now(); |
| 84 } else { | 85 } else { |
| 85 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 86 position_.error_code = |
| 87 content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 86 } | 88 } |
| 87 } | 89 } |
| 88 virtual bool StartProvider(bool high_accuracy) { | 90 virtual bool StartProvider(bool high_accuracy) { |
| 89 MockLocationProvider::StartProvider(high_accuracy); | 91 MockLocationProvider::StartProvider(high_accuracy); |
| 90 if (!requires_permission_to_start_) { | 92 if (!requires_permission_to_start_) { |
| 91 UpdateListenersIfNeeded(); | 93 UpdateListenersIfNeeded(); |
| 92 } | 94 } |
| 93 return true; | 95 return true; |
| 94 } | 96 } |
| 95 | 97 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 123 return new AutoMockLocationProvider(true, false); | 125 return new AutoMockLocationProvider(true, false); |
| 124 } | 126 } |
| 125 | 127 |
| 126 LocationProviderBase* NewAutoFailMockLocationProvider() { | 128 LocationProviderBase* NewAutoFailMockLocationProvider() { |
| 127 return new AutoMockLocationProvider(false, false); | 129 return new AutoMockLocationProvider(false, false); |
| 128 } | 130 } |
| 129 | 131 |
| 130 LocationProviderBase* NewAutoSuccessMockNetworkLocationProvider() { | 132 LocationProviderBase* NewAutoSuccessMockNetworkLocationProvider() { |
| 131 return new AutoMockLocationProvider(true, true); | 133 return new AutoMockLocationProvider(true, true); |
| 132 } | 134 } |
| OLD | NEW |