Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #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.
| |
| 6 | |
| 7 #include "chrome/common/chrome_notification_types.h" | |
| 8 #include "chrome/test/base/testing_browser_process.h" | |
| 9 #include "chrome/test/base/testing_pref_service.h" | |
| 10 #include "content/public/browser/notification_service.h" | |
| 11 #include "content/public/test/test_browser_thread.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 // A test class used to validate expected functionality in | |
| 15 // ResourceRequestAllowedNotifier. | |
| 16 class TestRequesterService : public ResourceRequestAllowedNotifier::Observer { | |
| 17 public: | |
| 18 TestRequesterService() : request_attempted_(false) { | |
| 19 resource_request_allowed_notifier_.AddObserver(this); | |
| 20 } | |
| 21 | |
| 22 virtual ~TestRequesterService() { | |
| 23 resource_request_allowed_notifier_.RemoveObserver(this); | |
| 24 } | |
| 25 | |
| 26 void SetWasOfflineDuringLastRequestAttemptForTesting(bool offline) { | |
| 27 resource_request_allowed_notifier_. | |
| 28 SetWasOfflineDuringLastRequestAttemptForTesting(offline); | |
| 29 } | |
| 30 | |
| 31 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.
| |
| 32 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
| |
| 33 | |
| 34 // ResourceRequestAllowedNotifier::Observer overrides: | |
| 35 virtual void OnResourceRequestsAllowed() OVERRIDE { | |
| 36 request_attempted_ = true; | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 bool request_attempted_; | |
| 41 | |
| 42 ResourceRequestAllowedNotifier resource_request_allowed_notifier_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(TestRequesterService); | |
| 45 }; | |
| 46 | |
| 47 // Override NetworkChangeNotifier to simulate connection type changes for tests. | |
| 48 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { | |
| 49 public: | |
| 50 TestNetworkChangeNotifier() | |
| 51 : net::NetworkChangeNotifier(), | |
| 52 connection_type_to_return_( | |
| 53 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { | |
| 54 } | |
| 55 | |
| 56 void SimulateNetworkConnectionChange( | |
| 57 net::NetworkChangeNotifier::ConnectionType type) { | |
| 58 connection_type_to_return_ = type; | |
| 59 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); | |
| 60 MessageLoop::current()->RunAllPending(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { | |
| 65 return connection_type_to_return_; | |
| 66 } | |
| 67 | |
| 68 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); | |
| 71 }; | |
| 72 | |
| 73 // A test fixture class for ResourceRequestAllowedNotifier tests that require | |
| 74 // network state simulations. | |
| 75 class ResourceRequestAllowedNotifierTest : public testing::Test { | |
| 76 public: | |
| 77 ResourceRequestAllowedNotifierTest() | |
| 78 : scoped_testing_local_state_( | |
| 79 static_cast<TestingBrowserProcess*>(g_browser_process)), | |
| 80 local_state_(scoped_testing_local_state_.Get()), | |
| 81 ui_thread(content::BrowserThread::UI, &message_loop) { } | |
| 82 ~ResourceRequestAllowedNotifierTest() { } | |
| 83 | |
| 84 void SetWasOfflineDuringLastRequestAttempt(bool offline) { | |
| 85 test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline); | |
| 86 } | |
| 87 | |
| 88 void SimulateNetworkConnectionChange( | |
| 89 net::NetworkChangeNotifier::ConnectionType type) { | |
| 90 notifier.SimulateNetworkConnectionChange(type); | |
| 91 } | |
| 92 | |
| 93 bool request_attempted() const { | |
| 94 return test_service.request_attempted(); | |
| 95 } | |
| 96 | |
| 97 #if defined(OS_CHROMEOS) | |
| 98 void SetEulaAcceptedPref(bool accepted) { | |
| 99 // TODO(stevet): Share this string with the wizard_controller file that | |
| 100 // defines it. | |
| 101 local_state_->SetUserPref("EulaAccepted", | |
| 102 Value::CreateBooleanValue(accepted)); | |
| 103 } | |
| 104 | |
| 105 void SimulateEulaAccepted() { | |
| 106 SetEulaAcceptedPref(true); | |
| 107 content::NotificationService::current()->Notify( | |
| 108 chrome::NOTIFICATION_WIZARD_EULA_ACCEPTED, | |
| 109 content::NotificationService::AllSources(), | |
| 110 content::NotificationService::NoDetails()); | |
| 111 } | |
| 112 | |
| 113 void SetUp() { | |
| 114 SetEulaAcceptedPref(true); | |
| 115 } | |
| 116 #endif | |
| 117 | |
| 118 private: | |
| 119 MessageLoopForUI message_loop; | |
| 120 ScopedTestingLocalState scoped_testing_local_state_; | |
| 121 TestingPrefService* local_state_; | |
| 122 content::TestBrowserThread ui_thread; | |
| 123 TestNetworkChangeNotifier notifier; | |
| 124 TestRequesterService test_service; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(ResourceRequestAllowedNotifierTest); | |
| 127 }; | |
| 128 | |
| 129 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOffline) { | |
| 130 SetWasOfflineDuringLastRequestAttempt(true); | |
| 131 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 132 EXPECT_FALSE(request_attempted()); | |
| 133 } | |
| 134 | |
| 135 TEST_F(ResourceRequestAllowedNotifierTest, DoNotRequestIfOnlineToOnline) { | |
| 136 SetWasOfflineDuringLastRequestAttempt(false); | |
| 137 SimulateNetworkConnectionChange( | |
| 138 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 139 EXPECT_FALSE(request_attempted()); | |
| 140 } | |
| 141 | |
| 142 TEST_F(ResourceRequestAllowedNotifierTest, RequestOnReconnect) { | |
| 143 SetWasOfflineDuringLastRequestAttempt(true); | |
| 144 SimulateNetworkConnectionChange( | |
| 145 net::NetworkChangeNotifier::CONNECTION_ETHERNET); | |
| 146 EXPECT_TRUE(request_attempted()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnWardriving) { | |
| 150 SetWasOfflineDuringLastRequestAttempt(false); | |
| 151 SimulateNetworkConnectionChange( | |
| 152 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 153 EXPECT_FALSE(request_attempted()); | |
| 154 SimulateNetworkConnectionChange( | |
| 155 net::NetworkChangeNotifier::CONNECTION_3G); | |
| 156 EXPECT_FALSE(request_attempted()); | |
| 157 SimulateNetworkConnectionChange( | |
| 158 net::NetworkChangeNotifier::CONNECTION_4G); | |
| 159 EXPECT_FALSE(request_attempted()); | |
| 160 SimulateNetworkConnectionChange( | |
| 161 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 162 EXPECT_FALSE(request_attempted()); | |
| 163 } | |
| 164 | |
| 165 TEST_F(ResourceRequestAllowedNotifierTest, NoRequestOnFlakyConnection) { | |
| 166 SetWasOfflineDuringLastRequestAttempt(false); | |
| 167 SimulateNetworkConnectionChange( | |
| 168 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 169 EXPECT_FALSE(request_attempted()); | |
| 170 SimulateNetworkConnectionChange( | |
| 171 net::NetworkChangeNotifier::CONNECTION_NONE); | |
| 172 EXPECT_FALSE(request_attempted()); | |
| 173 SimulateNetworkConnectionChange( | |
| 174 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 175 EXPECT_FALSE(request_attempted()); | |
| 176 } | |
| 177 | |
| 178 #if defined(OS_CHROMEOS) | |
| 179 TEST_F(ResourceRequestAllowedNotifierTest, EulaOnly) { | |
| 180 SetWasOfflineDuringLastRequestAttempt(true); | |
| 181 SetEulaAcceptedPref(false); | |
| 182 SimulateEulaAccepted(); | |
| 183 EXPECT_FALSE(request_attempted()); | |
| 184 } | |
| 185 | |
| 186 TEST_F(ResourceRequestAllowedNotifierTest, EulaFirst) { | |
| 187 SetWasOfflineDuringLastRequestAttempt(true); | |
| 188 SetEulaAcceptedPref(false); | |
| 189 SimulateEulaAccepted(); | |
| 190 EXPECT_FALSE(request_attempted()); | |
| 191 SimulateNetworkConnectionChange( | |
| 192 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 193 EXPECT_TRUE(request_attempted()); | |
| 194 } | |
| 195 | |
| 196 TEST_F(ResourceRequestAllowedNotifierTest, NetworkFirst) { | |
| 197 SetWasOfflineDuringLastRequestAttempt(true); | |
| 198 SetEulaAcceptedPref(false); | |
| 199 SimulateNetworkConnectionChange( | |
| 200 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
| 201 EXPECT_FALSE(request_attempted()); | |
| 202 SimulateEulaAccepted(); | |
| 203 EXPECT_TRUE(request_attempted()); | |
| 204 } | |
| 205 #endif // OS_CHROMEOS | |
| OLD | NEW |