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 "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" | 5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" |
6 | 6 |
7 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 7 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
8 #include "chrome/test/base/testing_profile.h" | 8 #include "chrome/test/base/testing_profile.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
| 13 class TestNotificationInterface { |
| 14 public: |
| 15 virtual ~TestNotificationInterface() {} |
| 16 virtual void OnImageChanged() = 0; |
| 17 virtual void OnRequestFinished() = 0; |
| 18 }; |
| 19 |
13 class TestObserver : public BitmapFetcherService::Observer { | 20 class TestObserver : public BitmapFetcherService::Observer { |
14 public: | 21 public: |
15 explicit TestObserver(BitmapFetcherService::Observer* target) | 22 explicit TestObserver(TestNotificationInterface* target) : target_(target) {} |
16 : target_(target) {} | 23 virtual ~TestObserver() { target_->OnRequestFinished(); } |
17 virtual ~TestObserver() {} | |
18 | 24 |
19 virtual void OnImageChanged(BitmapFetcherService::RequestId request_id, | 25 virtual void OnImageChanged(BitmapFetcherService::RequestId request_id, |
20 const SkBitmap& answers_image) OVERRIDE { | 26 const SkBitmap& answers_image) OVERRIDE { |
21 target_->OnImageChanged(request_id, answers_image); | 27 target_->OnImageChanged(); |
22 } | 28 } |
23 | 29 |
24 BitmapFetcherService::Observer* target_; | 30 TestNotificationInterface* target_; |
25 }; | 31 }; |
26 | 32 |
27 class TestService : public BitmapFetcherService { | 33 class TestService : public BitmapFetcherService { |
28 public: | 34 public: |
29 explicit TestService(content::BrowserContext* context) | 35 explicit TestService(content::BrowserContext* context) |
30 : BitmapFetcherService(context) {} | 36 : BitmapFetcherService(context) {} |
31 virtual ~TestService() {} | 37 virtual ~TestService() {} |
32 | 38 |
33 // Create a fetcher, but don't start downloading. That allows side-stepping | 39 // Create a fetcher, but don't start downloading. That allows side-stepping |
34 // the decode step, which requires a utility process. | 40 // the decode step, which requires a utility process. |
35 virtual chrome::BitmapFetcher* CreateFetcher(const GURL& url) OVERRIDE { | 41 virtual chrome::BitmapFetcher* CreateFetcher(const GURL& url) OVERRIDE { |
36 return new chrome::BitmapFetcher(url, this); | 42 return new chrome::BitmapFetcher(url, this); |
37 } | 43 } |
38 }; | 44 }; |
39 | 45 |
40 } // namespace | 46 } // namespace |
41 | 47 |
42 class BitmapFetcherServiceTest : public testing::Test, | 48 class BitmapFetcherServiceTest : public testing::Test, |
43 public BitmapFetcherService::Observer { | 49 public TestNotificationInterface { |
44 public: | 50 public: |
45 virtual void SetUp() OVERRIDE { | 51 virtual void SetUp() OVERRIDE { |
46 service_.reset(new TestService(&profile_)); | 52 service_.reset(new TestService(&profile_)); |
47 requestsFinished_ = 0; | 53 requestsFinished_ = 0; |
48 imagesChanged_ = 0; | 54 imagesChanged_ = 0; |
49 url1_ = GURL("http://example.org/sample-image-1.png"); | 55 url1_ = GURL("http://example.org/sample-image-1.png"); |
50 url2_ = GURL("http://example.org/sample-image-2.png"); | 56 url2_ = GURL("http://example.org/sample-image-2.png"); |
51 } | 57 } |
52 | 58 |
53 const ScopedVector<BitmapFetcherRequest>& requests() { | 59 const ScopedVector<BitmapFetcherRequest>& requests() { |
54 return service_->requests_; | 60 return service_->requests_; |
55 } | 61 } |
56 const ScopedVector<chrome::BitmapFetcher>& active_fetchers() { | 62 const ScopedVector<chrome::BitmapFetcher>& active_fetchers() { |
57 return service_->active_fetchers_; | 63 return service_->active_fetchers_; |
58 } | 64 } |
59 size_t cache_size() { return service_->cache_.size(); } | 65 size_t cache_size() { return service_->cache_.size(); } |
60 | 66 |
61 virtual void OnImageChanged(BitmapFetcherService::RequestId request_id, | 67 virtual void OnImageChanged() OVERRIDE { imagesChanged_++; } |
62 const SkBitmap& answers_image) OVERRIDE { | 68 |
63 if (answers_image.empty()) | 69 virtual void OnRequestFinished() OVERRIDE { requestsFinished_++; } |
64 requestsFinished_++; | |
65 else | |
66 imagesChanged_++; | |
67 } | |
68 | 70 |
69 // Simulate finishing a URL fetch and decode for the given fetcher. | 71 // Simulate finishing a URL fetch and decode for the given fetcher. |
70 void CompleteFetch(const GURL& url) { | 72 void CompleteFetch(const GURL& url) { |
71 const chrome::BitmapFetcher* fetcher = service_->FindFetcherForUrl(url); | 73 const chrome::BitmapFetcher* fetcher = service_->FindFetcherForUrl(url); |
72 ASSERT_TRUE(NULL != fetcher); | 74 ASSERT_TRUE(NULL != fetcher); |
73 | 75 |
74 // Create a non-empty bitmap. | 76 // Create a non-empty bitmap. |
75 SkBitmap image; | 77 SkBitmap image; |
76 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); | 78 image.setConfig(SkBitmap::kARGB_8888_Config, 2, 2); |
77 image.allocPixels(); | 79 image.allocPixels(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 service_->RequestImage(url1_, new TestObserver(this)); | 153 service_->RequestImage(url1_, new TestObserver(this)); |
152 service_->RequestImage(url2_, new TestObserver(this)); | 154 service_->RequestImage(url2_, new TestObserver(this)); |
153 EXPECT_EQ(0U, cache_size()); | 155 EXPECT_EQ(0U, cache_size()); |
154 | 156 |
155 CompleteFetch(url1_); | 157 CompleteFetch(url1_); |
156 EXPECT_EQ(1U, cache_size()); | 158 EXPECT_EQ(1U, cache_size()); |
157 | 159 |
158 FailFetch(url2_); | 160 FailFetch(url2_); |
159 EXPECT_EQ(1U, cache_size()); | 161 EXPECT_EQ(1U, cache_size()); |
160 } | 162 } |
OLD | NEW |