Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: chrome/browser/chromeos/file_system_provider/request_manager_unittest.cc

Issue 194693002: [fsp] Add requestUnmount() method together with the request manager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed some too strict thread checks. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 <string>
6
7 #include "base/bind.h"
8 #include "base/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace chromeos {
18 namespace file_system_provider {
19 namespace {
20
21 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj";
22 const int kFileSystemId = 1;
23 const char kFileSystemName[] = "Camera Pictures";
24 const base::FilePath::CharType kMountPath[] = FILE_PATH_LITERAL(
25 "/provided/mbflcebpggnecokmikipoihdbecnjfoj-1-testing_profile-hash");
26
27 // Logs calls of the success and error callbacks on requests.
28 class EventLogger {
29 public:
30 class SuccessEvent {
31 public:
32 SuccessEvent(scoped_ptr<base::DictionaryValue> result, bool has_next)
33 : result_(result.Pass()), has_next_(has_next) {}
34 ~SuccessEvent() {}
35
36 base::DictionaryValue* result() { return result_.get(); }
37 bool has_next() { return has_next_; }
38
39 private:
40 scoped_ptr<base::DictionaryValue> result_;
41 bool has_next_;
42 };
43
44 class ErrorEvent {
45 public:
46 explicit ErrorEvent(base::File::Error error) : error_(error) {}
47 ~ErrorEvent() {}
48
49 base::File::Error error() { return error_; }
50
51 private:
52 base::File::Error error_;
53 };
54
55 EventLogger() : weak_ptr_factory_(this) {}
56 virtual ~EventLogger() {}
57
58 void OnSuccess(scoped_ptr<base::DictionaryValue> result, bool has_next) {
59 success_events_.push_back(new SuccessEvent(result.Pass(), has_next));
60 }
61
62 void OnError(base::File::Error error) {
63 error_events_.push_back(new ErrorEvent(error));
64 }
65
66 ScopedVector<SuccessEvent>& success_events() { return success_events_; }
67 ScopedVector<ErrorEvent>& error_events() { return error_events_; }
68
69 base::WeakPtr<EventLogger> GetWeakPtr() {
70 return weak_ptr_factory_.GetWeakPtr();
71 }
72
73 private:
74 ScopedVector<SuccessEvent> success_events_;
75 ScopedVector<ErrorEvent> error_events_;
76 base::WeakPtrFactory<EventLogger> weak_ptr_factory_;
77 };
78
79 } // namespace
80
81 class FileSystemProviderRequestManagerTest : public testing::Test {
82 protected:
83 FileSystemProviderRequestManagerTest() {}
84 virtual ~FileSystemProviderRequestManagerTest() {}
85
86 virtual void SetUp() OVERRIDE {
87 request_manager_.reset(new RequestManager());
88
89 // Configure the testing file system.
90 file_system = ProvidedFileSystem(kExtensionId,
91 kFileSystemId,
92 kFileSystemName,
93 base::FilePath(kMountPath));
94 }
95
96 scoped_ptr<RequestManager> request_manager_;
97 ProvidedFileSystem file_system;
98 };
99
100 TEST_F(FileSystemProviderRequestManagerTest, CreateAndFulFill) {
101 EventLogger logger;
102
103 int request_id = request_manager_->CreateRequest(
104 file_system,
105 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
106 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
107
108 EXPECT_EQ(1, request_id);
109 EXPECT_EQ(0u, logger.success_events().size());
110 EXPECT_EQ(0u, logger.error_events().size());
111
112 scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
113 const bool has_next = false;
114 response->SetString("path", "i-like-vanilla");
115
116 bool result = request_manager_->FulfillRequest(
117 file_system, request_id, response.Pass(), has_next);
118 EXPECT_TRUE(result);
119
120 // Validate if the callback has correct arguments.
121 ASSERT_EQ(1u, logger.success_events().size());
122 EXPECT_EQ(0u, logger.error_events().size());
123 EventLogger::SuccessEvent* event = logger.success_events()[0];
124 ASSERT_TRUE(event->result());
125 std::string response_test_string;
126 EXPECT_TRUE(event->result()->GetString("path", &response_test_string));
127 EXPECT_EQ("i-like-vanilla", response_test_string);
128 EXPECT_EQ(has_next, event->has_next());
129
130 // Confirm, that the request is removed. Basically, fulfilling again for the
131 // same request, should fail.
132 {
133 bool retry = request_manager_->FulfillRequest(
134 file_system, request_id, response.Pass(), has_next);
135 EXPECT_FALSE(retry);
136 }
137
138 // Rejecting should also fail.
139 {
140 bool retry = request_manager_->RejectRequest(
141 file_system, request_id, base::File::FILE_ERROR_FAILED);
142 EXPECT_FALSE(retry);
143 }
144 }
145
146 TEST_F(FileSystemProviderRequestManagerTest, CreateAndFulFill_WithHasNext) {
147 EventLogger logger;
148
149 int request_id = request_manager_->CreateRequest(
150 file_system,
151 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
152 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
153
154 EXPECT_EQ(1, request_id);
155 EXPECT_EQ(0u, logger.success_events().size());
156 EXPECT_EQ(0u, logger.error_events().size());
157
158 scoped_ptr<base::DictionaryValue> response;
159 const bool has_next = true;
160
161 bool result = request_manager_->FulfillRequest(
162 file_system, request_id, response.Pass(), has_next);
163 EXPECT_TRUE(result);
164
165 // Validate if the callback has correct arguments.
166 ASSERT_EQ(1u, logger.success_events().size());
167 EXPECT_EQ(0u, logger.error_events().size());
168 EventLogger::SuccessEvent* event = logger.success_events()[0];
169 EXPECT_FALSE(event->result());
170 EXPECT_EQ(has_next, event->has_next());
171
172 // Confirm, that the request is not removed (since it has has_next == true).
173 // Basically, fulfilling again for the same request, should not fail.
174 {
175 bool new_has_next = false;
176 bool retry = request_manager_->FulfillRequest(
177 file_system, request_id, response.Pass(), new_has_next);
178 EXPECT_TRUE(retry);
179 }
180
181 // Since |new_has_next| is false, then the request should be removed. To check
182 // it, try to fulfill again, what should fail.
183 {
184 bool new_has_next = false;
185 bool retry = request_manager_->FulfillRequest(
186 file_system, request_id, response.Pass(), new_has_next);
187 EXPECT_FALSE(retry);
188 }
189 }
190
191 TEST_F(FileSystemProviderRequestManagerTest, CreateAndReject) {
192 EventLogger logger;
193
194 int request_id = request_manager_->CreateRequest(
195 file_system,
196 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
197 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
198
199 EXPECT_EQ(1, request_id);
200 EXPECT_EQ(0u, logger.success_events().size());
201 EXPECT_EQ(0u, logger.error_events().size());
202
203 base::File::Error error = base::File::FILE_ERROR_NO_MEMORY;
204 bool result = request_manager_->RejectRequest(file_system, request_id, error);
205 EXPECT_TRUE(result);
206
207 // Validate if the callback has correct arguments.
208 ASSERT_EQ(1u, logger.error_events().size());
209 EXPECT_EQ(0u, logger.success_events().size());
210 EventLogger::ErrorEvent* event = logger.error_events()[0];
211 EXPECT_EQ(error, event->error());
212
213 // Confirm, that the request is removed. Basically, fulfilling again for the
214 // same request, should fail.
215 {
216 scoped_ptr<base::DictionaryValue> response;
217 bool has_next = false;
218 bool retry = request_manager_->FulfillRequest(
219 file_system, request_id, response.Pass(), has_next);
220 EXPECT_FALSE(retry);
221 }
222
223 // Rejecting should also fail.
224 {
225 bool retry =
226 request_manager_->RejectRequest(file_system, request_id, error);
227 EXPECT_FALSE(retry);
228 }
229 }
230
231 TEST_F(FileSystemProviderRequestManagerTest,
232 CreateAndFulfillWithWrongRequestId) {
233 EventLogger logger;
234
235 int request_id = request_manager_->CreateRequest(
236 file_system,
237 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
238 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
239
240 EXPECT_EQ(1, request_id);
241 EXPECT_EQ(0u, logger.success_events().size());
242 EXPECT_EQ(0u, logger.error_events().size());
243
244 base::File::Error error = base::File::FILE_ERROR_NO_MEMORY;
245 bool result =
246 request_manager_->RejectRequest(file_system, request_id + 1, error);
247 EXPECT_FALSE(result);
248
249 // Callbacks should not be called.
250 EXPECT_EQ(0u, logger.error_events().size());
251 EXPECT_EQ(0u, logger.success_events().size());
252
253 // Confirm, that the request hasn't been removed, by rejecting it correctly.
254 {
255 bool retry =
256 request_manager_->RejectRequest(file_system, request_id, error);
257 EXPECT_TRUE(retry);
258 }
259 }
260
261 TEST_F(FileSystemProviderRequestManagerTest,
262 CreateAndRejectWithWrongRequestId) {
263 EventLogger logger;
264
265 int request_id = request_manager_->CreateRequest(
266 file_system,
267 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
268 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
269
270 EXPECT_EQ(1, request_id);
271 EXPECT_EQ(0u, logger.success_events().size());
272 EXPECT_EQ(0u, logger.error_events().size());
273
274 base::File::Error error = base::File::FILE_ERROR_NO_MEMORY;
275 bool result =
276 request_manager_->RejectRequest(file_system, request_id + 1, error);
277 EXPECT_FALSE(result);
278
279 // Callbacks should not be called.
280 EXPECT_EQ(0u, logger.error_events().size());
281 EXPECT_EQ(0u, logger.success_events().size());
282
283 // Confirm, that the request hasn't been removed, by rejecting it correctly.
284 {
285 bool retry =
286 request_manager_->RejectRequest(file_system, request_id, error);
287 EXPECT_TRUE(retry);
288 }
289 }
290
291 TEST_F(FileSystemProviderRequestManagerTest,
292 CreateAndFulfillWithUnownedRequestId) {
293 EventLogger logger;
294
295 int request_id = request_manager_->CreateRequest(
296 file_system,
297 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
298 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
299 EXPECT_EQ(1, request_id);
300
301 // Create another file system, which has just a different fiel system id
302 // (1 -> 2).
303 ProvidedFileSystem another_file_system(
304 kExtensionId,
305 2, // file_system_id
306 "Music",
307 base::FilePath::FromUTF8Unsafe(
308 "/provided/mbflcebpggnecokmikipoihdbecnjfoj-2-testing_profile-hash"));
309
310 scoped_ptr<base::DictionaryValue> response;
311 const bool has_next = false;
312
313 bool result = request_manager_->FulfillRequest(
314 another_file_system, request_id, response.Pass(), has_next);
315 EXPECT_FALSE(result);
316
317 // Callbacks should not be called.
318 EXPECT_EQ(0u, logger.error_events().size());
319 EXPECT_EQ(0u, logger.success_events().size());
320
321 // Confirm, that the request hasn't been removed, by fulfilling it again, but
322 // with a correct file system.
323 {
324 bool retry = request_manager_->FulfillRequest(
325 file_system, request_id, response.Pass(), has_next);
326 EXPECT_TRUE(retry);
327 }
328 }
329
330 TEST_F(FileSystemProviderRequestManagerTest, UniqueIds) {
331 EventLogger logger;
332
333 int first_request_id = request_manager_->CreateRequest(
334 file_system,
335 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
336 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
337
338 int second_request_id = request_manager_->CreateRequest(
339 file_system,
340 base::Bind(&EventLogger::OnSuccess, logger.GetWeakPtr()),
341 base::Bind(&EventLogger::OnError, logger.GetWeakPtr()));
342
343 EXPECT_EQ(1, first_request_id);
344 EXPECT_EQ(2, second_request_id);
345 }
346
347 } // namespace file_system_provider
348 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698