OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/gdata/gdata_files.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_files.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <string> | 8 #include <string> |
8 #include <utility> | 9 #include <utility> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
12 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
14 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
15 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 16 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
16 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 ASSERT_FALSE(entry.FromProto(proto)); | 192 ASSERT_FALSE(entry.FromProto(proto)); |
192 | 193 |
193 // Set a upload URL. | 194 // Set a upload URL. |
194 proto.set_upload_url(kResumableEditMediaUrl); | 195 proto.set_upload_url(kResumableEditMediaUrl); |
195 | 196 |
196 // This should succeed as the upload URL is set. | 197 // This should succeed as the upload URL is set. |
197 ASSERT_TRUE(entry.FromProto(proto)); | 198 ASSERT_TRUE(entry.FromProto(proto)); |
198 EXPECT_EQ(kResumableEditMediaUrl, entry.upload_url().spec()); | 199 EXPECT_EQ(kResumableEditMediaUrl, entry.upload_url().spec()); |
199 } | 200 } |
200 | 201 |
201 TEST(GDataRootDirectoryTest, VersionCheck) { | 202 TEST(GDataDirectoryServiceTest, VersionCheck) { |
202 // Set up the root directory. | 203 // Set up the root directory. |
203 GDataRootDirectoryProto proto; | 204 GDataRootDirectoryProto proto; |
204 GDataEntryProto* mutable_entry = | 205 GDataEntryProto* mutable_entry = |
205 proto.mutable_gdata_directory()->mutable_gdata_entry(); | 206 proto.mutable_gdata_directory()->mutable_gdata_entry(); |
206 mutable_entry->mutable_file_info()->set_is_directory(true); | 207 mutable_entry->mutable_file_info()->set_is_directory(true); |
207 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | 208 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); |
208 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 209 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
209 mutable_entry->set_title("drive"); | 210 mutable_entry->set_title("drive"); |
210 | 211 |
211 GDataDirectoryService directory_service; | 212 GDataDirectoryService directory_service; |
(...skipping 15 matching lines...) Expand all Loading... |
227 // This should succeed as the version matches the current number. | 228 // This should succeed as the version matches the current number. |
228 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 229 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
229 | 230 |
230 // Set a newer version, and serialize. | 231 // Set a newer version, and serialize. |
231 proto.set_version(kProtoVersion + 1); | 232 proto.set_version(kProtoVersion + 1); |
232 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 233 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
233 // This should fail as the version is newer. | 234 // This should fail as the version is newer. |
234 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | 235 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); |
235 } | 236 } |
236 | 237 |
237 TEST(GDataRootDirectoryTest, ParseFromString_DetectBadTitle) { | 238 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadTitle) { |
238 GDataRootDirectoryProto proto; | 239 GDataRootDirectoryProto proto; |
239 proto.set_version(kProtoVersion); | 240 proto.set_version(kProtoVersion); |
240 | 241 |
241 GDataEntryProto* mutable_entry = | 242 GDataEntryProto* mutable_entry = |
242 proto.mutable_gdata_directory()->mutable_gdata_entry(); | 243 proto.mutable_gdata_directory()->mutable_gdata_entry(); |
243 mutable_entry->mutable_file_info()->set_is_directory(true); | 244 mutable_entry->mutable_file_info()->set_is_directory(true); |
244 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | 245 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); |
245 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 246 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
246 | 247 |
247 std::string serialized_proto; | 248 std::string serialized_proto; |
(...skipping 17 matching lines...) Expand all Loading... |
265 | 266 |
266 // Setting the title to kGDataRootDirectory. | 267 // Setting the title to kGDataRootDirectory. |
267 mutable_entry->set_title(kGDataRootDirectory); | 268 mutable_entry->set_title(kGDataRootDirectory); |
268 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 269 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
269 | 270 |
270 // This should succeed as the title is kGDataRootDirectory. | 271 // This should succeed as the title is kGDataRootDirectory. |
271 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 272 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
272 ASSERT_EQ(kGDataRootDirectory, root->title()); | 273 ASSERT_EQ(kGDataRootDirectory, root->title()); |
273 } | 274 } |
274 | 275 |
275 TEST(GDataRootDirectoryTest, ParseFromString_DetectBadResourceID) { | 276 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadResourceID) { |
276 GDataRootDirectoryProto proto; | 277 GDataRootDirectoryProto proto; |
277 proto.set_version(kProtoVersion); | 278 proto.set_version(kProtoVersion); |
278 | 279 |
279 GDataEntryProto* mutable_entry = | 280 GDataEntryProto* mutable_entry = |
280 proto.mutable_gdata_directory()->mutable_gdata_entry(); | 281 proto.mutable_gdata_directory()->mutable_gdata_entry(); |
281 mutable_entry->mutable_file_info()->set_is_directory(true); | 282 mutable_entry->mutable_file_info()->set_is_directory(true); |
282 mutable_entry->set_title(kGDataRootDirectory); | 283 mutable_entry->set_title(kGDataRootDirectory); |
283 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 284 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
284 | 285 |
285 std::string serialized_proto; | 286 std::string serialized_proto; |
(...skipping 11 matching lines...) Expand all Loading... |
297 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 298 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
298 | 299 |
299 // This should succeed as the resource ID is correct. | 300 // This should succeed as the resource ID is correct. |
300 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 301 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
301 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | 302 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); |
302 } | 303 } |
303 | 304 |
304 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here | 305 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here |
305 // is to ensure that an error in GDataFile::FromProto() is properly | 306 // is to ensure that an error in GDataFile::FromProto() is properly |
306 // propagated to GDataRootDirectory::ParseFromString(). | 307 // propagated to GDataRootDirectory::ParseFromString(). |
307 TEST(GDataRootDirectoryTest, ParseFromString_DetectNoUploadUrl) { | 308 TEST(GDataDirectoryServiceTest, ParseFromString_DetectNoUploadUrl) { |
308 // Set up the root directory properly. | 309 // Set up the root directory properly. |
309 GDataRootDirectoryProto root_directory_proto; | 310 GDataRootDirectoryProto root_directory_proto; |
310 root_directory_proto.set_version(kProtoVersion); | 311 root_directory_proto.set_version(kProtoVersion); |
311 | 312 |
312 GDataEntryProto* mutable_entry = | 313 GDataEntryProto* mutable_entry = |
313 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); | 314 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); |
314 mutable_entry->mutable_file_info()->set_is_directory(true); | 315 mutable_entry->mutable_file_info()->set_is_directory(true); |
315 mutable_entry->set_title(kGDataRootDirectory); | 316 mutable_entry->set_title(kGDataRootDirectory); |
316 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | 317 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); |
317 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 318 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | 365 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); |
365 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 366 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
366 // No file should be added to the root directory. | 367 // No file should be added to the root directory. |
367 ASSERT_TRUE(root->child_files().empty()); | 368 ASSERT_TRUE(root->child_files().empty()); |
368 // Two directories ("empty", "dir") should be added to the root directory. | 369 // Two directories ("empty", "dir") should be added to the root directory. |
369 ASSERT_EQ(2U, root->child_directories().size()); | 370 ASSERT_EQ(2U, root->child_directories().size()); |
370 // The origin should change to FROM_CACHE because we loaded from the cache. | 371 // The origin should change to FROM_CACHE because we loaded from the cache. |
371 ASSERT_EQ(FROM_CACHE, directory_service.origin()); | 372 ASSERT_EQ(FROM_CACHE, directory_service.origin()); |
372 } | 373 } |
373 | 374 |
374 TEST(GDataRootDirectoryTest, RefreshFile) { | 375 TEST(GDataDirectoryServiceTest, RefreshFile) { |
375 MessageLoopForUI message_loop; | 376 MessageLoopForUI message_loop; |
376 GDataDirectoryService directory_service; | 377 GDataDirectoryService directory_service; |
377 GDataDirectory* root(directory_service.root()); | 378 GDataDirectory* root(directory_service.root()); |
378 // Add a directory to the file system. | 379 // Add a directory to the file system. |
379 GDataDirectory* directory_entry = new GDataDirectory(root, | 380 GDataDirectory* directory_entry = new GDataDirectory(root, |
380 &directory_service); | 381 &directory_service); |
381 directory_entry->set_resource_id("folder:directory_resource_id"); | 382 directory_entry->set_resource_id("folder:directory_resource_id"); |
382 directory_entry->set_title("directory"); | 383 directory_entry->set_title("directory"); |
383 directory_entry->SetBaseNameFromTitle(); | 384 directory_entry->SetBaseNameFromTitle(); |
384 GDataFileError error = GDATA_FILE_ERROR_FAILED; | 385 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 421 |
421 // Let's try refreshing file that didn't prviously exist. | 422 // Let's try refreshing file that didn't prviously exist. |
422 GDataFile* non_existent_entry = new GDataFile(NULL, &directory_service); | 423 GDataFile* non_existent_entry = new GDataFile(NULL, &directory_service); |
423 non_existent_entry->set_resource_id("file:does_not_exist"); | 424 non_existent_entry->set_resource_id("file:does_not_exist"); |
424 directory_service.RefreshFile( | 425 directory_service.RefreshFile( |
425 scoped_ptr<GDataFile>(non_existent_entry).Pass()); | 426 scoped_ptr<GDataFile>(non_existent_entry).Pass()); |
426 // File with non existent resource id should not be added. | 427 // File with non existent resource id should not be added. |
427 EXPECT_FALSE(directory_service.GetEntryByResourceId("file:does_not_exist")); | 428 EXPECT_FALSE(directory_service.GetEntryByResourceId("file:does_not_exist")); |
428 } | 429 } |
429 | 430 |
430 TEST(GDataRootDirectoryTest, GetEntryByResourceId_RootDirectory) { | 431 TEST(GDataDirectoryServiceTest, GetEntryByResourceId_RootDirectory) { |
431 GDataDirectoryService directory_service; | 432 GDataDirectoryService directory_service; |
432 // Look up the root directory by its resource ID. | 433 // Look up the root directory by its resource ID. |
433 GDataEntry* entry = directory_service.GetEntryByResourceId( | 434 GDataEntry* entry = directory_service.GetEntryByResourceId( |
434 kGDataRootDirectoryResourceId); | 435 kGDataRootDirectoryResourceId); |
435 ASSERT_TRUE(entry); | 436 ASSERT_TRUE(entry); |
436 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); | 437 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); |
437 } | 438 } |
438 | 439 |
439 TEST(GDataRootDirectoryTest, DBTest) { | 440 TEST(GDataDirectoryServiceTest, GetEntryInfoByPath) { |
| 441 MessageLoopForUI message_loop; |
| 442 GDataDirectoryService directory_service; |
| 443 InitDirectoryService(&directory_service); |
| 444 |
| 445 // Confirm that an existing file is found. |
| 446 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 447 scoped_ptr<GDataEntryProto> entry_proto; |
| 448 directory_service.GetEntryInfoByPath( |
| 449 FilePath::FromUTF8Unsafe("drive/dir1/file4"), |
| 450 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, |
| 451 &error, &entry_proto)); |
| 452 test_util::RunBlockingPoolTask(); |
| 453 EXPECT_EQ(GDATA_FILE_OK, error); |
| 454 ASSERT_TRUE(entry_proto.get()); |
| 455 EXPECT_EQ("file4", entry_proto->base_name()); |
| 456 |
| 457 // Confirm that a non existing file is not found. |
| 458 error = GDATA_FILE_ERROR_FAILED; |
| 459 entry_proto.reset(); |
| 460 directory_service.GetEntryInfoByPath( |
| 461 FilePath::FromUTF8Unsafe("drive/dir1/non_existing"), |
| 462 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, |
| 463 &error, &entry_proto)); |
| 464 test_util::RunBlockingPoolTask(); |
| 465 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); |
| 466 EXPECT_FALSE(entry_proto.get()); |
| 467 } |
| 468 |
| 469 TEST(GDataDirectoryServiceTest, ReadDirectoryByPath) { |
| 470 MessageLoopForUI message_loop; |
| 471 GDataDirectoryService directory_service; |
| 472 InitDirectoryService(&directory_service); |
| 473 |
| 474 // Confirm that an existing directory is found. |
| 475 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 476 scoped_ptr<GDataEntryProtoVector> entries; |
| 477 directory_service.ReadDirectoryByPath( |
| 478 FilePath::FromUTF8Unsafe("drive/dir1"), |
| 479 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, |
| 480 &error, &entries)); |
| 481 test_util::RunBlockingPoolTask(); |
| 482 EXPECT_EQ(GDATA_FILE_OK, error); |
| 483 ASSERT_TRUE(entries.get()); |
| 484 ASSERT_EQ(3U, entries->size()); |
| 485 |
| 486 // The order is not guaranteed so we should sort the base names. |
| 487 std::vector<std::string> base_names; |
| 488 for (size_t i = 0; i < 3; ++i) |
| 489 base_names.push_back(entries->at(i).base_name()); |
| 490 std::sort(base_names.begin(), base_names.end()); |
| 491 |
| 492 EXPECT_EQ("dir3", base_names[0]); |
| 493 EXPECT_EQ("file4", base_names[1]); |
| 494 EXPECT_EQ("file5", base_names[2]); |
| 495 |
| 496 // Confirm that a non existing directory is not found. |
| 497 error = GDATA_FILE_ERROR_FAILED; |
| 498 entries.reset(); |
| 499 directory_service.ReadDirectoryByPath( |
| 500 FilePath::FromUTF8Unsafe("drive/non_existing"), |
| 501 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, |
| 502 &error, &entries)); |
| 503 test_util::RunBlockingPoolTask(); |
| 504 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error); |
| 505 EXPECT_FALSE(entries.get()); |
| 506 |
| 507 // Confirm that reading a file results in GDATA_FILE_ERROR_NOT_A_DIRECTORY. |
| 508 error = GDATA_FILE_ERROR_FAILED; |
| 509 entries.reset(); |
| 510 directory_service.ReadDirectoryByPath( |
| 511 FilePath::FromUTF8Unsafe("drive/dir1/file4"), |
| 512 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, |
| 513 &error, &entries)); |
| 514 test_util::RunBlockingPoolTask(); |
| 515 EXPECT_EQ(GDATA_FILE_ERROR_NOT_A_DIRECTORY, error); |
| 516 EXPECT_FALSE(entries.get()); |
| 517 } |
| 518 |
| 519 TEST(GDataDirectoryServiceTest, DBTest) { |
440 MessageLoopForUI message_loop; | 520 MessageLoopForUI message_loop; |
441 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 521 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
442 &message_loop); | 522 &message_loop); |
443 | 523 |
444 scoped_ptr<TestingProfile> profile(new TestingProfile); | 524 scoped_ptr<TestingProfile> profile(new TestingProfile); |
445 scoped_refptr<base::SequencedWorkerPool> pool = | 525 scoped_refptr<base::SequencedWorkerPool> pool = |
446 content::BrowserThread::GetBlockingPool(); | 526 content::BrowserThread::GetBlockingPool(); |
447 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = | 527 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
448 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | 528 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
449 | 529 |
(...skipping 14 matching lines...) Expand all Loading... |
464 GDataDirectoryService directory_service2; | 544 GDataDirectoryService directory_service2; |
465 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. | 545 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. |
466 directory_service2.InitFromDB(db_path, blocking_task_runner, | 546 directory_service2.InitFromDB(db_path, blocking_task_runner, |
467 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); | 547 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); |
468 test_util::RunBlockingPoolTask(); | 548 test_util::RunBlockingPoolTask(); |
469 | 549 |
470 VerifyDirectoryService(&directory_service2); | 550 VerifyDirectoryService(&directory_service2); |
471 } | 551 } |
472 | 552 |
473 } // namespace gdata | 553 } // namespace gdata |
OLD | NEW |