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