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 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "base/files/file_path.h" |
| 8 | #include "base/run_loop.h" |
| 9 | #include "components/leveldb_proto/proto_database.h" |
| 10 | #include "components/leveldb_proto/testing/fake_db.h" |
| 11 | #include "components/suggestions/image_fetcher.h" |
| 12 | #include "components/suggestions/image_fetcher_delegate.h" |
| 13 | #include "components/suggestions/image_manager.h" |
| 14 | #include "components/suggestions/proto/suggestions.pb.h" |
| 15 | #include "testing/gmock/include/gmock/gmock.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | #include "ui/gfx/image/image_skia.h" |
| 18 | #include "url/gurl.h" |
| 19 | |
| 20 | using ::testing::Return; |
| 21 | using ::testing::StrictMock; |
| 22 | using ::testing::_; |
| 23 | |
| 24 | namespace suggestions { |
| 25 | |
| 26 | const char kTestUrl1[] = "http://go.com/"; |
| 27 | const char kTestUrl2[] = "http://goal.com/"; |
| 28 | const char kTestImagePath[] = "files/image_decoding/droids.png"; |
| 29 | const char kInvalidImagePath[] = "files/DOESNOTEXIST"; |
| 30 | |
| 31 | using leveldb_proto::test::FakeDB; |
| 32 | using suggestions::ImageData; |
| 33 | using suggestions::ImageManager; |
| 34 | |
| 35 | typedef base::hash_map<std::string, ImageData> EntryMap; |
| 36 | |
| 37 | void AddEntry(const ImageData& d, EntryMap* map) { (*map)[d.url()] = d; } |
| 38 | |
| 39 | class MockImageFetcher : public suggestions::ImageFetcher { |
| 40 | public: |
| 41 | MockImageFetcher() {} |
| 42 | virtual ~MockImageFetcher() {} |
| 43 | MOCK_METHOD3(StartOrQueueNetworkRequest, |
| 44 | void(const GURL&, const GURL&, |
| 45 | base::Callback<void(const GURL&, const SkBitmap*)>)); |
| 46 | MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
| 47 | }; |
| 48 | |
| 49 | class ImageManagerTest : public testing::Test { |
| 50 | public: |
| 51 | ImageManagerTest() |
| 52 | : mock_image_fetcher_(NULL), |
| 53 | num_callback_null_called_(0), |
| 54 | num_callback_valid_called_(0) {} |
| 55 | |
| 56 | virtual void SetUp() OVERRIDE { |
| 57 | fake_db_ = new FakeDB<ImageData>(&db_model_); |
| 58 | image_manager_.reset(CreateImageManager(fake_db_)); |
| 59 | } |
| 60 | |
| 61 | virtual void TearDown() OVERRIDE { |
| 62 | fake_db_ = NULL; |
| 63 | db_model_.clear(); |
| 64 | image_manager_.reset(); |
| 65 | } |
| 66 | |
| 67 | void InitializeDefaultImageMapAndDatabase(ImageManager* image_manager, |
| 68 | FakeDB<ImageData>* fake_db) { |
| 69 | CHECK(image_manager); |
| 70 | CHECK(fake_db); |
| 71 | |
| 72 | suggestions::SuggestionsProfile suggestions_profile; |
| 73 | suggestions::ChromeSuggestion* suggestion = |
| 74 | suggestions_profile.add_suggestions(); |
| 75 | suggestion->set_url(kTestUrl1); |
| 76 | suggestion->set_thumbnail(kTestImagePath); |
| 77 | |
| 78 | image_manager->Initialize(suggestions_profile); |
| 79 | |
| 80 | // Initialize empty database. |
| 81 | fake_db->InitCallback(true); |
| 82 | fake_db->LoadCallback(true); |
| 83 | } |
| 84 | |
| 85 | ImageData GetSampleImageData(const std::string& url) { |
| 86 | // Create test bitmap. |
| 87 | SkBitmap bm; |
| 88 | // Being careful with the Bitmap. There are memory-related issue in |
| 89 | // crbug.com/101781. |
| 90 | bm.allocN32Pixels(4, 4); |
| 91 | bm.eraseColor(SK_ColorRED); |
| 92 | ImageData data; |
| 93 | data.set_url(url); |
| 94 | std::vector<unsigned char> encoded; |
| 95 | EXPECT_TRUE(ImageManager::EncodeImage(bm, &encoded)); |
| 96 | data.set_data(std::string(encoded.begin(), encoded.end())); |
| 97 | return data; |
| 98 | } |
| 99 | |
| 100 | void OnImageAvailable(base::RunLoop* loop, const GURL& url, |
| 101 | const SkBitmap* bitmap) { |
| 102 | if (bitmap) { |
| 103 | num_callback_valid_called_++; |
| 104 | } else { |
| 105 | num_callback_null_called_++; |
| 106 | } |
| 107 | loop->Quit(); |
| 108 | } |
| 109 | |
| 110 | ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { |
| 111 | mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); |
| 112 | EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); |
| 113 | return new ImageManager( |
| 114 | scoped_ptr<ImageFetcher>(mock_image_fetcher_), |
| 115 | scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), |
| 116 | FakeDB<ImageData>::DirectoryForTestDB()); |
| 117 | } |
| 118 | |
| 119 | EntryMap db_model_; |
| 120 | // Owned by the ImageManager under test. |
| 121 | FakeDB<ImageData>* fake_db_; |
| 122 | |
| 123 | MockImageFetcher* mock_image_fetcher_; |
| 124 | |
| 125 | int num_callback_null_called_; |
| 126 | int num_callback_valid_called_; |
| 127 | // Under test. |
| 128 | scoped_ptr<ImageManager> image_manager_; |
| 129 | }; |
| 130 | |
| 131 | TEST_F(ImageManagerTest, InitializeTest) { |
| 132 | SuggestionsProfile suggestions_profile; |
| 133 | ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 134 | suggestion->set_url(kTestUrl1); |
| 135 | suggestion->set_thumbnail(kTestImagePath); |
| 136 | |
| 137 | image_manager_->Initialize(suggestions_profile); |
| 138 | |
| 139 | GURL output; |
| 140 | EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl1), &output)); |
| 141 | EXPECT_EQ(GURL(kTestImagePath), output); |
| 142 | |
| 143 | EXPECT_FALSE(image_manager_->GetImageURL(GURL("http://b.com"), &output)); |
| 144 | } |
| 145 | |
| 146 | TEST_F(ImageManagerTest, GetImageForURLNetwork) { |
| 147 | InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); |
| 148 | |
| 149 | // We expect the fetcher to go to network and call the callback. |
| 150 | EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)); |
| 151 | |
| 152 | // Fetch existing URL. |
| 153 | base::RunLoop run_loop; |
| 154 | image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 155 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 156 | base::Unretained(this), &run_loop)); |
| 157 | |
| 158 | // Will not go to network and use the fetcher since URL is invalid. |
| 159 | // Fetch non-existing URL. |
| 160 | image_manager_->GetImageForURL(GURL(kTestUrl2), |
| 161 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 162 | base::Unretained(this), &run_loop)); |
| 163 | run_loop.Run(); |
| 164 | |
| 165 | EXPECT_EQ(1, num_callback_null_called_); |
| 166 | } |
| 167 | |
| 168 | TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { |
| 169 | SuggestionsProfile suggestions_profile; |
| 170 | ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 171 | suggestion->set_url(kTestUrl1); |
| 172 | // The URL we set is invalid, to show that it will fail from network. |
| 173 | suggestion->set_thumbnail(kInvalidImagePath); |
| 174 | |
| 175 | // Create the ImageManager with an added entry in the database. |
| 176 | AddEntry(GetSampleImageData(kTestUrl1), &db_model_); |
| 177 | FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); |
| 178 | image_manager_.reset(CreateImageManager(fake_db)); |
| 179 | image_manager_->Initialize(suggestions_profile); |
| 180 | fake_db->InitCallback(true); |
| 181 | fake_db->LoadCallback(true); |
| 182 | // Expect something in the cache. |
| 183 | SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); |
| 184 | EXPECT_FALSE(bitmap->isNull()); |
| 185 | |
| 186 | base::RunLoop run_loop; |
| 187 | image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 188 | base::Bind(&ImageManagerTest::OnImageAvailable, |
| 189 | base::Unretained(this), &run_loop)); |
| 190 | run_loop.Run(); |
| 191 | |
| 192 | EXPECT_EQ(0, num_callback_null_called_); |
| 193 | EXPECT_EQ(1, num_callback_valid_called_); |
| 194 | } |
| 195 | |
| 196 | } // namespace suggestions |