Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blob2164bb524852af3d230564f1ed0c2ab3416b23ae
1 // Copyright 2014 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.
5 #include "net/http/http_server_properties_manager.h"
7 #include "base/basictypes.h"
8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/testing_pref_service.h"
13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/test/test_simple_task_runner.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/values.h"
20 #include "net/base/ip_address_number.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "url/gurl.h"
25 namespace net {
27 namespace {
29 using base::StringPrintf;
30 using ::testing::_;
31 using ::testing::Invoke;
32 using ::testing::Mock;
33 using ::testing::StrictMock;
35 const char kTestHttpServerProperties[] = "TestHttpServerProperties";
37 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager {
38 public:
39 TestingHttpServerPropertiesManager(
40 PrefService* pref_service,
41 const char* pref_path,
42 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
43 : HttpServerPropertiesManager(pref_service, pref_path, io_task_runner) {
44 InitializeOnNetworkThread();
47 ~TestingHttpServerPropertiesManager() override {}
49 // Make these methods public for testing.
50 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread;
52 // Post tasks without a delay during tests.
53 void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay) override {
54 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread(
55 base::TimeDelta());
58 void UpdateCacheFromPrefsOnUIConcrete() {
59 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread();
62 // Post tasks without a delay during tests.
63 void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay) override {
64 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
65 base::TimeDelta());
68 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
69 const base::Closure& callback) {
70 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback);
73 void ScheduleUpdatePrefsOnNetworkThreadConcrete(Location location) {
74 HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(location);
77 void ScheduleUpdatePrefsOnNetworkThread() {
78 // Picked a random Location as caller.
79 HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(
80 DETECTED_CORRUPTED_PREFS);
83 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void());
84 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&));
85 MOCK_METHOD1(ScheduleUpdatePrefsOnNetworkThread, void(Location location));
86 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread,
87 void(std::vector<std::string>* spdy_servers,
88 SpdySettingsMap* spdy_settings_map,
89 AlternativeServiceMap* alternative_service_map,
90 IPAddressNumber* last_quic_address,
91 ServerNetworkStatsMap* server_network_stats_map,
92 bool detected_corrupted_prefs));
93 MOCK_METHOD5(UpdatePrefsOnPref,
94 void(base::ListValue* spdy_server_list,
95 SpdySettingsMap* spdy_settings_map,
96 AlternativeServiceMap* alternative_service_map,
97 IPAddressNumber* last_quic_address,
98 ServerNetworkStatsMap* server_network_stats_map));
100 private:
101 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager);
104 } // namespace
106 class HttpServerPropertiesManagerTest : public testing::Test {
107 protected:
108 HttpServerPropertiesManagerTest() {}
110 void SetUp() override {
111 one_day_from_now_ = base::Time::Now() + base::TimeDelta::FromDays(1);
112 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties);
113 http_server_props_manager_.reset(
114 new StrictMock<TestingHttpServerPropertiesManager>(
115 &pref_service_, kTestHttpServerProperties,
116 base::ThreadTaskRunnerHandle::Get()));
117 ExpectCacheUpdate();
118 base::RunLoop().RunUntilIdle();
121 void TearDown() override {
122 if (http_server_props_manager_.get())
123 http_server_props_manager_->ShutdownOnPrefThread();
124 base::RunLoop().RunUntilIdle();
125 http_server_props_manager_.reset();
128 void ExpectCacheUpdate() {
129 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefsOnPrefThread())
130 .WillOnce(Invoke(http_server_props_manager_.get(),
131 &TestingHttpServerPropertiesManager::
132 UpdateCacheFromPrefsOnUIConcrete));
135 void ExpectScheduleUpdatePrefsOnNetworkThread() {
136 EXPECT_CALL(*http_server_props_manager_,
137 ScheduleUpdatePrefsOnNetworkThread(_))
138 .WillOnce(Invoke(http_server_props_manager_.get(),
139 &TestingHttpServerPropertiesManager::
140 ScheduleUpdatePrefsOnNetworkThreadConcrete));
143 void ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly() {
144 EXPECT_CALL(*http_server_props_manager_,
145 ScheduleUpdatePrefsOnNetworkThread(_))
146 .WillRepeatedly(Invoke(http_server_props_manager_.get(),
147 &TestingHttpServerPropertiesManager::
148 ScheduleUpdatePrefsOnNetworkThreadConcrete));
151 void ExpectPrefsUpdate() {
152 EXPECT_CALL(*http_server_props_manager_,
153 UpdatePrefsFromCacheOnNetworkThread(_))
154 .WillOnce(Invoke(http_server_props_manager_.get(),
155 &TestingHttpServerPropertiesManager::
156 UpdatePrefsFromCacheOnNetworkThreadConcrete));
159 void ExpectPrefsUpdateRepeatedly() {
160 EXPECT_CALL(*http_server_props_manager_,
161 UpdatePrefsFromCacheOnNetworkThread(_))
162 .WillRepeatedly(
163 Invoke(http_server_props_manager_.get(),
164 &TestingHttpServerPropertiesManager::
165 UpdatePrefsFromCacheOnNetworkThreadConcrete));
168 bool HasAlternativeService(const HostPortPair& server) {
169 const AlternativeServiceVector alternative_service_vector =
170 http_server_props_manager_->GetAlternativeServices(server);
171 return !alternative_service_vector.empty();
174 //base::RunLoop loop_;
175 TestingPrefServiceSimple pref_service_;
176 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_;
177 base::Time one_day_from_now_;
179 private:
180 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
183 TEST_F(HttpServerPropertiesManagerTest,
184 SingleUpdateForTwoSpdyServerPrefChanges) {
185 ExpectCacheUpdate();
187 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
188 // it twice. Only expect a single cache update.
190 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
191 HostPortPair google_server("www.google.com", 80);
192 HostPortPair mail_server("mail.google.com", 80);
194 // Set supports_spdy for www.google.com:80.
195 server_pref_dict->SetBoolean("supports_spdy", true);
197 // Set up alternative_services for www.google.com:80.
198 base::DictionaryValue* alternative_service_dict0 = new base::DictionaryValue;
199 alternative_service_dict0->SetInteger("port", 443);
200 alternative_service_dict0->SetString("protocol_str", "npn-h2");
201 base::DictionaryValue* alternative_service_dict1 = new base::DictionaryValue;
202 alternative_service_dict1->SetInteger("port", 1234);
203 alternative_service_dict1->SetString("protocol_str", "quic");
204 base::ListValue* alternative_service_list = new base::ListValue;
205 alternative_service_list->Append(alternative_service_dict0);
206 alternative_service_list->Append(alternative_service_dict1);
207 server_pref_dict->SetWithoutPathExpansion("alternative_service",
208 alternative_service_list);
210 // Set up ServerNetworkStats for www.google.com:80.
211 base::DictionaryValue* stats = new base::DictionaryValue;
212 stats->SetInteger("srtt", 10);
213 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
215 // Set the server preference for www.google.com:80.
216 base::DictionaryValue* servers_dict = new base::DictionaryValue;
217 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
219 // Set the preference for mail.google.com server.
220 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
222 // Set supports_spdy for mail.google.com:80
223 server_pref_dict1->SetBoolean("supports_spdy", true);
225 // Set up alternate_protocol for mail.google.com:80 to test migration to
226 // alternative_service.
227 base::DictionaryValue* alternate_protocol_dict = new base::DictionaryValue;
228 alternate_protocol_dict->SetString("protocol_str", "npn-spdy/3.1");
229 alternate_protocol_dict->SetInteger("port", 444);
230 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
231 alternate_protocol_dict);
233 // Set up ServerNetworkStats for mail.google.com:80.
234 base::DictionaryValue* stats1 = new base::DictionaryValue;
235 stats1->SetInteger("srtt", 20);
236 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
237 // Set the server preference for mail.google.com:80.
238 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
239 server_pref_dict1);
241 base::DictionaryValue* http_server_properties_dict =
242 new base::DictionaryValue;
243 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
244 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
245 base::DictionaryValue* supports_quic = new base::DictionaryValue;
246 supports_quic->SetBoolean("used_quic", true);
247 supports_quic->SetString("address", "127.0.0.1");
248 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
249 supports_quic);
251 // Set the same value for kHttpServerProperties multiple times.
252 pref_service_.SetManagedPref(kTestHttpServerProperties,
253 http_server_properties_dict);
254 base::DictionaryValue* http_server_properties_dict2 =
255 http_server_properties_dict->DeepCopy();
256 pref_service_.SetManagedPref(kTestHttpServerProperties,
257 http_server_properties_dict2);
259 base::RunLoop().RunUntilIdle();
260 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
262 // Verify SupportsSpdy.
263 EXPECT_TRUE(
264 http_server_props_manager_->SupportsRequestPriority(google_server));
265 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
266 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
267 HostPortPair::FromString("foo.google.com:1337")));
269 // Verify alternative service.
270 const AlternativeServiceMap& map =
271 http_server_props_manager_->alternative_service_map();
272 ASSERT_EQ(2u, map.size());
273 AlternativeServiceMap::const_iterator map_it = map.begin();
274 EXPECT_EQ("www.google.com", map_it->first.host());
275 ASSERT_EQ(2u, map_it->second.size());
276 EXPECT_EQ(NPN_HTTP_2, map_it->second[0].alternative_service.protocol);
277 EXPECT_TRUE(map_it->second[0].alternative_service.host.empty());
278 EXPECT_EQ(443, map_it->second[0].alternative_service.port);
279 EXPECT_EQ(QUIC, map_it->second[1].alternative_service.protocol);
280 EXPECT_TRUE(map_it->second[1].alternative_service.host.empty());
281 EXPECT_EQ(1234, map_it->second[1].alternative_service.port);
282 ++map_it;
283 EXPECT_EQ("mail.google.com", map_it->first.host());
284 ASSERT_EQ(1u, map_it->second.size());
285 EXPECT_EQ(NPN_SPDY_3_1, map_it->second[0].alternative_service.protocol);
286 EXPECT_TRUE(map_it->second[0].alternative_service.host.empty());
287 EXPECT_EQ(444, map_it->second[0].alternative_service.port);
289 // Verify SupportsQuic.
290 IPAddressNumber last_address;
291 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address));
292 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address));
294 // Verify ServerNetworkStats.
295 const ServerNetworkStats* stats2 =
296 http_server_props_manager_->GetServerNetworkStats(google_server);
297 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
298 const ServerNetworkStats* stats3 =
299 http_server_props_manager_->GetServerNetworkStats(mail_server);
300 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
303 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
304 ExpectCacheUpdate();
305 // The prefs are automaticalls updated in the case corruption is detected.
306 ExpectPrefsUpdate();
307 ExpectScheduleUpdatePrefsOnNetworkThread();
309 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
311 // Set supports_spdy for www.google.com:65536.
312 server_pref_dict->SetBoolean("supports_spdy", true);
314 // Set up alternative_service for www.google.com:65536.
315 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
316 alternative_service_dict->SetString("protocol_str", "npn-h2");
317 alternative_service_dict->SetInteger("port", 80);
318 base::ListValue* alternative_service_list = new base::ListValue;
319 alternative_service_list->Append(alternative_service_dict);
320 server_pref_dict->SetWithoutPathExpansion("alternative_service",
321 alternative_service_list);
323 // Set up ServerNetworkStats for www.google.com:65536.
324 base::DictionaryValue* stats = new base::DictionaryValue;
325 stats->SetInteger("srtt", 10);
326 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
328 // Set the server preference for www.google.com:65536.
329 base::DictionaryValue* servers_dict = new base::DictionaryValue;
330 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
331 server_pref_dict);
333 base::DictionaryValue* http_server_properties_dict =
334 new base::DictionaryValue;
335 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
336 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
338 // Set up the pref.
339 pref_service_.SetManagedPref(kTestHttpServerProperties,
340 http_server_properties_dict);
342 base::RunLoop().RunUntilIdle();
343 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
345 // Verify that nothing is set.
346 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
347 HostPortPair::FromString("www.google.com:65536")));
348 EXPECT_FALSE(
349 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
350 const ServerNetworkStats* stats1 =
351 http_server_props_manager_->GetServerNetworkStats(
352 HostPortPair::FromString("www.google.com:65536"));
353 EXPECT_EQ(NULL, stats1);
356 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
357 ExpectCacheUpdate();
358 // The prefs are automaticalls updated in the case corruption is detected.
359 ExpectPrefsUpdate();
360 ExpectScheduleUpdatePrefsOnNetworkThread();
362 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
364 // Set supports_spdy for www.google.com:80.
365 server_pref_dict->SetBoolean("supports_spdy", true);
367 // Set up alternative_service for www.google.com:80.
368 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
369 alternative_service_dict->SetString("protocol_str", "npn-h2");
370 alternative_service_dict->SetInteger("port", 65536);
371 base::ListValue* alternative_service_list = new base::ListValue;
372 alternative_service_list->Append(alternative_service_dict);
373 server_pref_dict->SetWithoutPathExpansion("alternative_service",
374 alternative_service_list);
376 // Set the server preference for www.google.com:80.
377 base::DictionaryValue* servers_dict = new base::DictionaryValue;
378 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
380 base::DictionaryValue* http_server_properties_dict =
381 new base::DictionaryValue;
382 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
383 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
385 // Set up the pref.
386 pref_service_.SetManagedPref(kTestHttpServerProperties,
387 http_server_properties_dict);
389 base::RunLoop().RunUntilIdle();
390 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
392 // Verify alternative service is not set.
393 EXPECT_FALSE(
394 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
397 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
398 ExpectPrefsUpdate();
399 ExpectScheduleUpdatePrefsOnNetworkThread();
401 // Post an update task to the network thread. SetSupportsSpdy calls
402 // ScheduleUpdatePrefsOnNetworkThread.
404 // Add mail.google.com:443 as a supporting spdy server.
405 HostPortPair spdy_server_mail("mail.google.com", 443);
406 EXPECT_FALSE(
407 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
408 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
409 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
410 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
412 // Run the task.
413 base::RunLoop().RunUntilIdle();
415 EXPECT_TRUE(
416 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
417 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
420 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
421 ExpectPrefsUpdate();
422 ExpectScheduleUpdatePrefsOnNetworkThread();
424 // Add SpdySetting for mail.google.com:443.
425 HostPortPair spdy_server_mail("mail.google.com", 443);
426 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
427 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
428 const uint32 value1 = 31337;
429 http_server_props_manager_->SetSpdySetting(
430 spdy_server_mail, id1, flags1, value1);
432 // Run the task.
433 base::RunLoop().RunUntilIdle();
435 const SettingsMap& settings_map1_ret =
436 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
437 ASSERT_EQ(1U, settings_map1_ret.size());
438 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
439 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
440 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
441 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
442 EXPECT_EQ(value1, flags_and_value1_ret.second);
444 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
447 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
448 ExpectPrefsUpdateRepeatedly();
449 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
451 // Add SpdySetting for mail.google.com:443.
452 HostPortPair spdy_server_mail("mail.google.com", 443);
453 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
454 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
455 const uint32 value1 = 31337;
456 http_server_props_manager_->SetSpdySetting(
457 spdy_server_mail, id1, flags1, value1);
459 // Run the task.
460 base::RunLoop().RunUntilIdle();
462 const SettingsMap& settings_map1_ret =
463 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
464 ASSERT_EQ(1U, settings_map1_ret.size());
465 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
466 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
467 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
468 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
469 EXPECT_EQ(value1, flags_and_value1_ret.second);
471 // Clear SpdySetting for mail.google.com:443.
472 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
474 // Run the task.
475 base::RunLoop().RunUntilIdle();
477 // Verify that there are no entries in the settings map for
478 // mail.google.com:443.
479 const SettingsMap& settings_map2_ret =
480 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
481 ASSERT_EQ(0U, settings_map2_ret.size());
483 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
486 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
487 ExpectPrefsUpdateRepeatedly();
488 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
490 // Add SpdySetting for mail.google.com:443.
491 HostPortPair spdy_server_mail("mail.google.com", 443);
492 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
493 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
494 const uint32 value1 = 31337;
495 http_server_props_manager_->SetSpdySetting(
496 spdy_server_mail, id1, flags1, value1);
498 // Run the task.
499 base::RunLoop().RunUntilIdle();
501 const SettingsMap& settings_map1_ret =
502 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
503 ASSERT_EQ(1U, settings_map1_ret.size());
504 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
505 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
506 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
507 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
508 EXPECT_EQ(value1, flags_and_value1_ret.second);
510 // Clear All SpdySettings.
511 http_server_props_manager_->ClearAllSpdySettings();
513 // Run the task.
514 base::RunLoop().RunUntilIdle();
516 // Verify that there are no entries in the settings map.
517 const SpdySettingsMap& spdy_settings_map2_ret =
518 http_server_props_manager_->spdy_settings_map();
519 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
521 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
524 TEST_F(HttpServerPropertiesManagerTest, GetAlternativeServices) {
525 ExpectPrefsUpdate();
526 ExpectScheduleUpdatePrefsOnNetworkThread();
528 HostPortPair spdy_server_mail("mail.google.com", 80);
529 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
530 const AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com",
531 443);
532 http_server_props_manager_->SetAlternativeService(
533 spdy_server_mail, alternative_service, 1.0, one_day_from_now_);
534 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
535 http_server_props_manager_->SetAlternativeService(
536 spdy_server_mail, alternative_service, 1.0, one_day_from_now_);
538 // Run the task.
539 base::RunLoop().RunUntilIdle();
540 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
542 AlternativeServiceVector alternative_service_vector =
543 http_server_props_manager_->GetAlternativeServices(spdy_server_mail);
544 ASSERT_EQ(1u, alternative_service_vector.size());
545 EXPECT_EQ(alternative_service, alternative_service_vector[0]);
548 TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServices) {
549 ExpectPrefsUpdate();
550 ExpectScheduleUpdatePrefsOnNetworkThread();
552 HostPortPair spdy_server_mail("mail.google.com", 80);
553 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
554 AlternativeServiceInfoVector alternative_service_info_vector;
555 const AlternativeService alternative_service1(NPN_HTTP_2, "mail.google.com",
556 443);
557 alternative_service_info_vector.push_back(
558 AlternativeServiceInfo(alternative_service1, 1.0, one_day_from_now_));
559 const AlternativeService alternative_service2(QUIC, "mail.google.com", 1234);
560 alternative_service_info_vector.push_back(
561 AlternativeServiceInfo(alternative_service2, 1.0, one_day_from_now_));
562 http_server_props_manager_->SetAlternativeServices(
563 spdy_server_mail, alternative_service_info_vector);
564 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
565 http_server_props_manager_->SetAlternativeServices(
566 spdy_server_mail, alternative_service_info_vector);
568 // Run the task.
569 base::RunLoop().RunUntilIdle();
570 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
572 AlternativeServiceVector alternative_service_vector =
573 http_server_props_manager_->GetAlternativeServices(spdy_server_mail);
574 ASSERT_EQ(2u, alternative_service_vector.size());
575 EXPECT_EQ(alternative_service1, alternative_service_vector[0]);
576 EXPECT_EQ(alternative_service2, alternative_service_vector[1]);
579 TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServicesEmpty) {
580 HostPortPair spdy_server_mail("mail.google.com", 80);
581 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
582 const AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com",
583 443);
584 http_server_props_manager_->SetAlternativeServices(
585 spdy_server_mail, AlternativeServiceInfoVector());
586 // ExpectScheduleUpdatePrefsOnNetworkThread() should not be called.
588 // Run the task.
589 base::RunLoop().RunUntilIdle();
590 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
592 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
595 TEST_F(HttpServerPropertiesManagerTest, ClearAlternativeServices) {
596 ExpectPrefsUpdate();
597 ExpectScheduleUpdatePrefsOnNetworkThread();
599 HostPortPair spdy_server_mail("mail.google.com", 80);
600 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
601 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 443);
602 http_server_props_manager_->SetAlternativeService(
603 spdy_server_mail, alternative_service, 1.0, one_day_from_now_);
604 ExpectScheduleUpdatePrefsOnNetworkThread();
605 http_server_props_manager_->ClearAlternativeServices(spdy_server_mail);
606 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
607 http_server_props_manager_->ClearAlternativeServices(spdy_server_mail);
609 // Run the task.
610 base::RunLoop().RunUntilIdle();
611 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
613 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
616 TEST_F(HttpServerPropertiesManagerTest, ConfirmAlternativeService) {
617 ExpectPrefsUpdate();
619 HostPortPair spdy_server_mail("mail.google.com", 80);
620 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
621 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 443);
623 ExpectScheduleUpdatePrefsOnNetworkThread();
624 http_server_props_manager_->SetAlternativeService(
625 spdy_server_mail, alternative_service, 1.0, one_day_from_now_);
627 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
628 alternative_service));
629 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
630 alternative_service));
632 ExpectScheduleUpdatePrefsOnNetworkThread();
633 http_server_props_manager_->MarkAlternativeServiceBroken(alternative_service);
634 EXPECT_TRUE(http_server_props_manager_->IsAlternativeServiceBroken(
635 alternative_service));
636 EXPECT_TRUE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
637 alternative_service));
639 ExpectScheduleUpdatePrefsOnNetworkThread();
640 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
641 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
642 alternative_service));
643 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
644 alternative_service));
645 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
646 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
647 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
648 alternative_service));
649 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
650 alternative_service));
652 // Run the task.
653 base::RunLoop().RunUntilIdle();
654 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
656 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
657 alternative_service));
658 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
659 alternative_service));
662 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
663 ExpectPrefsUpdate();
664 ExpectScheduleUpdatePrefsOnNetworkThread();
666 IPAddressNumber address;
667 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
669 IPAddressNumber actual_address;
670 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
671 http_server_props_manager_->SetSupportsQuic(true, actual_address);
672 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
673 http_server_props_manager_->SetSupportsQuic(true, actual_address);
675 // Run the task.
676 base::RunLoop().RunUntilIdle();
677 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
679 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
680 EXPECT_EQ(actual_address, address);
683 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
684 ExpectPrefsUpdate();
685 ExpectScheduleUpdatePrefsOnNetworkThread();
687 HostPortPair mail_server("mail.google.com", 80);
688 const ServerNetworkStats* stats =
689 http_server_props_manager_->GetServerNetworkStats(mail_server);
690 EXPECT_EQ(NULL, stats);
691 ServerNetworkStats stats1;
692 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
693 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
694 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
695 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
697 // Run the task.
698 base::RunLoop().RunUntilIdle();
699 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
701 const ServerNetworkStats* stats2 =
702 http_server_props_manager_->GetServerNetworkStats(mail_server);
703 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
706 TEST_F(HttpServerPropertiesManagerTest, Clear) {
707 ExpectPrefsUpdate();
708 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
710 HostPortPair spdy_server_mail("mail.google.com", 443);
711 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
712 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 1234);
713 http_server_props_manager_->SetAlternativeService(
714 spdy_server_mail, alternative_service, 1.0, one_day_from_now_);
715 IPAddressNumber actual_address;
716 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
717 http_server_props_manager_->SetSupportsQuic(true, actual_address);
718 ServerNetworkStats stats;
719 stats.srtt = base::TimeDelta::FromMicroseconds(10);
720 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
722 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
723 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
724 const uint32 value1 = 31337;
725 http_server_props_manager_->SetSpdySetting(
726 spdy_server_mail, id1, flags1, value1);
728 // Run the task.
729 base::RunLoop().RunUntilIdle();
731 EXPECT_TRUE(
732 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
733 EXPECT_TRUE(HasAlternativeService(spdy_server_mail));
734 IPAddressNumber address;
735 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
736 EXPECT_EQ(actual_address, address);
737 const ServerNetworkStats* stats1 =
738 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
739 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
741 // Check SPDY settings values.
742 const SettingsMap& settings_map1_ret =
743 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
744 ASSERT_EQ(1U, settings_map1_ret.size());
745 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
746 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
747 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
748 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
749 EXPECT_EQ(value1, flags_and_value1_ret.second);
751 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
753 ExpectPrefsUpdate();
755 // Clear http server data, time out if we do not get a completion callback.
756 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
757 base::RunLoop().Run();
759 EXPECT_FALSE(
760 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
761 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
762 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
763 const ServerNetworkStats* stats2 =
764 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
765 EXPECT_EQ(NULL, stats2);
767 const SettingsMap& settings_map2_ret =
768 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
769 EXPECT_EQ(0U, settings_map2_ret.size());
771 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
774 // https://crbug.com/444956: Add 200 alternative_service servers followed by
775 // supports_quic and verify we have read supports_quic from prefs.
776 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
777 ExpectCacheUpdate();
779 base::DictionaryValue* servers_dict = new base::DictionaryValue;
781 for (int i = 0; i < 200; ++i) {
782 // Set up alternative_service for www.google.com:i.
783 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
784 alternative_service_dict->SetString("protocol_str", "quic");
785 alternative_service_dict->SetInteger("port", i);
786 base::ListValue* alternative_service_list = new base::ListValue;
787 alternative_service_list->Append(alternative_service_dict);
788 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
789 server_pref_dict->SetWithoutPathExpansion("alternative_service",
790 alternative_service_list);
791 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
792 server_pref_dict);
795 // Set the preference for mail.google.com server.
796 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
798 // Set the server preference for mail.google.com:80.
799 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
800 server_pref_dict1);
802 base::DictionaryValue* http_server_properties_dict =
803 new base::DictionaryValue;
804 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
805 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
807 // Set up SupportsQuic for 127.0.0.1
808 base::DictionaryValue* supports_quic = new base::DictionaryValue;
809 supports_quic->SetBoolean("used_quic", true);
810 supports_quic->SetString("address", "127.0.0.1");
811 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
812 supports_quic);
814 // Set up the pref.
815 pref_service_.SetManagedPref(kTestHttpServerProperties,
816 http_server_properties_dict);
818 base::RunLoop().RunUntilIdle();
819 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
821 // Verify alternative service.
822 for (int i = 0; i < 200; ++i) {
823 std::string server = StringPrintf("www.google.com:%d", i);
824 AlternativeServiceVector alternative_service_vector =
825 http_server_props_manager_->GetAlternativeServices(
826 HostPortPair::FromString(server));
827 ASSERT_EQ(1u, alternative_service_vector.size());
828 EXPECT_EQ(QUIC, alternative_service_vector[0].protocol);
829 EXPECT_EQ(i, alternative_service_vector[0].port);
832 // Verify SupportsQuic.
833 IPAddressNumber address;
834 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
835 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
838 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
839 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
841 const HostPortPair server_www("www.google.com", 80);
842 const HostPortPair server_mail("mail.google.com", 80);
844 // Set alternate protocol.
845 AlternativeServiceInfoVector alternative_service_info_vector;
846 AlternativeService www_alternative_service1(NPN_HTTP_2, "", 443);
847 base::Time expiration1;
848 ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1));
849 alternative_service_info_vector.push_back(
850 AlternativeServiceInfo(www_alternative_service1, 1.0, expiration1));
851 AlternativeService www_alternative_service2(NPN_HTTP_2, "www.google.com",
852 1234);
853 base::Time expiration2;
854 ASSERT_TRUE(base::Time::FromUTCString("2036-12-31 10:00:00", &expiration2));
855 alternative_service_info_vector.push_back(
856 AlternativeServiceInfo(www_alternative_service2, 0.7, expiration2));
857 http_server_props_manager_->SetAlternativeServices(
858 server_www, alternative_service_info_vector);
860 AlternativeService mail_alternative_service(NPN_SPDY_3_1, "foo.google.com",
861 444);
862 base::Time expiration3 = base::Time::Max();
863 http_server_props_manager_->SetAlternativeService(
864 server_mail, mail_alternative_service, 0.2, expiration3);
866 // Set ServerNetworkStats.
867 ServerNetworkStats stats;
868 stats.srtt = base::TimeDelta::FromInternalValue(42);
869 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
871 // Set SupportsQuic.
872 IPAddressNumber actual_address;
873 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
874 http_server_props_manager_->SetSupportsQuic(true, actual_address);
876 // Update cache.
877 ExpectPrefsUpdate();
878 ExpectCacheUpdate();
879 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
880 base::RunLoop().RunUntilIdle();
882 // Verify preferences.
883 const char expected_json[] =
884 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
885 "\"expiration\":\"9223372036854775807\",\"host\":\"foo.google.com\","
886 "\"port\":444,\"probability\":0.2,\"protocol_str\":\"npn-spdy/3.1\"}],"
887 "\"network_stats\":{\"srtt\":42}},\"www.google.com:80\":{"
888 "\"alternative_service\":[{\"expiration\":\"13756212000000000\","
889 "\"port\":443,\"probability\":1.0,\"protocol_str\":\"npn-h2\"},"
890 "{\"expiration\":\"13758804000000000\",\"host\":\"www.google.com\","
891 "\"port\":1234,\"probability\":0.7,\"protocol_str\":\"npn-h2\"}]}},"
892 "\"supports_quic\":{\"address\":\"127.0.0.1\",\"used_quic\":true},"
893 "\"version\":3}";
895 const base::Value* http_server_properties =
896 pref_service_.GetUserPref(kTestHttpServerProperties);
897 ASSERT_NE(nullptr, http_server_properties);
898 std::string preferences_json;
899 EXPECT_TRUE(
900 base::JSONWriter::Write(*http_server_properties, &preferences_json));
901 EXPECT_EQ(expected_json, preferences_json);
904 TEST_F(HttpServerPropertiesManagerTest, AddToAlternativeServiceMap) {
905 scoped_ptr<base::Value> server_value = base::JSONReader::Read(
906 "{\"alternative_service\":[{\"port\":443,\"protocol_str\":\"npn-h2\"},"
907 "{\"port\":123,\"protocol_str\":\"quic\",\"probability\":0.7,"
908 "\"expiration\":\"9223372036854775807\"},{\"host\":\"example.org\","
909 "\"port\":1234,\"protocol_str\":\"npn-h2\",\"probability\":0.2,"
910 "\"expiration\":\"13758804000000000\"}]}");
911 ASSERT_TRUE(server_value);
912 base::DictionaryValue* server_dict;
913 ASSERT_TRUE(server_value->GetAsDictionary(&server_dict));
915 const HostPortPair host_port_pair("example.com", 443);
916 AlternativeServiceMap alternative_service_map(/*max_size=*/5);
917 EXPECT_TRUE(http_server_props_manager_->AddToAlternativeServiceMap(
918 host_port_pair, *server_dict, &alternative_service_map));
920 AlternativeServiceMap::iterator it =
921 alternative_service_map.Get(host_port_pair);
922 ASSERT_NE(alternative_service_map.end(), it);
923 AlternativeServiceInfoVector alternative_service_info_vector = it->second;
924 ASSERT_EQ(3u, alternative_service_info_vector.size());
926 EXPECT_EQ(NPN_HTTP_2,
927 alternative_service_info_vector[0].alternative_service.protocol);
928 EXPECT_EQ("", alternative_service_info_vector[0].alternative_service.host);
929 EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service.port);
930 // Probability defaults to 1.0.
931 EXPECT_DOUBLE_EQ(1.0, alternative_service_info_vector[0].probability);
932 // Expiration defaults to one day from now, testing with tolerance.
933 const base::Time now = base::Time::Now();
934 const base::Time expiration = alternative_service_info_vector[0].expiration;
935 EXPECT_LE(now + base::TimeDelta::FromHours(23), expiration);
936 EXPECT_GE(now + base::TimeDelta::FromDays(1), expiration);
938 EXPECT_EQ(QUIC,
939 alternative_service_info_vector[1].alternative_service.protocol);
940 EXPECT_EQ("", alternative_service_info_vector[1].alternative_service.host);
941 EXPECT_EQ(123, alternative_service_info_vector[1].alternative_service.port);
942 EXPECT_DOUBLE_EQ(0.7, alternative_service_info_vector[1].probability);
943 // numeric_limits<int64>::max() represents base::Time::Max().
944 EXPECT_EQ(base::Time::Max(), alternative_service_info_vector[1].expiration);
946 EXPECT_EQ(NPN_HTTP_2,
947 alternative_service_info_vector[2].alternative_service.protocol);
948 EXPECT_EQ("example.org",
949 alternative_service_info_vector[2].alternative_service.host);
950 EXPECT_EQ(1234, alternative_service_info_vector[2].alternative_service.port);
951 EXPECT_DOUBLE_EQ(0.2, alternative_service_info_vector[2].probability);
952 base::Time expected_expiration;
953 ASSERT_TRUE(
954 base::Time::FromUTCString("2036-12-31 10:00:00", &expected_expiration));
955 EXPECT_EQ(expected_expiration, alternative_service_info_vector[2].expiration);
958 // Early release 46 Dev and Canary builds serialized alternative service
959 // expiration as double. Test that they are properly parsed.
960 // TODO(bnc) Remove this test around 2015-10-01,
961 // and remove corresponding FRIEND macro from header file.
962 TEST_F(HttpServerPropertiesManagerTest, AlternativeServiceExpirationDouble) {
963 scoped_ptr<base::Value> server_value = base::JSONReader::Read(
964 "{\"alternative_service\":[{\"port\":443,\"protocol_str\":\"npn-h2\","
965 "\"expiration\":1234567890.0}]}");
966 ASSERT_TRUE(server_value);
967 base::DictionaryValue* server_dict;
968 ASSERT_TRUE(server_value->GetAsDictionary(&server_dict));
970 const HostPortPair host_port_pair("example.com", 443);
971 AlternativeServiceMap alternative_service_map(/*max_size=*/5);
972 EXPECT_TRUE(http_server_props_manager_->AddToAlternativeServiceMap(
973 host_port_pair, *server_dict, &alternative_service_map));
975 AlternativeServiceMap::iterator it =
976 alternative_service_map.Get(host_port_pair);
977 ASSERT_NE(alternative_service_map.end(), it);
978 AlternativeServiceInfoVector alternative_service_info_vector = it->second;
979 ASSERT_EQ(1u, alternative_service_info_vector.size());
981 EXPECT_EQ(NPN_HTTP_2,
982 alternative_service_info_vector[0].alternative_service.protocol);
983 EXPECT_EQ("", alternative_service_info_vector[0].alternative_service.host);
984 EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service.port);
985 // Probability defaults to 1.0.
986 EXPECT_DOUBLE_EQ(1.0, alternative_service_info_vector[0].probability);
987 base::Time expected_expiration;
988 ASSERT_TRUE(
989 base::Time::FromUTCString("2009-02-13 23:31:30", &expected_expiration));
990 EXPECT_EQ(expected_expiration, alternative_service_info_vector[0].expiration);
993 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
994 // Post an update task to the UI thread.
995 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
996 // Shutdown comes before the task is executed.
997 http_server_props_manager_->ShutdownOnPrefThread();
998 http_server_props_manager_.reset();
999 // Run the task after shutdown and deletion.
1000 base::RunLoop().RunUntilIdle();
1003 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
1004 // Post an update task.
1005 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
1006 // Shutdown comes before the task is executed.
1007 http_server_props_manager_->ShutdownOnPrefThread();
1008 // Run the task after shutdown, but before deletion.
1009 base::RunLoop().RunUntilIdle();
1010 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
1011 http_server_props_manager_.reset();
1012 base::RunLoop().RunUntilIdle();
1015 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
1016 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
1017 // Shutdown comes before the task is executed.
1018 http_server_props_manager_->ShutdownOnPrefThread();
1019 // Run the task after shutdown, but before deletion.
1020 base::RunLoop().RunUntilIdle();
1021 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
1022 http_server_props_manager_.reset();
1023 base::RunLoop().RunUntilIdle();
1027 // Tests for shutdown when updating prefs.
1029 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
1030 // Post an update task to the IO thread.
1031 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
1032 // Shutdown comes before the task is executed.
1033 http_server_props_manager_->ShutdownOnPrefThread();
1034 http_server_props_manager_.reset();
1035 // Run the task after shutdown and deletion.
1036 base::RunLoop().RunUntilIdle();
1039 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
1040 ExpectPrefsUpdate();
1041 // Post an update task.
1042 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
1043 // Shutdown comes before the task is executed.
1044 http_server_props_manager_->ShutdownOnPrefThread();
1045 // Run the task after shutdown, but before deletion.
1046 base::RunLoop().RunUntilIdle();
1047 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
1048 http_server_props_manager_.reset();
1049 base::RunLoop().RunUntilIdle();
1052 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
1053 // This posts a task to the UI thread.
1054 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
1055 base::Closure());
1056 // Shutdown comes before the task is executed.
1057 http_server_props_manager_->ShutdownOnPrefThread();
1058 // Run the task after shutdown, but before deletion.
1059 base::RunLoop().RunUntilIdle();
1060 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
1061 http_server_props_manager_.reset();
1062 base::RunLoop().RunUntilIdle();
1065 } // namespace net