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 "chrome/browser/chromeos/drive/file_write_helper.h" | 5 #include "chrome/browser/chromeos/drive/file_write_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/chromeos/drive/dummy_file_system.h" | 8 #include "chrome/browser/chromeos/drive/dummy_file_system.h" |
9 #include "chrome/browser/chromeos/drive/test_util.h" | 9 #include "chrome/browser/chromeos/drive/test_util.h" |
10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace drive { | 13 namespace drive { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const base::FilePath::CharType kDrivePath[] = | 17 const base::FilePath::CharType kDrivePath[] = |
18 FILE_PATH_LITERAL("drive/root/file.txt"); | 18 FILE_PATH_LITERAL("drive/root/file.txt"); |
19 const base::FilePath::CharType kInvalidPath[] = | 19 const base::FilePath::CharType kInvalidPath[] = |
20 FILE_PATH_LITERAL("drive/invalid/path"); | 20 FILE_PATH_LITERAL("drive/invalid/path"); |
21 const base::FilePath::CharType kLocalPath[] = | 21 const base::FilePath::CharType kLocalPath[] = |
22 FILE_PATH_LITERAL("/tmp/local.txt"); | 22 FILE_PATH_LITERAL("/tmp/local.txt"); |
23 | 23 |
24 class TestFileSystem : public DummyFileSystem { | 24 class TestFileSystem : public DummyFileSystem { |
25 public: | 25 public: |
| 26 TestFileSystem() : num_closed_(0) { |
| 27 } |
| 28 |
| 29 int num_closed() const { return num_closed_; } |
| 30 |
26 // Mimics OpenFile. It fails if the |file_path| points to a hosted document. | 31 // Mimics OpenFile. It fails if the |file_path| points to a hosted document. |
27 virtual void OpenFile(const base::FilePath& file_path, | 32 virtual void OpenFile(const base::FilePath& file_path, |
28 OpenMode open_mode, | 33 OpenMode open_mode, |
29 const OpenFileCallback& callback) OVERRIDE { | 34 const OpenFileCallback& callback) OVERRIDE { |
30 EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode); | 35 EXPECT_EQ(OPEN_OR_CREATE_FILE, open_mode); |
31 | 36 |
32 // Emulate a case of opening a hosted document. | 37 // Emulate a case of opening a hosted document. |
33 if (file_path == base::FilePath(kInvalidPath)) { | 38 if (file_path == base::FilePath(kInvalidPath)) { |
34 callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath()); | 39 callback.Run(FILE_ERROR_INVALID_OPERATION, base::FilePath(), |
| 40 base::Closure()); |
35 return; | 41 return; |
36 } | 42 } |
37 | 43 |
38 callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath)); | 44 callback.Run(FILE_ERROR_OK, base::FilePath(kLocalPath), |
| 45 base::Bind(&TestFileSystem::CloseFile, |
| 46 base::Unretained(this))); |
39 } | 47 } |
40 | 48 |
41 virtual void CloseFile(const base::FilePath& file_path, | 49 private: |
42 const FileOperationCallback& callback) OVERRIDE { | 50 |
43 callback.Run(FILE_ERROR_OK); | 51 void CloseFile() { |
| 52 ++num_closed_; |
44 } | 53 } |
| 54 |
| 55 int num_closed_; |
45 }; | 56 }; |
46 | 57 |
47 } // namespace | 58 } // namespace |
48 | 59 |
49 class FileWriteHelperTest : public testing::Test { | 60 class FileWriteHelperTest : public testing::Test { |
50 public: | 61 public: |
51 FileWriteHelperTest() | 62 FileWriteHelperTest() |
52 : test_file_system_(new TestFileSystem) { | 63 : test_file_system_(new TestFileSystem) { |
53 } | 64 } |
54 | 65 |
55 protected: | 66 protected: |
56 content::TestBrowserThreadBundle thread_bundle_; | 67 content::TestBrowserThreadBundle thread_bundle_; |
57 scoped_ptr<TestFileSystem> test_file_system_; | 68 scoped_ptr<TestFileSystem> test_file_system_; |
58 }; | 69 }; |
59 | 70 |
60 TEST_F(FileWriteHelperTest, PrepareFileForWritingSuccess) { | 71 TEST_F(FileWriteHelperTest, PrepareFileForWritingSuccess) { |
61 FileWriteHelper file_write_helper(test_file_system_.get()); | 72 FileWriteHelper file_write_helper(test_file_system_.get()); |
62 FileError error = FILE_ERROR_FAILED; | 73 FileError error = FILE_ERROR_FAILED; |
63 base::FilePath path; | 74 base::FilePath path; |
64 // The file should successfully be opened. | 75 // The file should successfully be opened. |
65 file_write_helper.PrepareWritableFileAndRun( | 76 file_write_helper.PrepareWritableFileAndRun( |
66 base::FilePath(kDrivePath), | 77 base::FilePath(kDrivePath), |
67 google_apis::test_util::CreateCopyResultCallback(&error, &path)); | 78 google_apis::test_util::CreateCopyResultCallback(&error, &path)); |
68 test_util::RunBlockingPoolTask(); | 79 test_util::RunBlockingPoolTask(); |
69 | 80 |
70 EXPECT_EQ(FILE_ERROR_OK, error); | 81 EXPECT_EQ(FILE_ERROR_OK, error); |
71 EXPECT_EQ(kLocalPath, path.value()); | 82 EXPECT_EQ(kLocalPath, path.value()); |
| 83 |
| 84 // Make sure that the file is actually closed. |
| 85 EXPECT_EQ(1, test_file_system_->num_closed()); |
72 } | 86 } |
73 | 87 |
74 TEST_F(FileWriteHelperTest, PrepareFileForWritingCreateFail) { | 88 TEST_F(FileWriteHelperTest, PrepareFileForWritingCreateFail) { |
75 FileWriteHelper file_write_helper(test_file_system_.get()); | 89 FileWriteHelper file_write_helper(test_file_system_.get()); |
76 FileError error = FILE_ERROR_FAILED; | 90 FileError error = FILE_ERROR_FAILED; |
77 base::FilePath path; | 91 base::FilePath path; |
78 // Access to kInvalidPath should fail, and FileWriteHelper should not try to | 92 // Access to kInvalidPath should fail, and FileWriteHelper should not try to |
79 // open or close the file. | 93 // open or close the file. |
80 file_write_helper.PrepareWritableFileAndRun( | 94 file_write_helper.PrepareWritableFileAndRun( |
81 base::FilePath(kInvalidPath), | 95 base::FilePath(kInvalidPath), |
82 google_apis::test_util::CreateCopyResultCallback(&error, &path)); | 96 google_apis::test_util::CreateCopyResultCallback(&error, &path)); |
83 test_util::RunBlockingPoolTask(); | 97 test_util::RunBlockingPoolTask(); |
84 | 98 |
85 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); | 99 EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error); |
86 EXPECT_TRUE(path.empty()); | 100 EXPECT_TRUE(path.empty()); |
87 } | 101 } |
88 | 102 |
89 } // namespace drive | 103 } // namespace drive |
OLD | NEW |