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 times_connection_type_has_been_changed_(0), |
| 21 current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { |
| 22 } |
| 23 |
| 24 void OnConnectionTypeChanged( |
| 25 NetworkChangeNotifier::ConnectionType type) { |
| 26 times_connection_type_has_been_changed_++; |
| 27 current_connection_ = type; |
| 28 } |
| 29 |
| 30 int times_connection_type_has_been_changed() const { |
| 31 return times_connection_type_has_been_changed_; |
| 32 } |
| 33 |
| 34 NetworkChangeNotifier::ConnectionType current_connection() const { |
| 35 return current_connection_; |
| 36 } |
| 37 |
| 38 private: |
| 39 int times_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) { |
| 76 // This test exercises JNI calls between the native-side |
| 77 // NetworkChangeNotifierAndroid and java-side NetworkChangeNotifier. |
| 78 EXPECT_EQ(0, observer()->times_connection_type_has_been_changed()); |
| 79 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 80 observer()->current_connection()); |
| 81 |
| 82 ForceConnectivityState(false); |
| 83 MessageLoop::current()->RunAllPending(); |
| 84 |
| 85 EXPECT_EQ(1, observer()->times_connection_type_has_been_changed()); |
| 86 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 87 observer()->current_connection()); |
| 88 |
| 89 ForceConnectivityState(false); |
| 90 MessageLoop::current()->RunAllPending(); |
| 91 |
| 92 EXPECT_EQ(1, observer()->times_connection_type_has_been_changed()); |
| 93 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| 94 observer()->current_connection()); |
| 95 |
| 96 ForceConnectivityState(true); |
| 97 MessageLoop::current()->RunAllPending(); |
| 98 |
| 99 EXPECT_EQ(2, observer()->times_connection_type_has_been_changed()); |
| 100 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 101 observer()->current_connection()); |
| 102 } |
| 103 |
| 104 } // namespace net |
OLD | NEW |