Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Unified Diff: net/android/network_change_notifier_android_unittest.cc

Issue 10928193: Add native-side unit test for Android NetworkChangeNotifier (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed Java-side observers for now Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..42ee03ac02d752e1eeeba7067f1033ec833f3d36
--- /dev/null
+++ b/net/android/network_change_notifier_android_unittest.cc
@@ -0,0 +1,87 @@
+// 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.h"
+#include "net/base/network_change_notifier.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+namespace android {
+
+namespace {
+
+class TestConnectionTypeObserver :
+ public net::NetworkChangeNotifier::ConnectionTypeObserver {
+ public:
+ TestConnectionTypeObserver() :
+ connection_type_has_been_changed_(false),
+ current_connection_(net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {
+ }
+
+ void OnConnectionTypeChanged(
+ net::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_;
+ }
+
+ net::NetworkChangeNotifier::ConnectionType current_connection() const {
+ return current_connection_;
+ }
+
+ private:
+ bool connection_type_has_been_changed_;
+ net::NetworkChangeNotifier::ConnectionType current_connection_;
szym 2012/09/14 19:07:29 nit: DISALLOW_COPY_AND_ASSIGN(TestConnectionTypeOb
gone 2012/09/15 01:01:55 Done.
+};
+
+} // namespace
+
+class NetworkChangeNotifierAndroidTest : public testing::Test {
+ public:
+ NetworkChangeNotifierAndroidTest() : connection_type_observer_(NULL) {
+ }
+
+ virtual void SetUp() {
szym 2012/09/14 19:07:29 Everything from here down can be protected:
gone 2012/09/15 01:01:55 Done.
+ notifier_.reset(new net::android::NetworkChangeNotifier());
szym 2012/09/14 19:07:29 You don't really need "net::android::" here. I und
gone 2012/09/15 01:01:55 Renamed.
+ connection_type_observer_.reset(new TestConnectionTypeObserver());
+ net::NetworkChangeNotifier::AddConnectionTypeObserver(
+ connection_type_observer_.get());
+ }
+
+ net::android::NetworkChangeNotifier* notifier() {
szym 2012/09/14 19:07:29 Rather than the getter I suggest a forwarding Forc
gone 2012/09/15 01:01:55 Done.
+ return notifier_.get();
+ }
+
+ TestConnectionTypeObserver* observer() {
szym 2012/09/14 19:07:29 nit: const ... const?
gone 2012/09/15 01:01:55 Done.
+ return connection_type_observer_.get();
+ }
+
+ private:
+ NetworkChangeNotifier::DisableForTest disable_for_test_;
+ scoped_ptr<net::android::NetworkChangeNotifier> notifier_;
+ scoped_ptr<TestConnectionTypeObserver> connection_type_observer_;
+};
+
+
+TEST_F(NetworkChangeNotifierAndroidTest, ObserverNotified) {
+ EXPECT_FALSE(observer()->connection_type_has_been_changed());
+ EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_UNKNOWN,
+ observer()->current_connection());
+
+ notifier()->ForceConnectivityState(false);
+ MessageLoop::current()->RunAllPending();
+
+ EXPECT_TRUE(observer()->connection_type_has_been_changed());
+ EXPECT_EQ(net::NetworkChangeNotifier::CONNECTION_NONE,
+ observer()->current_connection());
+}
+
+} // namespace anrdoid
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698