mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 501f801 | 2016-04-22 22:49:25 | [diff] [blame] | 5 | #include "components/suggestions/image_manager.h" |
| 6 | |
| 7 | #include <memory> |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 8 | #include <string> |
| 9 | |
| 10 | #include "base/files/file_path.h" |
danakj | 501f801 | 2016-04-22 22:49:25 | [diff] [blame] | 11 | #include "base/memory/ptr_util.h" |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 12 | #include "base/run_loop.h" |
fdoray | 0d83127 | 2017-04-06 16:50:19 | [diff] [blame] | 13 | #include "base/test/scoped_task_environment.h" |
mastiz | f4d088a | 2017-03-21 17:41:40 | [diff] [blame] | 14 | #include "components/image_fetcher/core/image_fetcher.h" |
Dan Harrington | 343f6d9f | 2018-03-19 19:11:16 | [diff] [blame] | 15 | #include "components/image_fetcher/core/mock_image_fetcher.h" |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 16 | #include "components/leveldb_proto/proto_database.h" |
| 17 | #include "components/leveldb_proto/testing/fake_db.h" |
mathp | 3377c626 | 2014-10-10 21:13:00 | [diff] [blame] | 18 | #include "components/suggestions/image_encoder.h" |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 19 | #include "components/suggestions/proto/suggestions.pb.h" |
rhalavati | 6072a6b7 | 2017-05-19 14:52:13 | [diff] [blame] | 20 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
tschumann | 30ee108 | 2017-03-02 19:37:53 | [diff] [blame] | 23 | #include "ui/gfx/geometry/size.h" |
markusheintz | 049dce3 | 2016-05-19 08:43:27 | [diff] [blame] | 24 | #include "ui/gfx/image/image.h" |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 25 | #include "url/gurl.h" |
| 26 | |
| 27 | using ::testing::Return; |
| 28 | using ::testing::StrictMock; |
| 29 | using ::testing::_; |
| 30 | |
Dan Harrington | 343f6d9f | 2018-03-19 19:11:16 | [diff] [blame] | 31 | using image_fetcher::MockImageFetcher; |
treib | 0a7f020 | 2016-04-29 11:39:08 | [diff] [blame] | 32 | |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 33 | namespace suggestions { |
| 34 | |
| 35 | const char kTestUrl1[] = "http://go.com/"; |
| 36 | const char kTestUrl2[] = "http://goal.com/"; |
| 37 | const char kTestImagePath[] = "files/image_decoding/droids.png"; |
| 38 | const char kInvalidImagePath[] = "files/DOESNOTEXIST"; |
| 39 | |
| 40 | using leveldb_proto::test::FakeDB; |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 41 | |
brettw | 1ce49f6 | 2017-04-27 19:42:32 | [diff] [blame] | 42 | typedef std::map<std::string, ImageData> EntryMap; |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 43 | |
Dan Harrington | 343f6d9f | 2018-03-19 19:11:16 | [diff] [blame] | 44 | void AddEntry(const ImageData& d, EntryMap* map) { |
| 45 | (*map)[d.url()] = d; |
| 46 | } |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 47 | |
| 48 | class ImageManagerTest : public testing::Test { |
| 49 | public: |
| 50 | ImageManagerTest() |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 51 | : mock_image_fetcher_(nullptr), |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 52 | num_callback_null_called_(0), |
| 53 | num_callback_valid_called_(0) {} |
| 54 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 55 | void SetUp() override { |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 56 | fake_db_ = new FakeDB<ImageData>(&db_model_); |
mathp | 60143a3 | 2014-10-08 14:52:45 | [diff] [blame] | 57 | image_manager_.reset(CreateImageManager(fake_db_)); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 58 | } |
| 59 | |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 60 | void TearDown() override { |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 61 | fake_db_ = nullptr; |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 62 | db_model_.clear(); |
| 63 | image_manager_.reset(); |
| 64 | } |
| 65 | |
| 66 | void InitializeDefaultImageMapAndDatabase(ImageManager* image_manager, |
| 67 | FakeDB<ImageData>* fake_db) { |
| 68 | CHECK(image_manager); |
| 69 | CHECK(fake_db); |
| 70 | |
| 71 | suggestions::SuggestionsProfile suggestions_profile; |
| 72 | suggestions::ChromeSuggestion* suggestion = |
| 73 | suggestions_profile.add_suggestions(); |
| 74 | suggestion->set_url(kTestUrl1); |
| 75 | suggestion->set_thumbnail(kTestImagePath); |
| 76 | |
| 77 | image_manager->Initialize(suggestions_profile); |
| 78 | |
| 79 | // Initialize empty database. |
| 80 | fake_db->InitCallback(true); |
| 81 | fake_db->LoadCallback(true); |
| 82 | } |
| 83 | |
| 84 | ImageData GetSampleImageData(const std::string& url) { |
| 85 | // Create test bitmap. |
| 86 | SkBitmap bm; |
| 87 | // Being careful with the Bitmap. There are memory-related issue in |
| 88 | // crbug.com/101781. |
| 89 | bm.allocN32Pixels(4, 4); |
| 90 | bm.eraseColor(SK_ColorRED); |
| 91 | ImageData data; |
| 92 | data.set_url(url); |
| 93 | std::vector<unsigned char> encoded; |
mathp | a27b85e | 2014-10-10 19:19:57 | [diff] [blame] | 94 | EXPECT_TRUE(EncodeSkBitmapToJPEG(bm, &encoded)); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 95 | data.set_data(std::string(encoded.begin(), encoded.end())); |
| 96 | return data; |
| 97 | } |
| 98 | |
Dan Harrington | 343f6d9f | 2018-03-19 19:11:16 | [diff] [blame] | 99 | void OnImageAvailable(base::RunLoop* loop, |
| 100 | const GURL& url, |
markusheintz | 55ae116 | 2016-05-25 08:20:57 | [diff] [blame] | 101 | const gfx::Image& image) { |
| 102 | if (!image.IsEmpty()) { |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 103 | num_callback_valid_called_++; |
| 104 | } else { |
| 105 | num_callback_null_called_++; |
| 106 | } |
| 107 | loop->Quit(); |
| 108 | } |
| 109 | |
mathp | 60143a3 | 2014-10-08 14:52:45 | [diff] [blame] | 110 | ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 111 | mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); |
danakj | 501f801 | 2016-04-22 22:49:25 | [diff] [blame] | 112 | return new ImageManager(base::WrapUnique(mock_image_fetcher_), |
| 113 | base::WrapUnique(fake_db), |
Marc Treib | fa8e035a | 2017-06-16 12:15:47 | [diff] [blame] | 114 | FakeDB<ImageData>::DirectoryForTestDB()); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | EntryMap db_model_; |
| 118 | // Owned by the ImageManager under test. |
| 119 | FakeDB<ImageData>* fake_db_; |
| 120 | |
| 121 | MockImageFetcher* mock_image_fetcher_; |
| 122 | |
| 123 | int num_callback_null_called_; |
| 124 | int num_callback_valid_called_; |
dskiba | 960b00e | 2015-06-11 22:44:46 | [diff] [blame] | 125 | |
fdoray | 0d83127 | 2017-04-06 16:50:19 | [diff] [blame] | 126 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
dskiba | 960b00e | 2015-06-11 22:44:46 | [diff] [blame] | 127 | |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 128 | // Under test. |
danakj | 501f801 | 2016-04-22 22:49:25 | [diff] [blame] | 129 | std::unique_ptr<ImageManager> image_manager_; |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | TEST_F(ImageManagerTest, InitializeTest) { |
| 133 | SuggestionsProfile suggestions_profile; |
| 134 | ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 135 | suggestion->set_url(kTestUrl1); |
| 136 | suggestion->set_thumbnail(kTestImagePath); |
| 137 | |
| 138 | image_manager_->Initialize(suggestions_profile); |
| 139 | |
| 140 | GURL output; |
| 141 | EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl1), &output)); |
| 142 | EXPECT_EQ(GURL(kTestImagePath), output); |
| 143 | |
| 144 | EXPECT_FALSE(image_manager_->GetImageURL(GURL("http://b.com"), &output)); |
| 145 | } |
| 146 | |
treib | ac5372f | 2015-09-09 09:08:22 | [diff] [blame] | 147 | TEST_F(ImageManagerTest, AddImageURL) { |
| 148 | InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); |
| 149 | |
| 150 | GURL output; |
| 151 | // Try a URL the ImageManager doesn't know about. |
| 152 | ASSERT_FALSE(image_manager_->GetImageURL(GURL(kTestUrl2), &output)); |
| 153 | |
| 154 | // Explicitly add the URL and try again. |
| 155 | image_manager_->AddImageURL(GURL(kTestUrl2), GURL(kTestImagePath)); |
| 156 | EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl2), &output)); |
| 157 | EXPECT_EQ(GURL(kTestImagePath), output); |
| 158 | } |
| 159 | |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 160 | TEST_F(ImageManagerTest, GetImageForURLNetwork) { |
| 161 | InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); |
| 162 | |
| 163 | // We expect the fetcher to go to network and call the callback. |
Dan Harrington | 343f6d9f | 2018-03-19 19:11:16 | [diff] [blame] | 164 | EXPECT_CALL(*mock_image_fetcher_, FetchImageAndData_(_, _, _, _, _)); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 165 | |
| 166 | // Fetch existing URL. |
| 167 | base::RunLoop run_loop; |
| 168 | image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 169 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 170 | base::Unretained(this), &run_loop)); |
| 171 | |
| 172 | // Will not go to network and use the fetcher since URL is invalid. |
| 173 | // Fetch non-existing URL. |
| 174 | image_manager_->GetImageForURL(GURL(kTestUrl2), |
| 175 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 176 | base::Unretained(this), &run_loop)); |
| 177 | run_loop.Run(); |
| 178 | |
| 179 | EXPECT_EQ(1, num_callback_null_called_); |
| 180 | } |
| 181 | |
| 182 | TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { |
| 183 | SuggestionsProfile suggestions_profile; |
| 184 | ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 185 | suggestion->set_url(kTestUrl1); |
| 186 | // The URL we set is invalid, to show that it will fail from network. |
| 187 | suggestion->set_thumbnail(kInvalidImagePath); |
| 188 | |
| 189 | // Create the ImageManager with an added entry in the database. |
| 190 | AddEntry(GetSampleImageData(kTestUrl1), &db_model_); |
| 191 | FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); |
mathp | 60143a3 | 2014-10-08 14:52:45 | [diff] [blame] | 192 | image_manager_.reset(CreateImageManager(fake_db)); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 193 | image_manager_->Initialize(suggestions_profile); |
| 194 | fake_db->InitCallback(true); |
| 195 | fake_db->LoadCallback(true); |
| 196 | // Expect something in the cache. |
dskiba | 960b00e | 2015-06-11 22:44:46 | [diff] [blame] | 197 | auto encoded_image = |
| 198 | image_manager_->GetEncodedImageFromCache(GURL(kTestUrl1)); |
scheib | 4dac7f0 | 2016-05-12 00:55:03 | [diff] [blame] | 199 | EXPECT_TRUE(encoded_image); |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 200 | |
| 201 | base::RunLoop run_loop; |
| 202 | image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 203 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 204 | base::Unretained(this), &run_loop)); |
| 205 | run_loop.Run(); |
| 206 | |
| 207 | EXPECT_EQ(0, num_callback_null_called_); |
| 208 | EXPECT_EQ(1, num_callback_valid_called_); |
| 209 | } |
| 210 | |
Siddhartha | 68c5d49 | 2018-02-12 21:00:10 | [diff] [blame] | 211 | TEST_F(ImageManagerTest, QueueImageRequest) { |
| 212 | SuggestionsProfile suggestions_profile; |
| 213 | ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 214 | suggestion->set_url(kTestUrl1); |
| 215 | // The URL we set is invalid, to show that it will fail from network. |
| 216 | suggestion->set_thumbnail(kInvalidImagePath); |
| 217 | |
| 218 | // Create the ImageManager with an added entry in the database. |
| 219 | AddEntry(GetSampleImageData(kTestUrl1), &db_model_); |
| 220 | FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); |
| 221 | image_manager_.reset(CreateImageManager(fake_db)); |
| 222 | image_manager_->Initialize(suggestions_profile); |
| 223 | |
| 224 | base::RunLoop run_loop1; |
| 225 | base::RunLoop run_loop2; |
| 226 | image_manager_->GetImageForURL( |
| 227 | GURL(kTestUrl1), base::BindRepeating(&ImageManagerTest::OnImageAvailable, |
| 228 | base::Unretained(this), &run_loop1)); |
| 229 | image_manager_->GetImageForURL( |
| 230 | GURL(kTestUrl1), base::BindRepeating(&ImageManagerTest::OnImageAvailable, |
| 231 | base::Unretained(this), &run_loop2)); |
| 232 | base::RunLoop().RunUntilIdle(); |
| 233 | |
| 234 | EXPECT_EQ(0, num_callback_null_called_); |
| 235 | EXPECT_EQ(0, num_callback_valid_called_); |
| 236 | EXPECT_EQ(1u, image_manager_->pending_cache_requests_.size()); |
| 237 | EXPECT_EQ( |
| 238 | 2u, |
| 239 | image_manager_->pending_cache_requests_.begin()->second.callbacks.size()); |
| 240 | |
| 241 | fake_db->InitCallback(true); |
| 242 | fake_db->LoadCallback(true); |
| 243 | run_loop1.Run(); |
| 244 | run_loop2.Run(); |
| 245 | |
| 246 | EXPECT_EQ(0, num_callback_null_called_); |
| 247 | EXPECT_EQ(2, num_callback_valid_called_); |
| 248 | EXPECT_EQ(0u, image_manager_->pending_cache_requests_.size()); |
| 249 | } |
| 250 | |
mathp | c3c8b0e | 2014-09-29 15:25:13 | [diff] [blame] | 251 | } // namespace suggestions |