| 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 |
| 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 "base/thread_task_runner_handle.h" |
| 9 #include "components/leveldb_proto/proto_database.h" | 10 #include "components/leveldb_proto/proto_database.h" |
| 10 #include "components/leveldb_proto/testing/fake_db.h" | 11 #include "components/leveldb_proto/testing/fake_db.h" |
| 11 #include "components/suggestions/image_encoder.h" | 12 #include "components/suggestions/image_encoder.h" |
| 12 #include "components/suggestions/image_fetcher.h" | 13 #include "components/suggestions/image_fetcher.h" |
| 13 #include "components/suggestions/image_fetcher_delegate.h" | 14 #include "components/suggestions/image_fetcher_delegate.h" |
| 14 #include "components/suggestions/image_manager.h" | 15 #include "components/suggestions/image_manager.h" |
| 15 #include "components/suggestions/proto/suggestions.pb.h" | 16 #include "components/suggestions/proto/suggestions.pb.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/gfx/image/image_skia.h" | 19 #include "ui/gfx/image/image_skia.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 num_callback_null_called_++; | 107 num_callback_null_called_++; |
| 107 } | 108 } |
| 108 loop->Quit(); | 109 loop->Quit(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { | 112 ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { |
| 112 mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); | 113 mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); |
| 113 EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); | 114 EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); |
| 114 return new ImageManager( | 115 return new ImageManager( |
| 115 scoped_ptr<ImageFetcher>(mock_image_fetcher_), | 116 scoped_ptr<ImageFetcher>(mock_image_fetcher_), |
| 116 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), | 117 scoped_ptr<leveldb_proto::ProtoDatabase<ImageData>>(fake_db), |
| 117 FakeDB<ImageData>::DirectoryForTestDB()); | 118 FakeDB<ImageData>::DirectoryForTestDB(), |
| 119 base::ThreadTaskRunnerHandle::Get()); |
| 118 } | 120 } |
| 119 | 121 |
| 120 EntryMap db_model_; | 122 EntryMap db_model_; |
| 121 // Owned by the ImageManager under test. | 123 // Owned by the ImageManager under test. |
| 122 FakeDB<ImageData>* fake_db_; | 124 FakeDB<ImageData>* fake_db_; |
| 123 | 125 |
| 124 MockImageFetcher* mock_image_fetcher_; | 126 MockImageFetcher* mock_image_fetcher_; |
| 125 | 127 |
| 126 int num_callback_null_called_; | 128 int num_callback_null_called_; |
| 127 int num_callback_valid_called_; | 129 int num_callback_valid_called_; |
| 130 |
| 131 base::MessageLoop message_loop_; |
| 132 |
| 128 // Under test. | 133 // Under test. |
| 129 scoped_ptr<ImageManager> image_manager_; | 134 scoped_ptr<ImageManager> image_manager_; |
| 130 }; | 135 }; |
| 131 | 136 |
| 132 TEST_F(ImageManagerTest, InitializeTest) { | 137 TEST_F(ImageManagerTest, InitializeTest) { |
| 133 SuggestionsProfile suggestions_profile; | 138 SuggestionsProfile suggestions_profile; |
| 134 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 139 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 135 suggestion->set_url(kTestUrl1); | 140 suggestion->set_url(kTestUrl1); |
| 136 suggestion->set_thumbnail(kTestImagePath); | 141 suggestion->set_thumbnail(kTestImagePath); |
| 137 | 142 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 suggestion->set_thumbnail(kInvalidImagePath); | 179 suggestion->set_thumbnail(kInvalidImagePath); |
| 175 | 180 |
| 176 // Create the ImageManager with an added entry in the database. | 181 // Create the ImageManager with an added entry in the database. |
| 177 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); | 182 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); |
| 178 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); | 183 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); |
| 179 image_manager_.reset(CreateImageManager(fake_db)); | 184 image_manager_.reset(CreateImageManager(fake_db)); |
| 180 image_manager_->Initialize(suggestions_profile); | 185 image_manager_->Initialize(suggestions_profile); |
| 181 fake_db->InitCallback(true); | 186 fake_db->InitCallback(true); |
| 182 fake_db->LoadCallback(true); | 187 fake_db->LoadCallback(true); |
| 183 // Expect something in the cache. | 188 // Expect something in the cache. |
| 184 SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); | 189 auto encoded_image = |
| 185 EXPECT_FALSE(bitmap->isNull()); | 190 image_manager_->GetEncodedImageFromCache(GURL(kTestUrl1)); |
| 191 EXPECT_NE(nullptr, encoded_image); |
| 186 | 192 |
| 187 base::RunLoop run_loop; | 193 base::RunLoop run_loop; |
| 188 image_manager_->GetImageForURL(GURL(kTestUrl1), | 194 image_manager_->GetImageForURL(GURL(kTestUrl1), |
| 189 base::Bind(&ImageManagerTest::OnImageAvailable, | 195 base::Bind(&ImageManagerTest::OnImageAvailable, |
| 190 base::Unretained(this), &run_loop)); | 196 base::Unretained(this), &run_loop)); |
| 191 run_loop.Run(); | 197 run_loop.Run(); |
| 192 | 198 |
| 193 EXPECT_EQ(0, num_callback_null_called_); | 199 EXPECT_EQ(0, num_callback_null_called_); |
| 194 EXPECT_EQ(1, num_callback_valid_called_); | 200 EXPECT_EQ(1, num_callback_valid_called_); |
| 195 } | 201 } |
| 196 | 202 |
| 197 } // namespace suggestions | 203 } // namespace suggestions |
| OLD | NEW |