Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / dial / dial_registry_unittest.cc
blob69dd839efc475eda4c348140256f73e4eada5a22
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
3 // found in the LICENSE file.
5 #include "base/compiler_specific.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
8 #include "chrome/browser/extensions/api/dial/dial_registry.h"
9 #include "chrome/browser/extensions/api/dial/dial_service.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "url/gurl.h"
15 using base::Time;
16 using base::TimeDelta;
17 using ::testing::A;
18 using ::testing::AtLeast;
19 using ::testing::Return;
20 using ::testing::InSequence;
22 namespace extensions {
24 class MockDialObserver : public DialRegistry::Observer {
25 public:
26 MOCK_METHOD1(OnDialDeviceEvent,
27 void(const DialRegistry::DeviceList& devices));
28 MOCK_METHOD1(OnDialError, void(DialRegistry::DialErrorCode type));
31 class MockDialService : public DialService {
32 public:
33 virtual ~MockDialService() {}
35 MOCK_METHOD0(Discover, bool());
36 MOCK_METHOD1(AddObserver, void(DialService::Observer*));
37 MOCK_METHOD1(RemoveObserver, void(DialService::Observer*));
38 MOCK_CONST_METHOD1(HasObserver, bool(const DialService::Observer*));
41 class MockDialRegistry : public DialRegistry {
42 public:
43 MockDialRegistry(Observer *dial_api,
44 const base::TimeDelta& refresh_interval,
45 const base::TimeDelta& expiration,
46 const size_t max_devices)
47 : DialRegistry(dial_api, refresh_interval, expiration, max_devices) {
48 time_ = Time::Now();
51 ~MockDialRegistry() override {
52 // Don't let the DialRegistry delete this.
53 DialService* tmp = dial_.release();
54 if (tmp != NULL)
55 CHECK_EQ(&mock_service_, tmp);
58 // Returns the mock Dial service.
59 MockDialService& mock_service() {
60 return mock_service_;
63 // Set to mock out the current time.
64 Time time_;
66 protected:
67 base::Time Now() const override { return time_; }
69 DialService* CreateDialService() override { return &mock_service_; }
71 void ClearDialService() override {
72 // Release the pointer but don't delete the object because the test owns it.
73 CHECK_EQ(&mock_service_, dial_.release());
76 private:
77 MockDialService mock_service_;
80 class DialRegistryTest : public testing::Test {
81 public:
82 DialRegistryTest()
83 : first_device_("first", GURL("http://127.0.0.1/dd.xml"), Time::Now()),
84 second_device_("second", GURL("http://127.0.0.2/dd.xml"), Time::Now()),
85 third_device_("third", GURL("http://127.0.0.3/dd.xml"), Time::Now()) {
86 registry_.reset(new MockDialRegistry(&mock_observer_,
87 TimeDelta::FromSeconds(1000),
88 TimeDelta::FromSeconds(10),
89 10));
90 list_with_first_device_.push_back(first_device_);
91 list_with_second_device_.push_back(second_device_);
94 protected:
95 scoped_ptr<MockDialRegistry> registry_;
96 MockDialObserver mock_observer_;
97 const DialDeviceData first_device_;
98 const DialDeviceData second_device_;
99 const DialDeviceData third_device_;
101 const DialRegistry::DeviceList empty_list_;
102 DialRegistry::DeviceList list_with_first_device_;
103 DialRegistry::DeviceList list_with_second_device_;
105 // Must instantiate a MessageLoop for the thread, as the registry starts a
106 // RepeatingTimer when there are listeners.
107 base::MessageLoop message_loop_;
109 void SetListenerExpectations() {
110 EXPECT_CALL(registry_->mock_service(),
111 AddObserver(A<DialService::Observer*>()))
112 .Times(1);
113 EXPECT_CALL(registry_->mock_service(),
114 RemoveObserver(A<DialService::Observer*>()))
115 .Times(1);
119 TEST_F(DialRegistryTest, TestAddRemoveListeners) {
120 SetListenerExpectations();
121 EXPECT_CALL(registry_->mock_service(), Discover())
122 .Times(1);
124 EXPECT_FALSE(registry_->repeating_timer_.IsRunning());
125 registry_->OnListenerAdded();
126 EXPECT_TRUE(registry_->repeating_timer_.IsRunning());
127 registry_->OnListenerAdded();
128 EXPECT_TRUE(registry_->repeating_timer_.IsRunning());
129 registry_->OnListenerRemoved();
130 EXPECT_TRUE(registry_->repeating_timer_.IsRunning());
131 registry_->OnListenerRemoved();
132 EXPECT_FALSE(registry_->repeating_timer_.IsRunning());
135 TEST_F(DialRegistryTest, TestNoDevicesDiscovered) {
136 SetListenerExpectations();
137 EXPECT_CALL(registry_->mock_service(), Discover())
138 .Times(1);
139 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
140 .Times(1);
142 registry_->OnListenerAdded();
143 registry_->OnDiscoveryRequest(NULL);
144 registry_->OnDiscoveryFinished(NULL);
145 registry_->OnListenerRemoved();
148 TEST_F(DialRegistryTest, TestDevicesDiscovered) {
149 DialRegistry::DeviceList expected_list2;
150 expected_list2.push_back(first_device_);
151 expected_list2.push_back(second_device_);
153 SetListenerExpectations();
154 EXPECT_CALL(registry_->mock_service(), Discover())
155 .Times(2);
156 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
157 .Times(1);
158 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_))
159 .Times(2);
160 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(expected_list2))
161 .Times(1);
163 registry_->OnListenerAdded();
164 registry_->OnDiscoveryRequest(NULL);
165 registry_->OnDeviceDiscovered(NULL, first_device_);
166 registry_->OnDiscoveryFinished(NULL);
168 registry_->DoDiscovery();
169 registry_->OnDiscoveryRequest(NULL);
170 registry_->OnDeviceDiscovered(NULL, second_device_);
171 registry_->OnDiscoveryFinished(NULL);
172 registry_->OnListenerRemoved();
175 TEST_F(DialRegistryTest, TestDeviceExpires) {
176 SetListenerExpectations();
177 EXPECT_CALL(registry_->mock_service(), Discover())
178 .Times(2);
179 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
180 .Times(2);
181 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_))
182 .Times(2);
184 registry_->OnListenerAdded();
185 registry_->OnDiscoveryRequest(NULL);
186 registry_->OnDeviceDiscovered(NULL, first_device_);
187 registry_->OnDiscoveryFinished(NULL);
189 registry_->time_ = Time::Now() + TimeDelta::FromSeconds(30);
191 registry_->DoDiscovery();
192 registry_->OnDiscoveryRequest(NULL);
193 registry_->OnDiscoveryFinished(NULL);
194 registry_->OnListenerRemoved();
197 TEST_F(DialRegistryTest, TestExpiredDeviceIsRediscovered) {
198 std::vector<Time> discovery_times;
199 discovery_times.push_back(Time::Now());
200 discovery_times.push_back(discovery_times[0] + TimeDelta::FromSeconds(30));
201 discovery_times.push_back(discovery_times[1] + TimeDelta::FromSeconds(30));
203 DialDeviceData rediscovered_device("first",
204 GURL("http://127.0.0.1/dd.xml"),
205 discovery_times[2]);
207 SetListenerExpectations();
209 // TODO(mfoltz): Convert other tests to use InSequence to make expectations
210 // more obvious.
211 InSequence s;
213 EXPECT_CALL(registry_->mock_service(), Discover());
214 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
215 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_));
217 EXPECT_CALL(registry_->mock_service(), Discover());
218 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_));
219 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
221 EXPECT_CALL(registry_->mock_service(), Discover());
222 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
223 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_));
225 registry_->time_ = discovery_times[0];
226 registry_->OnListenerAdded();
227 registry_->OnDiscoveryRequest(NULL);
228 registry_->OnDeviceDiscovered(NULL, first_device_);
229 registry_->OnDiscoveryFinished(NULL);
231 // Will expire "first" device as it is not discovered this time.
232 registry_->time_ = discovery_times[1];
233 registry_->DoDiscovery();
234 registry_->OnDiscoveryRequest(NULL);
235 registry_->OnDiscoveryFinished(NULL);
237 // "first" device is rediscovered 30 seconds later. We pass a device object
238 // with a newer discovery time so it is not pruned immediately.
239 registry_->time_ = discovery_times[2];
240 registry_->DoDiscovery();
241 registry_->OnDiscoveryRequest(NULL);
242 registry_->OnDeviceDiscovered(NULL, rediscovered_device);
243 registry_->OnDiscoveryFinished(NULL);
245 registry_->OnListenerRemoved();
248 TEST_F(DialRegistryTest, TestRemovingListenerDoesNotClearList) {
249 EXPECT_CALL(registry_->mock_service(),
250 AddObserver(A<DialService::Observer*>()))
251 .Times(2);
252 EXPECT_CALL(registry_->mock_service(),
253 RemoveObserver(A<DialService::Observer*>()))
254 .Times(2);
256 EXPECT_CALL(registry_->mock_service(), Discover())
257 .Times(2);
259 InSequence s;
260 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
261 .Times(1);
262 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_))
263 .Times(2);
265 registry_->OnListenerAdded();
266 registry_->OnDiscoveryRequest(NULL);
267 registry_->OnDeviceDiscovered(NULL, first_device_);
268 registry_->OnDiscoveryFinished(NULL);
269 registry_->OnListenerRemoved();
271 registry_->OnListenerAdded();
272 registry_->OnDiscoveryRequest(NULL);
273 registry_->OnDiscoveryFinished(NULL);
274 registry_->OnListenerRemoved();
277 TEST_F(DialRegistryTest, TestNetworkEventConnectionLost) {
278 SetListenerExpectations();
280 EXPECT_CALL(registry_->mock_service(), Discover())
281 .Times(1);
283 InSequence s;
284 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
285 .Times(1);
286 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_))
287 .Times(1);
288 EXPECT_CALL(mock_observer_, OnDialError(
289 DialRegistry::DIAL_NETWORK_DISCONNECTED)).Times(1);
290 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
291 .Times(1);
293 registry_->OnListenerAdded();
294 registry_->OnDiscoveryRequest(NULL);
295 registry_->OnDeviceDiscovered(NULL, first_device_);
296 registry_->OnDiscoveryFinished(NULL);
298 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE);
300 registry_->OnDiscoveryRequest(NULL);
301 registry_->OnDiscoveryFinished(NULL);
302 registry_->OnListenerRemoved();
305 TEST_F(DialRegistryTest, TestNetworkEventConnectionRestored) {
306 DialRegistry::DeviceList expected_list3;
307 expected_list3.push_back(second_device_);
308 expected_list3.push_back(third_device_);
310 // A disconnection should shutdown the DialService, so we expect the observer
311 // to be added twice.
312 EXPECT_CALL(registry_->mock_service(),
313 AddObserver(A<DialService::Observer*>()))
314 .Times(2);
315 EXPECT_CALL(registry_->mock_service(),
316 RemoveObserver(A<DialService::Observer*>()))
317 .Times(2);
319 EXPECT_CALL(registry_->mock_service(), Discover())
320 .Times(2);
322 InSequence s;
323 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
324 .Times(1);
325 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_))
326 .Times(1);
327 EXPECT_CALL(mock_observer_,
328 OnDialError(DialRegistry::DIAL_NETWORK_DISCONNECTED))
329 .Times(1);
330 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_))
331 .Times(2);
332 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_second_device_))
333 .Times(1);
334 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(expected_list3))
335 .Times(1);
337 registry_->OnListenerAdded();
338 registry_->OnDiscoveryRequest(NULL);
339 registry_->OnDeviceDiscovered(NULL, first_device_);
340 registry_->OnDiscoveryFinished(NULL);
342 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE);
344 registry_->OnDiscoveryRequest(NULL);
345 registry_->OnDiscoveryFinished(NULL);
347 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI);
349 registry_->OnDiscoveryRequest(NULL);
350 registry_->OnDeviceDiscovered(NULL, second_device_);
351 registry_->OnDiscoveryFinished(NULL);
353 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
355 registry_->OnDiscoveryRequest(NULL);
356 registry_->OnDeviceDiscovered(NULL, third_device_);
357 registry_->OnDiscoveryFinished(NULL);
359 registry_->OnListenerRemoved();
362 } // namespace extensions