| 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 <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 13 #include "base/string16.h" | 15 #include "base/string16.h" |
| 14 #include "base/time.h" | 16 #include "base/time.h" |
| 15 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 16 #include "base/values.h" | 18 #include "base/values.h" |
| 17 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" | 19 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
| 20 #include "chrome/browser/chromeos/gdata/gdata_mock.h" |
| 18 #include "chrome/browser/chromeos/gdata/gdata_parser.h" | 21 #include "chrome/browser/chromeos/gdata/gdata_parser.h" |
| 19 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/test/test_browser_thread.h" | 24 #include "content/test/test_browser_thread.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 27 |
| 25 using ::testing::AnyNumber; | 28 using ::testing::AnyNumber; |
| 26 using ::testing::Return; | 29 using ::testing::Return; |
| 27 using ::testing::ReturnNull; | 30 using ::testing::ReturnNull; |
| 28 using ::testing::_; | 31 using ::testing::_; |
| 29 | 32 |
| 30 using base::Value; | 33 using base::Value; |
| 31 using base::DictionaryValue; | 34 using base::DictionaryValue; |
| 32 using base::ListValue; | 35 using base::ListValue; |
| 33 | 36 |
| 34 namespace gdata { | 37 namespace gdata { |
| 35 | 38 |
| 36 class GDataFileSystemTest : public testing::Test { | 39 class GDataFileSystemTest : public testing::Test { |
| 37 protected: | 40 protected: |
| 38 GDataFileSystemTest() | 41 GDataFileSystemTest() |
| 39 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 42 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 40 } | 43 } |
| 41 | 44 |
| 42 virtual void SetUp() { | 45 virtual void SetUp() OVERRIDE { |
| 46 callback_helper_ = new CallbackHelper; |
| 43 profile_.reset(new TestingProfile); | 47 profile_.reset(new TestingProfile); |
| 44 file_system_ = GDataFileSystemFactory::GetForProfile(profile_.get()); | 48 file_system_ = GDataFileSystemFactory::GetForProfile(profile_.get()); |
| 49 |
| 50 // Allocate and keep a weak pointer to the mock. |
| 51 mock_doc_service_ = new MockDocumentsService; |
| 52 EXPECT_CALL(*mock_doc_service_, Initialize(profile_.get())).Times(1); |
| 53 |
| 54 // Pass the service for the filesystem to own. |
| 55 scoped_ptr<DocumentsServiceInterface> service(mock_doc_service_); |
| 56 file_system_->ReplaceDocumentsService(service.Pass()); |
| 57 } |
| 58 |
| 59 virtual void TearDown() OVERRIDE { |
| 60 EXPECT_CALL(*mock_doc_service_, CancelAll()).Times(1); |
| 45 } | 61 } |
| 46 | 62 |
| 47 // Loads test json file as root ("/gdata") element. | 63 // Loads test json file as root ("/gdata") element. |
| 48 void LoadRootFeedDocument(const std::string& filename) { | 64 void LoadRootFeedDocument(const std::string& filename) { |
| 49 LoadSubdirFeedDocument(FilePath(FILE_PATH_LITERAL("gdata")), filename); | 65 LoadSubdirFeedDocument(FilePath(FILE_PATH_LITERAL("gdata")), filename); |
| 50 } | 66 } |
| 51 | 67 |
| 52 // Loads test json file as subdirectory content of |directory_path|. | 68 // Loads test json file as subdirectory content of |directory_path|. |
| 53 void LoadSubdirFeedDocument(const FilePath& directory_path, | 69 void LoadSubdirFeedDocument(const FilePath& directory_path, |
| 54 const std::string& filename) { | 70 const std::string& filename) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 EXPECT_TRUE(file_util::PathExists(path)) << | 148 EXPECT_TRUE(file_util::PathExists(path)) << |
| 133 "Couldn't find " << path.value(); | 149 "Couldn't find " << path.value(); |
| 134 | 150 |
| 135 JSONFileValueSerializer serializer(path); | 151 JSONFileValueSerializer serializer(path); |
| 136 Value* value = serializer.Deserialize(NULL, &error); | 152 Value* value = serializer.Deserialize(NULL, &error); |
| 137 EXPECT_TRUE(value) << | 153 EXPECT_TRUE(value) << |
| 138 "Parse error " << path.value() << ": " << error; | 154 "Parse error " << path.value() << ": " << error; |
| 139 return value; | 155 return value; |
| 140 } | 156 } |
| 141 | 157 |
| 158 // This is used as a helper for registering callbacks that need to be |
| 159 // RefCountedThreadSafe, and a place where we can fetch results from various |
| 160 // operations. |
| 161 class CallbackHelper |
| 162 : public base::RefCountedThreadSafe<CallbackHelper> { |
| 163 public: |
| 164 CallbackHelper() : last_error_(base::PLATFORM_FILE_OK) {} |
| 165 virtual ~CallbackHelper() {} |
| 166 virtual void GetFileCallback(base::PlatformFileError error, |
| 167 const FilePath& file_path) { |
| 168 last_error_ = error; |
| 169 download_path_ = file_path; |
| 170 } |
| 171 virtual void FileOperationCallback(base::PlatformFileError error) { |
| 172 last_error_ = error; |
| 173 } |
| 174 |
| 175 base::PlatformFileError last_error_; |
| 176 FilePath download_path_; |
| 177 }; |
| 178 |
| 142 MessageLoopForUI message_loop_; | 179 MessageLoopForUI message_loop_; |
| 143 content::TestBrowserThread ui_thread_; | 180 content::TestBrowserThread ui_thread_; |
| 144 scoped_ptr<TestingProfile> profile_; | 181 scoped_ptr<TestingProfile> profile_; |
| 182 scoped_refptr<CallbackHelper> callback_helper_; |
| 145 GDataFileSystem* file_system_; | 183 GDataFileSystem* file_system_; |
| 184 MockDocumentsService* mock_doc_service_; |
| 146 }; | 185 }; |
| 147 | 186 |
| 148 | 187 |
| 149 // Delegate used to find a directory element for file system updates. | 188 // Delegate used to find a directory element for file system updates. |
| 150 class MockFindFileDelegate : public gdata::FindFileDelegate { | 189 class MockFindFileDelegate : public gdata::FindFileDelegate { |
| 151 public: | 190 public: |
| 152 MockFindFileDelegate() { | 191 MockFindFileDelegate() { |
| 153 } | 192 } |
| 154 | 193 |
| 155 virtual ~MockFindFileDelegate() { | 194 virtual ~MockFindFileDelegate() { |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 EXPECT_EQ( | 458 EXPECT_EQ( |
| 420 GDataFileSystem::FOUND_MISSING, | 459 GDataFileSystem::FOUND_MISSING, |
| 421 file_system_->FindFirstMissingParentDirectory(dir_path2, | 460 file_system_->FindFirstMissingParentDirectory(dir_path2, |
| 422 &last_dir_content_url, | 461 &last_dir_content_url, |
| 423 &first_missing_parent_path)); | 462 &first_missing_parent_path)); |
| 424 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("gdata/Directory 1/New Folder 2")), | 463 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("gdata/Directory 1/New Folder 2")), |
| 425 first_missing_parent_path); | 464 first_missing_parent_path); |
| 426 EXPECT_FALSE(last_dir_content_url.is_empty()); // non-root directory. | 465 EXPECT_FALSE(last_dir_content_url.is_empty()); // non-root directory. |
| 427 | 466 |
| 428 // Missing two folders on the path. | 467 // Missing two folders on the path. |
| 429 FilePath dir_path3 = dir_path2.Append(FILE_PATH_LITERAL("Another Foder")); | 468 FilePath dir_path3 = dir_path2.Append(FILE_PATH_LITERAL("Another Folder")); |
| 430 EXPECT_EQ( | 469 EXPECT_EQ( |
| 431 GDataFileSystem::FOUND_MISSING, | 470 GDataFileSystem::FOUND_MISSING, |
| 432 file_system_->FindFirstMissingParentDirectory(dir_path3, | 471 file_system_->FindFirstMissingParentDirectory(dir_path3, |
| 433 &last_dir_content_url, | 472 &last_dir_content_url, |
| 434 &first_missing_parent_path)); | 473 &first_missing_parent_path)); |
| 435 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("gdata/Directory 1/New Folder 2")), | 474 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("gdata/Directory 1/New Folder 2")), |
| 436 first_missing_parent_path); | 475 first_missing_parent_path); |
| 437 EXPECT_FALSE(last_dir_content_url.is_empty()); // non-root directory. | 476 EXPECT_FALSE(last_dir_content_url.is_empty()); // non-root directory. |
| 438 | 477 |
| 439 // Folders on top of an existing file. | 478 // Folders on top of an existing file. |
| 440 EXPECT_EQ( | 479 EXPECT_EQ( |
| 441 GDataFileSystem::FOUND_INVALID, | 480 GDataFileSystem::FOUND_INVALID, |
| 442 file_system_->FindFirstMissingParentDirectory( | 481 file_system_->FindFirstMissingParentDirectory( |
| 443 FilePath(FILE_PATH_LITERAL("gdata/File 1.txt/BadDir")), | 482 FilePath(FILE_PATH_LITERAL("gdata/File 1.txt/BadDir")), |
| 444 &last_dir_content_url, | 483 &last_dir_content_url, |
| 445 &first_missing_parent_path)); | 484 &first_missing_parent_path)); |
| 446 | 485 |
| 447 // Existing folder. | 486 // Existing folder. |
| 448 EXPECT_EQ( | 487 EXPECT_EQ( |
| 449 GDataFileSystem::DIRECTORY_ALREADY_PRESENT, | 488 GDataFileSystem::DIRECTORY_ALREADY_PRESENT, |
| 450 file_system_->FindFirstMissingParentDirectory( | 489 file_system_->FindFirstMissingParentDirectory( |
| 451 FilePath(FILE_PATH_LITERAL("gdata/Directory 1")), | 490 FilePath(FILE_PATH_LITERAL("gdata/Directory 1")), |
| 452 &last_dir_content_url, | 491 &last_dir_content_url, |
| 453 &first_missing_parent_path)); | 492 &first_missing_parent_path)); |
| 454 } | 493 } |
| 455 | 494 |
| 456 // TODO(satorux): Write a test for GetFile() once DocumentsService is | |
| 457 // mockable. | |
| 458 | |
| 459 TEST_F(GDataFileSystemTest, GetGDataFileInfoFromPath) { | 495 TEST_F(GDataFileSystemTest, GetGDataFileInfoFromPath) { |
| 460 LoadRootFeedDocument("root_feed.json"); | 496 LoadRootFeedDocument("root_feed.json"); |
| 461 | 497 |
| 462 GDataFileBase* file_info = file_system_->GetGDataFileInfoFromPath( | 498 GDataFileBase* file_info = file_system_->GetGDataFileInfoFromPath( |
| 463 FilePath(FILE_PATH_LITERAL("gdata/File 1.txt"))); | 499 FilePath(FILE_PATH_LITERAL("gdata/File 1.txt"))); |
| 464 ASSERT_TRUE(file_info != NULL); | 500 ASSERT_TRUE(file_info != NULL); |
| 465 EXPECT_EQ("https://file_link_self/", file_info->self_url().spec()); | 501 EXPECT_EQ("https://file_link_self/", file_info->self_url().spec()); |
| 466 EXPECT_EQ("https://file_content_url/", file_info->content_url().spec()); | 502 EXPECT_EQ("https://file_content_url/", file_info->content_url().spec()); |
| 467 | 503 |
| 468 GDataFileBase* non_existent = file_system_->GetGDataFileInfoFromPath( | 504 GDataFileBase* non_existent = file_system_->GetGDataFileInfoFromPath( |
| 469 FilePath(FILE_PATH_LITERAL("gdata/Nonexistent.txt"))); | 505 FilePath(FILE_PATH_LITERAL("gdata/Nonexistent.txt"))); |
| 470 ASSERT_TRUE(non_existent == NULL); | 506 ASSERT_TRUE(non_existent == NULL); |
| 471 } | 507 } |
| 472 | 508 |
| 509 // Create a directory through the document service |
| 510 TEST_F(GDataFileSystemTest, CreateDirectoryWithService) { |
| 511 LoadRootFeedDocument("root_feed.json"); |
| 512 EXPECT_CALL(*mock_doc_service_, |
| 513 CreateDirectory(_, "Sample Directory Title", _)).Times(1); |
| 514 |
| 515 // Set last error so it's not a valid error code. |
| 516 callback_helper_->last_error_ = static_cast<base::PlatformFileError>(1); |
| 517 file_system_->CreateDirectory( |
| 518 FilePath(FILE_PATH_LITERAL("gdata/Sample Directory Title")), |
| 519 false, // is_exclusive |
| 520 true, // is_recursive |
| 521 base::Bind(&CallbackHelper::FileOperationCallback, |
| 522 callback_helper_.get())); |
| 523 // TODO(gspencer): Uncomment this when we get a blob that |
| 524 // works that can be returned from the mock. |
| 525 // EXPECT_EQ(base::PLATFORM_FILE_OK, callback_helper_->last_error_); |
| 526 } |
| 527 |
| 528 TEST_F(GDataFileSystemTest, GetFile) { |
| 529 LoadRootFeedDocument("root_feed.json"); |
| 530 |
| 531 GDataFileSystem::GetFileCallback callback = |
| 532 base::Bind(&CallbackHelper::GetFileCallback, |
| 533 callback_helper_.get()); |
| 534 |
| 535 EXPECT_CALL(*mock_doc_service_, |
| 536 DownloadFile(GURL("https://file_content_url/"), _)); |
| 537 |
| 538 FilePath file_in_root(FILE_PATH_LITERAL("gdata/File 1.txt")); |
| 539 file_system_->GetFile(file_in_root, callback); |
| 540 EXPECT_STREQ("file_content_url/", |
| 541 callback_helper_->download_path_.value().c_str()); |
| 542 } |
| 473 } // namespace gdata | 543 } // namespace gdata |
| OLD | NEW |