| 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 "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/search/suggestions/thumbnail_manager.h" | |
| 11 #include "chrome/browser/ui/browser.h" | |
| 12 #include "chrome/test/base/in_process_browser_test.h" | |
| 13 #include "components/leveldb_proto/proto_database.h" | |
| 14 #include "components/leveldb_proto/testing/fake_db.h" | |
| 15 #include "components/suggestions/proto/suggestions.pb.h" | |
| 16 #include "content/public/test/test_utils.h" | |
| 17 #include "net/base/load_flags.h" | |
| 18 #include "net/test/spawned_test_server/spawned_test_server.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 #include "ui/gfx/image/image_skia.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace suggestions { | |
| 24 | |
| 25 const char kTestUrl1[] = "http://go.com/"; | |
| 26 const char kTestUrl2[] = "http://goal.com/"; | |
| 27 const char kTestBitmapUrl[] = "http://test.com"; | |
| 28 const char kTestImagePath[] = "files/image_decoding/droids.png"; | |
| 29 const char kInvalidImagePath[] = "files/DOESNOTEXIST"; | |
| 30 | |
| 31 const base::FilePath::CharType kDocRoot[] = | |
| 32 FILE_PATH_LITERAL("chrome/test/data"); | |
| 33 | |
| 34 using chrome::BitmapFetcher; | |
| 35 using content::BrowserThread; | |
| 36 using leveldb_proto::test::FakeDB; | |
| 37 using suggestions::ThumbnailData; | |
| 38 using suggestions::ThumbnailManager; | |
| 39 | |
| 40 typedef base::hash_map<std::string, ThumbnailData> EntryMap; | |
| 41 | |
| 42 void AddEntry(const ThumbnailData& d, EntryMap* map) { (*map)[d.url()] = d; } | |
| 43 | |
| 44 class ThumbnailManagerBrowserTest : public InProcessBrowserTest { | |
| 45 public: | |
| 46 ThumbnailManagerBrowserTest() | |
| 47 : num_callback_null_called_(0), | |
| 48 num_callback_valid_called_(0), | |
| 49 test_server_(net::SpawnedTestServer::TYPE_HTTP, | |
| 50 net::SpawnedTestServer::kLocalhost, | |
| 51 base::FilePath(kDocRoot)) {} | |
| 52 | |
| 53 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
| 54 ASSERT_TRUE(test_server_.Start()); | |
| 55 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); | |
| 56 } | |
| 57 | |
| 58 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
| 59 test_server_.Stop(); | |
| 60 } | |
| 61 | |
| 62 virtual void SetUpOnMainThread() OVERRIDE { | |
| 63 fake_db_ = new FakeDB<ThumbnailData>(&db_model_); | |
| 64 thumbnail_manager_.reset(CreateThumbnailManager(fake_db_)); | |
| 65 } | |
| 66 | |
| 67 virtual void TearDownOnMainThread() OVERRIDE { | |
| 68 fake_db_ = NULL; | |
| 69 db_model_.clear(); | |
| 70 thumbnail_manager_.reset(); | |
| 71 test_thumbnail_manager_.reset(); | |
| 72 } | |
| 73 | |
| 74 void InitializeTestBitmapData() { | |
| 75 FakeDB<ThumbnailData>* test_fake_db = new FakeDB<ThumbnailData>(&db_model_); | |
| 76 test_thumbnail_manager_.reset(CreateThumbnailManager(test_fake_db)); | |
| 77 | |
| 78 suggestions::SuggestionsProfile suggestions_profile; | |
| 79 suggestions::ChromeSuggestion* suggestion = | |
| 80 suggestions_profile.add_suggestions(); | |
| 81 suggestion->set_url(kTestBitmapUrl); | |
| 82 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); | |
| 83 | |
| 84 test_thumbnail_manager_->Initialize(suggestions_profile); | |
| 85 | |
| 86 // Initialize empty database. | |
| 87 test_fake_db->InitCallback(true); | |
| 88 test_fake_db->LoadCallback(true); | |
| 89 | |
| 90 base::RunLoop run_loop; | |
| 91 // Fetch existing URL. | |
| 92 test_thumbnail_manager_->GetImageForURL( | |
| 93 GURL(kTestBitmapUrl), | |
| 94 base::Bind(&ThumbnailManagerBrowserTest::OnTestThumbnailAvailable, | |
| 95 base::Unretained(this), &run_loop)); | |
| 96 run_loop.Run(); | |
| 97 } | |
| 98 | |
| 99 void OnTestThumbnailAvailable(base::RunLoop* loop, const GURL& url, | |
| 100 const SkBitmap* bitmap) { | |
| 101 CHECK(bitmap); | |
| 102 // Copy the resource locally. | |
| 103 test_bitmap_ = *bitmap; | |
| 104 loop->Quit(); | |
| 105 } | |
| 106 | |
| 107 void InitializeDefaultThumbnailMapAndDatabase( | |
| 108 ThumbnailManager* thumbnail_manager, FakeDB<ThumbnailData>* fake_db) { | |
| 109 CHECK(thumbnail_manager); | |
| 110 CHECK(fake_db); | |
| 111 | |
| 112 suggestions::SuggestionsProfile suggestions_profile; | |
| 113 suggestions::ChromeSuggestion* suggestion = | |
| 114 suggestions_profile.add_suggestions(); | |
| 115 suggestion->set_url(kTestUrl1); | |
| 116 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec()); | |
| 117 | |
| 118 thumbnail_manager->Initialize(suggestions_profile); | |
| 119 | |
| 120 // Initialize empty database. | |
| 121 fake_db->InitCallback(true); | |
| 122 fake_db->LoadCallback(true); | |
| 123 } | |
| 124 | |
| 125 ThumbnailData GetSampleThumbnailData(const std::string& url) { | |
| 126 ThumbnailData data; | |
| 127 data.set_url(url); | |
| 128 std::vector<unsigned char> encoded; | |
| 129 EXPECT_TRUE(ThumbnailManager::EncodeThumbnail(test_bitmap_, &encoded)); | |
| 130 data.set_data(std::string(encoded.begin(), encoded.end())); | |
| 131 return data; | |
| 132 } | |
| 133 | |
| 134 void OnThumbnailAvailable(base::RunLoop* loop, const GURL& url, | |
| 135 const SkBitmap* bitmap) { | |
| 136 if (bitmap) { | |
| 137 num_callback_valid_called_++; | |
| 138 /*std::vector<unsigned char> actual; | |
| 139 std::vector<unsigned char> expected; | |
| 140 EXPECT_TRUE(ThumbnailManager::EncodeThumbnail(*bitmap, &actual)); | |
| 141 EXPECT_TRUE(ThumbnailManager::EncodeThumbnail(test_bitmap_, &expected)); | |
| 142 // Check first 100 bytes. | |
| 143 std::string actual_str(actual.begin(), actual.begin() + 100); | |
| 144 std::string expected_str(expected.begin(), expected.begin() + 100); | |
| 145 EXPECT_EQ(expected_str, actual_str);*/ | |
| 146 } else { | |
| 147 num_callback_null_called_++; | |
| 148 } | |
| 149 loop->Quit(); | |
| 150 } | |
| 151 | |
| 152 ThumbnailManager* CreateThumbnailManager(FakeDB<ThumbnailData>* fake_db) { | |
| 153 return new ThumbnailManager( | |
| 154 browser()->profile()->GetRequestContext(), | |
| 155 scoped_ptr<leveldb_proto::ProtoDatabase<ThumbnailData> >(fake_db), | |
| 156 FakeDB<ThumbnailData>::DirectoryForTestDB()); | |
| 157 } | |
| 158 | |
| 159 EntryMap db_model_; | |
| 160 // Owned by the ThumbnailManager under test. | |
| 161 FakeDB<ThumbnailData>* fake_db_; | |
| 162 | |
| 163 SkBitmap test_bitmap_; | |
| 164 scoped_ptr<ThumbnailManager> test_thumbnail_manager_; | |
| 165 | |
| 166 int num_callback_null_called_; | |
| 167 int num_callback_valid_called_; | |
| 168 net::SpawnedTestServer test_server_; | |
| 169 // Under test. | |
| 170 scoped_ptr<ThumbnailManager> thumbnail_manager_; | |
| 171 }; | |
| 172 | |
| 173 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, GetImageForURLNetwork) { | |
| 174 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); | |
| 175 | |
| 176 base::RunLoop run_loop; | |
| 177 // Fetch existing URL. | |
| 178 thumbnail_manager_->GetImageForURL( | |
| 179 GURL(kTestUrl1), | |
| 180 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | |
| 181 base::Unretained(this), &run_loop)); | |
| 182 run_loop.Run(); | |
| 183 | |
| 184 EXPECT_EQ(0, num_callback_null_called_); | |
| 185 EXPECT_EQ(1, num_callback_valid_called_); | |
| 186 | |
| 187 base::RunLoop run_loop2; | |
| 188 // Fetch non-existing URL. | |
| 189 thumbnail_manager_->GetImageForURL( | |
| 190 GURL(kTestUrl2), | |
| 191 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | |
| 192 base::Unretained(this), &run_loop2)); | |
| 193 run_loop2.Run(); | |
| 194 | |
| 195 EXPECT_EQ(1, num_callback_null_called_); | |
| 196 EXPECT_EQ(1, num_callback_valid_called_); | |
| 197 } | |
| 198 | |
| 199 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | |
| 200 GetImageForURLNetworkMultiple) { | |
| 201 InitializeDefaultThumbnailMapAndDatabase(thumbnail_manager_.get(), fake_db_); | |
| 202 | |
| 203 // Fetch non-existing URL, and add more while request is in flight. | |
| 204 base::RunLoop run_loop; | |
| 205 for (int i = 0; i < 5; i++) { | |
| 206 // Fetch existing URL. | |
| 207 thumbnail_manager_->GetImageForURL( | |
| 208 GURL(kTestUrl1), | |
| 209 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | |
| 210 base::Unretained(this), &run_loop)); | |
| 211 } | |
| 212 run_loop.Run(); | |
| 213 | |
| 214 EXPECT_EQ(0, num_callback_null_called_); | |
| 215 EXPECT_EQ(5, num_callback_valid_called_); | |
| 216 } | |
| 217 | |
| 218 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | |
| 219 GetImageForURLNetworkInvalid) { | |
| 220 SuggestionsProfile suggestions_profile; | |
| 221 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | |
| 222 suggestion->set_url(kTestUrl1); | |
| 223 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); | |
| 224 | |
| 225 thumbnail_manager_->Initialize(suggestions_profile); | |
| 226 | |
| 227 // Database will be initialized and loaded without anything in it. | |
| 228 fake_db_->InitCallback(true); | |
| 229 fake_db_->LoadCallback(true); | |
| 230 | |
| 231 base::RunLoop run_loop; | |
| 232 // Fetch existing URL that has invalid thumbnail. | |
| 233 thumbnail_manager_->GetImageForURL( | |
| 234 GURL(kTestUrl1), | |
| 235 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | |
| 236 base::Unretained(this), &run_loop)); | |
| 237 run_loop.Run(); | |
| 238 | |
| 239 EXPECT_EQ(1, num_callback_null_called_); | |
| 240 EXPECT_EQ(0, num_callback_valid_called_); | |
| 241 } | |
| 242 | |
| 243 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, | |
| 244 GetImageForURLNetworkCacheHit) { | |
| 245 InitializeTestBitmapData(); | |
| 246 | |
| 247 SuggestionsProfile suggestions_profile; | |
| 248 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); | |
| 249 suggestion->set_url(kTestUrl1); | |
| 250 // The URL we set is invalid, to show that it will fail from network. | |
| 251 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec()); | |
| 252 | |
| 253 // Create the ThumbnailManager with an added entry in the database. | |
| 254 AddEntry(GetSampleThumbnailData(kTestUrl1), &db_model_); | |
| 255 FakeDB<ThumbnailData>* fake_db = new FakeDB<ThumbnailData>(&db_model_); | |
| 256 thumbnail_manager_.reset(CreateThumbnailManager(fake_db)); | |
| 257 thumbnail_manager_->Initialize(suggestions_profile); | |
| 258 fake_db->InitCallback(true); | |
| 259 fake_db->LoadCallback(true); | |
| 260 // Expect something in the cache. | |
| 261 SkBitmap* bitmap = thumbnail_manager_->GetBitmapFromCache(GURL(kTestUrl1)); | |
| 262 EXPECT_FALSE(bitmap->isNull()); | |
| 263 | |
| 264 base::RunLoop run_loop; | |
| 265 thumbnail_manager_->GetImageForURL( | |
| 266 GURL(kTestUrl1), | |
| 267 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable, | |
| 268 base::Unretained(this), &run_loop)); | |
| 269 run_loop.Run(); | |
| 270 | |
| 271 EXPECT_EQ(0, num_callback_null_called_); | |
| 272 EXPECT_EQ(1, num_callback_valid_called_); | |
| 273 } | |
| 274 | |
| 275 } // namespace suggestions | |
| OLD | NEW |