| 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_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 bm.allocN32Pixels(2, 2); | |
| 89 ImageData data; | |
| 90 data.set_url(url); | |
| 91 std::vector<unsigned char> encoded; | |
| 92 EXPECT_TRUE(ImageManager::EncodeImage(bm, &encoded)); | |
| 93 data.set_data(std::string(encoded.begin(), encoded.end())); | |
| 94 return data; | |
| 95 } | |
| 96 | |
| 97 void OnImageAvailable(base::RunLoop* loop, const GURL& url, | |
| 98 const SkBitmap* bitmap) { | |
| 99 if (bitmap) { | |
| 100 num_callback_valid_called_++; | |
| 101 } else { | |
| 102 num_callback_null_called_++; | |
| 103 } | |
| 104 loop->Quit(); | |
| 105 } | |
| 106 | |
| 107 ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { | |
| 108 mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); | |
| 109 EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); | |
| 110 return new ImageManager( | |
| 111 scoped_ptr<ImageFetcher>(mock_image_fetcher_), | |
| 112 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), | |
| 113 FakeDB<ImageData>::DirectoryForTestDB()); | |
| 114 } | |
| 115 | |
| 116 EntryMap db_model_; | |
| 117 // Owned by the ImageManager under test. | |
| 118 FakeDB<ImageData>* fake_db_; | |
| 119 | |
| 120 MockImageFetcher* mock_image_fetcher_; | |
| 121 | |
| 122 int num_callback_null_called_; | |
| 123 int num_callback_valid_called_; | |
| 124 // Under test. | |
| 125 scoped_ptr<ImageManager> image_manager_; | |
| 126 }; | |
| 127 | |
| 128 TEST_F(ImageManagerTest, InitializeTest) { | |
| 129 SuggestionsProfile suggestions_profile; | |
| 130 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | |
| 131 suggestion->set_url(kTestUrl1); | |
| 132 suggestion->set_thumbnail(kTestImagePath); | |
| 133 | |
| 134 image_manager_->Initialize(suggestions_profile); | |
| 135 | |
| 136 GURL output; | |
| 137 EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl1), &output)); | |
| 138 EXPECT_EQ(GURL(kTestImagePath), output); | |
| 139 | |
| 140 EXPECT_FALSE(image_manager_->GetImageURL(GURL("http://b.com"), &output)); | |
| 141 } | |
| 142 | |
| 143 TEST_F(ImageManagerTest, GetImageForURLNetwork) { | |
| 144 InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); | |
| 145 | |
| 146 // We expect the fetcher to go to network and call the callback. | |
| 147 EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)); | |
| 148 | |
| 149 // Fetch existing URL. | |
| 150 base::RunLoop run_loop; | |
| 151 image_manager_->GetImageForURL(GURL(kTestUrl1), | |
| 152 base::Bind(&ImageManagerTest::OnImageAvailable, | |
| 153 base::Unretained(this), &run_loop)); | |
| 154 | |
| 155 // Will not go to network and use the fetcher since URL is invalid. | |
| 156 // Fetch non-existing URL. | |
| 157 image_manager_->GetImageForURL(GURL(kTestUrl2), | |
| 158 base::Bind(&ImageManagerTest::OnImageAvailable, | |
| 159 base::Unretained(this), &run_loop)); | |
| 160 run_loop.Run(); | |
| 161 | |
| 162 EXPECT_EQ(1, num_callback_null_called_); | |
| 163 } | |
| 164 | |
| 165 TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { | |
| 166 SuggestionsProfile suggestions_profile; | |
| 167 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | |
| 168 suggestion->set_url(kTestUrl1); | |
| 169 // The URL we set is invalid, to show that it will fail from network. | |
| 170 suggestion->set_thumbnail(kInvalidImagePath); | |
| 171 | |
| 172 // Create the ImageManager with an added entry in the database. | |
| 173 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); | |
| 174 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); | |
| 175 image_manager_.reset(CreateImageManager(fake_db)); | |
| 176 image_manager_->Initialize(suggestions_profile); | |
| 177 fake_db->InitCallback(true); | |
| 178 fake_db->LoadCallback(true); | |
| 179 // Expect something in the cache. | |
| 180 SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); | |
| 181 EXPECT_FALSE(bitmap->isNull()); | |
| 182 | |
| 183 base::RunLoop run_loop; | |
| 184 image_manager_->GetImageForURL(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 |