Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: components/suggestions/image_manager_unittest.cc

Issue 1156003008: Decode ImageManager cached images on demand. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Include stddef to get size_t Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "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_encoder.h"
12 #include "components/suggestions/image_fetcher.h" 12 #include "components/suggestions/image_fetcher.h"
13 #include "components/suggestions/image_fetcher_delegate.h" 13 #include "components/suggestions/image_fetcher_delegate.h"
14 #include "components/suggestions/image_manager.h" 14 #include "components/suggestions/image_manager.h"
15 #include "components/suggestions/proto/suggestions.pb.h" 15 #include "components/suggestions/proto/suggestions.pb.h"
16 #include "content/public/test/test_browser_thread.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"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
21 using ::testing::Return; 22 using ::testing::Return;
22 using ::testing::StrictMock; 23 using ::testing::StrictMock;
23 using ::testing::_; 24 using ::testing::_;
25 using content::BrowserThread;
24 26
25 namespace suggestions { 27 namespace suggestions {
26 28
27 const char kTestUrl1[] = "http://go.com/"; 29 const char kTestUrl1[] = "http://go.com/";
28 const char kTestUrl2[] = "http://goal.com/"; 30 const char kTestUrl2[] = "http://goal.com/";
29 const char kTestImagePath[] = "files/image_decoding/droids.png"; 31 const char kTestImagePath[] = "files/image_decoding/droids.png";
30 const char kInvalidImagePath[] = "files/DOESNOTEXIST"; 32 const char kInvalidImagePath[] = "files/DOESNOTEXIST";
31 33
32 using leveldb_proto::test::FakeDB; 34 using leveldb_proto::test::FakeDB;
33 using suggestions::ImageData; 35 using suggestions::ImageData;
(...skipping 11 matching lines...) Expand all
45 void(const GURL&, const GURL&, 47 void(const GURL&, const GURL&,
46 base::Callback<void(const GURL&, const SkBitmap*)>)); 48 base::Callback<void(const GURL&, const SkBitmap*)>));
47 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); 49 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*));
48 }; 50 };
49 51
50 class ImageManagerTest : public testing::Test { 52 class ImageManagerTest : public testing::Test {
51 public: 53 public:
52 ImageManagerTest() 54 ImageManagerTest()
53 : mock_image_fetcher_(NULL), 55 : mock_image_fetcher_(NULL),
54 num_callback_null_called_(0), 56 num_callback_null_called_(0),
55 num_callback_valid_called_(0) {} 57 num_callback_valid_called_(0),
58 db_thread_(BrowserThread::DB) {
59 db_thread_.Start();
60 }
56 61
57 void SetUp() override { 62 void SetUp() override {
58 fake_db_ = new FakeDB<ImageData>(&db_model_); 63 fake_db_ = new FakeDB<ImageData>(&db_model_);
59 image_manager_.reset(CreateImageManager(fake_db_)); 64 image_manager_.reset(CreateImageManager(fake_db_));
60 } 65 }
61 66
62 void TearDown() override { 67 void TearDown() override {
63 fake_db_ = NULL; 68 fake_db_ = NULL;
64 db_model_.clear(); 69 db_model_.clear();
65 image_manager_.reset(); 70 image_manager_.reset();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 } 123 }
119 124
120 EntryMap db_model_; 125 EntryMap db_model_;
121 // Owned by the ImageManager under test. 126 // Owned by the ImageManager under test.
122 FakeDB<ImageData>* fake_db_; 127 FakeDB<ImageData>* fake_db_;
123 128
124 MockImageFetcher* mock_image_fetcher_; 129 MockImageFetcher* mock_image_fetcher_;
125 130
126 int num_callback_null_called_; 131 int num_callback_null_called_;
127 int num_callback_valid_called_; 132 int num_callback_valid_called_;
133
134 base::MessageLoop message_loop_;
135 content::TestBrowserThread db_thread_;
136
128 // Under test. 137 // Under test.
129 scoped_ptr<ImageManager> image_manager_; 138 scoped_ptr<ImageManager> image_manager_;
130 }; 139 };
131 140
132 TEST_F(ImageManagerTest, InitializeTest) { 141 TEST_F(ImageManagerTest, InitializeTest) {
133 SuggestionsProfile suggestions_profile; 142 SuggestionsProfile suggestions_profile;
134 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); 143 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions();
135 suggestion->set_url(kTestUrl1); 144 suggestion->set_url(kTestUrl1);
136 suggestion->set_thumbnail(kTestImagePath); 145 suggestion->set_thumbnail(kTestImagePath);
137 146
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 suggestion->set_thumbnail(kInvalidImagePath); 183 suggestion->set_thumbnail(kInvalidImagePath);
175 184
176 // Create the ImageManager with an added entry in the database. 185 // Create the ImageManager with an added entry in the database.
177 AddEntry(GetSampleImageData(kTestUrl1), &db_model_); 186 AddEntry(GetSampleImageData(kTestUrl1), &db_model_);
178 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); 187 FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_);
179 image_manager_.reset(CreateImageManager(fake_db)); 188 image_manager_.reset(CreateImageManager(fake_db));
180 image_manager_->Initialize(suggestions_profile); 189 image_manager_->Initialize(suggestions_profile);
181 fake_db->InitCallback(true); 190 fake_db->InitCallback(true);
182 fake_db->LoadCallback(true); 191 fake_db->LoadCallback(true);
183 // Expect something in the cache. 192 // Expect something in the cache.
184 SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); 193 auto encoded_image =
185 EXPECT_FALSE(bitmap->isNull()); 194 image_manager_->GetEncodedImageFromCache(GURL(kTestUrl1));
195 EXPECT_NE(nullptr, encoded_image);
186 196
187 base::RunLoop run_loop; 197 base::RunLoop run_loop;
188 image_manager_->GetImageForURL(GURL(kTestUrl1), 198 image_manager_->GetImageForURL(GURL(kTestUrl1),
189 base::Bind(&ImageManagerTest::OnImageAvailable, 199 base::Bind(&ImageManagerTest::OnImageAvailable,
190 base::Unretained(this), &run_loop)); 200 base::Unretained(this), &run_loop));
191 run_loop.Run(); 201 run_loop.Run();
192 202
193 EXPECT_EQ(0, num_callback_null_called_); 203 EXPECT_EQ(0, num_callback_null_called_);
194 EXPECT_EQ(1, num_callback_valid_called_); 204 EXPECT_EQ(1, num_callback_valid_called_);
195 } 205 }
196 206
197 } // namespace suggestions 207 } // namespace suggestions
OLDNEW
« components/suggestions/image_manager.cc ('K') | « components/suggestions/image_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698