Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_files_unittest.cc

Issue 10837148: gdata: Add GetEntryInfoByPath() and ReadDirectoryByPath() to GDataDirectoryService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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
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
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) {
440 MessageLoopForUI message_loop; 441 MessageLoopForUI message_loop;
441 content::TestBrowserThread ui_thread(content::BrowserThread::UI, 442 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
442 &message_loop); 443 &message_loop);
444 GDataDirectoryService directory_service;
445 InitDirectoryService(&directory_service);
446
447 // Confirm that an existing file is found.
448 GDataFileError error = GDATA_FILE_ERROR_FAILED;
449 scoped_ptr<GDataEntryProto> entry_proto;
450 directory_service.GetEntryInfoByPath(
451 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
452 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback,
453 &error, &entry_proto));
454 test_util::RunBlockingPoolTask();
455 EXPECT_EQ(GDATA_FILE_OK, error);
456 ASSERT_TRUE(entry_proto.get());
457 EXPECT_EQ("file4", entry_proto->base_name());
458
459 // Confirm that a non existing file is not found.
460 error = GDATA_FILE_ERROR_FAILED;
461 entry_proto.reset();
462 directory_service.GetEntryInfoByPath(
463 FilePath::FromUTF8Unsafe("drive/dir1/non_existing"),
464 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback,
465 &error, &entry_proto));
466 test_util::RunBlockingPoolTask();
467 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error);
468 EXPECT_FALSE(entry_proto.get());
469 }
470
471 TEST(GDataDirectoryServiceTest, ReadDirectoryByPath) {
472 MessageLoopForUI message_loop;
473 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
474 &message_loop);
475 GDataDirectoryService directory_service;
476 InitDirectoryService(&directory_service);
477
478 // Confirm that an existing directory is found.
479 GDataFileError error = GDATA_FILE_ERROR_FAILED;
480 scoped_ptr<GDataEntryProtoVector> entries;
481 directory_service.ReadDirectoryByPath(
482 FilePath::FromUTF8Unsafe("drive/dir1"),
483 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback,
484 &error, &entries));
485 test_util::RunBlockingPoolTask();
486 EXPECT_EQ(GDATA_FILE_OK, error);
487 ASSERT_TRUE(entries.get());
488 ASSERT_EQ(3U, entries->size());
489
490 // The order is not guaranteed so we should sort the base names.
491 std::vector<std::string> base_names;
492 for (size_t i = 0; i < 3; ++i)
493 base_names.push_back(entries->at(i).base_name());
494 std::sort(base_names.begin(), base_names.end());
495
496 EXPECT_EQ("dir3", base_names[0]);
497 EXPECT_EQ("file4", base_names[1]);
498 EXPECT_EQ("file5", base_names[2]);
499
500 // Confirm that a non existing directory is not found.
501 error = GDATA_FILE_ERROR_FAILED;
502 entries.reset();
503 directory_service.ReadDirectoryByPath(
504 FilePath::FromUTF8Unsafe("drive/non_existing"),
505 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback,
506 &error, &entries));
507 test_util::RunBlockingPoolTask();
508 EXPECT_EQ(GDATA_FILE_ERROR_NOT_FOUND, error);
509 EXPECT_FALSE(entries.get());
510
511 // Confirm that reading a file results in GDATA_FILE_ERROR_NOT_A_DIRECTORY.
512 error = GDATA_FILE_ERROR_FAILED;
513 entries.reset();
514 directory_service.ReadDirectoryByPath(
515 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
516 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback,
517 &error, &entries));
518 test_util::RunBlockingPoolTask();
519 EXPECT_EQ(GDATA_FILE_ERROR_NOT_A_DIRECTORY, error);
520 EXPECT_FALSE(entries.get());
521 }
522
523 TEST(GDataDirectoryServiceTest, DBTest) {
524 MessageLoopForUI message_loop;
525 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
526 &message_loop);
443 527
444 scoped_ptr<TestingProfile> profile(new TestingProfile); 528 scoped_ptr<TestingProfile> profile(new TestingProfile);
445 scoped_refptr<base::SequencedWorkerPool> pool = 529 scoped_refptr<base::SequencedWorkerPool> pool =
446 content::BrowserThread::GetBlockingPool(); 530 content::BrowserThread::GetBlockingPool();
447 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = 531 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner =
448 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); 532 pool->GetSequencedTaskRunner(pool->GetSequenceToken());
449 533
450 GDataDirectoryService directory_service; 534 GDataDirectoryService directory_service;
451 FilePath db_path(GDataCache::GetCacheRootPath(profile.get()). 535 FilePath db_path(GDataCache::GetCacheRootPath(profile.get()).
452 AppendASCII("meta").AppendASCII("resource_metadata.db")); 536 AppendASCII("meta").AppendASCII("resource_metadata.db"));
(...skipping 11 matching lines...) Expand all
464 GDataDirectoryService directory_service2; 548 GDataDirectoryService directory_service2;
465 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. 549 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists.
466 directory_service2.InitFromDB(db_path, blocking_task_runner, 550 directory_service2.InitFromDB(db_path, blocking_task_runner,
467 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); 551 base::Bind(&InitFromDBCallback, GDATA_FILE_OK));
468 test_util::RunBlockingPoolTask(); 552 test_util::RunBlockingPoolTask();
469 553
470 VerifyDirectoryService(&directory_service2); 554 VerifyDirectoryService(&directory_service2);
471 } 555 }
472 556
473 } // namespace gdata 557 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/gdata_files.cc ('k') | chrome/browser/chromeos/gdata/gdata_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698