| 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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 // This should succeed as the version matches the current number. | 230 // This should succeed as the version matches the current number. |
| 231 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | 231 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); |
| 232 | 232 |
| 233 // Set a newer version, and serialize. | 233 // Set a newer version, and serialize. |
| 234 proto.set_version(kProtoVersion + 1); | 234 proto.set_version(kProtoVersion + 1); |
| 235 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | 235 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); |
| 236 // This should fail as the version is newer. | 236 // This should fail as the version is newer. |
| 237 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | 237 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); |
| 238 } | 238 } |
| 239 | 239 |
| 240 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadTitle) { | |
| 241 GDataRootDirectoryProto proto; | |
| 242 proto.set_version(kProtoVersion); | |
| 243 | |
| 244 GDataEntryProto* mutable_entry = | |
| 245 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 246 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 247 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 248 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 249 | |
| 250 std::string serialized_proto; | |
| 251 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 252 | |
| 253 GDataDirectoryService directory_service; | |
| 254 GDataDirectory* root(directory_service.root()); | |
| 255 // This should fail as the title is empty. | |
| 256 // root.title() should be unchanged. | |
| 257 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 258 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 259 | |
| 260 // Setting the title to "gdata". | |
| 261 mutable_entry->set_title("gdata"); | |
| 262 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 263 | |
| 264 // This should fail as the title is not kGDataRootDirectory. | |
| 265 // root.title() should be unchanged. | |
| 266 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 267 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 268 | |
| 269 // Setting the title to kGDataRootDirectory. | |
| 270 mutable_entry->set_title(kGDataRootDirectory); | |
| 271 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 272 | |
| 273 // This should succeed as the title is kGDataRootDirectory. | |
| 274 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 275 ASSERT_EQ(kGDataRootDirectory, root->title()); | |
| 276 } | |
| 277 | |
| 278 TEST(GDataDirectoryServiceTest, ParseFromString_DetectBadResourceID) { | |
| 279 GDataRootDirectoryProto proto; | |
| 280 proto.set_version(kProtoVersion); | |
| 281 | |
| 282 GDataEntryProto* mutable_entry = | |
| 283 proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 284 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 285 mutable_entry->set_title(kGDataRootDirectory); | |
| 286 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 287 | |
| 288 std::string serialized_proto; | |
| 289 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 290 | |
| 291 GDataDirectoryService directory_service; | |
| 292 GDataDirectory* root(directory_service.root()); | |
| 293 // This should fail as the resource ID is empty. | |
| 294 // root.resource_id() should be unchanged. | |
| 295 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 296 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | |
| 297 | |
| 298 // Set the correct resource ID. | |
| 299 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 300 ASSERT_TRUE(proto.SerializeToString(&serialized_proto)); | |
| 301 | |
| 302 // This should succeed as the resource ID is correct. | |
| 303 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 304 EXPECT_EQ(kGDataRootDirectoryResourceId, root->resource_id()); | |
| 305 } | |
| 306 | |
| 307 // We have a similar test in FromProto_DetectBadUploadUrl, but the test here | |
| 308 // is to ensure that an error in GDataFile::FromProto() is properly | |
| 309 // propagated to GDataRootDirectory::ParseFromString(). | |
| 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 | |
| 317 // Set up the root directory properly. | |
| 318 GDataRootDirectoryProto root_directory_proto; | |
| 319 root_directory_proto.set_version(kProtoVersion); | |
| 320 | |
| 321 GDataEntryProto* mutable_entry = | |
| 322 root_directory_proto.mutable_gdata_directory()->mutable_gdata_entry(); | |
| 323 mutable_entry->mutable_file_info()->set_is_directory(true); | |
| 324 mutable_entry->set_title(kGDataRootDirectory); | |
| 325 mutable_entry->set_resource_id(kGDataRootDirectoryResourceId); | |
| 326 mutable_entry->set_upload_url(kResumableCreateMediaUrl); | |
| 327 | |
| 328 // Add an empty sub directory under the root directory. This directory is | |
| 329 // added to ensure that nothing is left when the parsing failed. | |
| 330 GDataDirectoryProto* sub_directory_proto = | |
| 331 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | |
| 332 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | |
| 333 set_is_directory(true); | |
| 334 sub_directory_proto->mutable_gdata_entry()->set_title("empty"); | |
| 335 sub_directory_proto->mutable_gdata_entry()->set_resource_id("res:empty"); | |
| 336 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | |
| 337 kResumableCreateMediaUrl); | |
| 338 | |
| 339 // Add a sub directory under the root directory. | |
| 340 sub_directory_proto = | |
| 341 root_directory_proto.mutable_gdata_directory()->add_child_directories(); | |
| 342 sub_directory_proto->mutable_gdata_entry()->mutable_file_info()-> | |
| 343 set_is_directory(true); | |
| 344 sub_directory_proto->mutable_gdata_entry()->set_title("dir"); | |
| 345 sub_directory_proto->mutable_gdata_entry()->set_resource_id("res:dir"); | |
| 346 sub_directory_proto->mutable_gdata_entry()->set_upload_url( | |
| 347 kResumableCreateMediaUrl); | |
| 348 | |
| 349 // Add a new file under the sub directory "dir". | |
| 350 GDataEntryProto* entry_proto = | |
| 351 sub_directory_proto->add_child_files(); | |
| 352 entry_proto->set_title("test.txt"); | |
| 353 entry_proto->set_resource_id("res:file"); | |
| 354 entry_proto->mutable_file_specific_info()->set_file_md5("md5"); | |
| 355 | |
| 356 GDataDirectoryService directory_service; | |
| 357 // The origin is set to UNINITIALIZED by default. | |
| 358 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | |
| 359 std::string serialized_proto; | |
| 360 // Serialize the proto and check if it's loaded. | |
| 361 // This should fail as the upload URL is not set for |entry_proto|. | |
| 362 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | |
| 363 ASSERT_FALSE(directory_service.ParseFromString(serialized_proto)); | |
| 364 | |
| 365 // Nothing should be added to the root directory if the parse failed. | |
| 366 scoped_ptr<GDataEntryProtoVector> result; | |
| 367 directory_service.ReadDirectoryByPath(FilePath(kGDataRootDirectory), | |
| 368 base::Bind(&ReadDirectoryByPathCallback, &result)); | |
| 369 test_util::RunBlockingPoolTask(); | |
| 370 EXPECT_TRUE(result->empty()); | |
| 371 | |
| 372 // The origin should remain UNINITIALIZED because the loading failed. | |
| 373 ASSERT_EQ(UNINITIALIZED, directory_service.origin()); | |
| 374 | |
| 375 // Set an upload URL. | |
| 376 entry_proto->set_upload_url(kResumableEditMediaUrl); | |
| 377 | |
| 378 // Serialize the proto and check if it's loaded. | |
| 379 // This should succeed as the upload URL is set for |entry_proto|. | |
| 380 ASSERT_TRUE(root_directory_proto.SerializeToString(&serialized_proto)); | |
| 381 ASSERT_TRUE(directory_service.ParseFromString(serialized_proto)); | |
| 382 directory_service.ReadDirectoryByPath(FilePath(kGDataRootDirectory), | |
| 383 base::Bind(&ReadDirectoryByPathCallback, &result)); | |
| 384 test_util::RunBlockingPoolTask(); | |
| 385 // Two directories ("empty", "dir") should be added to the root directory. | |
| 386 EXPECT_EQ(2U, result->size()); | |
| 387 // The origin should change to FROM_CACHE because we loaded from the cache. | |
| 388 ASSERT_EQ(FROM_CACHE, directory_service.origin()); | |
| 389 } | |
| 390 | |
| 391 TEST(GDataDirectoryServiceTest, RefreshFile) { | 240 TEST(GDataDirectoryServiceTest, RefreshFile) { |
| 392 MessageLoopForUI message_loop; | 241 MessageLoopForUI message_loop; |
| 393 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | 242 content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 394 &message_loop); | 243 &message_loop); |
| 395 | 244 |
| 396 GDataDirectoryService directory_service; | 245 GDataDirectoryService directory_service; |
| 397 // Add a directory to the file system. | 246 // Add a directory to the file system. |
| 398 GDataDirectory* directory_entry = directory_service.CreateGDataDirectory(); | 247 GDataDirectory* directory_entry = directory_service.CreateGDataDirectory(); |
| 399 directory_entry->set_resource_id("folder:directory_resource_id"); | 248 directory_entry->set_resource_id("folder:directory_resource_id"); |
| 400 directory_entry->set_title("directory"); | 249 directory_entry->set_title("directory"); |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 GDataDirectoryService directory_service2; | 493 GDataDirectoryService directory_service2; |
| 645 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. | 494 // InitFromDB should succeed with GDATA_FILE_OK as the db now exists. |
| 646 directory_service2.InitFromDB(db_path, blocking_task_runner, | 495 directory_service2.InitFromDB(db_path, blocking_task_runner, |
| 647 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); | 496 base::Bind(&InitFromDBCallback, GDATA_FILE_OK)); |
| 648 test_util::RunBlockingPoolTask(); | 497 test_util::RunBlockingPoolTask(); |
| 649 | 498 |
| 650 VerifyDirectoryService(&directory_service2); | 499 VerifyDirectoryService(&directory_service2); |
| 651 } | 500 } |
| 652 | 501 |
| 653 } // namespace gdata | 502 } // namespace gdata |
| OLD | NEW |