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>( |
| 55 url, this, 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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 = |
132 service_->RequestImage(invalid_url, new TestObserver(this)); | 135 service_->RequestImage(invalid_url, |
| 136 new TestObserver(this), |
| 137 TRAFFIC_ANNOTATION_FOR_TESTS); |
133 EXPECT_EQ(invalid_request_id, request_id); | 138 EXPECT_EQ(invalid_request_id, request_id); |
134 } | 139 } |
135 | 140 |
136 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) { | 141 TEST_F(BitmapFetcherServiceTest, CancelInvalidRequest) { |
137 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID); | 142 service_->CancelRequest(BitmapFetcherService::REQUEST_ID_INVALID); |
138 service_->CancelRequest(23); | 143 service_->CancelRequest(23); |
139 } | 144 } |
140 | 145 |
141 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) { | 146 TEST_F(BitmapFetcherServiceTest, OnlyFirstRequestCreatesFetcher) { |
142 EXPECT_EQ(0U, active_fetchers().size()); | 147 EXPECT_EQ(0U, active_fetchers().size()); |
143 | 148 |
144 service_->RequestImage(url1_, new TestObserver(this)); | 149 service_->RequestImage( |
| 150 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
145 EXPECT_EQ(1U, active_fetchers().size()); | 151 EXPECT_EQ(1U, active_fetchers().size()); |
146 | 152 |
147 service_->RequestImage(url1_, new TestObserver(this)); | 153 service_->RequestImage( |
| 154 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
148 EXPECT_EQ(1U, active_fetchers().size()); | 155 EXPECT_EQ(1U, active_fetchers().size()); |
149 } | 156 } |
150 | 157 |
151 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) { | 158 TEST_F(BitmapFetcherServiceTest, CompletedFetchNotifiesAllObservers) { |
152 service_->RequestImage(url1_, new TestObserver(this)); | 159 service_->RequestImage( |
153 service_->RequestImage(url1_, new TestObserver(this)); | 160 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
154 service_->RequestImage(url1_, new TestObserver(this)); | 161 service_->RequestImage( |
155 service_->RequestImage(url1_, new TestObserver(this)); | 162 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 163 service_->RequestImage( |
| 164 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 165 service_->RequestImage( |
| 166 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
156 EXPECT_EQ(1U, active_fetchers().size()); | 167 EXPECT_EQ(1U, active_fetchers().size()); |
157 EXPECT_EQ(4U, requests().size()); | 168 EXPECT_EQ(4U, requests().size()); |
158 | 169 |
159 CompleteFetch(url1_); | 170 CompleteFetch(url1_); |
160 EXPECT_EQ(4, images_changed_); | 171 EXPECT_EQ(4, images_changed_); |
161 EXPECT_EQ(4, requests_finished_); | 172 EXPECT_EQ(4, requests_finished_); |
162 } | 173 } |
163 | 174 |
164 TEST_F(BitmapFetcherServiceTest, CancelRequest) { | 175 TEST_F(BitmapFetcherServiceTest, CancelRequest) { |
165 service_->RequestImage(url1_, new TestObserver(this)); | 176 service_->RequestImage( |
166 service_->RequestImage(url1_, new TestObserver(this)); | 177 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 178 service_->RequestImage( |
| 179 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
167 BitmapFetcherService::RequestId requestId = | 180 BitmapFetcherService::RequestId requestId = |
168 service_->RequestImage(url2_, new TestObserver(this)); | 181 service_->RequestImage( |
169 service_->RequestImage(url1_, new TestObserver(this)); | 182 url2_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
170 service_->RequestImage(url1_, new TestObserver(this)); | 183 service_->RequestImage( |
| 184 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 185 service_->RequestImage( |
| 186 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
171 EXPECT_EQ(5U, requests().size()); | 187 EXPECT_EQ(5U, requests().size()); |
172 | 188 |
173 service_->CancelRequest(requestId); | 189 service_->CancelRequest(requestId); |
174 EXPECT_EQ(4U, requests().size()); | 190 EXPECT_EQ(4U, requests().size()); |
175 | 191 |
176 CompleteFetch(url2_); | 192 CompleteFetch(url2_); |
177 EXPECT_EQ(0, images_changed_); | 193 EXPECT_EQ(0, images_changed_); |
178 | 194 |
179 CompleteFetch(url1_); | 195 CompleteFetch(url1_); |
180 EXPECT_EQ(4, images_changed_); | 196 EXPECT_EQ(4, images_changed_); |
181 } | 197 } |
182 | 198 |
183 TEST_F(BitmapFetcherServiceTest, FailedNullRequestsAreHandled) { | 199 TEST_F(BitmapFetcherServiceTest, FailedNullRequestsAreHandled) { |
184 service_->RequestImage(url1_, new TestObserver(this)); | 200 service_->RequestImage( |
185 service_->RequestImage(url2_, new TestObserver(this)); | 201 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 202 service_->RequestImage( |
| 203 url2_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
186 EXPECT_EQ(0U, cache_size()); | 204 EXPECT_EQ(0U, cache_size()); |
187 | 205 |
188 CompleteFetch(url1_); | 206 CompleteFetch(url1_); |
189 EXPECT_EQ(1U, cache_size()); | 207 EXPECT_EQ(1U, cache_size()); |
190 | 208 |
191 FailDecode(url2_); | 209 FailDecode(url2_); |
192 EXPECT_EQ(1U, cache_size()); | 210 EXPECT_EQ(1U, cache_size()); |
193 } | 211 } |
194 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) { | 212 TEST_F(BitmapFetcherServiceTest, FailedRequestsDontEnterCache) { |
195 service_->RequestImage(url1_, new TestObserver(this)); | 213 service_->RequestImage( |
196 service_->RequestImage(url2_, new TestObserver(this)); | 214 url1_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
| 215 service_->RequestImage( |
| 216 url2_, new TestObserver(this), TRAFFIC_ANNOTATION_FOR_TESTS); |
197 EXPECT_EQ(0U, cache_size()); | 217 EXPECT_EQ(0U, cache_size()); |
198 | 218 |
199 CompleteFetch(url1_); | 219 CompleteFetch(url1_); |
200 EXPECT_EQ(1U, cache_size()); | 220 EXPECT_EQ(1U, cache_size()); |
201 | 221 |
202 FailFetch(url2_); | 222 FailFetch(url2_); |
203 EXPECT_EQ(1U, cache_size()); | 223 EXPECT_EQ(1U, cache_size()); |
204 } | 224 } |
OLD | NEW |