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