1 // Copyright 2015 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 "content/browser/background_sync/background_sync_network_observer.h"
7 #include "base/run_loop.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "net/base/network_change_notifier.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class BackgroundSyncNetworkObserverTest
: public testing::Test
{
16 BackgroundSyncNetworkObserverTest()
17 : network_change_notifier(net::NetworkChangeNotifier::CreateMock()),
18 network_observer_(new BackgroundSyncNetworkObserver(
19 base::Bind(&BackgroundSyncNetworkObserverTest::OnNetworkChanged
,
20 base::Unretained(this)))),
21 network_changed_count_(0) {}
23 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type
) {
24 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
26 base::RunLoop().RunUntilIdle();
29 void OnNetworkChanged() { network_changed_count_
++; }
31 TestBrowserThreadBundle browser_thread_bundle_
;
33 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier
;
34 scoped_ptr
<BackgroundSyncNetworkObserver
> network_observer_
;
35 int network_changed_count_
;
38 TEST_F(BackgroundSyncNetworkObserverTest
, NetworkChangeInvokesCallback
) {
39 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
40 network_changed_count_
= 0;
42 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
43 EXPECT_EQ(1, network_changed_count_
);
44 SetNetwork(net::NetworkChangeNotifier::CONNECTION_3G
);
45 EXPECT_EQ(2, network_changed_count_
);
46 SetNetwork(net::NetworkChangeNotifier::CONNECTION_UNKNOWN
);
47 EXPECT_EQ(3, network_changed_count_
);
48 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
49 EXPECT_EQ(4, network_changed_count_
);
50 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
51 EXPECT_EQ(4, network_changed_count_
);
54 TEST_F(BackgroundSyncNetworkObserverTest
, NetworkSufficientAnyNetwork
) {
55 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
56 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ANY
));
58 SetNetwork(net::NetworkChangeNotifier::CONNECTION_3G
);
59 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ANY
));
61 SetNetwork(net::NetworkChangeNotifier::CONNECTION_UNKNOWN
);
62 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ANY
));
64 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
65 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ANY
));
68 TEST_F(BackgroundSyncNetworkObserverTest
, NetworkSufficientAvoidCellular
) {
69 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
71 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
73 SetNetwork(net::NetworkChangeNotifier::CONNECTION_UNKNOWN
);
75 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
77 SetNetwork(net::NetworkChangeNotifier::CONNECTION_2G
);
79 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
81 SetNetwork(net::NetworkChangeNotifier::CONNECTION_3G
);
83 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
85 SetNetwork(net::NetworkChangeNotifier::CONNECTION_4G
);
87 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
89 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
91 network_observer_
->NetworkSufficient(NETWORK_STATE_AVOID_CELLULAR
));
94 TEST_F(BackgroundSyncNetworkObserverTest
, ConditionsMetOnline
) {
95 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
96 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ONLINE
));
98 SetNetwork(net::NetworkChangeNotifier::CONNECTION_3G
);
99 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ONLINE
));
101 SetNetwork(net::NetworkChangeNotifier::CONNECTION_UNKNOWN
);
102 EXPECT_TRUE(network_observer_
->NetworkSufficient(NETWORK_STATE_ONLINE
));
104 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
105 EXPECT_FALSE(network_observer_
->NetworkSufficient(NETWORK_STATE_ONLINE
));
108 } // namespace content