Chromium Code Reviews| Index: chrome/browser/resource_request_allowed_notifier_unittest.cc |
| diff --git a/chrome/browser/resource_request_allowed_notifier_unittest.cc b/chrome/browser/resource_request_allowed_notifier_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3387c5fae43a46575ca1553a7126916e4d64065a |
| --- /dev/null |
| +++ b/chrome/browser/resource_request_allowed_notifier_unittest.cc |
| @@ -0,0 +1,205 @@ |
| +// 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/resource_request_allowed_notifier.h" |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
This is an anti-pattern. The style guide requires
SteveT
2012/09/14 20:25:09
Good to know! Fixed.
|
| + |
| +#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() : request_attempted_(false) { |
| + resource_request_allowed_notifier_.AddObserver(this); |
| + } |
| + |
| + virtual ~TestRequesterService() { |
| + resource_request_allowed_notifier_.RemoveObserver(this); |
| + } |
| + |
| + void SetWasOfflineDuringLastRequestAttemptForTesting(bool offline) { |
| + resource_request_allowed_notifier_. |
| + SetWasOfflineDuringLastRequestAttemptForTesting(offline); |
| + } |
| + |
| + bool request_attempted() const { return request_attempted_; } |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
I would call this "was_notified".
SteveT
2012/09/14 20:25:09
Done.
|
| + void SetRequestAttempted(bool attempted) { request_attempted_ = attempted; } |
|
Alexei Svitkine (slow)
2012/09/12 15:15:46
This should use unix_hacker style since it's not d
SteveT
2012/09/14 20:25:09
Didn't realize the style was called that.
Also, t
|
| + |
| + // ResourceRequestAllowedNotifier::Observer overrides: |
| + virtual void OnResourceRequestsAllowed() OVERRIDE { |
| + request_attempted_ = true; |
| + } |
| + |
| + private: |
| + bool request_attempted_; |
| + |
| + 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 SetWasOfflineDuringLastRequestAttempt(bool offline) { |
| + test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline); |
| + } |
| + |
| + 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 SimulateEulaAccepted() { |
| + SetEulaAcceptedPref(true); |
| + content::NotificationService::current()->Notify( |
| + chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, |
| + content::NotificationService::AllSources(), |
| + content::NotificationService::NoDetails()); |
| + } |
| + |
| + void SetUp() { |
| + 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) { |
| + SetWasOfflineDuringLastRequestAttempt(true); |
| + SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); |
| + EXPECT_FALSE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { |
| + SetWasOfflineDuringLastRequestAttempt(false); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| + EXPECT_FALSE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { |
| + SetWasOfflineDuringLastRequestAttempt(true); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { |
| + SetWasOfflineDuringLastRequestAttempt(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) { |
| + SetWasOfflineDuringLastRequestAttempt(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) { |
| + SetWasOfflineDuringLastRequestAttempt(true); |
| + SetEulaAcceptedPref(false); |
| + SimulateEulaAccepted(); |
| + EXPECT_FALSE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { |
| + SetWasOfflineDuringLastRequestAttempt(true); |
| + SetEulaAcceptedPref(false); |
| + SimulateEulaAccepted(); |
| + EXPECT_FALSE(request_attempted()); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_WIFI); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| + |
| +TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { |
| + SetWasOfflineDuringLastRequestAttempt(true); |
| + SetEulaAcceptedPref(false); |
| + SimulateNetworkConnectionChange( |
| + net::NetworkChangeNotifier::CONNECTION_WIFI); |
| + EXPECT_FALSE(request_attempted()); |
| + SimulateEulaAccepted(); |
| + EXPECT_TRUE(request_attempted()); |
| +} |
| +#endif // OS_CHROMEOS |