This source file includes following definitions.
- RecomputeCurrentConnectionType
- ExitMessageLoopAndReturnFalse
- StartWatchingAndSucceed
- StartWatchingAndFail
- SignalAndSucceed
- SignalAndFail
- RetryAndSucceed
- RetryAndFail
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/message_loop/message_loop.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_change_notifier_factory.h"
#include "net/base/network_change_notifier_win.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::StrictMock;
namespace net {
namespace {
class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin {
public:
TestNetworkChangeNotifierWin() {}
virtual ~TestNetworkChangeNotifierWin() {
set_is_watching(false);
}
virtual NetworkChangeNotifier::ConnectionType
RecomputeCurrentConnectionType() const OVERRIDE {
return NetworkChangeNotifier::CONNECTION_UNKNOWN;
}
MOCK_METHOD0(WatchForAddressChangeInternal, bool());
private:
DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifierWin);
};
class TestIPAddressObserver
: public net::NetworkChangeNotifier::IPAddressObserver {
public:
TestIPAddressObserver() {
NetworkChangeNotifier::AddIPAddressObserver(this);
}
~TestIPAddressObserver() {
NetworkChangeNotifier::RemoveIPAddressObserver(this);
}
MOCK_METHOD0(OnIPAddressChanged, void());
private:
DISALLOW_COPY_AND_ASSIGN(TestIPAddressObserver);
};
bool ExitMessageLoopAndReturnFalse() {
base::MessageLoop::current()->Quit();
return false;
}
}
class NetworkChangeNotifierWinTest : public testing::Test {
public:
void StartWatchingAndSucceed() {
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0);
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(1)
.WillOnce(Return(true));
network_change_notifier_.WatchForAddressChange();
EXPECT_TRUE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
base::MessageLoop::current()->RunUntilIdle();
}
void StartWatchingAndFail() {
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0);
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(AtLeast(1))
.WillRepeatedly(Return(false));
network_change_notifier_.WatchForAddressChange();
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_LT(0, network_change_notifier_.sequential_failures());
base::MessageLoop::current()->RunUntilIdle();
}
void SignalAndSucceed() {
EXPECT_TRUE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1);
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(1)
.WillOnce(Return(true));
network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE);
EXPECT_TRUE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
base::MessageLoop::current()->RunUntilIdle();
}
void SignalAndFail() {
EXPECT_TRUE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1);
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(AtLeast(1))
.WillRepeatedly(Return(false));
network_change_notifier_.OnObjectSignaled(INVALID_HANDLE_VALUE);
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_LT(0, network_change_notifier_.sequential_failures());
base::MessageLoop::current()->RunUntilIdle();
}
void RetryAndSucceed() {
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_LT(0, network_change_notifier_.sequential_failures());
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(1)
.WillOnce(
Invoke(base::MessageLoop::current(), &base::MessageLoop::Quit));
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(1).WillOnce(Return(true));
base::MessageLoop::current()->Run();
EXPECT_TRUE(network_change_notifier_.is_watching());
EXPECT_EQ(0, network_change_notifier_.sequential_failures());
}
void RetryAndFail() {
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_LT(0, network_change_notifier_.sequential_failures());
int initial_sequential_failures =
network_change_notifier_.sequential_failures();
EXPECT_CALL(test_ip_address_observer_, OnIPAddressChanged()).Times(0);
EXPECT_CALL(network_change_notifier_, WatchForAddressChangeInternal())
.Times(AtLeast(1))
.WillRepeatedly(Invoke(ExitMessageLoopAndReturnFalse));
base::MessageLoop::current()->Run();
EXPECT_FALSE(network_change_notifier_.is_watching());
EXPECT_LT(initial_sequential_failures,
network_change_notifier_.sequential_failures());
base::MessageLoop::current()->RunUntilIdle();
}
private:
NetworkChangeNotifier::DisableForTest disable_for_test_;
StrictMock<TestNetworkChangeNotifierWin> network_change_notifier_;
StrictMock<TestIPAddressObserver> test_ip_address_observer_;
};
TEST_F(NetworkChangeNotifierWinTest, NetChangeWinBasic) {
StartWatchingAndSucceed();
}
TEST_F(NetworkChangeNotifierWinTest, NetChangeWinFailStart) {
StartWatchingAndFail();
}
TEST_F(NetworkChangeNotifierWinTest, FLAKY_NetChangeWinFailStartOnce) {
StartWatchingAndFail();
RetryAndSucceed();
}
TEST_F(NetworkChangeNotifierWinTest, FLAKY_NetChangeWinFailStartTwice) {
StartWatchingAndFail();
RetryAndFail();
RetryAndSucceed();
}
TEST_F(NetworkChangeNotifierWinTest, NetChangeWinSignal) {
StartWatchingAndSucceed();
SignalAndSucceed();
}
TEST_F(NetworkChangeNotifierWinTest, FLAKY_NetChangeWinFailSignalOnce) {
StartWatchingAndSucceed();
SignalAndFail();
RetryAndSucceed();
}
TEST_F(NetworkChangeNotifierWinTest, FLAKY_NetChangeWinFailSignalTwice) {
StartWatchingAndSucceed();
SignalAndFail();
RetryAndFail();
RetryAndSucceed();
}
}