OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/net/http_server_properties_manager.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/testing_pref_service.h" |
| 12 #include "content/browser/browser_thread.h" |
| 13 #include "content/common/notification_service.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace chrome_browser_net { |
| 19 |
| 20 namespace { |
| 21 |
| 22 using ::testing::_; |
| 23 using ::testing::Invoke; |
| 24 using ::testing::Mock; |
| 25 |
| 26 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager { |
| 27 public: |
| 28 explicit TestingHttpServerPropertiesManager(PrefService* pref_service) |
| 29 : HttpServerPropertiesManager(pref_service) { |
| 30 InitializeOnIOThread(); |
| 31 } |
| 32 |
| 33 virtual ~TestingHttpServerPropertiesManager() { |
| 34 } |
| 35 |
| 36 // Make this method public for testing. |
| 37 using HttpServerPropertiesManager::ScheduleUpdateCacheOnUI; |
| 38 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO; |
| 39 |
| 40 // Post tasks without a delay during tests. |
| 41 virtual void PostUpdateTaskOnUI(Task* task) OVERRIDE { |
| 42 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 43 } |
| 44 |
| 45 // Makes a direct call to UpdateCacheFromPrefsOnIO during tests. |
| 46 void UpdateCacheFromPrefsOnIO() { |
| 47 StringVector* spdy_servers = new StringVector; |
| 48 spdy_servers->push_back("www.google.com:443"); |
| 49 HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(spdy_servers, true); |
| 50 } |
| 51 |
| 52 void UpdateCacheFromPrefsConcrete() { |
| 53 HttpServerPropertiesManager::UpdateCacheFromPrefs(); |
| 54 } |
| 55 |
| 56 // Post tasks without a delay during tests. |
| 57 virtual void PostUpdateTaskOnIO(Task* task) OVERRIDE { |
| 58 MessageLoop::current()->PostTask(FROM_HERE, task); |
| 59 } |
| 60 |
| 61 // Makes a direct call to SetSpdyServersInPrefsOnUI during tests. |
| 62 void SetSpdyServersInPrefsOnUI() { |
| 63 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 64 std::string spdy_server_m = |
| 65 net::HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail); |
| 66 |
| 67 scoped_refptr<RefCountedListValue> spdy_server_list = |
| 68 new RefCountedListValue(); |
| 69 spdy_server_list->data.Append(new StringValue(spdy_server_m)); |
| 70 |
| 71 HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI(spdy_server_list); |
| 72 } |
| 73 |
| 74 void UpdatePrefsFromCacheConcrete() { |
| 75 HttpServerPropertiesManager::UpdatePrefsFromCache(); |
| 76 } |
| 77 |
| 78 MOCK_METHOD0(UpdateCacheFromPrefs, void()); |
| 79 MOCK_METHOD0(UpdatePrefsFromCache, void()); |
| 80 MOCK_METHOD2(UpdateCacheFromPrefsOnIO, |
| 81 void(StringVector* spdy_servers, bool support_spdy)); |
| 82 MOCK_METHOD1(SetSpdyServersInPrefsOnUI, |
| 83 void(scoped_refptr<RefCountedListValue> spdy_server_list)); |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); |
| 87 }; |
| 88 |
| 89 class HttpServerPropertiesManagerTest : public testing::Test { |
| 90 protected: |
| 91 HttpServerPropertiesManagerTest() |
| 92 : ui_thread_(BrowserThread::UI, &loop_), |
| 93 io_thread_(BrowserThread::IO, &loop_) { |
| 94 } |
| 95 |
| 96 virtual void SetUp() OVERRIDE { |
| 97 pref_service_.RegisterListPref(prefs::kSpdyServers); |
| 98 http_server_props_manager_.reset( |
| 99 new TestingHttpServerPropertiesManager(&pref_service_)); |
| 100 loop_.RunAllPending(); |
| 101 } |
| 102 |
| 103 virtual void TearDown() OVERRIDE { |
| 104 if (http_server_props_manager_.get()) |
| 105 http_server_props_manager_->ShutdownOnUIThread(); |
| 106 loop_.RunAllPending(); |
| 107 // Delete |http_server_props_manager_| while |io_thread_| is mapping IO to |
| 108 // |loop_|. |
| 109 http_server_props_manager_.reset(); |
| 110 } |
| 111 |
| 112 void ExpectCacheUpdate() { |
| 113 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()) |
| 114 .WillOnce( |
| 115 Invoke(http_server_props_manager_.get(), |
| 116 &TestingHttpServerPropertiesManager::UpdateCacheFromPrefsConcrete)); |
| 117 } |
| 118 |
| 119 void ExpectPrefsUpdate() { |
| 120 EXPECT_CALL(*http_server_props_manager_, UpdatePrefsFromCache()) |
| 121 .WillOnce( |
| 122 Invoke(http_server_props_manager_.get(), |
| 123 &TestingHttpServerPropertiesManager::UpdatePrefsFromCacheConcrete)); |
| 124 } |
| 125 |
| 126 MessageLoop loop_; |
| 127 TestingPrefService pref_service_; |
| 128 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_; |
| 129 |
| 130 private: |
| 131 BrowserThread ui_thread_; |
| 132 BrowserThread io_thread_; |
| 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); |
| 135 }; |
| 136 |
| 137 TEST_F(HttpServerPropertiesManagerTest, SingleUpdateForTwoPrefChanges) { |
| 138 ExpectCacheUpdate(); |
| 139 |
| 140 ListValue* http_server_props = new ListValue; |
| 141 http_server_props->Append(new StringValue("www.google.com:443")); |
| 142 http_server_props->Append(new StringValue("mail.google.com:443")); |
| 143 pref_service_.SetManagedPref(prefs::kSpdyServers, http_server_props); |
| 144 loop_.RunAllPending(); |
| 145 |
| 146 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 147 } |
| 148 |
| 149 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { |
| 150 ExpectPrefsUpdate(); |
| 151 |
| 152 // Post an update task to the IO thread. SetSupportsSpdy calls |
| 153 // ScheduleUpdatePrefsOnIO. |
| 154 |
| 155 // Add mail.google.com:443 as a supporting spdy server. |
| 156 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 157 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 158 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); |
| 159 |
| 160 // Run the task. |
| 161 loop_.RunAllPending(); |
| 162 |
| 163 EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 164 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 165 } |
| 166 |
| 167 TEST_F(HttpServerPropertiesManagerTest, DeleteAll) { |
| 168 ExpectPrefsUpdate(); |
| 169 |
| 170 // Add mail.google.com:443 as a supporting spdy server. |
| 171 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 172 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); |
| 173 |
| 174 // Run the task. |
| 175 loop_.RunAllPending(); |
| 176 |
| 177 EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 178 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 179 |
| 180 // DeleteAll http server data. |
| 181 http_server_props_manager_->DeleteAll(); |
| 182 |
| 183 // Run the task. |
| 184 loop_.RunAllPending(); |
| 185 |
| 186 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 187 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 188 } |
| 189 |
| 190 // |
| 191 // Tests for shutdown when updating cache. |
| 192 // |
| 193 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) { |
| 194 EXPECT_CALL(*http_server_props_manager_, |
| 195 UpdateCacheFromPrefsOnIO(_, _)).Times(0); |
| 196 // Post an update task to the UI thread. |
| 197 http_server_props_manager_->ScheduleUpdateCacheOnUI(); |
| 198 // Shutdown comes before the task is executed. |
| 199 http_server_props_manager_->ShutdownOnUIThread(); |
| 200 http_server_props_manager_.reset(); |
| 201 // Run the task after shutdown and deletion. |
| 202 loop_.RunAllPending(); |
| 203 } |
| 204 |
| 205 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) { |
| 206 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()).Times(0); |
| 207 // Post an update task. |
| 208 http_server_props_manager_->ScheduleUpdateCacheOnUI(); |
| 209 // Shutdown comes before the task is executed. |
| 210 http_server_props_manager_->ShutdownOnUIThread(); |
| 211 // Run the task after shutdown, but before deletion. |
| 212 loop_.RunAllPending(); |
| 213 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 214 http_server_props_manager_.reset(); |
| 215 loop_.RunAllPending(); |
| 216 } |
| 217 |
| 218 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) { |
| 219 EXPECT_CALL(*http_server_props_manager_, |
| 220 UpdateCacheFromPrefsOnIO(_, _)).Times(0); |
| 221 http_server_props_manager_->UpdateCacheFromPrefsConcrete(); |
| 222 // Shutdown comes before the task is executed. |
| 223 http_server_props_manager_->ShutdownOnUIThread(); |
| 224 // Run the task after shutdown, but before deletion. |
| 225 loop_.RunAllPending(); |
| 226 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 227 http_server_props_manager_.reset(); |
| 228 loop_.RunAllPending(); |
| 229 } |
| 230 |
| 231 // |
| 232 // Tests for shutdown when updating prefs. |
| 233 // |
| 234 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) { |
| 235 EXPECT_CALL(*http_server_props_manager_, |
| 236 SetSpdyServersInPrefsOnUI(_)).Times(0); |
| 237 // Post an update task to the IO thread. |
| 238 http_server_props_manager_->ScheduleUpdatePrefsOnIO(); |
| 239 // Shutdown comes before the task is executed. |
| 240 http_server_props_manager_->ShutdownOnUIThread(); |
| 241 http_server_props_manager_.reset(); |
| 242 // Run the task after shutdown and deletion. |
| 243 loop_.RunAllPending(); |
| 244 } |
| 245 |
| 246 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) { |
| 247 EXPECT_CALL(*http_server_props_manager_, |
| 248 SetSpdyServersInPrefsOnUI(_)).Times(0); |
| 249 // Post an update task. |
| 250 http_server_props_manager_->ScheduleUpdatePrefsOnIO(); |
| 251 // Shutdown comes before the task is executed. |
| 252 http_server_props_manager_->ShutdownOnUIThread(); |
| 253 // Run the task after shutdown, but before deletion. |
| 254 loop_.RunAllPending(); |
| 255 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 256 http_server_props_manager_.reset(); |
| 257 loop_.RunAllPending(); |
| 258 } |
| 259 |
| 260 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) { |
| 261 EXPECT_CALL(*http_server_props_manager_, |
| 262 SetSpdyServersInPrefsOnUI(_)).Times(0); |
| 263 // This posts a task to the UI thread. |
| 264 http_server_props_manager_->UpdatePrefsFromCacheConcrete(); |
| 265 // Shutdown comes before the task is executed. |
| 266 http_server_props_manager_->ShutdownOnUIThread(); |
| 267 // Run the task after shutdown, but before deletion. |
| 268 loop_.RunAllPending(); |
| 269 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 270 http_server_props_manager_.reset(); |
| 271 loop_.RunAllPending(); |
| 272 } |
| 273 |
| 274 } // namespace |
| 275 |
| 276 } // namespace chrome_browser_net |
OLD | NEW |