| 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_directory_service.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_directory_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 EXPECT_EQ(file10, directory_service->GetEntryByResourceId( | 183 EXPECT_EQ(file10, directory_service->GetEntryByResourceId( |
| 184 "file_resource_id:file10")); | 184 "file_resource_id:file10")); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Callback for GDataDirectoryService::InitFromDB. | 187 // Callback for GDataDirectoryService::InitFromDB. |
| 188 void InitFromDBCallback(GDataFileError expected_error, | 188 void InitFromDBCallback(GDataFileError expected_error, |
| 189 GDataFileError actual_error) { | 189 GDataFileError actual_error) { |
| 190 EXPECT_EQ(expected_error, actual_error); | 190 EXPECT_EQ(expected_error, actual_error); |
| 191 } | 191 } |
| 192 | 192 |
| 193 // Callback for GDataDirectoryService::ReadDirectoryByPath. |
| 194 void ReadDirectoryByPathCallback( |
| 195 scoped_ptr<GDataEntryProtoVector>* result, |
| 196 GDataFileError error, |
| 197 scoped_ptr<GDataEntryProtoVector> entries) { |
| 198 EXPECT_EQ(GDATA_FILE_OK, error); |
| 199 *result = entries.Pass(); |
| 200 } |
| 201 |
| 193 } // namespace | 202 } // namespace |
| 194 | 203 |
| 195 TEST(GDataDirectoryServiceTest, VersionCheck) { | 204 TEST(GDataDirectoryServiceTest, VersionCheck) { |
| 196 // Set up the root directory. | 205 // Set up the root directory. |
| 197 GDataRootDirectoryProto proto; | 206 GDataRootDirectoryProto proto; |
| 198 GDataEntryProto* mutable_entry = | 207 GDataEntryProto* mutable_entry = |
| 199 proto.mutable_gdata_directory()->mutable_gdata_entry(); | 208 proto.mutable_gdata_directory()->mutable_gdata_entry(); |
| 200 mutable_entry->mutable_file_info()->set_is_directory(true); | 209 mutable_entry->mutable_file_info()->set_is_directory(true); |
| 201 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | 210 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); |
| 202 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 211 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 | 301 |
| 293 // This should succeed as the resource ID is correct. | 302 // This should succeed as the resource ID is correct. |
| 294 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 303 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
| 295 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | 304 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); |
| 296 } | 305 } |
| 297 | 306 |
| 298 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here | 307 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here |
| 299 // is to ensure that an error in GDataFile::FromProto() is properly | 308 // is to ensure that an error in GDataFile::FromProto() is properly |
| 300 // propagated to GDataRootDirectory::ParseFromString(). | 309 // propagated to GDataRootDirectory::ParseFromString(). |
| 301 TEST(GDataDirectoryServiceTest, ParseFromString_DetectNoUploadUrl) { | 310 TEST(GDataDirectoryServiceTest, ParseFromString_DetectNoUploadUrl) { |
| 311 // Need to run on UI thread to call |
| 312 // GDataDirectoryService::ReadDirectoryByPath. |
| 313 MessageLoopForUI message_loop; |
| 314 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 315 &message_loop); |
| 316 |
| 302 // Set up the root directory properly. | 317 // Set up the root directory properly. |
| 303 GDataRootDirectoryProto root_directory_proto; | 318 GDataRootDirectoryProto root_directory_proto; |
| 304 root_directory_proto.set_version(kProtoVersion); | 319 root_directory_proto.set_version(kProtoVersion); |
| 305 | 320 |
| 306 GDataEntryProto* mutable_entry = | 321 GDataEntryProto* mutable_entry = |
| 307 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); | 322 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); |
| 308 mutable_entry->mutable_file_info()->set_is_directory(true); | 323 mutable_entry->mutable_file_info()->set_is_directory(true); |
| 309 mutable_entry->set_title(kGDataRootDirectory); | 324 mutable_entry->set_title(kGDataRootDirectory); |
| 310 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | 325 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); |
| 311 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | 326 mutable_entry->set_upload_url(kResumableCreateMediaUrl); |
| 312 | 327 |
| 313 // Add an empty sub directory under the root directory. This directory is | 328 // Add an empty sub directory under the root directory. This directory is |
| 314 // added to ensure that nothing is left when the parsing failed. | 329 // added to ensure that nothing is left when the parsing failed. |
| 315 GDataDirectoryProto* sub_directory_proto = | 330 GDataDirectoryProto* sub_directory_proto = |
| 316 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | 331 root_directory_proto.mutable_gdata_directory()->add_child_directories(); |
| 317 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | 332 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> |
| 318 set_is_directory(true); | 333 set_is_directory(true); |
| 319 sub_directory_proto->mutable_gdata_entry()->set_title("empty"); | 334 sub_directory_proto->mutable_gdata_entry()->set_title("empty"); |
| 335 sub_directory_proto->mutable_gdata_entry()->set_resource_id("res:empty"); |
| 320 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | 336 sub_directory_proto->mutable_gdata_entry()->set_upload_url( |
| 321 kResumableCreateMediaUrl); | 337 kResumableCreateMediaUrl); |
| 322 | 338 |
| 323 // Add a sub directory under the root directory. | 339 // Add a sub directory under the root directory. |
| 324 sub_directory_proto = | 340 sub_directory_proto = |
| 325 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | 341 root_directory_proto.mutable_gdata_directory()->add_child_directories(); |
| 326 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | 342 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> |
| 327 set_is_directory(true); | 343 set_is_directory(true); |
| 328 sub_directory_proto->mutable_gdata_entry()->set_title("dir"); | 344 sub_directory_proto->mutable_gdata_entry()->set_title("dir"); |
| 345 sub_directory_proto->mutable_gdata_entry()->set_resource_id("res:dir"); |
| 329 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | 346 sub_directory_proto->mutable_gdata_entry()->set_upload_url( |
| 330 kResumableCreateMediaUrl); | 347 kResumableCreateMediaUrl); |
| 331 | 348 |
| 332 // Add a new file under the sub directory "dir". | 349 // Add a new file under the sub directory "dir". |
| 333 GDataEntryProto* entry_proto = | 350 GDataEntryProto* entry_proto = |
| 334 sub_directory_proto->add_child_files(); | 351 sub_directory_proto->add_child_files(); |
| 335 entry_proto->set_title("test.txt"); | 352 entry_proto->set_title("test.txt"); |
| 353 entry_proto->set_resource_id("res:file"); |
| 336 entry_proto->mutable_file_specific_info()->set_file_md5("md5"); | 354 entry_proto->mutable_file_specific_info()->set_file_md5("md5"); |
| 337 | 355 |
| 338 GDataDirectoryService directory_service; | 356 GDataDirectoryService directory_service; |
| 339 GDataDirectory* root(directory_service.root()); | |
| 340 // The origin is set to UNINITIALIZED by default. | 357 // The origin is set to UNINITIALIZED by default. |
| 341 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | 358 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); |
| 342 std::string serialized_proto; | 359 std::string serialized_proto; |
| 343 // Serialize the proto and check if it's loaded. | 360 // Serialize the proto and check if it's loaded. |
| 344 // This should fail as the upload URL is not set for |entry_proto|. | 361 // This should fail as the upload URL is not set for |entry_proto|. |
| 345 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | 362 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); |
| 346 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | 363 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); |
| 364 |
| 347 // Nothing should be added to the root directory if the parse failed. | 365 // Nothing should be added to the root directory if the parse failed. |
| 348 ASSERT_TRUE(root->child_files().empty()); | 366 scoped_ptr<GDataEntryProtoVector> result; |
| 349 ASSERT_TRUE(root->child_directories().empty()); | 367 directory_service.ReadDirectoryByPath(FilePath(kGDataRootDirectory), |
| 368 base::Bind(&ReadDirectoryByPathCallback, &result)); |
| 369 test_util::RunBlockingPoolTask(); |
| 370 EXPECT_TRUE(result->empty()); |
| 371 |
| 350 // The origin should remain UNINITIALIZED because the loading failed. | 372 // The origin should remain UNINITIALIZED because the loading failed. |
| 351 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | 373 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); |
| 352 | 374 |
| 353 // Set an upload URL. | 375 // Set an upload URL. |
| 354 entry_proto->set_upload_url(kResumableEditMediaUrl); | 376 entry_proto->set_upload_url(kResumableEditMediaUrl); |
| 355 | 377 |
| 356 // Serialize the proto and check if it's loaded. | 378 // Serialize the proto and check if it's loaded. |
| 357 // This should succeed as the upload URL is set for |entry_proto|. | 379 // This should succeed as the upload URL is set for |entry_proto|. |
| 358 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | 380 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); |
| 359 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 381 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
| 360 // No file should be added to the root directory. | 382 directory_service.ReadDirectoryByPath(FilePath(kGDataRootDirectory), |
| 361 ASSERT_TRUE(root->child_files().empty()); | 383 base::Bind(&ReadDirectoryByPathCallback, &result)); |
| 384 test_util::RunBlockingPoolTask(); |
| 362 // Two directories ("empty", "dir") should be added to the root directory. | 385 // Two directories ("empty", "dir") should be added to the root directory. |
| 363 ASSERT_EQ(2U, root->child_directories().size()); | 386 EXPECT_EQ(2U, result->size()); |
| 364 // The origin should change to FROM_CACHE because we loaded from the cache. | 387 // The origin should change to FROM_CACHE because we loaded from the cache. |
| 365 ASSERT_EQ(FROM_CACHE, directory_service.origin()); | 388 ASSERT_EQ(FROM_CACHE, directory_service.origin()); |
| 366 } | 389 } |
| 367 | 390 |
| 368 TEST(GDataDirectoryServiceTest, RefreshFile) { | 391 TEST(GDataDirectoryServiceTest, RefreshFile) { |
| 369 MessageLoopForUI message_loop; | 392 MessageLoopForUI message_loop; |
| 370 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 393 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 371 &message_loop); | 394 &message_loop); |
| 372 | 395 |
| 373 GDataDirectoryService directory_service; | 396 GDataDirectoryService directory_service; |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 GDataDirectoryService directory_service2; | 644 GDataDirectoryService directory_service2; |
| 622 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. | 645 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. |
| 623 directory_service2.InitFromDB(db_path, blocking_task_runner, | 646 directory_service2.InitFromDB(db_path, blocking_task_runner, |
| 624 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); | 647 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); |
| 625 test_util::RunBlockingPoolTask(); | 648 test_util::RunBlockingPoolTask(); |
| 626 | 649 |
| 627 VerifyDirectoryService(&directory_service2); | 650 VerifyDirectoryService(&directory_service2); |
| 628 } | 651 } |
| 629 | 652 |
| 630 } // namespace gdata | 653 } // namespace gdata |
| OLD | NEW |