Chromium Code Reviews| 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..e88030855ef449233d48d73866a1db4383ebc51d |
| --- /dev/null |
| +++ b/chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc |
| @@ -0,0 +1,254 @@ |
| +// 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_test_util.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 { |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
I wonder if you could just merge this with the Res
SteveT
2012/09/21 15:16:17
Yes! That works and is definitely simpler. Thanks.
|
| + public: |
| + TestRequesterService() : was_notified_(false) { |
| + // Set this flag to false so the Init call sets up the wait on the EULA. |
| + SetEulaAccepted(false); |
| + resource_request_allowed_notifier_.Init(this); |
| + } |
| + |
| + virtual ~TestRequesterService() { |
| + } |
| + |
| + void SetWasWaitingForNetworkForTesting(bool waiting) { |
| + resource_request_allowed_notifier_. |
| + SetWasWaitingForNetworkForTesting(waiting); |
| + } |
| + |
| + void SetEulaAccepted(bool accepted) { |
| + resource_request_allowed_notifier_.SetEulaAccepted(accepted); |
| + } |
| + |
| + void OverrideRequestsAllowed(bool override) { |
| + resource_request_allowed_notifier_.OverrideRequestsAllowed(override); |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Maybe just expose a getter for |resource_request_a
SteveT
2012/09/21 15:16:17
Not applicable due to the above refactoring.
|
| + } |
| + |
| +#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_; |
| + |
| + TestRequestAllowedNotifier 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) { |
| + network_notifier.SimulateNetworkConnectionChange(type); |
| + } |
| + |
| + bool request_attempted() const { |
| + return test_service.request_attempted(); |
| + } |
| + |
| + void OverrideRequestsAllowed(bool override) { |
| + test_service.OverrideRequestsAllowed(override); |
| + } |
| + |
| +#if defined(OS_CHROMEOS) |
| + void SetEulaAccepted(bool accepted) { |
| + test_service.SetEulaAccepted(accepted); |
| + } |
| + |
| + void SetWasWaitingForEula(bool waiting) { |
| + test_service.SetWasWaitingForEulaForTesting(waiting); |
| + } |
| + |
| + void SimulateEulaAccepted() { |
| + SetEulaAccepted(true); |
| + content::NotificationService::current()->Notify( |
| + chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| + content::NotificationService::AllSources(), |
| + content::NotificationService::NoDetails()); |
| + } |
| + |
| + // Used in tests involving the EULA. Disables both the EULA accepted state |
| + // and the network. |
| + void DisableEulaAndNetwork() { |
| + SetWasWaitingForNetwork(true); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_NONE); |
| + SetWasWaitingForEula(true); |
| + SetEulaAccepted(false); |
| + } |
| +#endif |
| + |
| + void SetUp() { |
| + // Disable the override of ResourceRequestsAllowed, as these tests do not |
| + // need to mock that state. |
| + OverrideRequestsAllowed(false); |
| +#if defined(OS_CHROMEOS) |
| + // Set default EULA state to done (not waiting and EULA accepted) to |
| + // simplify non-ChromeOS tests. |
| + SetWasWaitingForEula(false); |
| + SetEulaAccepted(true); |
| +#endif |
| + } |
| + |
| + private: |
| + MessageLoopForUI message_loop; |
| + ScopedTestingLocalState scoped_testing_local_state_; |
| + TestingPrefService* local_state_; |
|
Alexei Svitkine (slow)
2012/09/20 21:39:03
Do you need the local state / scoped testing local
SteveT
2012/09/21 15:16:17
Thanks for catching that - forgot to remove it aft
|
| + content::TestBrowserThread ui_thread; |
| + TestNetworkChangeNotifier network_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, EulaOnlyNetworkOffline) { |
| + DisableEulaAndNetwork(); |
| + |
| + SimulateEulaAccepted(); |
| + EXPECT_FALSE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| + SetWasWaitingForNetwork(true); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_NONE); |
| + SetWasWaitingForEula(true); |
| + SetEulaAccepted(false); |
| + OverrideRequestsAllowed(false); |
| + |
| + SimulateEulaAccepted(); |
| + EXPECT_FALSE(request_attempted()); |
| + |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_WIFI); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { |
| + SetWasWaitingForNetwork(true); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_NONE); |
| + SetWasWaitingForEula(true); |
| + SetEulaAccepted(false); |
| + OverrideRequestsAllowed(false); |
| + |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_WIFI); |
| + EXPECT_FALSE(request_attempted()); |
| + |
| + SimulateEulaAccepted(); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| +#endif // OS_CHROMEOS |