diff options
author | dfalcantara@chromium.org <dfalcantara@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-20 02:27:14 +0000 |
---|---|---|
committer | dfalcantara@chromium.org <dfalcantara@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-20 02:27:14 +0000 |
commit | ab3309da32a9436501b4837f9026d2a2eeae15f7 (patch) | |
tree | afdb3b07b9878a3b74f1d135910cf1238bbf0065 /net/android/network_change_notifier_android_unittest.cc | |
parent | b6f3769c7098ac010914b44040c1d7975bb919ac (diff) | |
download | chromium_src-ab3309da32a9436501b4837f9026d2a2eeae15f7.zip chromium_src-ab3309da32a9436501b4837f9026d2a2eeae15f7.tar.gz chromium_src-ab3309da32a9436501b4837f9026d2a2eeae15f7.tar.bz2 |
Add native-side unit test for Android NetworkChangeNotifier
* Renames some classes to move them out of the android namespace.
* Changes singleton handling in the Java NetworkChangeNotifier. The singleton
is unfortunately still required for Android to turn the AutoDetector on.
* Adds a native-side unit test to track that the JNI calls are correctly plumbed
to alert native-side observers of connection changes.
BUG=136984
Review URL: https://chromiumcodereview.appspot.com/10928193
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157687 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/android/network_change_notifier_android_unittest.cc')
-rw-r--r-- | net/android/network_change_notifier_android_unittest.cc | 104 |
1 files changed, 104 insertions, 0 deletions
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 0000000..356ac4c --- /dev/null +++ b/net/android/network_change_notifier_android_unittest.cc @@ -0,0 +1,104 @@ +// 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() : + times_connection_type_has_been_changed_(0), + current_connection_(NetworkChangeNotifier::CONNECTION_UNKNOWN) { + } + + void OnConnectionTypeChanged( + NetworkChangeNotifier::ConnectionType type) { + times_connection_type_has_been_changed_++; + current_connection_ = type; + } + + int times_connection_type_has_been_changed() const { + return times_connection_type_has_been_changed_; + } + + NetworkChangeNotifier::ConnectionType current_connection() const { + return current_connection_; + } + + private: + int times_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) { + // This test exercises JNI calls between the native-side + // NetworkChangeNotifierAndroid and java-side NetworkChangeNotifier. + EXPECT_EQ(0, observer()->times_connection_type_has_been_changed()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, + observer()->current_connection()); + + ForceConnectivityState(false); + MessageLoop::current()->RunAllPending(); + + EXPECT_EQ(1, observer()->times_connection_type_has_been_changed()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + observer()->current_connection()); + + ForceConnectivityState(false); + MessageLoop::current()->RunAllPending(); + + EXPECT_EQ(1, observer()->times_connection_type_has_been_changed()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + observer()->current_connection()); + + ForceConnectivityState(true); + MessageLoop::current()->RunAllPending(); + + EXPECT_EQ(2, observer()->times_connection_type_has_been_changed()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, + observer()->current_connection()); +} + +} // namespace net |