Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
|
huangs
2014/10/06 17:14:18
#include <vector>
Mathieu
2014/10/06 18:13:35
Done.
| |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "components/leveldb_proto/proto_database.h" | 9 #include "components/leveldb_proto/proto_database.h" |
| 10 #include "components/leveldb_proto/testing/fake_db.h" | 10 #include "components/leveldb_proto/testing/fake_db.h" |
| 11 #include "components/suggestions/image_encoder.h" | |
| 11 #include "components/suggestions/image_fetcher.h" | 12 #include "components/suggestions/image_fetcher.h" |
| 12 #include "components/suggestions/image_fetcher_delegate.h" | 13 #include "components/suggestions/image_fetcher_delegate.h" |
| 13 #include "components/suggestions/image_manager.h" | 14 #include "components/suggestions/image_manager.h" |
| 14 #include "components/suggestions/proto/suggestions.pb.h" | 15 #include "components/suggestions/proto/suggestions.pb.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "ui/gfx/image/image_skia.h" | 18 #include "ui/gfx/image/image_skia.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 using ::testing::Return; | 21 using ::testing::Return; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 39 class MockImageFetcher : public suggestions::ImageFetcher { | 40 class MockImageFetcher : public suggestions::ImageFetcher { |
| 40 public: | 41 public: |
| 41 MockImageFetcher() {} | 42 MockImageFetcher() {} |
| 42 virtual ~MockImageFetcher() {} | 43 virtual ~MockImageFetcher() {} |
| 43 MOCK_METHOD3(StartOrQueueNetworkRequest, | 44 MOCK_METHOD3(StartOrQueueNetworkRequest, |
| 44 void(const GURL&, const GURL&, | 45 void(const GURL&, const GURL&, |
| 45 base::Callback<void(const GURL&, const SkBitmap*)>)); | 46 base::Callback<void(const GURL&, const SkBitmap*)>)); |
| 46 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 47 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
| 47 }; | 48 }; |
| 48 | 49 |
| 50 class MockImageEncoder : public suggestions::ImageEncoder { | |
| 51 public: | |
| 52 MockImageEncoder() {} | |
| 53 virtual ~MockImageEncoder() {} | |
| 54 MOCK_METHOD1(DecodeImage, | |
| 55 SkBitmap*(const std::vector<unsigned char>&)); | |
| 56 MOCK_METHOD2(EncodeImage, bool(const SkBitmap&, std::vector<unsigned char>*)); | |
| 57 }; | |
| 58 | |
| 49 class ImageManagerTest : public testing::Test { | 59 class ImageManagerTest : public testing::Test { |
| 50 public: | 60 public: |
| 51 ImageManagerTest() | 61 ImageManagerTest() |
| 52 : mock_image_fetcher_(NULL), | 62 : mock_image_fetcher_(NULL), |
| 53 num_callback_null_called_(0), | 63 num_callback_null_called_(0), |
| 54 num_callback_valid_called_(0) {} | 64 num_callback_valid_called_(0) {} |
| 55 | 65 |
| 56 virtual void SetUp() OVERRIDE { | 66 virtual void SetUp() OVERRIDE { |
| 57 fake_db_ = new FakeDB<ImageData>(&db_model_); | 67 fake_db_ = new FakeDB<ImageData>(&db_model_); |
| 58 image_manager_.reset(CreateImageManager(fake_db_)); | 68 image_encoder_ = new ImageEncoder(); |
| 69 image_manager_.reset(CreateImageManager(fake_db_, image_encoder_)); | |
| 59 } | 70 } |
| 60 | 71 |
| 61 virtual void TearDown() OVERRIDE { | 72 virtual void TearDown() OVERRIDE { |
| 62 fake_db_ = NULL; | 73 fake_db_ = NULL; |
| 63 db_model_.clear(); | 74 db_model_.clear(); |
| 64 image_manager_.reset(); | 75 image_manager_.reset(); |
| 65 } | 76 } |
| 66 | 77 |
| 67 void InitializeDefaultImageMapAndDatabase(ImageManager* image_manager, | 78 void InitializeDefaultImageMapAndDatabase(ImageManager* image_manager, |
| 68 FakeDB<ImageData>* fake_db) { | 79 FakeDB<ImageData>* fake_db) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 85 ImageData GetSampleImageData(const std::string& url) { | 96 ImageData GetSampleImageData(const std::string& url) { |
| 86 // Create test bitmap. | 97 // Create test bitmap. |
| 87 SkBitmap bm; | 98 SkBitmap bm; |
| 88 // Being careful with the Bitmap. There are memory-related issue in | 99 // Being careful with the Bitmap. There are memory-related issue in |
| 89 // crbug.com/101781. | 100 // crbug.com/101781. |
| 90 bm.allocN32Pixels(4, 4); | 101 bm.allocN32Pixels(4, 4); |
| 91 bm.eraseColor(SK_ColorRED); | 102 bm.eraseColor(SK_ColorRED); |
| 92 ImageData data; | 103 ImageData data; |
| 93 data.set_url(url); | 104 data.set_url(url); |
| 94 std::vector<unsigned char> encoded; | 105 std::vector<unsigned char> encoded; |
| 95 EXPECT_TRUE(ImageManager::EncodeImage(bm, &encoded)); | 106 EXPECT_TRUE(image_encoder_->EncodeImage(bm, &encoded)); |
| 96 data.set_data(std::string(encoded.begin(), encoded.end())); | 107 data.set_data(std::string(encoded.begin(), encoded.end())); |
| 97 return data; | 108 return data; |
| 98 } | 109 } |
| 99 | 110 |
| 100 void OnImageAvailable(base::RunLoop* loop, const GURL& url, | 111 void OnImageAvailable(base::RunLoop* loop, const GURL& url, |
| 101 const SkBitmap* bitmap) { | 112 const SkBitmap* bitmap) { |
| 102 if (bitmap) { | 113 if (bitmap) { |
| 103 num_callback_valid_called_++; | 114 num_callback_valid_called_++; |
| 104 } else { | 115 } else { |
| 105 num_callback_null_called_++; | 116 num_callback_null_called_++; |
| 106 } | 117 } |
| 107 loop->Quit(); | 118 loop->Quit(); |
| 108 } | 119 } |
| 109 | 120 |
| 110 ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { | 121 ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db, |
| 122 ImageEncoder* image_encoder) { | |
| 111 mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); | 123 mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); |
| 112 EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); | 124 EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); |
| 113 return new ImageManager( | 125 return new ImageManager( |
| 114 scoped_ptr<ImageFetcher>(mock_image_fetcher_), | 126 scoped_ptr<ImageFetcher>(mock_image_fetcher_), |
| 127 scoped_ptr<ImageEncoder>(image_encoder), | |
| 115 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), | 128 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), |
| 116 FakeDB<ImageData>::DirectoryForTestDB()); | 129 FakeDB<ImageData>::DirectoryForTestDB()); |
| 117 } | 130 } |
| 118 | 131 |
| 119 EntryMap db_model_; | 132 EntryMap db_model_; |
| 120 // Owned by the ImageManager under test. | 133 // Owned by the ImageManager under test. |
| 121 FakeDB<ImageData>* fake_db_; | 134 FakeDB<ImageData>* fake_db_; |
| 135 ImageEncoder* image_encoder_; | |
| 122 | 136 |
| 123 MockImageFetcher* mock_image_fetcher_; | 137 MockImageFetcher* mock_image_fetcher_; |
| 124 | 138 |
| 125 int num_callback_null_called_; | 139 int num_callback_null_called_; |
| 126 int num_callback_valid_called_; | 140 int num_callback_valid_called_; |
| 127 // Under test. | 141 // Under test. |
| 128 scoped_ptr<ImageManager> image_manager_; | 142 scoped_ptr<ImageManager> image_manager_; |
| 129 }; | 143 }; |
| 130 | 144 |
| 131 TEST_F(ImageManagerTest, InitializeTest) { | 145 TEST_F(ImageManagerTest, InitializeTest) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { | 182 TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { |
| 169 SuggestionsProfile suggestions_profile; | 183 SuggestionsProfile suggestions_profile; |
| 170 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 184 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 171 suggestion->set_url(kTestUrl1); | 185 suggestion->set_url(kTestUrl1); |
| 172 // The URL we set is invalid, to show that it will fail from network. | 186 // The URL we set is invalid, to show that it will fail from network. |
| 173 suggestion->set_thumbnail(kInvalidImagePath); | 187 suggestion->set_thumbnail(kInvalidImagePath); |
| 174 | 188 |
| 175 // Create the ImageManager with an added entry in the database. | 189 // Create the ImageManager with an added entry in the database. |
| 176 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); | 190 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); |
| 177 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); | 191 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); |
| 178 image_manager_.reset(CreateImageManager(fake_db)); | 192 ImageEncoder* image_encoder = new ImageEncoder(); |
| 193 image_manager_.reset(CreateImageManager(fake_db, image_encoder)); | |
|
huangs
2014/10/06 17:14:18
Inline rather than create temp variable, since own
Mathieu
2014/10/06 18:13:35
Done.
| |
| 179 image_manager_->Initialize(suggestions_profile); | 194 image_manager_->Initialize(suggestions_profile); |
| 180 fake_db->InitCallback(true); | 195 fake_db->InitCallback(true); |
| 181 fake_db->LoadCallback(true); | 196 fake_db->LoadCallback(true); |
| 182 // Expect something in the cache. | 197 // Expect something in the cache. |
| 183 SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); | 198 SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); |
| 184 EXPECT_FALSE(bitmap->isNull()); | 199 EXPECT_FALSE(bitmap->isNull()); |
| 185 | 200 |
| 186 base::RunLoop run_loop; | 201 base::RunLoop run_loop; |
| 187 image_manager_->GetImageForURL(GURL(kTestUrl1), | 202 image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 188 base::Bind(&ImageManagerTest::OnImageAvailable, | 203 base::Bind(&ImageManagerTest::OnImageAvailable, |
| 189 base::Unretained(this), &run_loop)); | 204 base::Unretained(this), &run_loop)); |
| 190 run_loop.Run(); | 205 run_loop.Run(); |
| 191 | 206 |
| 192 EXPECT_EQ(0, num_callback_null_called_); | 207 EXPECT_EQ(0, num_callback_null_called_); |
| 193 EXPECT_EQ(1, num_callback_valid_called_); | 208 EXPECT_EQ(1, num_callback_valid_called_); |
| 194 } | 209 } |
| 195 | 210 |
| 196 } // namespace suggestions | 211 } // namespace suggestions |
| OLD | NEW |