| 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 "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 10 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 void InitializeTestBitmapData() { | 74 void InitializeTestBitmapData() { |
| 75 FakeDB<ThumbnailData>* test_fake_db = new FakeDB<ThumbnailData>(&db_model_); | 75 FakeDB<ThumbnailData>* test_fake_db = new FakeDB<ThumbnailData>(&db_model_); |
| 76 test_thumbnail_manager_.reset(CreateThumbnailManager(test_fake_db)); | 76 test_thumbnail_manager_.reset(CreateThumbnailManager(test_fake_db)); |
| 77 | 77 |
| 78 suggestions::SuggestionsProfile suggestions_profile; | 78 suggestions::SuggestionsProfile suggestions_profile; |
| 79 suggestions::ChromeSuggestion* suggestion = | 79 suggestions::ChromeSuggestion* suggestion = |
| 80 suggestions_profile.add_suggestions(); | 80 suggestions_profile.add_suggestions(); |
| 81 suggestion->set_url(kTestBitmapUrl); | 81 suggestion->set_url(kTestBitmapUrl); |
| 82 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); | 82 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); |
| 83 | 83 |
| 84 test_thumbnail_manager_->InitializeThumbnailMap(suggestions_profile); | 84 test_thumbnail_manager_->InitializeImageMap(suggestions_profile); |
| 85 | 85 |
| 86 // Initialize empty database. | 86 // Initialize empty database. |
| 87 test_fake_db->InitCallback(true); | 87 test_fake_db->InitCallback(true); |
| 88 test_fake_db->LoadCallback(true); | 88 test_fake_db->LoadCallback(true); |
| 89 | 89 |
| 90 base::RunLoop run_loop; | 90 base::RunLoop run_loop; |
| 91 // Fetch existing URL. | 91 // Fetch existing URL. |
| 92 test_thumbnail_manager_->GetPageThumbnail( | 92 test_thumbnail_manager_->GetImageForURL( |
| 93 GURL(kTestBitmapUrl), | 93 GURL(kTestBitmapUrl), |
| 94 base::Bind(&ThumbnailManagerBrowserTest::OnTestThumbnailAvailable, | 94 base::Bind(&ThumbnailManagerBrowserTest::OnTestThumbnailAvailable, |
| 95 base::Unretained(this), &run_loop)); | 95 base::Unretained(this), &run_loop)); |
| 96 run_loop.Run(); | 96 run_loop.Run(); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void OnTestThumbnailAvailable(base::RunLoop* loop, const GURL& url, | 99 void OnTestThumbnailAvailable(base::RunLoop* loop, const GURL& url, |
| 100 const SkBitmap* bitmap) { | 100 const SkBitmap* bitmap) { |
| 101 CHECK(bitmap); | 101 CHECK(bitmap); |
| 102 // Copy the resource locally. | 102 // Copy the resource locally. |
| 103 test_bitmap_ = *bitmap; | 103 test_bitmap_ = *bitmap; |
| 104 loop->Quit(); | 104 loop->Quit(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 void InitializeDefaultThumbnailMapAndDatabase( | 107 void InitializeDefaultThumbnailMapAndDatabase( |
| 108 ThumbnailManager* thumbnail_manager, FakeDB<ThumbnailData>* fake_db) { | 108 ThumbnailManager* thumbnail_manager, FakeDB<ThumbnailData>* fake_db) { |
| 109 CHECK(thumbnail_manager); | 109 CHECK(thumbnail_manager); |
| 110 CHECK(fake_db); | 110 CHECK(fake_db); |
| 111 | 111 |
| 112 suggestions::SuggestionsProfile suggestions_profile; | 112 suggestions::SuggestionsProfile suggestions_profile; |
| 113 suggestions::ChromeSuggestion* suggestion = | 113 suggestions::ChromeSuggestion* suggestion = |
| 114 suggestions_profile.add_suggestions(); | 114 suggestions_profile.add_suggestions(); |
| 115 suggestion->set_url(kTestUrl1); | 115 suggestion->set_url(kTestUrl1); |
| 116 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); | 116 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); |
| 117 | 117 |
| 118 thumbnail_manager->InitializeThumbnailMap(suggestions_profile); | 118 thumbnail_manager->InitializeImageMap(suggestions_profile); |
| 119 | 119 |
| 120 // Initialize empty database. | 120 // Initialize empty database. |
| 121 fake_db->InitCallback(true); | 121 fake_db->InitCallback(true); |
| 122 fake_db->LoadCallback(true); | 122 fake_db->LoadCallback(true); |
| 123 } | 123 } |
| 124 | 124 |
| 125 ThumbnailData GetSampleThumbnailData(const std::string& url) { | 125 ThumbnailData GetSampleThumbnailData(const std::string& url) { |
| 126 ThumbnailData data; | 126 ThumbnailData data; |
| 127 data.set_url(url); | 127 data.set_url(url); |
| 128 std::vector<unsigned char> encoded; | 128 std::vector<unsigned char> encoded; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 SkBitmap test_bitmap_; | 163 SkBitmap test_bitmap_; |
| 164 scoped_ptr<ThumbnailManager> test_thumbnail_manager_; | 164 scoped_ptr<ThumbnailManager> test_thumbnail_manager_; |
| 165 | 165 |
| 166 int num_callback_null_called_; | 166 int num_callback_null_called_; |
| 167 int num_callback_valid_called_; | 167 int num_callback_valid_called_; |
| 168 net::SpawnedTestServer test_server_; | 168 net::SpawnedTestServer test_server_; |
| 169 // Under test. | 169 // Under test. |
| 170 scoped_ptr<ThumbnailManager> thumbnail_manager_; | 170 scoped_ptr<ThumbnailManager> thumbnail_manager_; |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, GetPageThumbnailNetwork) { | 173 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, GetImageForURLNetwork) { |
| 174 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); | 174 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); |
| 175 | 175 |
| 176 base::RunLoop run_loop; | 176 base::RunLoop run_loop; |
| 177 // Fetch existing URL. | 177 // Fetch existing URL. |
| 178 thumbnail_manager_->GetPageThumbnail( | 178 thumbnail_manager_->GetImageForURL( |
| 179 GURL(kTestUrl1), | 179 GURL(kTestUrl1), |
| 180 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | 180 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, |
| 181 base::Unretained(this), &run_loop)); | 181 base::Unretained(this), &run_loop)); |
| 182 run_loop.Run(); | 182 run_loop.Run(); |
| 183 | 183 |
| 184 EXPECT_EQ(0, num_callback_null_called_); | 184 EXPECT_EQ(0, num_callback_null_called_); |
| 185 EXPECT_EQ(1, num_callback_valid_called_); | 185 EXPECT_EQ(1, num_callback_valid_called_); |
| 186 | 186 |
| 187 base::RunLoop run_loop2; | 187 base::RunLoop run_loop2; |
| 188 // Fetch non-existing URL. | 188 // Fetch non-existing URL. |
| 189 thumbnail_manager_->GetPageThumbnail( | 189 thumbnail_manager_->GetImageForURL( |
| 190 GURL(kTestUrl2), | 190 GURL(kTestUrl2), |
| 191 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | 191 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, |
| 192 base::Unretained(this), &run_loop2)); | 192 base::Unretained(this), &run_loop2)); |
| 193 run_loop2.Run(); | 193 run_loop2.Run(); |
| 194 | 194 |
| 195 EXPECT_EQ(1, num_callback_null_called_); | 195 EXPECT_EQ(1, num_callback_null_called_); |
| 196 EXPECT_EQ(1, num_callback_valid_called_); | 196 EXPECT_EQ(1, num_callback_valid_called_); |
| 197 } | 197 } |
| 198 | 198 |
| 199 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | 199 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, |
| 200 GetPageThumbnailNetworkMultiple) { | 200 GetImageForURLNetworkMultiple) { |
| 201 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); | 201 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); |
| 202 | 202 |
| 203 // Fetch non-existing URL, and add more while request is in flight. | 203 // Fetch non-existing URL, and add more while request is in flight. |
| 204 base::RunLoop run_loop; | 204 base::RunLoop run_loop; |
| 205 for (int i = 0; i < 5; i++) { | 205 for (int i = 0; i < 5; i++) { |
| 206 // Fetch existing URL. | 206 // Fetch existing URL. |
| 207 thumbnail_manager_->GetPageThumbnail( | 207 thumbnail_manager_->GetImageForURL( |
| 208 GURL(kTestUrl1), | 208 GURL(kTestUrl1), |
| 209 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | 209 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, |
| 210 base::Unretained(this), &run_loop)); | 210 base::Unretained(this), &run_loop)); |
| 211 } | 211 } |
| 212 run_loop.Run(); | 212 run_loop.Run(); |
| 213 | 213 |
| 214 EXPECT_EQ(0, num_callback_null_called_); | 214 EXPECT_EQ(0, num_callback_null_called_); |
| 215 EXPECT_EQ(5, num_callback_valid_called_); | 215 EXPECT_EQ(5, num_callback_valid_called_); |
| 216 } | 216 } |
| 217 | 217 |
| 218 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | 218 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, |
| 219 GetPageThumbnailNetworkInvalid) { | 219 GetImageForURLNetworkInvalid) { |
| 220 SuggestionsProfile suggestions_profile; | 220 SuggestionsProfile suggestions_profile; |
| 221 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 221 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 222 suggestion->set_url(kTestUrl1); | 222 suggestion->set_url(kTestUrl1); |
| 223 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); | 223 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); |
| 224 | 224 |
| 225 thumbnail_manager_->InitializeThumbnailMap(suggestions_profile); | 225 thumbnail_manager_->InitializeImageMap(suggestions_profile); |
| 226 | 226 |
| 227 // Database will be initialized and loaded without anything in it. | 227 // Database will be initialized and loaded without anything in it. |
| 228 fake_db_->InitCallback(true); | 228 fake_db_->InitCallback(true); |
| 229 fake_db_->LoadCallback(true); | 229 fake_db_->LoadCallback(true); |
| 230 | 230 |
| 231 base::RunLoop run_loop; | 231 base::RunLoop run_loop; |
| 232 // Fetch existing URL that has invalid thumbnail. | 232 // Fetch existing URL that has invalid thumbnail. |
| 233 thumbnail_manager_->GetPageThumbnail( | 233 thumbnail_manager_->GetImageForURL( |
| 234 GURL(kTestUrl1), | 234 GURL(kTestUrl1), |
| 235 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | 235 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, |
| 236 base::Unretained(this), &run_loop)); | 236 base::Unretained(this), &run_loop)); |
| 237 run_loop.Run(); | 237 run_loop.Run(); |
| 238 | 238 |
| 239 EXPECT_EQ(1, num_callback_null_called_); | 239 EXPECT_EQ(1, num_callback_null_called_); |
| 240 EXPECT_EQ(0, num_callback_valid_called_); | 240 EXPECT_EQ(0, num_callback_valid_called_); |
| 241 } | 241 } |
| 242 | 242 |
| 243 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | 243 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, |
| 244 GetPageThumbnailNetworkCacheHit) { | 244 GetImageForURLNetworkCacheHit) { |
| 245 InitializeTestBitmapData(); | 245 InitializeTestBitmapData(); |
| 246 | 246 |
| 247 SuggestionsProfile suggestions_profile; | 247 SuggestionsProfile suggestions_profile; |
| 248 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | 248 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); |
| 249 suggestion->set_url(kTestUrl1); | 249 suggestion->set_url(kTestUrl1); |
| 250 // The URL we set is invalid, to show that it will fail from network. | 250 // The URL we set is invalid, to show that it will fail from network. |
| 251 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); | 251 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); |
| 252 | 252 |
| 253 // Create the ThumbnailManager with an added entry in the database. | 253 // Create the ThumbnailManager with an added entry in the database. |
| 254 AddEntry(GetSampleThumbnailData(kTestUrl1), &db_model_); | 254 AddEntry(GetSampleThumbnailData(kTestUrl1), &db_model_); |
| 255 FakeDB<ThumbnailData>* fake_db = new FakeDB<ThumbnailData>(&db_model_); | 255 FakeDB<ThumbnailData>* fake_db = new FakeDB<ThumbnailData>(&db_model_); |
| 256 thumbnail_manager_.reset(CreateThumbnailManager(fake_db)); | 256 thumbnail_manager_.reset(CreateThumbnailManager(fake_db)); |
| 257 thumbnail_manager_->InitializeThumbnailMap(suggestions_profile); | 257 thumbnail_manager_->InitializeImageMap(suggestions_profile); |
| 258 fake_db->InitCallback(true); | 258 fake_db->InitCallback(true); |
| 259 fake_db->LoadCallback(true); | 259 fake_db->LoadCallback(true); |
| 260 // Expect something in the cache. | 260 // Expect something in the cache. |
| 261 SkBitmap* bitmap = thumbnail_manager_->GetBitmapFromCache(GURL(kTestUrl1)); | 261 SkBitmap* bitmap = thumbnail_manager_->GetBitmapFromCache(GURL(kTestUrl1)); |
| 262 EXPECT_FALSE(bitmap->isNull()); | 262 EXPECT_FALSE(bitmap->isNull()); |
| 263 | 263 |
| 264 base::RunLoop run_loop; | 264 base::RunLoop run_loop; |
| 265 thumbnail_manager_->GetPageThumbnail( | 265 thumbnail_manager_->GetImageForURL( |
| 266 GURL(kTestUrl1), | 266 GURL(kTestUrl1), |
| 267 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | 267 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, |
| 268 base::Unretained(this), &run_loop)); | 268 base::Unretained(this), &run_loop)); |
| 269 run_loop.Run(); | 269 run_loop.Run(); |
| 270 | 270 |
| 271 EXPECT_EQ(0, num_callback_null_called_); | 271 EXPECT_EQ(0, num_callback_null_called_); |
| 272 EXPECT_EQ(1, num_callback_valid_called_); | 272 EXPECT_EQ(1, num_callback_valid_called_); |
| 273 } | 273 } |
| 274 | 274 |
| 275 } // namespace suggestions | 275 } // namespace suggestions |
| OLD | NEW |