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 "base/basictypes.h" | |
6 #include "base/files/file_path.h" | |
7 #include "base/files/scoped_temp_dir.h" | |
8 #include "base/platform_file.h" | |
9 #include "base/run_loop.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 #include "webkit/browser/fileapi/file_system_context.h" | |
12 #include "webkit/browser/fileapi/file_system_operation_runner.h" | |
13 #include "webkit/browser/fileapi/mock_file_system_context.h" | |
14 | |
15 namespace fileapi { | |
16 | |
17 void GetStatus(bool* done, | |
18 base::PlatformFileError *status_out, | |
19 base::PlatformFileError status) { | |
20 ASSERT_FALSE(*done); | |
21 *done = true; | |
22 *status_out = status; | |
23 } | |
24 | |
25 void GetCancelStatus(bool* operation_done, | |
26 bool* cancel_done, | |
27 base::PlatformFileError *status_out, | |
28 base::PlatformFileError status) { | |
29 // Cancel callback must be always called after the operation's callback. | |
30 ASSERT_TRUE(*operation_done); | |
31 ASSERT_FALSE(*cancel_done); | |
32 *cancel_done = true; | |
33 *status_out = status; | |
34 } | |
35 | |
36 class FileSystemOperationRunnerTest : public testing::Test { | |
37 protected: | |
38 FileSystemOperationRunnerTest() {} | |
39 virtual ~FileSystemOperationRunnerTest() {} | |
40 | |
41 virtual void SetUp() OVERRIDE { | |
42 ASSERT_TRUE(base_.CreateUniqueTempDir()); | |
43 base::FilePath base_dir = base_.path(); | |
44 file_system_context_ = | |
45 CreateFileSystemContextForTesting(NULL, base_dir); | |
46 } | |
47 | |
48 virtual void TearDown() OVERRIDE { | |
49 file_system_context_ = NULL; | |
50 base::RunLoop().RunUntilIdle(); | |
51 } | |
52 | |
53 FileSystemURL URL(const std::string& path) { | |
54 return file_system_context_->CreateCrackedFileSystemURL( | |
55 GURL("http://example.com"), kFileSystemTypeTemporary, | |
56 base::FilePath::FromUTF8Unsafe(path)); | |
57 } | |
58 | |
59 FileSystemOperationRunner* operation_runner() { | |
60 return file_system_context_->operation_runner(); | |
61 } | |
62 | |
63 private: | |
64 base::ScopedTempDir base_; | |
65 base::MessageLoop message_loop_; | |
66 scoped_refptr<FileSystemContext> file_system_context_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest); | |
69 }; | |
70 | |
71 TEST_F(FileSystemOperationRunnerTest, NotFoundError) { | |
72 bool done = false; | |
73 base::PlatformFileError status = base::PLATFORM_FILE_ERROR_FAILED; | |
74 | |
75 // Regular NOT_FOUND error, which is called asynchronously. | |
76 operation_runner()->Truncate(URL("foo"), 0, | |
77 base::Bind(&GetStatus, &done, &status)); | |
78 ASSERT_FALSE(done); | |
79 base::RunLoop().RunUntilIdle(); | |
80 ASSERT_TRUE(done); | |
81 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status); | |
82 } | |
83 | |
84 TEST_F(FileSystemOperationRunnerTest, InvalidURLError) { | |
85 bool done = false; | |
86 base::PlatformFileError status = base::PLATFORM_FILE_ERROR_FAILED; | |
87 | |
88 // Invalid URL error, which calls DidFinish synchronously. | |
89 operation_runner()->Truncate(FileSystemURL(), 0, | |
90 base::Bind(&GetStatus, &done, &status)); | |
91 // The error call back shouldn't be fired synchronously. | |
92 ASSERT_FALSE(done); | |
93 | |
94 base::RunLoop().RunUntilIdle(); | |
95 ASSERT_TRUE(done); | |
96 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_URL, status); | |
97 } | |
98 | |
99 TEST_F(FileSystemOperationRunnerTest, NotFoundErrorAndCancel) { | |
100 bool done = false; | |
101 bool cancel_done = false; | |
102 base::PlatformFileError status = base::PLATFORM_FILE_ERROR_FAILED; | |
103 base::PlatformFileError cancel_status = base::PLATFORM_FILE_ERROR_FAILED; | |
104 | |
105 // Call Truncate with non-existent URL, and try to cancel it immediately | |
106 // after that (before its callback is fired). | |
107 FileSystemOperationRunner::OperationID id = | |
108 operation_runner()->Truncate(URL("foo"), 0, | |
109 base::Bind(&GetStatus, &done, &status)); | |
110 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus, | |
111 &done, &cancel_done, | |
112 &cancel_status)); | |
113 | |
114 ASSERT_FALSE(done); | |
115 ASSERT_FALSE(cancel_done); | |
116 base::RunLoop().RunUntilIdle(); | |
117 | |
118 ASSERT_TRUE(done); | |
119 ASSERT_TRUE(cancel_done); | |
120 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, status); | |
121 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, cancel_status); | |
122 } | |
123 | |
124 TEST_F(FileSystemOperationRunnerTest, InvalidURLErrorAndCancel) { | |
125 bool done = false; | |
126 bool cancel_done = false; | |
127 base::PlatformFileError status = base::PLATFORM_FILE_ERROR_FAILED; | |
128 base::PlatformFileError cancel_status = base::PLATFORM_FILE_ERROR_FAILED; | |
129 | |
130 // Call Truncate with invalid URL, and try to cancel it immediately | |
131 // after that (before its callback is fired). | |
132 FileSystemOperationRunner::OperationID id = | |
133 operation_runner()->Truncate(FileSystemURL(), 0, | |
134 base::Bind(&GetStatus, &done, &status)); | |
135 operation_runner()->Cancel(id, base::Bind(&GetCancelStatus, | |
136 &done, &cancel_done, | |
137 &cancel_status)); | |
138 | |
139 ASSERT_FALSE(done); | |
140 ASSERT_FALSE(cancel_done); | |
141 base::RunLoop().RunUntilIdle(); | |
142 | |
143 ASSERT_TRUE(done); | |
144 ASSERT_TRUE(cancel_done); | |
145 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_URL, status); | |
146 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, cancel_status); | |
147 } | |
148 | |
149 TEST_F(FileSystemOperationRunnerTest, CancelWithInvalidId) { | |
150 const FileSystemOperationRunner::OperationID kInvalidId = -1; | |
151 bool done = true; // The operation is not running. | |
152 bool cancel_done = false; | |
153 base::PlatformFileError cancel_status = base::PLATFORM_FILE_ERROR_FAILED; | |
154 operation_runner()->Cancel(kInvalidId, base::Bind(&GetCancelStatus, | |
155 &done, &cancel_done, | |
156 &cancel_status)); | |
157 | |
158 ASSERT_TRUE(cancel_done); | |
159 ASSERT_EQ(base::PLATFORM_FILE_ERROR_INVALID_OPERATION, cancel_status); | |
160 } | |
161 | |
162 } // namespace fileapi | |
OLD | NEW |