OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/search_provider_logos/logo_tracker.h" | 5 #include "components/search_provider_logos/logo_tracker.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 Logo GetSampleLogo2(const GURL& logo_url, base::Time response_time) { | 105 Logo GetSampleLogo2(const GURL& logo_url, base::Time response_time) { |
106 Logo logo; | 106 Logo logo; |
107 logo.image = MakeBitmap(4, 3); | 107 logo.image = MakeBitmap(4, 3); |
108 logo.metadata.can_show_after_expiration = true; | 108 logo.metadata.can_show_after_expiration = true; |
109 logo.metadata.expiration_time = base::Time(); | 109 logo.metadata.expiration_time = base::Time(); |
110 logo.metadata.fingerprint = "71082741021409127"; | 110 logo.metadata.fingerprint = "71082741021409127"; |
111 logo.metadata.source_url = logo_url.spec(); | 111 logo.metadata.source_url = logo_url.spec(); |
112 logo.metadata.on_click_url = "http://example.com/page25"; | 112 logo.metadata.on_click_url = "http://example.com/page25"; |
113 logo.metadata.alt_text = "The logo for example.com"; | 113 logo.metadata.alt_text = "The logo for example.com"; |
114 logo.metadata.mime_type = "image/png"; | 114 logo.metadata.mime_type = "image/jpeg"; |
115 return logo; | 115 return logo; |
116 } | 116 } |
117 | 117 |
118 std::string MakeServerResponse( | 118 std::string MakeServerResponse( |
119 const SkBitmap& image, | 119 const SkBitmap& image, |
120 const std::string& on_click_url, | 120 const std::string& on_click_url, |
121 const std::string& alt_text, | 121 const std::string& alt_text, |
122 const std::string& animated_url, | 122 const std::string& animated_url, |
123 const std::string& mime_type, | 123 const std::string& mime_type, |
124 const std::string& fingerprint, | 124 const std::string& fingerprint, |
125 base::TimeDelta time_to_live) { | 125 base::TimeDelta time_to_live) { |
126 base::DictionaryValue dict; | 126 base::DictionaryValue dict; |
127 if (!image.isNull()) { | 127 if (!image.isNull()) |
128 dict.SetString("update.logo.data", EncodeBitmapAsPNGBase64(image)); | 128 dict.SetString("update.logo.data", EncodeBitmapAsPNGBase64(image)); |
129 } | |
130 | 129 |
131 dict.SetString("update.logo.target", on_click_url); | 130 dict.SetString("update.logo.target", on_click_url); |
132 dict.SetString("update.logo.alt", alt_text); | 131 dict.SetString("update.logo.alt", alt_text); |
133 if (!animated_url.empty()) { | 132 if (!animated_url.empty()) |
134 dict.SetString("update.logo.url", animated_url); | 133 dict.SetString("update.logo.url", animated_url); |
135 } | 134 if (!mime_type.empty()) |
136 dict.SetString("update.logo.mime_type", mime_type); | 135 dict.SetString("update.logo.mime_type", mime_type); |
137 dict.SetString("update.logo.fingerprint", fingerprint); | 136 dict.SetString("update.logo.fingerprint", fingerprint); |
138 if (time_to_live.ToInternalValue() != 0) | 137 if (time_to_live.ToInternalValue() != 0) |
139 dict.SetInteger("update.logo.time_to_live", | 138 dict.SetInteger("update.logo.time_to_live", |
140 static_cast<int>(time_to_live.InMilliseconds())); | 139 static_cast<int>(time_to_live.InMilliseconds())); |
141 | 140 |
142 std::string output; | 141 std::string output; |
143 base::JSONWriter::Write(&dict, &output); | 142 base::JSONWriter::Write(&dict, &output); |
144 return output; | 143 return output; |
145 } | 144 } |
146 | 145 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 SetCachedLogo(&encoded_logo); | 214 SetCachedLogo(&encoded_logo); |
216 } | 215 } |
217 | 216 |
218 void ExpectSetCachedLogo(const Logo* expected_logo) { | 217 void ExpectSetCachedLogo(const Logo* expected_logo) { |
219 Mock::VerifyAndClearExpectations(this); | 218 Mock::VerifyAndClearExpectations(this); |
220 EXPECT_CALL(*this, SetCachedLogo(_)) | 219 EXPECT_CALL(*this, SetCachedLogo(_)) |
221 .WillOnce(ExpectLogosEqualAction(expected_logo)); | 220 .WillOnce(ExpectLogosEqualAction(expected_logo)); |
222 } | 221 } |
223 | 222 |
224 void UpdateCachedLogoMetadataInternal(const LogoMetadata& metadata) { | 223 void UpdateCachedLogoMetadataInternal(const LogoMetadata& metadata) { |
| 224 ASSERT_TRUE(logo_.get()); |
| 225 ASSERT_TRUE(metadata_.get()); |
| 226 EXPECT_EQ(metadata_->fingerprint, metadata.fingerprint); |
225 metadata_.reset(new LogoMetadata(metadata)); | 227 metadata_.reset(new LogoMetadata(metadata)); |
| 228 logo_->metadata = metadata; |
226 } | 229 } |
227 | 230 |
228 virtual const LogoMetadata* GetCachedLogoMetadataInternal() { | 231 virtual const LogoMetadata* GetCachedLogoMetadataInternal() { |
229 return metadata_.get(); | 232 return metadata_.get(); |
230 } | 233 } |
231 | 234 |
232 virtual void SetCachedLogoInternal(const EncodedLogo* logo) { | 235 virtual void SetCachedLogoInternal(const EncodedLogo* logo) { |
233 logo_.reset(logo ? new EncodedLogo(*logo) : NULL); | 236 logo_.reset(logo ? new EncodedLogo(*logo) : NULL); |
234 metadata_.reset(logo ? new LogoMetadata(logo->metadata) : NULL); | 237 metadata_.reset(logo ? new LogoMetadata(logo->metadata) : NULL); |
235 } | 238 } |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 net::URLRequestStatus::FAILED, | 470 net::URLRequestStatus::FAILED, |
468 net::HTTP_OK); | 471 net::HTTP_OK); |
469 | 472 |
470 EXPECT_CALL(*logo_cache_, UpdateCachedLogoMetadata(_)).Times(0); | 473 EXPECT_CALL(*logo_cache_, UpdateCachedLogoMetadata(_)).Times(0); |
471 EXPECT_CALL(*logo_cache_, SetCachedLogo(_)).Times(0); | 474 EXPECT_CALL(*logo_cache_, SetCachedLogo(_)).Times(0); |
472 EXPECT_CALL(*logo_cache_, OnGetCachedLogo()).Times(AtMost(1)); | 475 EXPECT_CALL(*logo_cache_, OnGetCachedLogo()).Times(AtMost(1)); |
473 observer_.ExpectCachedLogo(&cached_logo); | 476 observer_.ExpectCachedLogo(&cached_logo); |
474 GetLogo(); | 477 GetLogo(); |
475 } | 478 } |
476 | 479 |
477 TEST_F(LogoTrackerTest, ValidateCachedLogoFingerprint) { | 480 TEST_F(LogoTrackerTest, ValidateCachedLogo) { |
478 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); | 481 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); |
479 logo_cache_->EncodeAndSetCachedLogo(cached_logo); | 482 logo_cache_->EncodeAndSetCachedLogo(cached_logo); |
480 | 483 |
| 484 // During revalidation, the image data and mime_type are absent. |
481 Logo fresh_logo = cached_logo; | 485 Logo fresh_logo = cached_logo; |
482 fresh_logo.image.reset(); | 486 fresh_logo.image.reset(); |
| 487 fresh_logo.metadata.mime_type.clear(); |
483 fresh_logo.metadata.expiration_time = | 488 fresh_logo.metadata.expiration_time = |
484 test_clock_->Now() + base::TimeDelta::FromDays(8); | 489 test_clock_->Now() + base::TimeDelta::FromDays(8); |
485 SetServerResponseWhenFingerprint(fresh_logo.metadata.fingerprint, | 490 SetServerResponseWhenFingerprint(fresh_logo.metadata.fingerprint, |
486 ServerResponse(fresh_logo)); | 491 ServerResponse(fresh_logo)); |
487 | 492 |
488 EXPECT_CALL(*logo_cache_, UpdateCachedLogoMetadata(_)).Times(1); | 493 EXPECT_CALL(*logo_cache_, UpdateCachedLogoMetadata(_)).Times(1); |
489 EXPECT_CALL(*logo_cache_, SetCachedLogo(_)).Times(0); | 494 EXPECT_CALL(*logo_cache_, SetCachedLogo(_)).Times(0); |
490 EXPECT_CALL(*logo_cache_, OnGetCachedLogo()).Times(AtMost(1)); | 495 EXPECT_CALL(*logo_cache_, OnGetCachedLogo()).Times(AtMost(1)); |
491 observer_.ExpectCachedLogo(&cached_logo); | 496 observer_.ExpectCachedLogo(&cached_logo); |
492 | |
493 GetLogo(); | 497 GetLogo(); |
494 | 498 |
495 EXPECT_TRUE(logo_cache_->GetCachedLogoMetadata() != NULL); | 499 EXPECT_TRUE(logo_cache_->GetCachedLogoMetadata() != NULL); |
496 EXPECT_EQ(logo_cache_->GetCachedLogoMetadata()->expiration_time, | 500 EXPECT_EQ(fresh_logo.metadata.expiration_time, |
497 fresh_logo.metadata.expiration_time); | 501 logo_cache_->GetCachedLogoMetadata()->expiration_time); |
| 502 |
| 503 // Ensure that cached logo is still returned correctly on subsequent requests. |
| 504 // In particular, the metadata should stay valid. http://crbug.com/480090 |
| 505 EXPECT_CALL(*logo_cache_, UpdateCachedLogoMetadata(_)).Times(1); |
| 506 EXPECT_CALL(*logo_cache_, SetCachedLogo(_)).Times(0); |
| 507 EXPECT_CALL(*logo_cache_, OnGetCachedLogo()).Times(AtMost(1)); |
| 508 observer_.ExpectCachedLogo(&cached_logo); |
| 509 GetLogo(); |
| 510 } |
| 511 |
| 512 TEST_F(LogoTrackerTest, UpdateCachedLogoMetadata) { |
| 513 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); |
| 514 logo_cache_->EncodeAndSetCachedLogo(cached_logo); |
| 515 |
| 516 Logo fresh_logo = cached_logo; |
| 517 fresh_logo.image.reset(); |
| 518 fresh_logo.metadata.mime_type.clear(); |
| 519 fresh_logo.metadata.on_click_url = "http://new.onclick.url"; |
| 520 fresh_logo.metadata.alt_text = "new alt text"; |
| 521 fresh_logo.metadata.animated_url = "http://new.animated.url"; |
| 522 fresh_logo.metadata.expiration_time = |
| 523 test_clock_->Now() + base::TimeDelta::FromDays(8); |
| 524 SetServerResponseWhenFingerprint(fresh_logo.metadata.fingerprint, |
| 525 ServerResponse(fresh_logo)); |
| 526 |
| 527 // On the first request, the cached logo should be used. |
| 528 observer_.ExpectCachedLogo(&cached_logo); |
| 529 GetLogo(); |
| 530 |
| 531 // Subsequently, the cached image should be returned along with the updated |
| 532 // metadata. |
| 533 Logo expected_logo = fresh_logo; |
| 534 expected_logo.image = cached_logo.image; |
| 535 expected_logo.metadata.mime_type = cached_logo.metadata.mime_type; |
| 536 observer_.ExpectCachedLogo(&expected_logo); |
| 537 GetLogo(); |
498 } | 538 } |
499 | 539 |
500 TEST_F(LogoTrackerTest, UpdateCachedLogo) { | 540 TEST_F(LogoTrackerTest, UpdateCachedLogo) { |
501 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); | 541 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); |
502 logo_cache_->EncodeAndSetCachedLogo(cached_logo); | 542 logo_cache_->EncodeAndSetCachedLogo(cached_logo); |
503 | 543 |
504 Logo fresh_logo = GetSampleLogo2(logo_url_, test_clock_->Now()); | 544 Logo fresh_logo = GetSampleLogo2(logo_url_, test_clock_->Now()); |
505 SetServerResponseWhenFingerprint(cached_logo.metadata.fingerprint, | 545 SetServerResponseWhenFingerprint(cached_logo.metadata.fingerprint, |
506 ServerResponse(fresh_logo)); | 546 ServerResponse(fresh_logo)); |
507 | 547 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 MockLogoObserver listener2; | 733 MockLogoObserver listener2; |
694 listener2.ExpectFreshLogo(&logo); | 734 listener2.ExpectFreshLogo(&logo); |
695 logo_tracker_->GetLogo(&listener2); | 735 logo_tracker_->GetLogo(&listener2); |
696 | 736 |
697 base::RunLoop().RunUntilIdle(); | 737 base::RunLoop().RunUntilIdle(); |
698 } | 738 } |
699 | 739 |
700 } // namespace | 740 } // namespace |
701 | 741 |
702 } // namespace search_provider_logos | 742 } // namespace search_provider_logos |
OLD | NEW |