OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "net/android/network_change_notifier_android.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "net/android/network_change_notifier_factory_android.h" | |
9 #include "net/base/network_change_notifier.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace net { | |
13 | |
14 namespace { | |
15 | |
16 class TestConnectionTypeObserver : | |
17 public NetworkChangeNotifier::ConnectionTypeObserver { | |
18 public: | |
19 TestConnectionTypeObserver() : | |
20 connection_type_has_been_changed_(false), | |
21 current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
22 } | |
23 | |
24 void OnConnectionTypeChanged( | |
25 NetworkChangeNotifier::ConnectionType type) { | |
26 connection_type_has_been_changed_ = true; | |
27 current_connection_ = type; | |
28 } | |
29 | |
30 bool connection_type_has_been_changed() const { | |
31 return connection_type_has_been_changed_; | |
32 } | |
33 | |
34 NetworkChangeNotifier::ConnectionType current_connection() const { | |
35 return current_connection_; | |
36 } | |
37 | |
38 private: | |
39 bool connection_type_has_been_changed_; | |
40 NetworkChangeNotifier::ConnectionType current_connection_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(TestConnectionTypeObserver); | |
43 }; | |
44 | |
45 } // namespace | |
46 | |
47 class NetworkChangeNotifierAndroidTest : public testing::Test { | |
48 public: | |
49 NetworkChangeNotifierAndroidTest() : connection_type_observer_(NULL) { | |
50 } | |
51 | |
52 void ForceConnectivityState(bool state) { | |
53 notifier_->ForceConnectivityState(state); | |
54 } | |
55 | |
56 const TestConnectionTypeObserver* observer() const { | |
57 return connection_type_observer_.get(); | |
58 } | |
59 | |
60 protected: | |
61 virtual void SetUp() { | |
62 notifier_.reset(new NetworkChangeNotifierAndroid()); | |
63 connection_type_observer_.reset(new TestConnectionTypeObserver()); | |
64 NetworkChangeNotifier::AddConnectionTypeObserver( | |
65 connection_type_observer_.get()); | |
66 } | |
67 | |
68 private: | |
69 NetworkChangeNotifier::DisableForTest disable_for_test_; | |
70 scoped_ptr<NetworkChangeNotifierAndroid> notifier_; | |
71 scoped_ptr<TestConnectionTypeObserver> connection_type_observer_; | |
72 }; | |
73 | |
74 | |
75 TEST_F(NetworkChangeNotifierAndroidTest, ObserverNotified) { | |
benm (inactive)
2012/09/17 10:35:43
Perhaps a comment here that this is exercising cal
gone
2012/09/17 23:34:21
Done.
| |
76 EXPECT_FALSE(observer()->connection_type_has_been_changed()); | |
77 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, | |
78 observer()->current_connection()); | |
79 | |
80 ForceConnectivityState(false); | |
81 MessageLoop::current()->RunAllPending(); | |
82 | |
83 EXPECT_TRUE(observer()->connection_type_has_been_changed()); | |
84 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, | |
85 observer()->current_connection()); | |
benm (inactive)
2012/09/17 10:35:43
Maybe worth checking we can set it to a connected
gone
2012/09/17 23:34:21
Done.
| |
86 } | |
87 | |
88 } // namespace net | |
OLD | NEW |