This source file includes following definitions.
- OnConnectionTypeChanged
- notifications_count
- OnConnectionTypeChanged
- notifications_count
- RunTest
- SetOnline
- SetOffline
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "net/android/network_change_notifier_android.h"
#include "net/android/network_change_notifier_delegate_android.h"
#include "net/base/network_change_notifier.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class NetworkChangeNotifierDelegateAndroidObserver
: public NetworkChangeNotifierDelegateAndroid::Observer {
public:
NetworkChangeNotifierDelegateAndroidObserver() : notifications_count_(0) {}
virtual void OnConnectionTypeChanged() OVERRIDE {
notifications_count_++;
}
int notifications_count() const {
return notifications_count_;
}
private:
int notifications_count_;
};
class NetworkChangeNotifierObserver
: public NetworkChangeNotifier::ConnectionTypeObserver {
public:
NetworkChangeNotifierObserver() : notifications_count_(0) {}
virtual void OnConnectionTypeChanged(
NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE {
notifications_count_++;
}
int notifications_count() const {
return notifications_count_;
}
private:
int notifications_count_;
};
}
class BaseNetworkChangeNotifierAndroidTest : public testing::Test {
protected:
typedef NetworkChangeNotifier::ConnectionType ConnectionType;
virtual ~BaseNetworkChangeNotifierAndroidTest() {}
void RunTest(
const base::Callback<int(void)>& notifications_count_getter,
const base::Callback<ConnectionType(void)>& connection_type_getter) {
EXPECT_EQ(0, notifications_count_getter.Run());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
connection_type_getter.Run());
SetOffline();
EXPECT_EQ(1, notifications_count_getter.Run());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
connection_type_getter.Run());
SetOffline();
EXPECT_EQ(1, notifications_count_getter.Run());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
connection_type_getter.Run());
SetOnline();
EXPECT_EQ(2, notifications_count_getter.Run());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
connection_type_getter.Run());
}
void SetOnline() {
delegate_.SetOnline();
base::MessageLoop::current()->RunUntilIdle();
}
void SetOffline() {
delegate_.SetOffline();
base::MessageLoop::current()->RunUntilIdle();
}
NetworkChangeNotifierDelegateAndroid delegate_;
};
TEST_F(BaseNetworkChangeNotifierAndroidTest,
DelegateIsInitializedWithCurrentConnectionType) {
SetOffline();
ASSERT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
delegate_.GetCurrentConnectionType());
scoped_ptr<NetworkChangeNotifierDelegateAndroid> other_delegate(
new NetworkChangeNotifierDelegateAndroid());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
other_delegate->GetCurrentConnectionType());
SetOnline();
ASSERT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
delegate_.GetCurrentConnectionType());
other_delegate.reset(new NetworkChangeNotifierDelegateAndroid());
EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN,
other_delegate->GetCurrentConnectionType());
}
class NetworkChangeNotifierDelegateAndroidTest
: public BaseNetworkChangeNotifierAndroidTest {
protected:
NetworkChangeNotifierDelegateAndroidTest() {
delegate_.AddObserver(&delegate_observer_);
delegate_.AddObserver(&other_delegate_observer_);
}
virtual ~NetworkChangeNotifierDelegateAndroidTest() {
delegate_.RemoveObserver(&delegate_observer_);
delegate_.RemoveObserver(&other_delegate_observer_);
}
NetworkChangeNotifierDelegateAndroidObserver delegate_observer_;
NetworkChangeNotifierDelegateAndroidObserver other_delegate_observer_;
};
TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) {
RunTest(
base::Bind(
&NetworkChangeNotifierDelegateAndroidObserver::notifications_count,
base::Unretained(&delegate_observer_)),
base::Bind(
&NetworkChangeNotifierDelegateAndroid::GetCurrentConnectionType,
base::Unretained(&delegate_)));
EXPECT_EQ(delegate_observer_.notifications_count(),
other_delegate_observer_.notifications_count());
}
class NetworkChangeNotifierAndroidTest
: public BaseNetworkChangeNotifierAndroidTest {
protected:
NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) {
NetworkChangeNotifier::AddConnectionTypeObserver(
&connection_type_observer_);
NetworkChangeNotifier::AddConnectionTypeObserver(
&other_connection_type_observer_);
}
NetworkChangeNotifierObserver connection_type_observer_;
NetworkChangeNotifierObserver other_connection_type_observer_;
NetworkChangeNotifier::DisableForTest disable_for_test_;
NetworkChangeNotifierAndroid notifier_;
};
TEST_F(NetworkChangeNotifierAndroidTest,
NotificationsSentToNetworkChangeNotifierAndroid) {
RunTest(
base::Bind(
&NetworkChangeNotifierObserver::notifications_count,
base::Unretained(&connection_type_observer_)),
base::Bind(
&NetworkChangeNotifierAndroid::GetCurrentConnectionType,
base::Unretained(¬ifier_)));
}
TEST_F(NetworkChangeNotifierAndroidTest,
NotificationsSentToClientsOfNetworkChangeNotifier) {
RunTest(
base::Bind(
&NetworkChangeNotifierObserver::notifications_count,
base::Unretained(&connection_type_observer_)),
base::Bind(&NetworkChangeNotifier::GetConnectionType));
EXPECT_EQ(connection_type_observer_.notifications_count(),
other_connection_type_observer_.notifications_count());
}
}