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 // TODO(lethalantidote): Convert location_arbitrator_impl to use actual mock | |
8 // instead of FakeLocationProvider. | |
7 | 9 |
8 #include "device/geolocation/mock_location_provider.h" | 10 #include "device/geolocation/fake_location_provider.h" |
9 | 11 |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
13 #include "base/location.h" | 15 #include "base/location.h" |
14 #include "base/logging.h" | 16 #include "base/logging.h" |
15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
17 | 19 |
18 namespace device { | 20 namespace device { |
19 | 21 |
20 MockLocationProvider::MockLocationProvider() | 22 FakeLocationProvider::FakeLocationProvider() |
21 : state_(STOPPED), | 23 : state_(STOPPED), |
22 is_permission_granted_(false), | 24 is_permission_granted_(false), |
23 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} | 25 provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
24 | 26 |
25 MockLocationProvider::~MockLocationProvider() {} | 27 FakeLocationProvider::~FakeLocationProvider() {} |
26 | 28 |
27 bool MockLocationProvider::IsProviderStarted() const { | 29 bool FakeLocationProvider::IsProviderStarted() const { |
28 return state_ != STOPPED; | 30 return state_ != STOPPED; |
29 } | 31 } |
30 | 32 |
31 void MockLocationProvider::HandlePositionChanged(const Geoposition& position) { | 33 void FakeLocationProvider::HandlePositionChanged(const Geoposition& position) { |
32 if (provider_task_runner_->BelongsToCurrentThread()) { | 34 if (provider_task_runner_->BelongsToCurrentThread()) { |
33 // The location arbitrator unit tests rely on this method running | 35 // The location arbitrator unit tests rely on this method running |
34 // synchronously. | 36 // synchronously. |
35 position_ = position; | 37 position_ = position; |
36 NotifyCallback(position_); | 38 NotifyCallback(position_); |
37 } else { | 39 } else { |
38 provider_task_runner_->PostTask( | 40 provider_task_runner_->PostTask( |
39 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 41 FROM_HERE, base::Bind(&FakeLocationProvider::HandlePositionChanged, |
40 base::Unretained(this), position)); | 42 base::Unretained(this), position)); |
41 } | 43 } |
42 } | 44 } |
43 | 45 |
44 bool MockLocationProvider::StartProvider(bool high_accuracy) { | 46 bool FakeLocationProvider::StartProvider(bool high_accuracy) { |
45 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; | 47 state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY; |
46 return true; | 48 return true; |
47 } | 49 } |
48 | 50 |
49 void MockLocationProvider::StopProvider() { | 51 void FakeLocationProvider::StopProvider() { |
50 state_ = STOPPED; | 52 state_ = STOPPED; |
51 } | 53 } |
52 | 54 |
53 const Geoposition& MockLocationProvider::GetPosition() { | 55 const Geoposition& FakeLocationProvider::GetPosition() { |
54 return position_; | 56 return position_; |
55 } | 57 } |
56 | 58 |
57 void MockLocationProvider::OnPermissionGranted() { | 59 void FakeLocationProvider::OnPermissionGranted() { |
58 is_permission_granted_ = true; | 60 is_permission_granted_ = true; |
59 } | 61 } |
60 | 62 |
61 // Mock location provider that automatically calls back its client at most | 63 // Fake location provider that automatically calls back its client at most |
62 // once, when StartProvider or OnPermissionGranted is called. Use | 64 // once, when StartProvider or OnPermissionGranted is called. Use |
63 // |requires_permission_to_start| to select which event triggers the callback. | 65 // |requires_permission_to_start| to select which event triggers the callback. |
64 class AutoMockLocationProvider : public MockLocationProvider { | 66 class AutoFakeLocationProvider : public FakeLocationProvider { |
Wez
2016/08/19 18:50:25
If you've removed the New*() methods below, surely
CJ
2016/08/19 22:22:34
I dont think this class is used anywhere. I'm remo
Wez
2016/08/20 01:42:11
My point was that it _can't_ be used from anywhere
CJ
2016/08/22 17:56:39
No I understood. Sorry poor phrasing by me I guess
| |
65 public: | 67 public: |
66 AutoMockLocationProvider(bool has_valid_location, | 68 AutoFakeLocationProvider(bool has_valid_location, |
67 bool requires_permission_to_start) | 69 bool requires_permission_to_start) |
68 : requires_permission_to_start_(requires_permission_to_start), | 70 : requires_permission_to_start_(requires_permission_to_start), |
69 listeners_updated_(false) { | 71 listeners_updated_(false) { |
70 if (has_valid_location) { | 72 if (has_valid_location) { |
71 position_.accuracy = 3; | 73 position_.accuracy = 3; |
72 position_.latitude = 4.3; | 74 position_.latitude = 4.3; |
73 position_.longitude = -7.8; | 75 position_.longitude = -7.8; |
74 // Webkit compares the timestamp to wall clock time, so we need it to be | 76 // Webkit compares the timestamp to wall clock time, so we need it to be |
75 // contemporary. | 77 // contemporary. |
76 position_.timestamp = base::Time::Now(); | 78 position_.timestamp = base::Time::Now(); |
77 } else { | 79 } else { |
78 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 80 position_.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
79 } | 81 } |
80 } | 82 } |
81 bool StartProvider(bool high_accuracy) override { | 83 bool StartProvider(bool high_accuracy) override { |
82 MockLocationProvider::StartProvider(high_accuracy); | 84 FakeLocationProvider::StartProvider(high_accuracy); |
83 if (!requires_permission_to_start_) { | 85 if (!requires_permission_to_start_) { |
84 UpdateListenersIfNeeded(); | 86 UpdateListenersIfNeeded(); |
85 } | 87 } |
86 return true; | 88 return true; |
87 } | 89 } |
88 | 90 |
89 void OnPermissionGranted() override { | 91 void OnPermissionGranted() override { |
90 MockLocationProvider::OnPermissionGranted(); | 92 FakeLocationProvider::OnPermissionGranted(); |
91 if (requires_permission_to_start_) { | 93 if (requires_permission_to_start_) { |
92 UpdateListenersIfNeeded(); | 94 UpdateListenersIfNeeded(); |
93 } | 95 } |
94 } | 96 } |
95 | 97 |
96 void UpdateListenersIfNeeded() { | 98 void UpdateListenersIfNeeded() { |
97 if (!listeners_updated_) { | 99 if (!listeners_updated_) { |
98 listeners_updated_ = true; | 100 listeners_updated_ = true; |
99 base::ThreadTaskRunnerHandle::Get()->PostTask( | 101 base::ThreadTaskRunnerHandle::Get()->PostTask( |
100 FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged, | 102 FROM_HERE, base::Bind(&FakeLocationProvider::HandlePositionChanged, |
101 base::Unretained(this), position_)); | 103 base::Unretained(this), position_)); |
102 } | 104 } |
103 } | 105 } |
104 | 106 |
105 private: | 107 private: |
106 const bool requires_permission_to_start_; | 108 const bool requires_permission_to_start_; |
107 bool listeners_updated_; | 109 bool listeners_updated_; |
108 | 110 |
109 DISALLOW_COPY_AND_ASSIGN(AutoMockLocationProvider); | 111 DISALLOW_COPY_AND_ASSIGN(AutoFakeLocationProvider); |
110 }; | 112 }; |
111 | 113 |
112 LocationProvider* NewMockLocationProvider() { | |
Wez
2016/08/19 18:50:25
Looks like you've removed these from the .cc but r
CJ
2016/08/19 22:22:33
Ah. Sorry, another merge failure on my part. Gone.
| |
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 | 114 } // namespace device |
OLD | NEW |