| Index: chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc
|
| diff --git a/chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc b/chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4d36110a8f4c909ae2508cbaab0ef0d89a4c4a11
|
| --- /dev/null
|
| +++ b/chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc
|
| @@ -0,0 +1,220 @@
|
| +// 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 "chrome/browser/metrics/variations/resource_request_allowed_notifier.h"
|
| +#include "chrome/common/chrome_notification_types.h"
|
| +#include "chrome/test/base/testing_browser_process.h"
|
| +#include "chrome/test/base/testing_pref_service.h"
|
| +#include "content/public/browser/notification_service.h"
|
| +#include "content/public/test/test_browser_thread.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +// A test class used to validate expected functionality in
|
| +// ResourceRequestAllowedNotifier.
|
| +class TestRequesterService : public ResourceRequestAllowedNotifier::Observer {
|
| + public:
|
| + TestRequesterService() : was_notified_(false) {
|
| + resource_request_allowed_notifier_.SetObserver(this);
|
| + resource_request_allowed_notifier_.RegisterForEulaAcceptedNotification(
|
| + NULL);
|
| + }
|
| +
|
| + virtual ~TestRequesterService() {
|
| + resource_request_allowed_notifier_.ClearObserver();
|
| + }
|
| +
|
| + void SetWasWaitingForNetworkForTesting(bool waiting) {
|
| + resource_request_allowed_notifier_.
|
| + SetWasWaitingForNetworkForTesting(waiting);
|
| + }
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| + void SetWasWaitingForEulaForTesting(bool waiting) {
|
| + resource_request_allowed_notifier_.
|
| + SetWasWaitingForEulaForTesting(waiting);
|
| + }
|
| +#endif
|
| +
|
| + bool request_attempted() const { return was_notified_; }
|
| +
|
| + // ResourceRequestAllowedNotifier::Observer overrides:
|
| + virtual void OnResourceRequestsAllowed() OVERRIDE {
|
| + was_notified_ = true;
|
| + }
|
| +
|
| + private:
|
| + bool was_notified_;
|
| +
|
| + ResourceRequestAllowedNotifier resource_request_allowed_notifier_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestRequesterService);
|
| +};
|
| +
|
| +// Override NetworkChangeNotifier to simulate connection type changes for tests.
|
| +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();
|
| + MessageLoop::current()->RunAllPending();
|
| + }
|
| +
|
| + private:
|
| + virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
|
| + return connection_type_to_return_;
|
| + }
|
| +
|
| + net::NetworkChangeNotifier::ConnectionType connection_type_to_return_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
|
| +};
|
| +
|
| +// A test fixture class for ResourceRequestAllowedNotifier tests that require
|
| +// network state simulations.
|
| +class ResourceRequestAllowedNotifierTest : public testing::Test {
|
| + public:
|
| + ResourceRequestAllowedNotifierTest()
|
| + : scoped_testing_local_state_(
|
| + static_cast<TestingBrowserProcess*>(g_browser_process)),
|
| + local_state_(scoped_testing_local_state_.Get()),
|
| + ui_thread(content::BrowserThread::UI, &message_loop) { }
|
| + ~ResourceRequestAllowedNotifierTest() { }
|
| +
|
| + void SetWasWaitingForNetwork(bool waiting) {
|
| + test_service.SetWasWaitingForNetworkForTesting(waiting);
|
| + }
|
| +
|
| + void SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::ConnectionType type) {
|
| + notifier.SimulateNetworkConnectionChange(type);
|
| + }
|
| +
|
| + bool request_attempted() const {
|
| + return test_service.request_attempted();
|
| + }
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| + void SetEulaAcceptedPref(bool accepted) {
|
| + // TODO(stevet): Share this string with the wizard_controller file that
|
| + // defines it.
|
| + local_state_->SetUserPref("EulaAccepted",
|
| + Value::CreateBooleanValue(accepted));
|
| + }
|
| +
|
| + void SetWasWaitingForEula(bool waiting) {
|
| + test_service.SetWasWaitingForEulaForTesting(waiting);
|
| + }
|
| +
|
| + void SimulateEulaAccepted() {
|
| + SetEulaAcceptedPref(true);
|
| + content::NotificationService::current()->Notify(
|
| + chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED,
|
| + content::NotificationService::AllSources(),
|
| + content::NotificationService::NoDetails());
|
| + }
|
| +
|
| + void SetUp() {
|
| + // Set default EULA state to done (not waiting and EULA accepted) to
|
| + // simplify non-ChromeOS tests.
|
| + SetWasWaitingForEula(false);
|
| + SetEulaAcceptedPref(true);
|
| + }
|
| +#endif
|
| +
|
| + private:
|
| + MessageLoopForUI message_loop;
|
| + ScopedTestingLocalState scoped_testing_local_state_;
|
| + TestingPrefService* local_state_;
|
| + content::TestBrowserThread ui_thread;
|
| + TestNetworkChangeNotifier notifier;
|
| + TestRequesterService test_service;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest);
|
| +};
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) {
|
| + SetWasWaitingForNetwork(true);
|
| + SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE);
|
| + EXPECT_FALSE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) {
|
| + SetWasWaitingForNetwork(false);
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_ETHERNET);
|
| + EXPECT_FALSE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) {
|
| + SetWasWaitingForNetwork(true);
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_ETHERNET);
|
| + EXPECT_TRUE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) {
|
| + SetWasWaitingForNetwork(false);
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_3G);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_4G);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_FALSE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) {
|
| + SetWasWaitingForNetwork(false);
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_NONE);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_FALSE(request_attempted());
|
| +}
|
| +
|
| +#if defined(OS_CHROMEOS)
|
| +TEST_F(ResourceRequestAllowedNotifierTest, EulaOnly) {
|
| + SetWasWaitingForNetwork(true);
|
| + SetEulaAcceptedPref(false);
|
| + SimulateEulaAccepted();
|
| + EXPECT_FALSE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) {
|
| + SetWasWaitingForNetwork(true);
|
| + SetEulaAcceptedPref(false);
|
| + SimulateEulaAccepted();
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_TRUE(request_attempted());
|
| +}
|
| +
|
| +TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) {
|
| + SetWasWaitingForNetwork(true);
|
| + SetWasWaitingForEula(true);
|
| + SetEulaAcceptedPref(false);
|
| + SimulateNetworkConnectionChange(
|
| + net::NetworkChangeNotifier::CONNECTION_WIFI);
|
| + EXPECT_FALSE(request_attempted());
|
| + SimulateEulaAccepted();
|
| + EXPECT_TRUE(request_attempted());
|
| +}
|
| +#endif // OS_CHROMEOS
|
|
|