Chromium Code Reviews| Index: net/android/network_change_notifier_android_unittest.cc |
| diff --git a/net/android/network_change_notifier_android_unittest.cc b/net/android/network_change_notifier_android_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..267dcae7f7b543dbd30d04cdcdd308d15490fa36 |
| --- /dev/null |
| +++ b/net/android/network_change_notifier_android_unittest.cc |
| @@ -0,0 +1,88 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/android/network_change_notifier_android.h" |
| + |
| +#include "base/message_loop.h" |
| +#include "net/android/network_change_notifier_factory_android.h" |
| +#include "net/base/network_change_notifier.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| + |
| +namespace { |
| + |
| +class TestConnectionTypeObserver : |
| + public NetworkChangeNotifier::ConnectionTypeObserver { |
| + public: |
| + TestConnectionTypeObserver() : |
| + connection_type_has_been_changed_(false), |
| + current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { |
| + } |
| + |
| + void OnConnectionTypeChanged( |
| + NetworkChangeNotifier::ConnectionType type) { |
| + connection_type_has_been_changed_ = true; |
| + current_connection_ = type; |
| + } |
| + |
| + bool connection_type_has_been_changed() const { |
| + return connection_type_has_been_changed_; |
| + } |
| + |
| + NetworkChangeNotifier::ConnectionType current_connection() const { |
| + return current_connection_; |
| + } |
| + |
| + private: |
| + bool connection_type_has_been_changed_; |
| + NetworkChangeNotifier::ConnectionType current_connection_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestConnectionTypeObserver); |
| +}; |
| + |
| +} // namespace |
| + |
| +class NetworkChangeNotifierAndroidTest : public testing::Test { |
| + public: |
| + NetworkChangeNotifierAndroidTest() : connection_type_observer_(NULL) { |
| + } |
| + |
| + void ForceConnectivityState(bool state) { |
| + notifier_->ForceConnectivityState(state); |
| + } |
| + |
| + const TestConnectionTypeObserver* observer() const { |
| + return connection_type_observer_.get(); |
| + } |
| + |
| + protected: |
| + virtual void SetUp() { |
| + notifier_.reset(new NetworkChangeNotifierAndroid()); |
| + connection_type_observer_.reset(new TestConnectionTypeObserver()); |
| + NetworkChangeNotifier::AddConnectionTypeObserver( |
| + connection_type_observer_.get()); |
| + } |
| + |
| + private: |
| + NetworkChangeNotifier::DisableForTest disable_for_test_; |
| + scoped_ptr<NetworkChangeNotifierAndroid> notifier_; |
| + scoped_ptr<TestConnectionTypeObserver> connection_type_observer_; |
| +}; |
| + |
| + |
| +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.
|
| + EXPECT_FALSE(observer()->connection_type_has_been_changed()); |
| + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| + observer()->current_connection()); |
| + |
| + ForceConnectivityState(false); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_TRUE(observer()->connection_type_has_been_changed()); |
| + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, |
| + 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.
|
| +} |
| + |
| +} // namespace net |