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 // See network_change_notifier_android.h for design explanations. | 5 // See network_change_notifier_android.h for design explanations. |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 class ObserverImpl : public BaseObserver { | 25 class ObserverImpl : public BaseObserver { |
26 public: | 26 public: |
27 ObserverImpl() | 27 ObserverImpl() |
28 : times_connection_type_changed_(0), | 28 : times_connection_type_changed_(0), |
29 current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { | 29 current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { |
30 } | 30 } |
31 | 31 |
32 // BaseObserver: | 32 // BaseObserver: |
33 virtual void OnConnectionTypeChanged( | 33 virtual void OnConnectionTypeChanged( |
34 NetworkChangeNotifier::ConnectionType type) OVERRIDE { | 34 NetworkChangeNotifier::ConnectionType type) OVERRIDE { |
| 35 if (type == current_connection_) |
| 36 return; |
35 times_connection_type_changed_++; | 37 times_connection_type_changed_++; |
36 current_connection_ = type; | 38 current_connection_ = type; |
37 } | 39 } |
38 | 40 |
39 int times_connection_type_changed() const { | 41 int times_connection_type_changed() const { |
40 return times_connection_type_changed_; | 42 return times_connection_type_changed_; |
41 } | 43 } |
42 | 44 |
43 NetworkChangeNotifier::ConnectionType current_connection() const { | 45 NetworkChangeNotifier::ConnectionType current_connection() const { |
44 return current_connection_; | 46 return current_connection_; |
(...skipping 30 matching lines...) Expand all Loading... |
75 EXPECT_EQ(1, times_connection_type_changed_callback.Run()); | 77 EXPECT_EQ(1, times_connection_type_changed_callback.Run()); |
76 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | 78 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
77 connection_type_getter.Run()); | 79 connection_type_getter.Run()); |
78 | 80 |
79 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::ONLINE); | 81 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::ONLINE); |
80 EXPECT_EQ(2, times_connection_type_changed_callback.Run()); | 82 EXPECT_EQ(2, times_connection_type_changed_callback.Run()); |
81 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | 83 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
82 connection_type_getter.Run()); | 84 connection_type_getter.Run()); |
83 } | 85 } |
84 | 86 |
| 87 void AddObserverToDelegate( |
| 88 NetworkChangeNotifierDelegateAndroid::Observer* observer) { |
| 89 delegate_.AddObserver(observer); |
| 90 // Note that this is needed because ObserverListThreadSafe uses PostTask(). |
| 91 MessageLoop::current()->RunUntilIdle(); |
| 92 } |
| 93 |
85 void ForceConnectivityState( | 94 void ForceConnectivityState( |
86 NetworkChangeNotifierDelegateAndroid::ConnectivityState state) { | 95 NetworkChangeNotifierDelegateAndroid::ConnectivityState state) { |
87 delegate_.ForceConnectivityState(state); | 96 delegate_.ForceConnectivityState(state); |
88 // Note that this is needed because ObserverListThreadSafe uses PostTask(). | 97 // See AddObserverToDelegate() above. |
89 MessageLoop::current()->RunUntilIdle(); | 98 MessageLoop::current()->RunUntilIdle(); |
90 } | 99 } |
91 | 100 |
92 NetworkChangeNotifierDelegateAndroid delegate_; | 101 NetworkChangeNotifierDelegateAndroid delegate_; |
93 }; | 102 }; |
94 | 103 |
| 104 // Tests that a notification is fired when |
| 105 // a NetworkChangeNotifierDelegateAndroid's observer registers. This validates |
| 106 // that NetworkChangeNotifierAndroid (the observer used in production) is |
| 107 // initialized with the actual connection type rather than CONNECTION_UNKNOWN. |
| 108 TEST_F(BaseNetworkChangeNotifierAndroidTest, |
| 109 NotifierIsInitializedWithCurrentDelegateConnectionType) { |
| 110 ObserverImpl<NetworkChangeNotifierDelegateAndroid::Observer> observer; |
| 111 |
| 112 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 113 delegate_.GetConnectionType(NULL, NULL)); |
| 114 |
| 115 ForceConnectivityState(NetworkChangeNotifierDelegateAndroid::OFFLINE); |
| 116 ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 117 delegate_.GetConnectionType(NULL, NULL)); |
| 118 |
| 119 AddObserverToDelegate(&observer); |
| 120 EXPECT_EQ(1, observer.times_connection_type_changed()); |
| 121 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 122 observer.current_connection()); |
| 123 delegate_.RemoveObserver(&observer); |
| 124 } |
| 125 |
95 class NetworkChangeNotifierDelegateAndroidTest | 126 class NetworkChangeNotifierDelegateAndroidTest |
96 : public BaseNetworkChangeNotifierAndroidTest { | 127 : public BaseNetworkChangeNotifierAndroidTest { |
97 protected: | 128 protected: |
98 typedef ObserverImpl< | 129 typedef ObserverImpl< |
99 NetworkChangeNotifierDelegateAndroid::Observer> TestDelegateObserver; | 130 NetworkChangeNotifierDelegateAndroid::Observer> TestDelegateObserver; |
100 | 131 |
101 NetworkChangeNotifierDelegateAndroidTest() { | 132 NetworkChangeNotifierDelegateAndroidTest() { |
102 delegate_.AddObserver(&delegate_observer_); | 133 AddObserverToDelegate(&delegate_observer_); |
103 delegate_.AddObserver(&other_delegate_observer_); | 134 AddObserverToDelegate(&other_delegate_observer_); |
104 } | 135 } |
105 | 136 |
106 virtual ~NetworkChangeNotifierDelegateAndroidTest() { | 137 virtual ~NetworkChangeNotifierDelegateAndroidTest() { |
107 delegate_.RemoveObserver(&delegate_observer_); | 138 delegate_.RemoveObserver(&delegate_observer_); |
108 delegate_.RemoveObserver(&other_delegate_observer_); | 139 delegate_.RemoveObserver(&other_delegate_observer_); |
109 } | 140 } |
110 | 141 |
111 TestDelegateObserver delegate_observer_; | 142 TestDelegateObserver delegate_observer_; |
112 TestDelegateObserver other_delegate_observer_; | 143 TestDelegateObserver other_delegate_observer_; |
113 }; | 144 }; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 &TestConnectionTypeObserver::current_connection, | 209 &TestConnectionTypeObserver::current_connection, |
179 base::Unretained(&connection_type_observer_))); | 210 base::Unretained(&connection_type_observer_))); |
180 // Check that *all* the observers are notified. | 211 // Check that *all* the observers are notified. |
181 EXPECT_EQ(connection_type_observer_.times_connection_type_changed(), | 212 EXPECT_EQ(connection_type_observer_.times_connection_type_changed(), |
182 other_connection_type_observer_.times_connection_type_changed()); | 213 other_connection_type_observer_.times_connection_type_changed()); |
183 EXPECT_EQ(connection_type_observer_.current_connection(), | 214 EXPECT_EQ(connection_type_observer_.current_connection(), |
184 other_connection_type_observer_.current_connection()); | 215 other_connection_type_observer_.current_connection()); |
185 } | 216 } |
186 | 217 |
187 } // namespace net | 218 } // namespace net |
OLD | NEW |