OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/engine/feature/geolocation/blimp_location_provider.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "blimp/engine/feature/geolocation/mock_blimp_location_provider_delegate
.h" | |
16 #include "testing/gmock/include/gmock/gmock.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using testing::SaveArg; | |
20 using testing::_; | |
21 | |
22 namespace blimp { | |
23 namespace engine { | |
24 | |
25 class BlimpLocationProviderTest : public testing::Test { | |
26 public: | |
27 BlimpLocationProviderTest() | |
28 : mock_callback_(base::Bind(&BlimpLocationProviderTest::OnLocationUpdate, | |
29 base::Unretained(this))), | |
30 delegate_(base::WrapUnique(new MockBlimpLocationProviderDelegate)), | |
31 location_provider_(base::MakeUnique<BlimpLocationProvider>( | |
32 delegate_->GetWeakPtr(), | |
33 base::ThreadTaskRunnerHandle::Get())) {} | |
34 | |
35 void SetUp() override { on_location_update_called_ = false; } | |
36 | |
37 void TearDown() override { | |
38 location_provider_.reset(); | |
39 base::RunLoop().RunUntilIdle(); | |
40 } | |
41 | |
42 void OnLocationUpdate(const device::LocationProvider* provider, | |
43 const device::Geoposition& geoposition) { | |
44 on_location_update_called_ = true; | |
45 } | |
46 | |
47 protected: | |
48 const base::MessageLoop loop_; | |
49 device::LocationProvider::LocationProviderUpdateCallback mock_callback_; | |
50 std::unique_ptr<MockBlimpLocationProviderDelegate> delegate_; | |
51 std::unique_ptr<BlimpLocationProvider> location_provider_; | |
52 bool on_location_update_called_ = false; | |
53 | |
54 private: | |
55 DISALLOW_COPY_AND_ASSIGN(BlimpLocationProviderTest); | |
56 }; | |
57 | |
58 TEST_F(BlimpLocationProviderTest, StartProviderRunsCorrectly) { | |
59 EXPECT_CALL( | |
60 *delegate_, | |
61 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
62 .Times(1); | |
63 EXPECT_CALL(*delegate_, | |
64 RequestAccuracy(GeolocationSetInterestLevelMessage::LOW_ACCURACY)) | |
65 .Times(1); | |
66 | |
67 // BlimpLocationProvider implicitly stops on teardown, if it was started. | |
68 EXPECT_CALL(*delegate_, | |
69 RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST)) | |
70 .Times(1); | |
71 | |
72 EXPECT_TRUE(location_provider_->StartProvider(true)); | |
73 EXPECT_TRUE(location_provider_->StartProvider(false)); | |
74 base::RunLoop().RunUntilIdle(); | |
75 } | |
76 | |
77 TEST_F(BlimpLocationProviderTest, StartProviderHandlesNullDelegate) { | |
78 EXPECT_CALL(*delegate_, RequestAccuracy(_)).Times(0); | |
79 | |
80 delegate_.reset(); | |
81 location_provider_->StartProvider(true); | |
82 location_provider_->StartProvider(false); | |
83 base::RunLoop().RunUntilIdle(); | |
84 } | |
85 | |
86 TEST_F(BlimpLocationProviderTest, StopProviderRunsCorrectly) { | |
87 EXPECT_CALL( | |
88 *delegate_, | |
89 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
90 .Times(1); | |
91 EXPECT_CALL(*delegate_, | |
92 RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST)) | |
93 .Times(1); | |
94 | |
95 location_provider_->StartProvider(true); | |
96 location_provider_->StopProvider(); | |
97 base::RunLoop().RunUntilIdle(); | |
98 } | |
99 | |
100 TEST_F(BlimpLocationProviderTest, StopProviderHandlesNullDelegate) { | |
101 EXPECT_CALL( | |
102 *delegate_, | |
103 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
104 .Times(1); | |
105 | |
106 location_provider_->StartProvider(true); | |
107 base::RunLoop().RunUntilIdle(); | |
108 delegate_.reset(); | |
109 location_provider_->StopProvider(); | |
110 base::RunLoop().RunUntilIdle(); | |
111 } | |
112 | |
113 TEST_F(BlimpLocationProviderTest, LocationProviderDeleted) { | |
114 EXPECT_CALL( | |
115 *delegate_, | |
116 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
117 .Times(1); | |
118 EXPECT_CALL(*delegate_, | |
119 RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST)) | |
120 .Times(1); | |
121 | |
122 location_provider_->StartProvider(true); | |
123 location_provider_.reset(); | |
124 base::RunLoop().RunUntilIdle(); | |
125 } | |
126 | |
127 TEST_F(BlimpLocationProviderTest, OnPermissionGranted) { | |
128 EXPECT_CALL(*delegate_, OnPermissionGranted()).Times(1); | |
129 EXPECT_CALL( | |
130 *delegate_, | |
131 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
132 .Times(1); | |
133 EXPECT_CALL(*delegate_, | |
134 RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST)) | |
135 .Times(1); | |
136 | |
137 location_provider_->StartProvider(true); | |
138 location_provider_->OnPermissionGranted(); | |
139 base::RunLoop().RunUntilIdle(); | |
140 } | |
141 | |
142 TEST_F(BlimpLocationProviderTest, OnPermissionGrantedHandlesNullDelegate) { | |
143 EXPECT_CALL( | |
144 *delegate_, | |
145 RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) | |
146 .Times(1); | |
147 | |
148 location_provider_->StartProvider(true); | |
149 base::RunLoop().RunUntilIdle(); | |
150 delegate_.reset(); | |
151 location_provider_->OnPermissionGranted(); | |
152 base::RunLoop().RunUntilIdle(); | |
153 } | |
154 | |
155 TEST_F(BlimpLocationProviderTest, SetUpdateCallbackPropagatesCallback) { | |
156 EXPECT_CALL(*delegate_, SetUpdateCallback(_)); | |
157 | |
158 location_provider_->SetUpdateCallback(mock_callback_); | |
159 base::RunLoop().RunUntilIdle(); | |
160 } | |
161 | |
162 TEST_F(BlimpLocationProviderTest, NoCallbackWhenProviderDeleted) { | |
163 base::Callback<void(const device::Geoposition&)> callback; | |
164 EXPECT_CALL(*delegate_, SetUpdateCallback(_)).WillOnce(SaveArg<0>(&callback)); | |
165 location_provider_->SetUpdateCallback(mock_callback_); | |
166 base::RunLoop().RunUntilIdle(); | |
167 | |
168 location_provider_.reset(); | |
169 device::Geoposition position; | |
170 callback.Run(position); | |
171 base::RunLoop().RunUntilIdle(); | |
172 EXPECT_FALSE(on_location_update_called_); | |
173 } | |
174 | |
175 TEST_F(BlimpLocationProviderTest, CallbackCalledProperly) { | |
176 base::Callback<void(const device::Geoposition&)> callback; | |
177 EXPECT_CALL(*delegate_, SetUpdateCallback(_)).WillOnce(SaveArg<0>(&callback)); | |
178 location_provider_->SetUpdateCallback(mock_callback_); | |
179 base::RunLoop().RunUntilIdle(); | |
180 | |
181 device::Geoposition position; | |
182 callback.Run(position); | |
183 base::RunLoop().RunUntilIdle(); | |
184 EXPECT_TRUE(on_location_update_called_); | |
185 } | |
186 | |
187 TEST_F(BlimpLocationProviderTest, SetUpdateCallbackHandlesNullDelegate) { | |
188 EXPECT_CALL(*delegate_, SetUpdateCallback(_)).Times(0); | |
189 | |
190 delegate_.reset(); | |
191 location_provider_->SetUpdateCallback(mock_callback_); | |
192 base::RunLoop().RunUntilIdle(); | |
193 } | |
194 | |
195 } // namespace engine | |
196 } // namespace blimp | |
OLD | NEW |