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..a9f4b8a5d3dd80c976350f7eb72564789367aae3 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/variations/resource_request_allowed_notifier_unittest.cc |
| @@ -0,0 +1,227 @@ |
| +// 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" |
| + |
| +// 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( |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Add a comment.
SteveT
2012/09/24 18:04:27
Done.
|
| + 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_; |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Add a comment.
SteveT
2012/09/24 18:04:27
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); |
| +}; |
| + |
| +// A test fixture class for ResourceRequestAllowedNotifier tests that require |
| +// network state simulations. This also acts as the service implementing the |
| +// ResourceRequestAllowedNotifier::Observer interface. |
| +class ResourceRequestAllowedNotifierTest |
| + : public testing::Test, |
| + public ResourceRequestAllowedNotifier::Observer { |
| + public: |
| + ResourceRequestAllowedNotifierTest() |
| + : ui_thread(content::BrowserThread::UI, &message_loop), |
| + was_notified_(false) { |
| +#if defined(OS_CHROMEOS) |
| + // Set this flag to false so the Init call sets up the wait on the EULA. |
| + SetEulaAccepted(false); |
| +#endif |
| + resource_request_allowed_notifier_.Init(this); |
| + } |
| + ~ResourceRequestAllowedNotifierTest() { } |
| + |
| + bool request_attempted() const { return was_notified_; } |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Rename the getter too.
SteveT
2012/09/24 18:04:27
Done.
|
| + |
| + // ResourceRequestAllowedNotifier::Observer override: |
| + virtual void OnResourceRequestsAllowed() OVERRIDE { |
| + was_notified_ = true; |
| + } |
| + |
| + // Network manipulation methods: |
| + void SetWasWaitingForNetwork(bool waiting) { |
| + resource_request_allowed_notifier_. |
| + SetWasWaitingForNetworkForTesting(waiting); |
| + } |
| + |
| + void SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::ConnectionType type) { |
| + network_notifier.SimulateNetworkConnectionChange(type); |
| + } |
| + |
| + void SetObserverRequestedPermission(bool requested) { |
| + resource_request_allowed_notifier_. |
| + SetObserverRequestedPermissionForTesting(requested); |
| + } |
| + |
| +#if defined(OS_CHROMEOS) |
| + // Eula manipulation methods: |
| + void SetEulaAccepted(bool accepted) { |
| + resource_request_allowed_notifier_.SetEulaAccepted(accepted); |
| + } |
| + |
| + void SetWasWaitingForEula(bool waiting) { |
| + resource_request_allowed_notifier_.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 |
| + |
| + virtual void SetUp() OVERRIDE { |
| + // Assume the test service has already requested permission, as all tests |
| + // just test that criteria changes notify the server. |
| + SetObserverRequestedPermission(true); |
| +#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; |
| + content::TestBrowserThread ui_thread; |
| + TestNetworkChangeNotifier network_notifier; |
| + TestRequestAllowedNotifier resource_request_allowed_notifier_; |
| + bool was_notified_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); |
| +}; |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Nit: Rename tests to not say "request", use the wo
SteveT
2012/09/24 18:04:27
Done.
|
| + 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()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, NoRequestNoNotify) { |
| + // Ensure that if the observing service does not request access, it does not |
| + // get notified, even if the criteria is met. |
|
Alexei Svitkine (slow)
2012/09/24 15:59:36
Can you add a similar test but with the Eula condi
SteveT
2012/09/24 18:04:27
Done.
|
| + SetObserverRequestedPermission(false); |
| + SetWasWaitingForNetwork(true); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| + 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); |
| + |
| + 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); |
| + |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_WIFI); |
| + EXPECT_FALSE(request_attempted()); |
| + |
| + SimulateEulaAccepted(); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| +#endif // OS_CHROMEOS |