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/suggestions/image_manager.h" | 5 #include "components/suggestions/image_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/test/scoped_task_environment.h" | 13 #include "base/test/scoped_task_environment.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "components/image_fetcher/core/image_fetcher.h" | 15 #include "components/image_fetcher/core/image_fetcher.h" |
16 #include "components/image_fetcher/core/image_fetcher_delegate.h" | 16 #include "components/image_fetcher/core/image_fetcher_delegate.h" |
17 #include "components/leveldb_proto/proto_database.h" | 17 #include "components/leveldb_proto/proto_database.h" |
18 #include "components/leveldb_proto/testing/fake_db.h" | 18 #include "components/leveldb_proto/testing/fake_db.h" |
19 #include "components/suggestions/image_encoder.h" | 19 #include "components/suggestions/image_encoder.h" |
20 #include "components/suggestions/proto/suggestions.pb.h" | 20 #include "components/suggestions/proto/suggestions.pb.h" |
| 21 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "ui/gfx/geometry/size.h" | 24 #include "ui/gfx/geometry/size.h" |
24 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
25 #include "ui/gfx/image/image_skia.h" | 26 #include "ui/gfx/image/image_skia.h" |
26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
27 | 28 |
28 using ::testing::Return; | 29 using ::testing::Return; |
29 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
30 using ::testing::_; | 31 using ::testing::_; |
(...skipping 11 matching lines...) Expand all Loading... |
42 using leveldb_proto::test::FakeDB; | 43 using leveldb_proto::test::FakeDB; |
43 | 44 |
44 typedef std::map<std::string, ImageData> EntryMap; | 45 typedef std::map<std::string, ImageData> EntryMap; |
45 | 46 |
46 void AddEntry(const ImageData& d, EntryMap* map) { (*map)[d.url()] = d; } | 47 void AddEntry(const ImageData& d, EntryMap* map) { (*map)[d.url()] = d; } |
47 | 48 |
48 class MockImageFetcher : public ImageFetcher { | 49 class MockImageFetcher : public ImageFetcher { |
49 public: | 50 public: |
50 MockImageFetcher() {} | 51 MockImageFetcher() {} |
51 virtual ~MockImageFetcher() {} | 52 virtual ~MockImageFetcher() {} |
52 MOCK_METHOD3(StartOrQueueNetworkRequest, | 53 MOCK_METHOD4(StartOrQueueNetworkRequest, |
53 void(const std::string&, | 54 void(const std::string&, |
54 const GURL&, | 55 const GURL&, |
55 const ImageFetcherCallback&)); | 56 const ImageFetcherCallback&, |
| 57 const net::NetworkTrafficAnnotationTag&)); |
56 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 58 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
57 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | 59 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); |
58 MOCK_METHOD1(SetImageDownloadLimit, | 60 MOCK_METHOD1(SetImageDownloadLimit, |
59 void(base::Optional<int64_t> max_download_bytes)); | 61 void(base::Optional<int64_t> max_download_bytes)); |
60 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size&)); | 62 MOCK_METHOD1(SetDesiredImageFrameSize, void(const gfx::Size&)); |
61 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); | 63 MOCK_METHOD0(GetImageDecoder, image_fetcher::ImageDecoder*()); |
62 }; | 64 }; |
63 | 65 |
64 class ImageManagerTest : public testing::Test { | 66 class ImageManagerTest : public testing::Test { |
65 public: | 67 public: |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 // Explicitly add the URL and try again. | 173 // Explicitly add the URL and try again. |
172 image_manager_->AddImageURL(GURL(kTestUrl2), GURL(kTestImagePath)); | 174 image_manager_->AddImageURL(GURL(kTestUrl2), GURL(kTestImagePath)); |
173 EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl2), &output)); | 175 EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl2), &output)); |
174 EXPECT_EQ(GURL(kTestImagePath), output); | 176 EXPECT_EQ(GURL(kTestImagePath), output); |
175 } | 177 } |
176 | 178 |
177 TEST_F(ImageManagerTest, GetImageForURLNetwork) { | 179 TEST_F(ImageManagerTest, GetImageForURLNetwork) { |
178 InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); | 180 InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); |
179 | 181 |
180 // We expect the fetcher to go to network and call the callback. | 182 // We expect the fetcher to go to network and call the callback. |
181 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)); | 183 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _, _)); |
182 | 184 |
183 // Fetch existing URL. | 185 // Fetch existing URL. |
184 base::RunLoop run_loop; | 186 base::RunLoop run_loop; |
185 image_manager_->GetImageForURL(GURL(kTestUrl1), | 187 image_manager_->GetImageForURL(GURL(kTestUrl1), |
186 base::Bind(&ImageManagerTest::OnImageAvailable, | 188 base::Bind(&ImageManagerTest::OnImageAvailable, |
187 base::Unretained(this), &run_loop)); | 189 base::Unretained(this), &run_loop)); |
188 | 190 |
189 // Will not go to network and use the fetcher since URL is invalid. | 191 // Will not go to network and use the fetcher since URL is invalid. |
190 // Fetch non-existing URL. | 192 // Fetch non-existing URL. |
191 image_manager_->GetImageForURL(GURL(kTestUrl2), | 193 image_manager_->GetImageForURL(GURL(kTestUrl2), |
(...skipping 27 matching lines...) Expand all Loading... |
219 image_manager_->GetImageForURL(GURL(kTestUrl1), | 221 image_manager_->GetImageForURL(GURL(kTestUrl1), |
220 base::Bind(&ImageManagerTest::OnImageAvailable, | 222 base::Bind(&ImageManagerTest::OnImageAvailable, |
221 base::Unretained(this), &run_loop)); | 223 base::Unretained(this), &run_loop)); |
222 run_loop.Run(); | 224 run_loop.Run(); |
223 | 225 |
224 EXPECT_EQ(0, num_callback_null_called_); | 226 EXPECT_EQ(0, num_callback_null_called_); |
225 EXPECT_EQ(1, num_callback_valid_called_); | 227 EXPECT_EQ(1, num_callback_valid_called_); |
226 } | 228 } |
227 | 229 |
228 } // namespace suggestions | 230 } // namespace suggestions |
OLD | NEW |