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 UpdateCacheNotMocked() { |
| 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 ListValue* spdy_server_list = new ListValue; |
| 64 |
| 65 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 66 std::string spdy_server_m = |
| 67 net::HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail); |
| 68 |
| 69 spdy_server_list->Append(new StringValue(spdy_server_m)); |
| 70 |
| 71 HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI(spdy_server_list); |
| 72 } |
| 73 |
| 74 void UpdatePrefsNotMocked() { |
| 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, void(ListValue* spdy_server_list)); |
| 83 |
| 84 private: |
| 85 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); |
| 86 }; |
| 87 |
| 88 class HttpServerPropertiesManagerTest : public testing::Test { |
| 89 protected: |
| 90 HttpServerPropertiesManagerTest() |
| 91 : ui_thread_(BrowserThread::UI, &loop_), |
| 92 io_thread_(BrowserThread::IO, &loop_) { |
| 93 } |
| 94 |
| 95 virtual void SetUp() OVERRIDE { |
| 96 pref_service_.RegisterListPref(prefs::kSpdyServers); |
| 97 http_server_props_manager_.reset( |
| 98 new TestingHttpServerPropertiesManager(&pref_service_)); |
| 99 loop_.RunAllPending(); |
| 100 } |
| 101 |
| 102 virtual void TearDown() OVERRIDE { |
| 103 if (http_server_props_manager_.get()) |
| 104 http_server_props_manager_->ShutdownOnUIThread(); |
| 105 loop_.RunAllPending(); |
| 106 // Delete |http_server_props_manager_| while |io_thread_| is mapping IO to |
| 107 // |loop_|. |
| 108 http_server_props_manager_.reset(); |
| 109 } |
| 110 |
| 111 void ExpectCacheUpdate() { |
| 112 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()) |
| 113 .WillOnce( |
| 114 Invoke(http_server_props_manager_.get(), |
| 115 &TestingHttpServerPropertiesManager::UpdateCacheNotMocked)); |
| 116 } |
| 117 |
| 118 void ExpectPrefsUpdate() { |
| 119 EXPECT_CALL(*http_server_props_manager_, UpdatePrefsFromCache()) |
| 120 .WillOnce( |
| 121 Invoke(http_server_props_manager_.get(), |
| 122 &TestingHttpServerPropertiesManager::UpdatePrefsNotMocked)); |
| 123 } |
| 124 |
| 125 MessageLoop loop_; |
| 126 TestingPrefService pref_service_; |
| 127 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_; |
| 128 |
| 129 private: |
| 130 BrowserThread ui_thread_; |
| 131 BrowserThread io_thread_; |
| 132 |
| 133 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); |
| 134 }; |
| 135 |
| 136 TEST_F(HttpServerPropertiesManagerTest, SingleUpdateForTwoPrefChanges) { |
| 137 ExpectCacheUpdate(); |
| 138 |
| 139 ListValue* http_server_props = new ListValue; |
| 140 http_server_props->Append(new StringValue("www.google.com:443")); |
| 141 http_server_props->Append(new StringValue("mail.google.com:443")); |
| 142 pref_service_.SetManagedPref(prefs::kSpdyServers, http_server_props); |
| 143 loop_.RunAllPending(); |
| 144 |
| 145 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 146 } |
| 147 |
| 148 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { |
| 149 ExpectPrefsUpdate(); |
| 150 |
| 151 // Post an update task to the IO thread. SetSupportsSpdy calls |
| 152 // ScheduleUpdatePrefsOnIO. |
| 153 |
| 154 // Add mail.google.com:443 as a supporting spdy server. |
| 155 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 156 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 157 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); |
| 158 |
| 159 // Run the task. |
| 160 loop_.RunAllPending(); |
| 161 |
| 162 EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 163 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 164 } |
| 165 |
| 166 TEST_F(HttpServerPropertiesManagerTest, DeleteAll) { |
| 167 ExpectPrefsUpdate(); |
| 168 |
| 169 // Add mail.google.com:443 as a supporting spdy server. |
| 170 net::HostPortPair spdy_server_mail("mail.google.com", 443); |
| 171 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); |
| 172 |
| 173 // Run the task. |
| 174 loop_.RunAllPending(); |
| 175 |
| 176 EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 177 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 178 |
| 179 // DeleteAll http server data. |
| 180 http_server_props_manager_->DeleteAll(); |
| 181 |
| 182 // Run the task. |
| 183 loop_.RunAllPending(); |
| 184 |
| 185 EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); |
| 186 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 187 } |
| 188 |
| 189 // |
| 190 // Tests for shutdown when updating cache. |
| 191 // |
| 192 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) { |
| 193 // Post an update task to the UI thread. |
| 194 http_server_props_manager_->ScheduleUpdateCacheOnUI(); |
| 195 // Shutdown comes before the task is executed. |
| 196 http_server_props_manager_->ShutdownOnUIThread(); |
| 197 http_server_props_manager_.reset(); |
| 198 // Run the task after shutdown and deletion. |
| 199 loop_.RunAllPending(); |
| 200 } |
| 201 |
| 202 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) { |
| 203 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefs()).Times(0); |
| 204 // Post an update task. |
| 205 http_server_props_manager_->ScheduleUpdateCacheOnUI(); |
| 206 // Shutdown comes before the task is executed. |
| 207 http_server_props_manager_->ShutdownOnUIThread(); |
| 208 // Run the task after shutdown, but before deletion. |
| 209 loop_.RunAllPending(); |
| 210 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 211 http_server_props_manager_.reset(); |
| 212 loop_.RunAllPending(); |
| 213 } |
| 214 |
| 215 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) { |
| 216 EXPECT_CALL(*http_server_props_manager_, |
| 217 UpdateCacheFromPrefsOnIO(_, _)).Times(0); |
| 218 http_server_props_manager_->UpdateCacheNotMocked(); |
| 219 // Shutdown comes before the task is executed. |
| 220 http_server_props_manager_->ShutdownOnUIThread(); |
| 221 // Run the task after shutdown, but before deletion. |
| 222 loop_.RunAllPending(); |
| 223 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 224 http_server_props_manager_.reset(); |
| 225 loop_.RunAllPending(); |
| 226 } |
| 227 |
| 228 // |
| 229 // Tests for shutdown when updating prefs. |
| 230 // |
| 231 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) { |
| 232 // Post an update task to the IO thread. |
| 233 http_server_props_manager_->ScheduleUpdatePrefsOnIO(); |
| 234 // Shutdown comes before the task is executed. |
| 235 http_server_props_manager_->ShutdownOnUIThread(); |
| 236 http_server_props_manager_.reset(); |
| 237 // Run the task after shutdown and deletion. |
| 238 loop_.RunAllPending(); |
| 239 } |
| 240 |
| 241 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) { |
| 242 EXPECT_CALL(*http_server_props_manager_, |
| 243 SetSpdyServersInPrefsOnUI(_)).Times(0); |
| 244 // Post an update task. |
| 245 http_server_props_manager_->ScheduleUpdatePrefsOnIO(); |
| 246 // Shutdown comes before the task is executed. |
| 247 http_server_props_manager_->ShutdownOnUIThread(); |
| 248 // Run the task after shutdown, but before deletion. |
| 249 loop_.RunAllPending(); |
| 250 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 251 http_server_props_manager_.reset(); |
| 252 loop_.RunAllPending(); |
| 253 } |
| 254 |
| 255 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) { |
| 256 EXPECT_CALL(*http_server_props_manager_, |
| 257 SetSpdyServersInPrefsOnUI(_)).Times(0); |
| 258 // This posts a task to the UI thread. |
| 259 http_server_props_manager_->UpdatePrefsNotMocked(); |
| 260 // Shutdown comes before the task is executed. |
| 261 http_server_props_manager_->ShutdownOnUIThread(); |
| 262 // Run the task after shutdown, but before deletion. |
| 263 loop_.RunAllPending(); |
| 264 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 265 http_server_props_manager_.reset(); |
| 266 loop_.RunAllPending(); |
| 267 } |
| 268 |
| 269 } // namespace |
| 270 |
| 271 } // namespace chrome_browser_net |
OLD | NEW |