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