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/open_file_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/open_file_operation.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "chrome/browser/chromeos/drive/drive.pb.h" | 12 #include "chrome/browser/chromeos/drive/drive.pb.h" |
13 #include "chrome/browser/chromeos/drive/file_errors.h" | 13 #include "chrome/browser/chromeos/drive/file_errors.h" |
14 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | 14 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
15 #include "chrome/browser/google_apis/test_util.h" | 15 #include "chrome/browser/google_apis/test_util.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace drive { | 18 namespace drive { |
19 namespace file_system { | 19 namespace file_system { |
20 | 20 |
21 class OpenFileOperationTest : public OperationTestBase { | 21 class OpenFileOperationTest : public OperationTestBase { |
22 protected: | 22 protected: |
23 virtual void SetUp() { | 23 virtual void SetUp() { |
24 OperationTestBase::SetUp(); | 24 OperationTestBase::SetUp(); |
25 | 25 |
26 operation_.reset(new OpenFileOperation( | 26 operation_.reset(new OpenFileOperation( |
27 blocking_task_runner(), observer(), scheduler(), metadata(), cache(), | 27 blocking_task_runner(), observer(), scheduler(), metadata(), cache(), |
28 temp_dir(), &open_files_)); | 28 temp_dir())); |
29 } | 29 } |
30 | 30 |
31 std::map<base::FilePath, int> open_files_; | |
32 scoped_ptr<OpenFileOperation> operation_; | 31 scoped_ptr<OpenFileOperation> operation_; |
33 }; | 32 }; |
34 | 33 |
35 TEST_F(OpenFileOperationTest, OpenExistingFile) { | 34 TEST_F(OpenFileOperationTest, OpenExistingFile) { |
36 const base::FilePath file_in_root( | 35 const base::FilePath file_in_root( |
37 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 36 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
38 ResourceEntry src_entry; | 37 ResourceEntry src_entry; |
39 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 38 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
40 const int64 file_size = src_entry.file_info().size(); | 39 const int64 file_size = src_entry.file_info().size(); |
41 | 40 |
42 FileError error = FILE_ERROR_FAILED; | 41 FileError error = FILE_ERROR_FAILED; |
43 base::FilePath file_path; | 42 base::FilePath file_path; |
43 base::Closure on_close_callback; | |
44 operation_->OpenFile( | 44 operation_->OpenFile( |
45 file_in_root, | 45 file_in_root, |
46 OPEN_FILE, | 46 OPEN_FILE, |
47 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 47 google_apis::test_util::CreateCopyResultCallback( |
48 &error, &file_path, &on_close_callback)); | |
48 test_util::RunBlockingPoolTask(); | 49 test_util::RunBlockingPoolTask(); |
49 | 50 |
50 EXPECT_EQ(FILE_ERROR_OK, error); | 51 EXPECT_EQ(FILE_ERROR_OK, error); |
51 ASSERT_TRUE(base::PathExists(file_path)); | 52 ASSERT_TRUE(base::PathExists(file_path)); |
52 int64 local_file_size; | 53 int64 local_file_size; |
53 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 54 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
54 EXPECT_EQ(file_size, local_file_size); | 55 EXPECT_EQ(file_size, local_file_size); |
hashimoto
2013/07/23 11:24:58
How about having EXPECT_FALSE(on_close_callback.is
hidehiko
2013/07/23 14:32:24
Done.
| |
55 | |
56 // The file_path should be added into the set. | |
57 EXPECT_EQ(1, open_files_[file_in_root]); | |
58 } | 56 } |
59 | 57 |
60 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { | 58 TEST_F(OpenFileOperationTest, OpenNonExistingFile) { |
61 const base::FilePath file_in_root( | 59 const base::FilePath file_in_root( |
62 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 60 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
63 | 61 |
64 FileError error = FILE_ERROR_FAILED; | 62 FileError error = FILE_ERROR_FAILED; |
65 base::FilePath file_path; | 63 base::FilePath file_path; |
64 base::Closure on_close_callback; | |
66 operation_->OpenFile( | 65 operation_->OpenFile( |
67 file_in_root, | 66 file_in_root, |
68 OPEN_FILE, | 67 OPEN_FILE, |
69 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 68 google_apis::test_util::CreateCopyResultCallback( |
69 &error, &file_path, &on_close_callback)); | |
70 test_util::RunBlockingPoolTask(); | 70 test_util::RunBlockingPoolTask(); |
71 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 71 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
72 | |
73 // The file shouldn't be in the set of opened files. | |
74 EXPECT_EQ(0U, open_files_.count(file_in_root)); | |
75 } | 72 } |
76 | 73 |
77 TEST_F(OpenFileOperationTest, CreateExistingFile) { | 74 TEST_F(OpenFileOperationTest, CreateExistingFile) { |
78 const base::FilePath file_in_root( | 75 const base::FilePath file_in_root( |
79 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 76 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
80 ResourceEntry src_entry; | 77 ResourceEntry src_entry; |
81 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 78 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
82 | 79 |
83 FileError error = FILE_ERROR_FAILED; | 80 FileError error = FILE_ERROR_FAILED; |
84 base::FilePath file_path; | 81 base::FilePath file_path; |
82 base::Closure on_close_callback; | |
85 operation_->OpenFile( | 83 operation_->OpenFile( |
86 file_in_root, | 84 file_in_root, |
87 CREATE_FILE, | 85 CREATE_FILE, |
88 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 86 google_apis::test_util::CreateCopyResultCallback( |
87 &error, &file_path, &on_close_callback)); | |
89 test_util::RunBlockingPoolTask(); | 88 test_util::RunBlockingPoolTask(); |
90 | 89 |
91 EXPECT_EQ(FILE_ERROR_EXISTS, error); | 90 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
92 | |
93 // The file shouldn't be in the set of opened files. | |
94 EXPECT_EQ(0U, open_files_.count(file_in_root)); | |
95 } | 91 } |
96 | 92 |
97 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { | 93 TEST_F(OpenFileOperationTest, CreateNonExistingFile) { |
98 const base::FilePath file_in_root( | 94 const base::FilePath file_in_root( |
99 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 95 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
100 | 96 |
101 FileError error = FILE_ERROR_FAILED; | 97 FileError error = FILE_ERROR_FAILED; |
102 base::FilePath file_path; | 98 base::FilePath file_path; |
99 base::Closure on_close_callback; | |
103 operation_->OpenFile( | 100 operation_->OpenFile( |
104 file_in_root, | 101 file_in_root, |
105 CREATE_FILE, | 102 CREATE_FILE, |
106 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 103 google_apis::test_util::CreateCopyResultCallback( |
104 &error, &file_path, &on_close_callback)); | |
107 test_util::RunBlockingPoolTask(); | 105 test_util::RunBlockingPoolTask(); |
108 | 106 |
109 EXPECT_EQ(FILE_ERROR_OK, error); | 107 EXPECT_EQ(FILE_ERROR_OK, error); |
110 ASSERT_TRUE(base::PathExists(file_path)); | 108 ASSERT_TRUE(base::PathExists(file_path)); |
111 int64 local_file_size; | 109 int64 local_file_size; |
112 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 110 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
113 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 111 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
114 | |
115 // The file_path should be added into the set. | |
116 EXPECT_EQ(1, open_files_[file_in_root]); | |
117 } | 112 } |
118 | 113 |
119 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) { | 114 TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) { |
120 const base::FilePath file_in_root( | 115 const base::FilePath file_in_root( |
121 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 116 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
122 ResourceEntry src_entry; | 117 ResourceEntry src_entry; |
123 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 118 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
124 const int64 file_size = src_entry.file_info().size(); | 119 const int64 file_size = src_entry.file_info().size(); |
125 | 120 |
126 FileError error = FILE_ERROR_FAILED; | 121 FileError error = FILE_ERROR_FAILED; |
127 base::FilePath file_path; | 122 base::FilePath file_path; |
123 base::Closure on_close_callback; | |
128 operation_->OpenFile( | 124 operation_->OpenFile( |
129 file_in_root, | 125 file_in_root, |
130 OPEN_OR_CREATE_FILE, | 126 OPEN_OR_CREATE_FILE, |
131 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 127 google_apis::test_util::CreateCopyResultCallback( |
128 &error, &file_path, &on_close_callback)); | |
132 test_util::RunBlockingPoolTask(); | 129 test_util::RunBlockingPoolTask(); |
133 | 130 |
134 EXPECT_EQ(FILE_ERROR_OK, error); | 131 EXPECT_EQ(FILE_ERROR_OK, error); |
135 ASSERT_TRUE(base::PathExists(file_path)); | 132 ASSERT_TRUE(base::PathExists(file_path)); |
136 int64 local_file_size; | 133 int64 local_file_size; |
137 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 134 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
138 EXPECT_EQ(file_size, local_file_size); | 135 EXPECT_EQ(file_size, local_file_size); |
139 | |
140 // The file_path should be added into the set. | |
141 EXPECT_EQ(1, open_files_[file_in_root]); | |
142 } | 136 } |
143 | 137 |
144 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) { | 138 TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) { |
145 const base::FilePath file_in_root( | 139 const base::FilePath file_in_root( |
146 FILE_PATH_LITERAL("drive/root/not-exist.txt")); | 140 FILE_PATH_LITERAL("drive/root/not-exist.txt")); |
147 | 141 |
148 FileError error = FILE_ERROR_FAILED; | 142 FileError error = FILE_ERROR_FAILED; |
149 base::FilePath file_path; | 143 base::FilePath file_path; |
144 base::Closure on_close_callback; | |
150 operation_->OpenFile( | 145 operation_->OpenFile( |
151 file_in_root, | 146 file_in_root, |
152 OPEN_OR_CREATE_FILE, | 147 OPEN_OR_CREATE_FILE, |
153 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 148 google_apis::test_util::CreateCopyResultCallback( |
149 &error, &file_path, &on_close_callback)); | |
154 test_util::RunBlockingPoolTask(); | 150 test_util::RunBlockingPoolTask(); |
155 | 151 |
156 EXPECT_EQ(FILE_ERROR_OK, error); | 152 EXPECT_EQ(FILE_ERROR_OK, error); |
157 ASSERT_TRUE(base::PathExists(file_path)); | 153 ASSERT_TRUE(base::PathExists(file_path)); |
158 int64 local_file_size; | 154 int64 local_file_size; |
159 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 155 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
160 EXPECT_EQ(0, local_file_size); // Should be an empty file. | 156 EXPECT_EQ(0, local_file_size); // Should be an empty file. |
161 | |
162 // The file_path should be added into the set. | |
163 EXPECT_EQ(1, open_files_[file_in_root]); | |
164 } | 157 } |
165 | 158 |
166 TEST_F(OpenFileOperationTest, OpenFileTwice) { | 159 TEST_F(OpenFileOperationTest, OpenFileTwice) { |
167 const base::FilePath file_in_root( | 160 const base::FilePath file_in_root( |
168 FILE_PATH_LITERAL("drive/root/File 1.txt")); | 161 FILE_PATH_LITERAL("drive/root/File 1.txt")); |
169 ResourceEntry src_entry; | 162 ResourceEntry src_entry; |
170 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | 163 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); |
171 const int64 file_size = src_entry.file_info().size(); | 164 const int64 file_size = src_entry.file_info().size(); |
172 | 165 |
173 FileError error = FILE_ERROR_FAILED; | 166 FileError error = FILE_ERROR_FAILED; |
174 base::FilePath file_path; | 167 base::FilePath file_path; |
168 base::Closure on_close_callback; | |
175 operation_->OpenFile( | 169 operation_->OpenFile( |
176 file_in_root, | 170 file_in_root, |
177 OPEN_FILE, | 171 OPEN_FILE, |
178 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 172 google_apis::test_util::CreateCopyResultCallback( |
173 &error, &file_path, &on_close_callback)); | |
179 test_util::RunBlockingPoolTask(); | 174 test_util::RunBlockingPoolTask(); |
180 | 175 |
181 EXPECT_EQ(FILE_ERROR_OK, error); | 176 EXPECT_EQ(FILE_ERROR_OK, error); |
182 ASSERT_TRUE(base::PathExists(file_path)); | 177 ASSERT_TRUE(base::PathExists(file_path)); |
183 int64 local_file_size; | 178 int64 local_file_size; |
184 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 179 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
185 EXPECT_EQ(file_size, local_file_size); | 180 EXPECT_EQ(file_size, local_file_size); |
186 | 181 |
187 // The file_path should be added into the set. | |
188 EXPECT_EQ(1, open_files_[file_in_root]); | |
189 | |
190 // Open again. | 182 // Open again. |
191 error = FILE_ERROR_FAILED; | 183 error = FILE_ERROR_FAILED; |
184 base::Closure on_close_callback2; | |
192 operation_->OpenFile( | 185 operation_->OpenFile( |
193 file_in_root, | 186 file_in_root, |
194 OPEN_FILE, | 187 OPEN_FILE, |
195 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 188 google_apis::test_util::CreateCopyResultCallback( |
189 &error, &file_path, &on_close_callback2)); | |
196 test_util::RunBlockingPoolTask(); | 190 test_util::RunBlockingPoolTask(); |
197 | 191 |
198 EXPECT_EQ(FILE_ERROR_OK, error); | 192 EXPECT_EQ(FILE_ERROR_OK, error); |
199 ASSERT_TRUE(base::PathExists(file_path)); | 193 ASSERT_TRUE(base::PathExists(file_path)); |
200 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); | 194 ASSERT_TRUE(file_util::GetFileSize(file_path, &local_file_size)); |
201 EXPECT_EQ(file_size, local_file_size); | 195 EXPECT_EQ(file_size, local_file_size); |
202 | 196 |
203 // The file_path should be added into the set. | 197 ASSERT_FALSE(on_close_callback.is_null()); |
204 EXPECT_EQ(2, open_files_[file_in_root]); | 198 ASSERT_FALSE(on_close_callback2.is_null()); |
199 | |
200 on_close_callback.Run(); | |
201 | |
202 // There still remains a client opening the file, so it shouldn't be | |
203 // uploaded yet. | |
204 EXPECT_TRUE(observer()->upload_needed_resource_ids().empty()); | |
205 | |
206 on_close_callback2.Run(); | |
207 | |
208 // Here, all the clients close the file, so it should be uploaded then. | |
209 EXPECT_EQ( | |
210 1U, | |
211 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); | |
205 } | 212 } |
206 | 213 |
207 } // namespace file_system | 214 } // namespace file_system |
208 } // namespace drive | 215 } // namespace drive |
OLD | NEW |