Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 "base/file_util.h" | |
| 6 #include "base/files/scoped_temp_dir.h" | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "chrome/browser/media_galleries/fileapi/media_file_system_backend.h" | |
| 12 #include "chrome/browser/media_galleries/fileapi/picasa/picasa_data_provider.h" | |
| 13 #include "chrome/browser/media_galleries/fileapi/safe_picasa_albums_indexer.h" | |
| 14 #include "chrome/common/media_galleries/picasa_types.h" | |
| 15 #include "chrome/common/media_galleries/pmp_test_helper.h" | |
| 16 #include "chrome/test/base/in_process_browser_test.h" | |
| 17 #include "content/public/test/test_browser_thread.h" | |
| 18 | |
| 19 using chrome::MediaFileSystemBackend; | |
| 20 | |
| 21 namespace picasa { | |
| 22 | |
| 23 class PicasaDataProviderTest : public InProcessBrowserTest { | |
| 24 public: | |
| 25 PicasaDataProviderTest() : test_helper_(kPicasaAlbumTableName) {} | |
| 26 virtual ~PicasaDataProviderTest() {} | |
| 27 | |
| 28 protected: | |
| 29 virtual void SetUp() OVERRIDE { InProcessBrowserTest::SetUp(); } | |
|
vandebo (ex-Chrome)
2013/08/14 23:48:11
Omit since all you do is call the parent class's i
tommycli
2013/08/15 22:52:36
Done.
| |
| 30 | |
| 31 // Runs on the MediaTaskRunner and designed to be overridden by subclasses. | |
| 32 virtual void InitializeTestData() { ASSERT_TRUE(test_helper_.Init()); } | |
| 33 | |
| 34 void RunTest() { | |
| 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 36 base::RunLoop loop; | |
| 37 quit_closure_ = loop.QuitClosure(); | |
| 38 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 39 FROM_HERE, | |
| 40 base::Bind(&PicasaDataProviderTest::StartTestOnMediaTaskRunner, | |
| 41 base::Unretained(this))); | |
| 42 loop.Run(); | |
| 43 } | |
| 44 | |
| 45 virtual PicasaDataProvider::DataType RequestedDataType() const = 0; | |
| 46 | |
| 47 // Start the test. The data provider is refreshed before calling StartTest | |
| 48 // and the result of the refresh is passed in. | |
| 49 virtual void StartTest(bool parse_success) = 0; | |
| 50 | |
| 51 void TestDone() { | |
| 52 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 53 | |
| 54 // The data provider must be destructed on the MediaTaskRunner. This is done | |
| 55 // in a posted task rather than directly because TestDone is called by | |
| 56 // PicasaDataProvider. The callee should not destroy the caller. | |
| 57 MediaFileSystemBackend::MediaTaskRunner()->PostTask( | |
| 58 FROM_HERE, | |
| 59 base::Bind(&PicasaDataProviderTest::DestructDataProviderThenQuit, | |
| 60 base::Unretained(this))); | |
| 61 } | |
| 62 | |
| 63 PmpTestHelper* test_helper() { return &test_helper_; } | |
| 64 | |
| 65 PicasaDataProvider* data_provider() const { | |
| 66 return picasa_data_provider_.get(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 void StartTestOnMediaTaskRunner() { | |
| 71 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 72 | |
| 73 InitializeTestData(); | |
| 74 | |
| 75 picasa_data_provider_.reset( | |
| 76 new PicasaDataProvider(test_helper_.GetTempDirPath())); | |
| 77 data_provider()->RefreshData( | |
| 78 RequestedDataType(), | |
| 79 base::Bind(&PicasaDataProviderTest::StartTest, base::Unretained(this))); | |
| 80 } | |
| 81 | |
| 82 void DestructDataProviderThenQuit() { | |
| 83 DCHECK(MediaFileSystemBackend::CurrentlyOnMediaTaskRunnerThread()); | |
| 84 picasa_data_provider_.reset(); | |
| 85 content::BrowserThread::PostTask( | |
| 86 content::BrowserThread::UI, FROM_HERE, quit_closure_); | |
| 87 } | |
| 88 | |
| 89 PmpTestHelper test_helper_; | |
| 90 scoped_ptr<PicasaDataProvider> picasa_data_provider_; | |
| 91 | |
| 92 base::Closure quit_closure_; | |
| 93 | |
| 94 DISALLOW_COPY_AND_ASSIGN(PicasaDataProviderTest); | |
| 95 }; | |
| 96 | |
| 97 class PicasaDataProviderNoDatabaseGetListTest : public PicasaDataProviderTest { | |
| 98 public: | |
| 99 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | |
| 100 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | |
| 101 } | |
| 102 virtual void StartTest(bool parse_success) OVERRIDE { | |
| 103 EXPECT_FALSE(parse_success); | |
| 104 TestDone(); | |
| 105 } | |
| 106 }; | |
| 107 | |
| 108 IN_PROC_BROWSER_TEST_F(PicasaDataProviderNoDatabaseGetListTest, | |
| 109 NoDatabaseGetList) { | |
| 110 RunTest(); | |
| 111 } | |
| 112 | |
| 113 class PicasaDataProviderNoDatabaseGetAlbumsImagesTest | |
| 114 : public PicasaDataProviderTest { | |
| 115 protected: | |
| 116 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | |
| 117 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | |
| 118 } | |
| 119 virtual void StartTest(bool parse_success) OVERRIDE { | |
| 120 EXPECT_FALSE(parse_success); | |
| 121 TestDone(); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 IN_PROC_BROWSER_TEST_F(PicasaDataProviderNoDatabaseGetAlbumsImagesTest, | |
| 126 NoDatabaseGetAlbumsImages) { | |
| 127 RunTest(); | |
| 128 } | |
| 129 | |
| 130 class PicasaDataProviderGetListTest : public PicasaDataProviderTest { | |
| 131 protected: | |
| 132 virtual void InitializeTestData() OVERRIDE { | |
| 133 PicasaDataProviderTest::InitializeTestData(); | |
| 134 | |
| 135 ASSERT_TRUE(test_folder_1_.CreateUniqueTempDir()); | |
| 136 ASSERT_TRUE(test_folder_2_.CreateUniqueTempDir()); | |
| 137 | |
| 138 std::vector<uint32> category_vector; | |
| 139 category_vector.push_back(kAlbumCategoryFolder); | |
| 140 category_vector.push_back(kAlbumCategoryInvalid); | |
| 141 category_vector.push_back(kAlbumCategoryAlbum); | |
| 142 category_vector.push_back(kAlbumCategoryFolder); | |
| 143 category_vector.push_back(kAlbumCategoryAlbum); | |
| 144 | |
| 145 std::vector<double> date_vector; | |
| 146 date_vector.push_back(0.0); | |
| 147 date_vector.push_back(0.0); | |
| 148 date_vector.push_back(0.0); | |
| 149 date_vector.push_back(0.0); | |
| 150 date_vector.push_back(0.0); | |
| 151 | |
| 152 std::vector<std::string> filename_vector; | |
| 153 filename_vector.push_back(test_folder_1_.path().AsUTF8Unsafe()); | |
| 154 filename_vector.push_back(""); | |
| 155 filename_vector.push_back(""); | |
| 156 filename_vector.push_back(test_folder_2_.path().AsUTF8Unsafe()); | |
| 157 filename_vector.push_back(""); | |
| 158 | |
| 159 std::vector<std::string> name_vector; | |
| 160 name_vector.push_back(test_folder_1_.path().BaseName().AsUTF8Unsafe()); | |
| 161 name_vector.push_back(""); | |
| 162 name_vector.push_back("Album 1 Name"); | |
| 163 name_vector.push_back(test_folder_2_.path().BaseName().AsUTF8Unsafe()); | |
| 164 name_vector.push_back("Album 2 Name"); | |
| 165 | |
| 166 std::vector<std::string> token_vector; | |
| 167 token_vector.push_back(""); | |
| 168 token_vector.push_back(""); | |
| 169 token_vector.push_back(std::string(kAlbumTokenPrefix) + "uid3"); | |
| 170 token_vector.push_back(""); | |
| 171 token_vector.push_back(std::string(kAlbumTokenPrefix) + "uid5"); | |
| 172 | |
| 173 std::vector<std::string> uid_vector; | |
| 174 uid_vector.push_back("uid1"); | |
| 175 uid_vector.push_back("uid2"); | |
| 176 uid_vector.push_back("uid3"); | |
| 177 uid_vector.push_back("uid4"); | |
| 178 uid_vector.push_back("uid5"); | |
| 179 | |
| 180 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 181 "category", PMP_TYPE_UINT32, category_vector)); | |
| 182 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 183 "date", PMP_TYPE_DOUBLE64, date_vector)); | |
| 184 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 185 "filename", PMP_TYPE_STRING, filename_vector)); | |
| 186 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 187 "name", PMP_TYPE_STRING, name_vector)); | |
| 188 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 189 "token", PMP_TYPE_STRING, token_vector)); | |
| 190 ASSERT_TRUE(test_helper()->WriteColumnFileFromVector( | |
| 191 "uid", PMP_TYPE_STRING, uid_vector)); | |
| 192 } | |
| 193 | |
| 194 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | |
| 195 return PicasaDataProvider::LIST_OF_ALBUMS_AND_FOLDERS_DATA; | |
| 196 } | |
| 197 | |
| 198 virtual void StartTest(bool parse_success) OVERRIDE { | |
| 199 ASSERT_TRUE(parse_success); | |
| 200 | |
| 201 scoped_ptr<AlbumMap> folders = data_provider()->GetFolders(); | |
| 202 ASSERT_TRUE(folders.get()); | |
| 203 EXPECT_EQ(2u, folders->size()); | |
| 204 | |
| 205 AlbumMap::const_iterator folder_1 = folders->find( | |
| 206 test_folder_1_.path().BaseName().AsUTF8Unsafe() + " 1899-12-30"); | |
| 207 EXPECT_NE(folders->end(), folder_1); | |
| 208 EXPECT_EQ(test_folder_1_.path().BaseName().AsUTF8Unsafe(), | |
| 209 folder_1->second.name); | |
| 210 EXPECT_EQ(test_folder_1_.path(), folder_1->second.path); | |
| 211 EXPECT_EQ("uid1", folder_1->second.uid); | |
| 212 | |
| 213 AlbumMap::const_iterator folder_2 = folders->find( | |
| 214 test_folder_2_.path().BaseName().AsUTF8Unsafe() + " 1899-12-30"); | |
| 215 EXPECT_NE(folders->end(), folder_2); | |
| 216 EXPECT_EQ(test_folder_2_.path().BaseName().AsUTF8Unsafe(), | |
| 217 folder_2->second.name); | |
| 218 EXPECT_EQ(test_folder_2_.path(), folder_2->second.path); | |
| 219 EXPECT_EQ("uid4", folder_2->second.uid); | |
| 220 | |
| 221 scoped_ptr<AlbumMap> albums = data_provider()->GetAlbums(); | |
| 222 ASSERT_TRUE(albums.get()); | |
| 223 EXPECT_EQ(2u, albums->size()); | |
| 224 | |
| 225 AlbumMap::const_iterator album_1 = albums->find("Album 1 Name 1899-12-30"); | |
| 226 EXPECT_NE(albums->end(), album_1); | |
| 227 EXPECT_EQ("Album 1 Name", album_1->second.name); | |
| 228 EXPECT_EQ(base::FilePath(), album_1->second.path); | |
| 229 EXPECT_EQ("uid3", album_1->second.uid); | |
| 230 | |
| 231 AlbumMap::const_iterator album_2 = albums->find("Album 2 Name 1899-12-30"); | |
| 232 EXPECT_NE(albums->end(), album_2); | |
| 233 EXPECT_EQ("Album 2 Name", album_2->second.name); | |
| 234 EXPECT_EQ(base::FilePath(), album_2->second.path); | |
| 235 EXPECT_EQ("uid5", album_2->second.uid); | |
| 236 | |
| 237 TestDone(); | |
| 238 } | |
| 239 | |
| 240 base::ScopedTempDir test_folder_1_; | |
| 241 base::ScopedTempDir test_folder_2_; | |
| 242 }; | |
| 243 | |
| 244 IN_PROC_BROWSER_TEST_F(PicasaDataProviderGetListTest, GetListTest) { | |
| 245 RunTest(); | |
| 246 } | |
| 247 | |
| 248 class PicasaDataProviderGetAlbumsImagesTest | |
| 249 : public PicasaDataProviderGetListTest { | |
| 250 protected: | |
| 251 virtual void InitializeTestData() OVERRIDE { | |
| 252 PicasaDataProviderGetListTest::InitializeTestData(); | |
| 253 | |
| 254 const char folder_1_test_ini[] = | |
| 255 "[InBoth.jpg]\n" | |
| 256 "albums=uid3,uid5\n" | |
| 257 "[InSecondAlbumOnly.jpg]\n" | |
| 258 "albums=uid5\n"; | |
| 259 ASSERT_TRUE(file_util::WriteFile( | |
| 260 test_folder_1_.path().AppendASCII(kPicasaINIFilename), | |
| 261 folder_1_test_ini, | |
| 262 arraysize(folder_1_test_ini))); | |
| 263 | |
| 264 const char folder_2_test_ini[] = | |
| 265 "[InFirstAlbumOnly.jpg]\n" | |
| 266 "albums=uid3\n"; | |
| 267 ASSERT_TRUE(file_util::WriteFile( | |
| 268 test_folder_2_.path().AppendASCII(kPicasaINIFilename), | |
| 269 folder_2_test_ini, | |
| 270 arraysize(folder_2_test_ini))); | |
| 271 } | |
| 272 | |
| 273 virtual PicasaDataProvider::DataType RequestedDataType() const OVERRIDE { | |
| 274 return PicasaDataProvider::ALBUMS_IMAGES_DATA; | |
| 275 } | |
| 276 | |
| 277 virtual void StartTest(bool parse_success) OVERRIDE { | |
| 278 ASSERT_TRUE(parse_success); | |
| 279 | |
| 280 scoped_ptr<AlbumImagesMap> album_images = | |
| 281 data_provider()->GetAlbumsImages(); | |
| 282 ASSERT_TRUE(album_images.get()); | |
| 283 | |
| 284 AlbumImagesMap::const_iterator album_1_images = album_images->find("uid3"); | |
| 285 ASSERT_NE(album_images->end(), album_1_images); | |
| 286 EXPECT_EQ(2u, album_1_images->second.size()); | |
| 287 EXPECT_EQ(1u, | |
| 288 album_1_images->second.count( | |
| 289 test_folder_1_.path().AppendASCII("InBoth.jpg"))); | |
| 290 EXPECT_EQ(1u, | |
| 291 album_1_images->second.count( | |
| 292 test_folder_2_.path().AppendASCII("InFirstAlbumOnly.jpg"))); | |
| 293 | |
| 294 AlbumImagesMap::const_iterator album_2_images = album_images->find("uid5"); | |
| 295 ASSERT_NE(album_images->end(), album_2_images); | |
| 296 EXPECT_EQ(2u, album_2_images->second.size()); | |
| 297 EXPECT_EQ(1u, | |
| 298 album_2_images->second.count( | |
| 299 test_folder_1_.path().AppendASCII("InBoth.jpg"))); | |
| 300 EXPECT_EQ(1u, | |
| 301 album_2_images->second.count( | |
| 302 test_folder_1_.path().AppendASCII("InSecondAlbumOnly.jpg"))); | |
| 303 | |
| 304 TestDone(); | |
| 305 } | |
| 306 }; | |
| 307 | |
| 308 IN_PROC_BROWSER_TEST_F(PicasaDataProviderGetAlbumsImagesTest, | |
| 309 GetAlbumsImagesTest) { | |
| 310 RunTest(); | |
| 311 } | |
| 312 | |
|
vandebo (ex-Chrome)
2013/08/14 23:48:11
There are a lot more tests that can be added.... I
tommycli
2013/08/15 22:52:36
Done.
| |
| 313 } // namespace picasa | |
| OLD | NEW |