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 <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
| 10 |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "base/string_number_conversions.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" |
10 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
11 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | 15 #include "chrome/browser/chromeos/gdata/gdata.pb.h" |
| 16 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
12 #include "chrome/browser/chromeos/gdata/gdata_test_util.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_test_util.h" |
| 18 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/public/test/test_browser_thread.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
14 | 21 |
15 namespace gdata { | 22 namespace gdata { |
16 | |
17 namespace { | 23 namespace { |
18 | 24 |
19 // See gdata.proto for the difference between the two URLs. | 25 // See gdata.proto for the difference between the two URLs. |
20 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; | 26 const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; |
21 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; | 27 const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; |
22 | 28 |
| 29 // Add a directory to |parent| and return that directory. The name and |
| 30 // resource_id are determined by the incrementing counter |sequence_id|. |
| 31 GDataDirectory* AddDirectory(GDataDirectory* parent, |
| 32 GDataDirectoryService* directory_service, |
| 33 int sequence_id) { |
| 34 GDataDirectory* dir = new GDataDirectory(parent, directory_service); |
| 35 const std::string dir_name = "dir" + base::IntToString(sequence_id); |
| 36 const std::string resource_id = std::string("dir_resource_id:") + |
| 37 dir_name; |
| 38 dir->set_title(dir_name); |
| 39 dir->set_resource_id(resource_id); |
| 40 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 41 directory_service->AddEntryToDirectory( |
| 42 parent->GetFilePath(), |
| 43 dir, |
| 44 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); |
| 45 test_util::RunBlockingPoolTask(); |
| 46 EXPECT_EQ(GDATA_FILE_OK, error); |
| 47 return dir; |
| 48 } |
| 49 |
| 50 // Add a file to |parent| and return that file. The name and |
| 51 // resource_id are determined by the incrementing counter |sequence_id|. |
| 52 GDataFile* AddFile(GDataDirectory* parent, |
| 53 GDataDirectoryService* directory_service, |
| 54 int sequence_id) { |
| 55 GDataFile* file = new GDataFile(parent, directory_service); |
| 56 const std::string title = "file" + base::IntToString(sequence_id); |
| 57 const std::string resource_id = std::string("file_resource_id:") + |
| 58 title; |
| 59 file->set_title(title); |
| 60 file->set_resource_id(resource_id); |
| 61 file->set_file_md5(std::string("file_md5:") + title); |
| 62 GDataFileError error = GDATA_FILE_ERROR_FAILED; |
| 63 directory_service->AddEntryToDirectory( |
| 64 parent->GetFilePath(), |
| 65 file, |
| 66 base::Bind(&test_util::CopyErrorCodeFromFileOperationCallback, &error)); |
| 67 test_util::RunBlockingPoolTask(); |
| 68 EXPECT_EQ(GDATA_FILE_OK, error); |
| 69 return file; |
| 70 } |
| 71 |
| 72 // Creates the following files/directories |
| 73 // drive/dir1/ |
| 74 // drive/dir2/ |
| 75 // drive/dir1/dir3/ |
| 76 // drive/dir1/file4 |
| 77 // drive/dir1/file5 |
| 78 // drive/dir2/file6 |
| 79 // drive/dir2/file7 |
| 80 // drive/dir2/file8 |
| 81 // drive/dir1/dir3/file9 |
| 82 // drive/dir1/dir3/file10 |
| 83 void InitDirectoryService(GDataDirectoryService* directory_service) { |
| 84 int sequence_id = 1; |
| 85 GDataDirectory* dir1 = AddDirectory(directory_service->root(), |
| 86 directory_service, sequence_id++); |
| 87 GDataDirectory* dir2 = AddDirectory(directory_service->root(), |
| 88 directory_service, sequence_id++); |
| 89 GDataDirectory* dir3 = AddDirectory(dir1, directory_service, sequence_id++); |
| 90 |
| 91 AddFile(dir1, directory_service, sequence_id++); |
| 92 AddFile(dir1, directory_service, sequence_id++); |
| 93 |
| 94 AddFile(dir2, directory_service, sequence_id++); |
| 95 AddFile(dir2, directory_service, sequence_id++); |
| 96 AddFile(dir2, directory_service, sequence_id++); |
| 97 |
| 98 AddFile(dir3, directory_service, sequence_id++); |
| 99 AddFile(dir3, directory_service, sequence_id++); |
| 100 } |
| 101 |
| 102 // Find directory by path. |
| 103 GDataDirectory* FindDirectory(GDataDirectoryService* directory_service, |
| 104 const char* path) { |
| 105 return directory_service->FindEntryByPathSync( |
| 106 FilePath(path))->AsGDataDirectory(); |
| 107 } |
| 108 |
| 109 // Find file by path. |
| 110 GDataFile* FindFile(GDataDirectoryService* directory_service, |
| 111 const char* path) { |
| 112 return directory_service->FindEntryByPathSync(FilePath(path))->AsGDataFile(); |
| 113 } |
| 114 |
| 115 // Verify that the recreated directory service matches what we created in |
| 116 // InitDirectoryService. |
| 117 void VerifyDirectoryService(GDataDirectoryService* directory_service) { |
| 118 ASSERT_TRUE(directory_service->root()); |
| 119 |
| 120 GDataDirectory* dir1 = FindDirectory(directory_service, "drive/dir1"); |
| 121 ASSERT_TRUE(dir1); |
| 122 GDataDirectory* dir2 = FindDirectory(directory_service, "drive/dir2"); |
| 123 ASSERT_TRUE(dir2); |
| 124 GDataDirectory* dir3 = FindDirectory(directory_service, "drive/dir1/dir3"); |
| 125 ASSERT_TRUE(dir3); |
| 126 |
| 127 GDataFile* file4 = FindFile(directory_service, "drive/dir1/file4"); |
| 128 ASSERT_TRUE(file4); |
| 129 EXPECT_EQ(file4->parent(), dir1); |
| 130 |
| 131 GDataFile* file5 = FindFile(directory_service, "drive/dir1/file5"); |
| 132 ASSERT_TRUE(file5); |
| 133 EXPECT_EQ(file5->parent(), dir1); |
| 134 |
| 135 GDataFile* file6 = FindFile(directory_service, "drive/dir2/file6"); |
| 136 ASSERT_TRUE(file6); |
| 137 EXPECT_EQ(file6->parent(), dir2); |
| 138 |
| 139 GDataFile* file7 = FindFile(directory_service, "drive/dir2/file7"); |
| 140 ASSERT_TRUE(file7); |
| 141 EXPECT_EQ(file7->parent(), dir2); |
| 142 |
| 143 GDataFile* file8 = FindFile(directory_service, "drive/dir2/file8"); |
| 144 ASSERT_TRUE(file8); |
| 145 EXPECT_EQ(file8->parent(), dir2); |
| 146 |
| 147 GDataFile* file9 = FindFile(directory_service, "drive/dir1/dir3/file9"); |
| 148 ASSERT_TRUE(file9); |
| 149 EXPECT_EQ(file9->parent(), dir3); |
| 150 |
| 151 GDataFile* file10 = FindFile(directory_service, "drive/dir1/dir3/file10"); |
| 152 ASSERT_TRUE(file10); |
| 153 EXPECT_EQ(file10->parent(), dir3); |
| 154 |
| 155 EXPECT_EQ(dir1, directory_service->GetEntryByResourceId( |
| 156 "dir_resource_id:dir1")); |
| 157 EXPECT_EQ(dir2, directory_service->GetEntryByResourceId( |
| 158 "dir_resource_id:dir2")); |
| 159 EXPECT_EQ(dir3, directory_service->GetEntryByResourceId( |
| 160 "dir_resource_id:dir3")); |
| 161 EXPECT_EQ(file4, directory_service->GetEntryByResourceId( |
| 162 "file_resource_id:file4")); |
| 163 EXPECT_EQ(file5, directory_service->GetEntryByResourceId( |
| 164 "file_resource_id:file5")); |
| 165 EXPECT_EQ(file6, directory_service->GetEntryByResourceId( |
| 166 "file_resource_id:file6")); |
| 167 EXPECT_EQ(file7, directory_service->GetEntryByResourceId( |
| 168 "file_resource_id:file7")); |
| 169 EXPECT_EQ(file8, directory_service->GetEntryByResourceId( |
| 170 "file_resource_id:file8")); |
| 171 EXPECT_EQ(file9, directory_service->GetEntryByResourceId( |
| 172 "file_resource_id:file9")); |
| 173 EXPECT_EQ(file10, directory_service->GetEntryByResourceId( |
| 174 "file_resource_id:file10")); |
| 175 } |
| 176 |
| 177 // Callback for GDataDirectoryService::InitFromDB. |
| 178 void InitFromDBCallback(GDataFileError expected_error, |
| 179 GDataFileError actual_error) { |
| 180 EXPECT_EQ(expected_error, actual_error); |
| 181 } |
| 182 |
23 } // namespace | 183 } // namespace |
24 | 184 |
25 TEST(GDataEntryTest, FromProto_DetectBadUploadUrl) { | 185 TEST(GDataEntryTest, FromProto_DetectBadUploadUrl) { |
26 GDataEntryProto proto; | 186 GDataEntryProto proto; |
27 proto.set_title("test.txt"); | 187 proto.set_title("test.txt"); |
28 | 188 |
29 GDataEntry entry(NULL, NULL); | 189 GDataEntry entry(NULL, NULL); |
30 // This should fail as the upload URL is empty. | 190 // This should fail as the upload URL is empty. |
31 ASSERT_FALSE(entry.FromProto(proto)); | 191 ASSERT_FALSE(entry.FromProto(proto)); |
32 | 192 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 429 |
270 TEST(GDataRootDirectoryTest, GetEntryByResourceId_RootDirectory) { | 430 TEST(GDataRootDirectoryTest, GetEntryByResourceId_RootDirectory) { |
271 GDataDirectoryService directory_service; | 431 GDataDirectoryService directory_service; |
272 // Look up the root directory by its resource ID. | 432 // Look up the root directory by its resource ID. |
273 GDataEntry* entry = directory_service.GetEntryByResourceId( | 433 GDataEntry* entry = directory_service.GetEntryByResourceId( |
274 kGDataRootDirectoryResourceId); | 434 kGDataRootDirectoryResourceId); |
275 ASSERT_TRUE(entry); | 435 ASSERT_TRUE(entry); |
276 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); | 436 EXPECT_EQ(kGDataRootDirectoryResourceId, entry->resource_id()); |
277 } | 437 } |
278 | 438 |
| 439 TEST(GDataRootDirectoryTest, DBTest) { |
| 440 MessageLoopForUI message_loop; |
| 441 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 442 &message_loop); |
| 443 |
| 444 scoped_ptr<TestingProfile> profile(new TestingProfile); |
| 445 scoped_refptr<base::SequencedWorkerPool> pool = |
| 446 content::BrowserThread::GetBlockingPool(); |
| 447 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = |
| 448 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
| 449 |
| 450 GDataDirectoryService directory_service; |
| 451 FilePath db_path(GDataCache::GetCacheRootPath(profile.get()). |
| 452 AppendASCII("meta").AppendASCII("resource_metadata.db")); |
| 453 // InitFromDB should fail with GDATA_FILE_ERROR_NOT_FOUND since the db |
| 454 // doesn't exist. |
| 455 directory_service.InitFromDB(db_path, blocking_task_runner, |
| 456 base::Bind(&InitFromDBCallback, GDATA_FILE_ERROR_NOT_FOUND)); |
| 457 test_util::RunBlockingPoolTask(); |
| 458 InitDirectoryService(&directory_service); |
| 459 |
| 460 // Write the filesystem to db. |
| 461 directory_service.SaveToDB(); |
| 462 test_util::RunBlockingPoolTask(); |
| 463 |
| 464 GDataDirectoryService directory_service2; |
| 465 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. |
| 466 directory_service2.InitFromDB(db_path, blocking_task_runner, |
| 467 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); |
| 468 test_util::RunBlockingPoolTask(); |
| 469 |
| 470 VerifyDirectoryService(&directory_service2); |
| 471 } |
| 472 |
279 } // namespace gdata | 473 } // namespace gdata |
OLD | NEW |