OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/drive/file_system/copy_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "chrome/browser/chromeos/drive/file_cache_observer.h" | 8 #include "chrome/browser/chromeos/drive/file_cache_observer.h" |
9 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | 9 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
10 #include "chrome/browser/chromeos/drive/file_system_util.h" | 10 #include "chrome/browser/chromeos/drive/file_system_util.h" |
11 #include "chrome/browser/google_apis/fake_drive_service.h" | 11 #include "chrome/browser/google_apis/fake_drive_service.h" |
12 #include "chrome/browser/google_apis/test_util.h" | 12 #include "chrome/browser/google_apis/test_util.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace drive { | 15 namespace drive { |
16 namespace file_system { | 16 namespace file_system { |
17 | 17 |
18 namespace { | |
19 class CacheCommitObserver : public internal::FileCacheObserver { | |
20 public: | |
21 virtual void OnCacheCommitted(const std::string& resource_id) OVERRIDE { | |
22 last_committed_resource_id_ = resource_id; | |
23 } | |
24 const std::string last_committed_resource_id() const { | |
25 return last_committed_resource_id_; | |
26 } | |
27 | |
28 private: | |
29 std::string last_committed_resource_id_; | |
30 }; | |
31 | |
32 } // namespace | |
33 | |
34 class CopyOperationTest : public OperationTestBase { | 18 class CopyOperationTest : public OperationTestBase { |
35 protected: | 19 protected: |
36 virtual void SetUp() OVERRIDE { | 20 virtual void SetUp() OVERRIDE { |
37 OperationTestBase::SetUp(); | 21 OperationTestBase::SetUp(); |
38 operation_.reset(new CopyOperation(blocking_task_runner(), | 22 operation_.reset(new CopyOperation(blocking_task_runner(), |
39 observer(), | 23 observer(), |
40 scheduler(), | 24 scheduler(), |
41 metadata(), | 25 metadata(), |
42 cache(), | 26 cache(), |
43 fake_service())); | 27 fake_service())); |
44 } | 28 } |
45 | 29 |
46 scoped_ptr<CopyOperation> operation_; | 30 scoped_ptr<CopyOperation> operation_; |
47 }; | 31 }; |
48 | 32 |
49 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_RegularFile) { | 33 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_RegularFile) { |
50 const base::FilePath local_src_path = temp_dir().AppendASCII("local.txt"); | 34 const base::FilePath local_src_path = temp_dir().AppendASCII("local.txt"); |
51 const base::FilePath remote_dest_path( | 35 const base::FilePath remote_dest_path( |
52 FILE_PATH_LITERAL("drive/root/remote.txt")); | 36 FILE_PATH_LITERAL("drive/root/remote.txt")); |
53 | 37 |
54 // Prepare a local file. | 38 // Prepare a local file. |
55 ASSERT_TRUE( | 39 ASSERT_TRUE( |
56 google_apis::test_util::WriteStringToFile(local_src_path, "hello")); | 40 google_apis::test_util::WriteStringToFile(local_src_path, "hello")); |
57 // Confirm that the remote file does not exist. | 41 // Confirm that the remote file does not exist. |
58 ResourceEntry entry; | 42 ResourceEntry entry; |
59 ASSERT_EQ(FILE_ERROR_NOT_FOUND, | 43 ASSERT_EQ(FILE_ERROR_NOT_FOUND, |
60 GetLocalResourceEntry(remote_dest_path, &entry)); | 44 GetLocalResourceEntry(remote_dest_path, &entry)); |
61 | 45 |
62 CacheCommitObserver cache_observer; | |
63 cache()->AddObserver(&cache_observer); | |
64 | |
65 // Transfer the local file to Drive. | 46 // Transfer the local file to Drive. |
66 FileError error = FILE_ERROR_FAILED; | 47 FileError error = FILE_ERROR_FAILED; |
67 operation_->TransferFileFromLocalToRemote( | 48 operation_->TransferFileFromLocalToRemote( |
68 local_src_path, | 49 local_src_path, |
69 remote_dest_path, | 50 remote_dest_path, |
70 google_apis::test_util::CreateCopyResultCallback(&error)); | 51 google_apis::test_util::CreateCopyResultCallback(&error)); |
71 google_apis::test_util::RunBlockingPoolTask(); | 52 google_apis::test_util::RunBlockingPoolTask(); |
72 EXPECT_EQ(FILE_ERROR_OK, error); | 53 EXPECT_EQ(FILE_ERROR_OK, error); |
73 | 54 |
74 // What TransferFileFromLocalToRemote does is to store the local file in | 55 // TransferFileFromLocalToRemote stores a copy of the local file in the cache, |
75 // the Drive file cache, and mark it as dirty+committed. Here we test that | 56 // marks it dirty and requests the observer to upload the file. |
76 // the "cache committed" event is indeed fired as a result of this test case. | |
77 // | |
78 // In the production environment, SyncClient listens this event and uploads | |
79 // the file to the remote server in background. This part should be tested in | |
80 // sync_client_unittest.cc | |
81 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry)); | 57 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry)); |
82 EXPECT_FALSE(cache_observer.last_committed_resource_id().empty()); | 58 EXPECT_EQ(1U, observer()->upload_needed_resource_ids().count( |
83 EXPECT_EQ(entry.resource_id(), cache_observer.last_committed_resource_id()); | 59 entry.resource_id())); |
| 60 FileCacheEntry cache_entry; |
| 61 bool found = false; |
| 62 cache()->GetCacheEntryOnUIThread( |
| 63 entry.resource_id(), std::string(), |
| 64 google_apis::test_util::CreateCopyResultCallback(&found, &cache_entry)); |
| 65 google_apis::test_util::RunBlockingPoolTask(); |
| 66 EXPECT_TRUE(found); |
| 67 EXPECT_TRUE(cache_entry.is_present()); |
| 68 EXPECT_TRUE(cache_entry.is_dirty()); |
84 | 69 |
85 EXPECT_EQ(1U, observer()->get_changed_paths().size()); | 70 EXPECT_EQ(1U, observer()->get_changed_paths().size()); |
86 EXPECT_TRUE(observer()->get_changed_paths().count( | 71 EXPECT_TRUE(observer()->get_changed_paths().count( |
87 remote_dest_path.DirName())); | 72 remote_dest_path.DirName())); |
88 } | 73 } |
89 | 74 |
90 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_HostedDocument) { | 75 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_HostedDocument) { |
91 const base::FilePath local_src_path = temp_dir().AppendASCII("local.gdoc"); | 76 const base::FilePath local_src_path = temp_dir().AppendASCII("local.gdoc"); |
92 const base::FilePath remote_dest_path(FILE_PATH_LITERAL( | 77 const base::FilePath remote_dest_path(FILE_PATH_LITERAL( |
93 "drive/root/Directory 1/Document 1 excludeDir-test.gdoc")); | 78 "drive/root/Directory 1/Document 1 excludeDir-test.gdoc")); |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 google_apis::test_util::RunBlockingPoolTask(); | 229 google_apis::test_util::RunBlockingPoolTask(); |
245 EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error); | 230 EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error); |
246 | 231 |
247 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry)); | 232 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry)); |
248 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry)); | 233 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry)); |
249 EXPECT_TRUE(observer()->get_changed_paths().empty()); | 234 EXPECT_TRUE(observer()->get_changed_paths().empty()); |
250 } | 235 } |
251 | 236 |
252 } // namespace file_system | 237 } // namespace file_system |
253 } // namespace drive | 238 } // namespace drive |
OLD | NEW |