This source file includes following definitions.
- connection_type_to_return_
- SimulateNetworkConnectionChange
- GetCurrentConnectionType
- eula_accepted_
- IsEulaAccepted
- SetEulaAcceptedForTesting
- SimulateEulaAccepted
- was_notified_
- was_notified
- OnResourceRequestsAllowed
- SetWaitingForNetwork
- SimulateNetworkConnectionChange
- SimulateResourceRequest
- SimulateEulaAccepted
- SetNeedsEulaAcceptance
- SetWaitingForEula
- DisableEulaAndNetwork
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/prefs/testing_pref_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/web_resource/eula_accepted_notifier.h"
#include "chrome/browser/web_resource/resource_request_allowed_notifier_test_util.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
public:
TestNetworkChangeNotifier()
: net::NetworkChangeNotifier(),
connection_type_to_return_(
net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {
}
void SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::ConnectionType type) {
connection_type_to_return_ = type;
net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
base::MessageLoop::current()->RunUntilIdle();
}
private:
virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
return connection_type_to_return_;
}
net::NetworkChangeNotifier::ConnectionType connection_type_to_return_;
DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
};
class TestEulaAcceptedNotifier : public EulaAcceptedNotifier {
public:
TestEulaAcceptedNotifier()
: EulaAcceptedNotifier(NULL),
eula_accepted_(false) {
}
virtual ~TestEulaAcceptedNotifier() {
}
virtual bool IsEulaAccepted() OVERRIDE {
return eula_accepted_;
}
void SetEulaAcceptedForTesting(bool eula_accepted) {
eula_accepted_ = eula_accepted;
}
void SimulateEulaAccepted() {
NotifyObserver();
}
private:
bool eula_accepted_;
DISALLOW_COPY_AND_ASSIGN(TestEulaAcceptedNotifier);
};
class ResourceRequestAllowedNotifierTest
: public testing::Test,
public ResourceRequestAllowedNotifier::Observer {
public:
ResourceRequestAllowedNotifierTest()
: ui_thread(content::BrowserThread::UI, &message_loop),
eula_notifier_(new TestEulaAcceptedNotifier),
was_notified_(false) {
resource_request_allowed_notifier_.InitWithEulaAcceptNotifier(
this, scoped_ptr<EulaAcceptedNotifier>(eula_notifier_));
}
virtual ~ResourceRequestAllowedNotifierTest() { }
bool was_notified() const { return was_notified_; }
virtual void OnResourceRequestsAllowed() OVERRIDE {
was_notified_ = true;
}
void SetWaitingForNetwork(bool waiting) {
resource_request_allowed_notifier_.SetWaitingForNetworkForTesting(waiting);
}
void SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::ConnectionType type) {
network_notifier.SimulateNetworkConnectionChange(type);
}
bool SimulateResourceRequest() {
return resource_request_allowed_notifier_.ResourceRequestsAllowed();
}
void SimulateEulaAccepted() {
eula_notifier_->SimulateEulaAccepted();
}
void SetNeedsEulaAcceptance(bool needs_acceptance) {
eula_notifier_->SetEulaAcceptedForTesting(!needs_acceptance);
}
void SetWaitingForEula(bool waiting) {
resource_request_allowed_notifier_.SetWaitingForEulaForTesting(waiting);
}
void DisableEulaAndNetwork() {
SetWaitingForNetwork(true);
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_NONE);
SetWaitingForEula(true);
SetNeedsEulaAcceptance(true);
}
virtual void SetUp() OVERRIDE {
SetWaitingForEula(false);
SetNeedsEulaAcceptance(false);
}
private:
base::MessageLoopForUI message_loop;
content::TestBrowserThread ui_thread;
TestNetworkChangeNotifier network_notifier;
TestRequestAllowedNotifier resource_request_allowed_notifier_;
TestEulaAcceptedNotifier* eula_notifier_;
bool was_notified_;
DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest);
};
TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOffline) {
SetWaitingForNetwork(true);
EXPECT_FALSE(SimulateResourceRequest());
SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE);
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, DoNotNotifyIfOnlineToOnline) {
SetWaitingForNetwork(false);
EXPECT_TRUE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnReconnect) {
SetWaitingForNetwork(true);
EXPECT_FALSE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
EXPECT_TRUE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnWardriving) {
SetWaitingForNetwork(false);
EXPECT_TRUE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_3G);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_4G);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnFlakyConnection) {
SetWaitingForNetwork(false);
EXPECT_TRUE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_NONE);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NotifyOnFlakyConnection) {
SetWaitingForNetwork(false);
EXPECT_TRUE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_NONE);
EXPECT_FALSE(SimulateResourceRequest());
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_TRUE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NoNotifyOnEulaAfterGoOffline) {
DisableEulaAndNetwork();
EXPECT_FALSE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_NONE);
EXPECT_FALSE(was_notified());
SimulateEulaAccepted();
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) {
SetWaitingForNetwork(true);
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, EulaOnlyNetworkOffline) {
DisableEulaAndNetwork();
EXPECT_FALSE(SimulateResourceRequest());
SimulateEulaAccepted();
EXPECT_FALSE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) {
DisableEulaAndNetwork();
EXPECT_FALSE(SimulateResourceRequest());
SimulateEulaAccepted();
EXPECT_FALSE(was_notified());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_TRUE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) {
DisableEulaAndNetwork();
EXPECT_FALSE(SimulateResourceRequest());
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateEulaAccepted();
EXPECT_TRUE(was_notified());
}
TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotifyEula) {
DisableEulaAndNetwork();
SimulateNetworkConnectionChange(
net::NetworkChangeNotifier::CONNECTION_WIFI);
EXPECT_FALSE(was_notified());
SimulateEulaAccepted();
EXPECT_FALSE(was_notified());
}