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