| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/location.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_vector.h" | 17 #include "base/memory/scoped_vector.h" |
| 17 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 20 #include "base/test/simple_test_clock.h" | 22 #include "base/test/simple_test_clock.h" |
| 23 #include "base/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 22 #include "base/values.h" | 25 #include "base/values.h" |
| 23 #include "components/search_provider_logos/google_logo_api.h" | 26 #include "components/search_provider_logos/google_logo_api.h" |
| 24 #include "net/base/url_util.h" | 27 #include "net/base/url_util.h" |
| 25 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 26 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
| 27 #include "net/url_request/test_url_fetcher_factory.h" | 30 #include "net/url_request/test_url_fetcher_factory.h" |
| 28 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
| 29 #include "net/url_request/url_request_test_util.h" | 32 #include "net/url_request/url_request_test_util.h" |
| 30 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 public: | 297 public: |
| 295 TestLogoDelegate() {} | 298 TestLogoDelegate() {} |
| 296 ~TestLogoDelegate() override {} | 299 ~TestLogoDelegate() override {} |
| 297 | 300 |
| 298 void DecodeUntrustedImage( | 301 void DecodeUntrustedImage( |
| 299 const scoped_refptr<base::RefCountedString>& encoded_image, | 302 const scoped_refptr<base::RefCountedString>& encoded_image, |
| 300 base::Callback<void(const SkBitmap&)> image_decoded_callback) override { | 303 base::Callback<void(const SkBitmap&)> image_decoded_callback) override { |
| 301 SkBitmap bitmap = | 304 SkBitmap bitmap = |
| 302 gfx::Image::CreateFrom1xPNGBytes(encoded_image->front(), | 305 gfx::Image::CreateFrom1xPNGBytes(encoded_image->front(), |
| 303 encoded_image->size()).AsBitmap(); | 306 encoded_image->size()).AsBitmap(); |
| 304 base::MessageLoopProxy::current()->PostTask( | 307 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 305 FROM_HERE, base::Bind(image_decoded_callback, bitmap)); | 308 FROM_HERE, base::Bind(image_decoded_callback, bitmap)); |
| 306 } | 309 } |
| 307 }; | 310 }; |
| 308 | 311 |
| 309 class LogoTrackerTest : public ::testing::Test { | 312 class LogoTrackerTest : public ::testing::Test { |
| 310 protected: | 313 protected: |
| 311 LogoTrackerTest() | 314 LogoTrackerTest() |
| 312 : message_loop_(new base::MessageLoop()), | 315 : message_loop_(new base::MessageLoop()), |
| 313 logo_url_("https://google.com/doodleoftheday?size=hp"), | 316 logo_url_("https://google.com/doodleoftheday?size=hp"), |
| 314 test_clock_(new base::SimpleTestClock()), | 317 test_clock_(new base::SimpleTestClock()), |
| 315 logo_cache_(new NiceMock<MockLogoCache>()), | 318 logo_cache_(new NiceMock<MockLogoCache>()), |
| 316 fake_url_fetcher_factory_(NULL) { | 319 fake_url_fetcher_factory_(NULL) { |
| 317 test_clock_->SetNow(base::Time::FromJsTime(INT64_C(1388686828000))); | 320 test_clock_->SetNow(base::Time::FromJsTime(INT64_C(1388686828000))); |
| 318 logo_tracker_ = new LogoTracker( | 321 logo_tracker_ = |
| 319 base::FilePath(), | 322 new LogoTracker(base::FilePath(), base::ThreadTaskRunnerHandle::Get(), |
| 320 base::MessageLoopProxy::current(), | 323 base::ThreadTaskRunnerHandle::Get(), |
| 321 base::MessageLoopProxy::current(), | 324 new net::TestURLRequestContextGetter( |
| 322 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()), | 325 base::ThreadTaskRunnerHandle::Get()), |
| 323 scoped_ptr<LogoDelegate>(new TestLogoDelegate())); | 326 scoped_ptr<LogoDelegate>(new TestLogoDelegate())); |
| 324 logo_tracker_->SetServerAPI(logo_url_, base::Bind(&GoogleParseLogoResponse), | 327 logo_tracker_->SetServerAPI(logo_url_, base::Bind(&GoogleParseLogoResponse), |
| 325 base::Bind(&GoogleAppendQueryparamsToLogoURL), | 328 base::Bind(&GoogleAppendQueryparamsToLogoURL), |
| 326 false); | 329 false); |
| 327 logo_tracker_->SetClockForTests(scoped_ptr<base::Clock>(test_clock_)); | 330 logo_tracker_->SetClockForTests(scoped_ptr<base::Clock>(test_clock_)); |
| 328 logo_tracker_->SetLogoCacheForTests(scoped_ptr<LogoCache>(logo_cache_)); | 331 logo_tracker_->SetLogoCacheForTests(scoped_ptr<LogoCache>(logo_cache_)); |
| 329 } | 332 } |
| 330 | 333 |
| 331 virtual void TearDown() { | 334 virtual void TearDown() { |
| 332 // logo_tracker_ owns logo_cache_, which gets destructed on the file thread | 335 // logo_tracker_ owns logo_cache_, which gets destructed on the file thread |
| 333 // after logo_tracker_'s destruction. Ensure that logo_cache_ is actually | 336 // after logo_tracker_'s destruction. Ensure that logo_cache_ is actually |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 | 683 |
| 681 // Tests that deal with multiple listeners. | 684 // Tests that deal with multiple listeners. |
| 682 | 685 |
| 683 void EnqueueObservers(LogoTracker* logo_tracker, | 686 void EnqueueObservers(LogoTracker* logo_tracker, |
| 684 const ScopedVector<MockLogoObserver>& observers, | 687 const ScopedVector<MockLogoObserver>& observers, |
| 685 size_t start_index) { | 688 size_t start_index) { |
| 686 if (start_index >= observers.size()) | 689 if (start_index >= observers.size()) |
| 687 return; | 690 return; |
| 688 | 691 |
| 689 logo_tracker->GetLogo(observers[start_index]); | 692 logo_tracker->GetLogo(observers[start_index]); |
| 690 base::MessageLoop::current()->PostTask(FROM_HERE, | 693 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 691 base::Bind(&EnqueueObservers, | 694 FROM_HERE, base::Bind(&EnqueueObservers, logo_tracker, |
| 692 logo_tracker, | 695 base::ConstRef(observers), start_index + 1)); |
| 693 base::ConstRef(observers), | |
| 694 start_index + 1)); | |
| 695 } | 696 } |
| 696 | 697 |
| 697 TEST_F(LogoTrackerTest, SupportOverlappingLogoRequests) { | 698 TEST_F(LogoTrackerTest, SupportOverlappingLogoRequests) { |
| 698 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); | 699 Logo cached_logo = GetSampleLogo(logo_url_, test_clock_->Now()); |
| 699 logo_cache_->EncodeAndSetCachedLogo(cached_logo); | 700 logo_cache_->EncodeAndSetCachedLogo(cached_logo); |
| 700 ON_CALL(*logo_cache_, SetCachedLogo(_)).WillByDefault(Return()); | 701 ON_CALL(*logo_cache_, SetCachedLogo(_)).WillByDefault(Return()); |
| 701 | 702 |
| 702 Logo fresh_logo = GetSampleLogo2(logo_url_, test_clock_->Now()); | 703 Logo fresh_logo = GetSampleLogo2(logo_url_, test_clock_->Now()); |
| 703 std::string response = ServerResponse(fresh_logo); | 704 std::string response = ServerResponse(fresh_logo); |
| 704 SetServerResponse(response); | 705 SetServerResponse(response); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 734 MockLogoObserver listener2; | 735 MockLogoObserver listener2; |
| 735 listener2.ExpectFreshLogo(&logo); | 736 listener2.ExpectFreshLogo(&logo); |
| 736 logo_tracker_->GetLogo(&listener2); | 737 logo_tracker_->GetLogo(&listener2); |
| 737 | 738 |
| 738 base::RunLoop().RunUntilIdle(); | 739 base::RunLoop().RunUntilIdle(); |
| 739 } | 740 } |
| 740 | 741 |
| 741 } // namespace | 742 } // namespace |
| 742 | 743 |
| 743 } // namespace search_provider_logos | 744 } // namespace search_provider_logos |
| OLD | NEW |