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 | |
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')
-rw-r--r-- | net/android/java/src/org/chromium/net/NetworkChangeNotifier.java | 62 | ||||
-rw-r--r-- | net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java | 2 | ||||
-rw-r--r-- | net/android/net_jni_registrar.cc | 3 | ||||
-rw-r--r-- | net/android/network_change_notifier_android.cc | 37 | ||||
-rw-r--r-- | net/android/network_change_notifier_android.h | 20 | ||||
-rw-r--r-- | net/android/network_change_notifier_android_unittest.cc | 104 | ||||
-rw-r--r-- | net/android/network_change_notifier_factory.cc | 19 | ||||
-rw-r--r-- | net/android/network_change_notifier_factory_android.cc | 17 | ||||
-rw-r--r-- | net/android/network_change_notifier_factory_android.h (renamed from net/android/network_change_notifier_factory.h) | 16 |
9 files changed, 203 insertions, 77 deletions
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java index 9521231..7f1bdc3 100644 --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java @@ -42,7 +42,41 @@ public class NetworkChangeNotifier { mContext = context; mNativeChangeNotifier = nativeChangeNotifier; mConnectionType = CONNECTION_UNKNOWN; - sInstance = this; + } + + private void destroy() { + if (mAutoDetector != null) { + mAutoDetector.destroy(); + } + mNativeChangeNotifier = 0; + } + + /** + * Creates the singleton used by the native-side NetworkChangeNotifier. + */ + @CalledByNative + static NetworkChangeNotifier createInstance(Context context, int nativeChangeNotifier) { + assert sInstance == null; + sInstance = new NetworkChangeNotifier(context, nativeChangeNotifier); + return sInstance; + } + + /** + * Destroys the singleton used by the native-side NetworkChangeNotifier. + */ + @CalledByNative + private static void destroyInstance() { + assert sInstance != null; + sInstance.destroy(); + sInstance = null; + } + + /** + * Returns the instance used by the native-side NetworkChangeNotifier. + */ + public static NetworkChangeNotifier getInstance() { + assert sInstance != null; + return sInstance; } /** @@ -80,6 +114,7 @@ public class NetworkChangeNotifier { * @param networkAvailable True if the NetworkChangeNotifier should * perceive a "connected" state, false implies "disconnected". */ + @CalledByNative public static void forceConnectivityState(boolean networkAvailable) { assert sInstance != null; setAutoDetectConnectivityState(false); @@ -90,13 +125,13 @@ public class NetworkChangeNotifier { boolean connectionCurrentlyExists = mConnectionType != CONNECTION_NONE; if (connectionCurrentlyExists != forceOnline) { mConnectionType = forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE; - notifyNativeObservers(); + notifyObserversOfConnectionTypeChange(); } } - void notifyNativeObservers() { + void notifyObserversOfConnectionTypeChange() { if (mNativeChangeNotifier != 0) { - nativeNotifyObservers(mNativeChangeNotifier); + nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier); } } @@ -108,25 +143,12 @@ public class NetworkChangeNotifier { return mConnectionType; } - @CalledByNative - private void destroy() { - if (mAutoDetector != null) { - mAutoDetector.destroy(); - } - mNativeChangeNotifier = 0; - sInstance = null; - } - - @CalledByNative - private static NetworkChangeNotifier create(Context context, int nativeNetworkChangeNotifier) { - return new NetworkChangeNotifier(context, nativeNetworkChangeNotifier); - } - - @NativeClassQualifiedName("android::NetworkChangeNotifier") - private native void nativeNotifyObservers(int nativePtr); + @NativeClassQualifiedName("NetworkChangeNotifierAndroid") + private native void nativeNotifyObserversOfConnectionTypeChange(int nativePtr); // For testing only. public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { + assert sInstance != null; return sInstance.mAutoDetector; } } diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java index c9e1016..573fb9e 100644 --- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java +++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java @@ -130,7 +130,7 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver if (newConnectionType != mConnectionType) { mConnectionType = newConnectionType; Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType); - mOwner.notifyNativeObservers(); + mOwner.notifyObserversOfConnectionTypeChange(); } } diff --git a/net/android/net_jni_registrar.cc b/net/android/net_jni_registrar.cc index 9667f08..bd0d155 100644 --- a/net/android/net_jni_registrar.cc +++ b/net/android/net_jni_registrar.cc @@ -16,7 +16,8 @@ namespace android { static base::android::RegistrationMethod kNetRegisteredMethods[] = { { "AndroidNetworkLibrary", net::android::RegisterNetworkLibrary }, - { "NetworkChangeNotifier", net::android::NetworkChangeNotifier::Register }, + { "NetworkChangeNotifierAndroid", + net::NetworkChangeNotifierAndroid::Register }, { "ProxyConfigService", net::ProxyConfigServiceAndroid::Register }, }; diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc index 473fa64..66aacaf 100644 --- a/net/android/network_change_notifier_android.cc +++ b/net/android/network_change_notifier_android.cc @@ -9,33 +9,35 @@ #include "jni/NetworkChangeNotifier_jni.h" namespace net { -namespace android { -NetworkChangeNotifier::NetworkChangeNotifier() { +NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid() { JNIEnv* env = base::android::AttachCurrentThread(); - CreateJavaObject(env); + java_network_change_notifier_.Reset( + Java_NetworkChangeNotifier_createInstance( + env, + base::android::GetApplicationContext(), + reinterpret_cast<jint>(this))); } -NetworkChangeNotifier::~NetworkChangeNotifier() { +NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { JNIEnv* env = base::android::AttachCurrentThread(); - Java_NetworkChangeNotifier_destroy( - env, java_network_change_notifier_.obj()); + Java_NetworkChangeNotifier_destroyInstance(env); + java_network_change_notifier_.Reset(); } -void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { - java_network_change_notifier_.Reset( - Java_NetworkChangeNotifier_create( - env, - base::android::GetApplicationContext(), - reinterpret_cast<jint>(this))); +void NetworkChangeNotifierAndroid::NotifyObserversOfConnectionTypeChange( + JNIEnv* env, + jobject obj) { + NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); } -void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { - NotifyObserversOfConnectionTypeChange(); +void NetworkChangeNotifierAndroid::ForceConnectivityState(bool state) { + JNIEnv* env = base::android::AttachCurrentThread(); + Java_NetworkChangeNotifier_forceConnectivityState(env, state); } -net::NetworkChangeNotifier::ConnectionType - NetworkChangeNotifier::GetCurrentConnectionType() const { +NetworkChangeNotifier::ConnectionType + NetworkChangeNotifierAndroid::GetCurrentConnectionType() const { JNIEnv* env = base::android::AttachCurrentThread(); // Pull the connection type from the Java-side then convert it to a @@ -64,9 +66,8 @@ net::NetworkChangeNotifier::ConnectionType } // static -bool NetworkChangeNotifier::Register(JNIEnv* env) { +bool NetworkChangeNotifierAndroid::Register(JNIEnv* env) { return RegisterNativesImpl(env); } -} // namespace android } // namespace net diff --git a/net/android/network_change_notifier_android.h b/net/android/network_change_notifier_android.h index 3c71dc5..2824600 100644 --- a/net/android/network_change_notifier_android.h +++ b/net/android/network_change_notifier_android.h @@ -11,30 +11,32 @@ #include "net/base/network_change_notifier.h" namespace net { -namespace android { -class NetworkChangeNotifier : public net::NetworkChangeNotifier { +class NetworkChangeNotifierAndroidTest; + +class NetworkChangeNotifierAndroid : public NetworkChangeNotifier { public: - NetworkChangeNotifier(); - virtual ~NetworkChangeNotifier(); + NetworkChangeNotifierAndroid(); + virtual ~NetworkChangeNotifierAndroid(); - void NotifyObservers(JNIEnv* env, jobject obj); + void NotifyObserversOfConnectionTypeChange(JNIEnv* env, jobject obj); static bool Register(JNIEnv* env); private: - void CreateJavaObject(JNIEnv* env); + friend class NetworkChangeNotifierAndroidTest; // NetworkChangeNotifier: - virtual net::NetworkChangeNotifier::ConnectionType + virtual NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() const OVERRIDE; + void ForceConnectivityState(bool state); + base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; - DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); + DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifierAndroid); }; -} // namespace android } // namespace net #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_ 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 diff --git a/net/android/network_change_notifier_factory.cc b/net/android/network_change_notifier_factory.cc deleted file mode 100644 index 82d276139..0000000 --- a/net/android/network_change_notifier_factory.cc +++ /dev/null @@ -1,19 +0,0 @@ -// 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_factory.h" - -#include "net/android/network_change_notifier_android.h" - -namespace net { -namespace android { - -NetworkChangeNotifierFactory::NetworkChangeNotifierFactory() {} - -net::NetworkChangeNotifier* NetworkChangeNotifierFactory::CreateInstance() { - return new NetworkChangeNotifier(); -} - -} // namespace android -} // namespace net diff --git a/net/android/network_change_notifier_factory_android.cc b/net/android/network_change_notifier_factory_android.cc new file mode 100644 index 0000000..f15b39a --- /dev/null +++ b/net/android/network_change_notifier_factory_android.cc @@ -0,0 +1,17 @@ +// 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_factory_android.h" + +#include "net/android/network_change_notifier_android.h" + +namespace net { + +NetworkChangeNotifierFactoryAndroid::NetworkChangeNotifierFactoryAndroid() {} + +NetworkChangeNotifier* NetworkChangeNotifierFactoryAndroid::CreateInstance() { + return new NetworkChangeNotifierAndroid(); +} + +} // namespace net diff --git a/net/android/network_change_notifier_factory.h b/net/android/network_change_notifier_factory_android.h index 0131b9e..3f5f0b6 100644 --- a/net/android/network_change_notifier_factory.h +++ b/net/android/network_change_notifier_factory_android.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_ -#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_ +#ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_ANDROID_H_ +#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_ANDROID_H_ #include "base/compiler_specific.h" #include "net/base/network_change_notifier_factory.h" @@ -12,19 +12,17 @@ namespace net { class NetworkChangeNotifier; -namespace android { - // NetworkChangeNotifierFactory creates Android-specific specialization of // NetworkChangeNotifier. -class NetworkChangeNotifierFactory : public net::NetworkChangeNotifierFactory { +class NetworkChangeNotifierFactoryAndroid : + public NetworkChangeNotifierFactory { public: - NetworkChangeNotifierFactory(); + NetworkChangeNotifierFactoryAndroid(); - // Overrides of net::NetworkChangeNotifierFactory. - virtual net::NetworkChangeNotifier* CreateInstance() OVERRIDE; + // Overrides of NetworkChangeNotifierFactory. + virtual NetworkChangeNotifier* CreateInstance() OVERRIDE; }; -} // namespace android } // namespace net #endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_ |