OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/drive/file_system/close_file_operation.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/files/file_path.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
12 #include "chrome/browser/chromeos/drive/file_errors.h" | |
13 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | |
14 #include "chrome/browser/google_apis/test_util.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace drive { | |
18 namespace file_system { | |
19 | |
20 class CloseFileOperationTest : public OperationTestBase { | |
21 protected: | |
22 virtual void SetUp() { | |
23 OperationTestBase::SetUp(); | |
24 | |
25 operation_.reset(new CloseFileOperation( | |
26 blocking_task_runner(), observer(), metadata(), &open_files_)); | |
27 } | |
28 | |
29 std::map<base::FilePath, int> open_files_; | |
30 scoped_ptr<CloseFileOperation> operation_; | |
31 }; | |
32 | |
33 TEST_F(CloseFileOperationTest, CloseFile) { | |
34 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
35 ResourceEntry src_entry; | |
36 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | |
37 | |
38 open_files_[file_in_root] = 1; | |
39 FileError error = FILE_ERROR_FAILED; | |
40 operation_->CloseFile( | |
41 file_in_root, | |
42 google_apis::test_util::CreateCopyResultCallback(&error)); | |
43 test_util::RunBlockingPoolTask(); | |
44 | |
45 EXPECT_EQ(FILE_ERROR_OK, error); | |
46 EXPECT_TRUE(open_files_.empty()); | |
47 EXPECT_EQ( | |
48 1U, | |
49 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); | |
50 } | |
51 | |
52 TEST_F(CloseFileOperationTest, NotOpenedFile) { | |
53 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
54 ResourceEntry src_entry; | |
55 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | |
56 | |
57 FileError error = FILE_ERROR_FAILED; | |
58 operation_->CloseFile( | |
59 file_in_root, | |
60 google_apis::test_util::CreateCopyResultCallback(&error)); | |
61 test_util::RunBlockingPoolTask(); | |
62 | |
63 // Even if the file is actually exists, NOT_FOUND should be returned if the | |
64 // file is not opened. | |
65 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | |
66 } | |
67 | |
68 TEST_F(CloseFileOperationTest, CloseFileTwice) { | |
69 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | |
70 ResourceEntry src_entry; | |
71 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry)); | |
72 | |
73 // Set the number of opening clients is two. | |
74 open_files_[file_in_root] = 2; | |
75 | |
76 // The first CloseFile. | |
77 FileError error = FILE_ERROR_FAILED; | |
78 operation_->CloseFile( | |
79 file_in_root, | |
80 google_apis::test_util::CreateCopyResultCallback(&error)); | |
81 test_util::RunBlockingPoolTask(); | |
82 | |
83 EXPECT_EQ(FILE_ERROR_OK, error); | |
84 EXPECT_EQ(1, open_files_[file_in_root]); | |
85 | |
86 // There still remains a client opening the file, so it shouldn't be | |
87 // uploaded yet. | |
88 EXPECT_TRUE(observer()->upload_needed_resource_ids().empty()); | |
89 | |
90 // The second CloseFile. | |
91 operation_->CloseFile( | |
92 file_in_root, | |
93 google_apis::test_util::CreateCopyResultCallback(&error)); | |
94 test_util::RunBlockingPoolTask(); | |
95 | |
96 EXPECT_EQ(FILE_ERROR_OK, error); | |
97 EXPECT_TRUE(open_files_.empty()); | |
98 // Here, all the clients close the file, so it should be uploaded then. | |
99 EXPECT_EQ( | |
100 1U, | |
101 observer()->upload_needed_resource_ids().count(src_entry.resource_id())); | |
102 } | |
103 | |
104 } // namespace file_system | |
105 } // namespace drive | |
OLD | NEW |