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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 11 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
12 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 class TestNotificationInterface { | 20 class TestNotificationInterface { |
20 public: | 21 public: |
21 virtual ~TestNotificationInterface() {} | 22 virtual ~TestNotificationInterface() {} |
22 virtual void OnImageChanged() = 0; | 23 virtual void OnImageChanged() = 0; |
23 virtual void OnRequestFinished() = 0; | 24 virtual void OnRequestFinished() = 0; |
(...skipping 17 matching lines...) Expand all Loading... |
41 | 42 |
42 class TestService : public BitmapFetcherService { | 43 class TestService : public BitmapFetcherService { |
43 public: | 44 public: |
44 explicit TestService(content::BrowserContext* context) | 45 explicit TestService(content::BrowserContext* context) |
45 : BitmapFetcherService(context) {} | 46 : BitmapFetcherService(context) {} |
46 ~TestService() override {} | 47 ~TestService() override {} |
47 | 48 |
48 // Create a fetcher, but don't start downloading. That allows side-stepping | 49 // Create a fetcher, but don't start downloading. That allows side-stepping |
49 // the decode step, which requires a utility process. | 50 // the decode step, which requires a utility process. |
50 std::unique_ptr<chrome::BitmapFetcher> CreateFetcher( | 51 std::unique_ptr<chrome::BitmapFetcher> CreateFetcher( |
51 const GURL& url) override { | 52 const GURL& url, |
52 return base::MakeUnique<chrome::BitmapFetcher>(url, this); | 53 const net::NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 54 return base::MakeUnique<chrome::BitmapFetcher>(url, this, |
| 55 traffic_annotation); |
53 } | 56 } |
54 }; | 57 }; |
55 | 58 |
56 } // namespace | 59 } // namespace |
57 | 60 |
58 class BitmapFetcherServiceTest : public testing::Test, | 61 class BitmapFetcherServiceTest : public testing::Test, |
59 public TestNotificationInterface { | 62 public TestNotificationInterface { |
60 public: | 63 public: |
61 BitmapFetcherServiceTest() | 64 BitmapFetcherServiceTest() |
62 : url1_(GURL("http://example.org/sample-image-1.png")), | 65 : url1_(GURL("http://example.org/sample-image-1.png")), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 content::TestBrowserThreadBundle thread_bundle_; | 124 content::TestBrowserThreadBundle thread_bundle_; |
122 TestingProfile profile_; | 125 TestingProfile profile_; |
123 }; | 126 }; |
124 | 127 |
125 TEST_F(BitmapFetcherServiceTest, RequestInvalidUrl) { | 128 TEST_F(BitmapFetcherServiceTest, RequestInvalidUrl) { |
126 const BitmapFetcherService::RequestId invalid_request_id = | 129 const BitmapFetcherService::RequestId invalid_request_id = |
127 BitmapFetcherService::REQUEST_ID_INVALID; | 130 BitmapFetcherService::REQUEST_ID_INVALID; |
128 GURL invalid_url; | 131 GURL invalid_url; |
129 ASSERT_FALSE(invalid_url.is_valid()); | 132 ASSERT_FALSE(invalid_url.is_valid()); |
130 | 133 |
131 BitmapFetcherService::RequestId request_id = | 134 BitmapFetcherService::RequestId request_id = service_->RequestImage( |
132 service_->RequestImage(invalid_url, new TestObserver(this)); | 135 invalid_url, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
133 EXPECT_EQ(invalid_request_id, request_id); | 136 EXPECT_EQ(invalid_request_id, request_id); |
134 } | 137 } |
135 | 138 |
136 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) { | 139 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) { |
137 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID); | 140 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID); |
138 service_->CancelRequest(23); | 141 service_->CancelRequest(23); |
139 } | 142 } |
140 | 143 |
141 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) { | 144 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) { |
142 EXPECT_EQ(0U, active_fetchers().size()); | 145 EXPECT_EQ(0U, active_fetchers().size()); |
143 | 146 |
144 service_->RequestImage(url1_, new TestObserver(this)); | 147 service_->RequestImage(url1_, new TestObserver(this), |
| 148 TRAFFIC_ANNOTATION_FOR_TESTS); |
145 EXPECT_EQ(1U, active_fetchers().size()); | 149 EXPECT_EQ(1U, active_fetchers().size()); |
146 | 150 |
147 service_->RequestImage(url1_, new TestObserver(this)); | 151 service_->RequestImage(url1_, new TestObserver(this), |
| 152 TRAFFIC_ANNOTATION_FOR_TESTS); |
148 EXPECT_EQ(1U, active_fetchers().size()); | 153 EXPECT_EQ(1U, active_fetchers().size()); |
149 } | 154 } |
150 | 155 |
151 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) { | 156 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) { |
152 service_->RequestImage(url1_, new TestObserver(this)); | 157 service_->RequestImage(url1_, new TestObserver(this), |
153 service_->RequestImage(url1_, new TestObserver(this)); | 158 TRAFFIC_ANNOTATION_FOR_TESTS); |
154 service_->RequestImage(url1_, new TestObserver(this)); | 159 service_->RequestImage(url1_, new TestObserver(this), |
155 service_->RequestImage(url1_, new TestObserver(this)); | 160 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 161 service_->RequestImage(url1_, new TestObserver(this), |
| 162 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 163 service_->RequestImage(url1_, new TestObserver(this), |
| 164 TRAFFIC_ANNOTATION_FOR_TESTS); |
156 EXPECT_EQ(1U, active_fetchers().size()); | 165 EXPECT_EQ(1U, active_fetchers().size()); |
157 EXPECT_EQ(4U, requests().size()); | 166 EXPECT_EQ(4U, requests().size()); |
158 | 167 |
159 CompleteFetch(url1_); | 168 CompleteFetch(url1_); |
160 EXPECT_EQ(4, images_changed_); | 169 EXPECT_EQ(4, images_changed_); |
161 EXPECT_EQ(4, requests_finished_); | 170 EXPECT_EQ(4, requests_finished_); |
162 } | 171 } |
163 | 172 |
164 TEST_F(BitmapFetcherServiceTest, CancelRequest) { | 173 TEST_F(BitmapFetcherServiceTest, CancelRequest) { |
165 service_->RequestImage(url1_, new TestObserver(this)); | 174 service_->RequestImage(url1_, new TestObserver(this), |
166 service_->RequestImage(url1_, new TestObserver(this)); | 175 TRAFFIC_ANNOTATION_FOR_TESTS); |
167 BitmapFetcherService::RequestId requestId = | 176 service_->RequestImage(url1_, new TestObserver(this), |
168 service_->RequestImage(url2_, new TestObserver(this)); | 177 TRAFFIC_ANNOTATION_FOR_TESTS); |
169 service_->RequestImage(url1_, new TestObserver(this)); | 178 BitmapFetcherService::RequestId requestId = service_->RequestImage( |
170 service_->RequestImage(url1_, new TestObserver(this)); | 179 url2_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 180 service_->RequestImage(url1_, new TestObserver(this), |
| 181 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 182 service_->RequestImage(url1_, new TestObserver(this), |
| 183 TRAFFIC_ANNOTATION_FOR_TESTS); |
171 EXPECT_EQ(5U, requests().size()); | 184 EXPECT_EQ(5U, requests().size()); |
172 | 185 |
173 service_->CancelRequest(requestId); | 186 service_->CancelRequest(requestId); |
174 EXPECT_EQ(4U, requests().size()); | 187 EXPECT_EQ(4U, requests().size()); |
175 | 188 |
176 CompleteFetch(url2_); | 189 CompleteFetch(url2_); |
177 EXPECT_EQ(0, images_changed_); | 190 EXPECT_EQ(0, images_changed_); |
178 | 191 |
179 CompleteFetch(url1_); | 192 CompleteFetch(url1_); |
180 EXPECT_EQ(4, images_changed_); | 193 EXPECT_EQ(4, images_changed_); |
181 } | 194 } |
182 | 195 |
183 TEST_F(BitmapFetcherServiceTest, FailedNullRequestsAreHandled) { | 196 TEST_F(BitmapFetcherServiceTest, FailedNullRequestsAreHandled) { |
184 service_->RequestImage(url1_, new TestObserver(this)); | 197 service_->RequestImage(url1_, new TestObserver(this), |
185 service_->RequestImage(url2_, new TestObserver(this)); | 198 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 199 service_->RequestImage(url2_, new TestObserver(this), |
| 200 TRAFFIC_ANNOTATION_FOR_TESTS); |
186 EXPECT_EQ(0U, cache_size()); | 201 EXPECT_EQ(0U, cache_size()); |
187 | 202 |
188 CompleteFetch(url1_); | 203 CompleteFetch(url1_); |
189 EXPECT_EQ(1U, cache_size()); | 204 EXPECT_EQ(1U, cache_size()); |
190 | 205 |
191 FailDecode(url2_); | 206 FailDecode(url2_); |
192 EXPECT_EQ(1U, cache_size()); | 207 EXPECT_EQ(1U, cache_size()); |
193 } | 208 } |
194 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) { | 209 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) { |
195 service_->RequestImage(url1_, new TestObserver(this)); | 210 service_->RequestImage(url1_, new TestObserver(this), |
196 service_->RequestImage(url2_, new TestObserver(this)); | 211 TRAFFIC_ANNOTATION_FOR_TESTS); |
| 212 service_->RequestImage(url2_, new TestObserver(this), |
| 213 TRAFFIC_ANNOTATION_FOR_TESTS); |
197 EXPECT_EQ(0U, cache_size()); | 214 EXPECT_EQ(0U, cache_size()); |
198 | 215 |
199 CompleteFetch(url1_); | 216 CompleteFetch(url1_); |
200 EXPECT_EQ(1U, cache_size()); | 217 EXPECT_EQ(1U, cache_size()); |
201 | 218 |
202 FailFetch(url2_); | 219 FailFetch(url2_); |
203 EXPECT_EQ(1U, cache_size()); | 220 EXPECT_EQ(1U, cache_size()); |
204 } | 221 } |
OLD | NEW |