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.
5 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/run_loop.h"
19 #include "chrome/browser/chromeos/file_system_provider/notification_manager_interface.h"
20 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "testing/gtest/include/gtest/gtest.h"
25 namespace file_system_provider
{
28 // Fake implementation for the notification manager. Simulates user action on
30 class FakeNotificationManager
: public NotificationManagerInterface
{
32 FakeNotificationManager() {}
33 ~FakeNotificationManager() override
{}
35 // NotificationManagerInterface overrides:
36 void ShowUnresponsiveNotification(
38 const NotificationCallback
& callback
) override
{
39 callbacks_
[id
] = callback
;
42 void HideUnresponsiveNotification(int id
) override
{ callbacks_
.erase(id
); }
44 // Aborts all of the virtually shown notifications.
45 void Abort() { OnNotificationResult(ABORT
); }
47 // Discards all of the virtually shown notifications.
48 void Continue() { OnNotificationResult(CONTINUE
); }
50 // Returns number of currently shown notifications.
51 size_t size() { return callbacks_
.size(); }
54 typedef std::map
<int, NotificationCallback
> CallbackMap
;
56 // Handles a notification result by calling all registered callbacks and
58 void OnNotificationResult(NotificationResult result
) {
59 CallbackMap::iterator it
= callbacks_
.begin();
60 while (it
!= callbacks_
.end()) {
61 CallbackMap::iterator current_it
= it
++;
62 NotificationCallback callback
= current_it
->second
;
63 callbacks_
.erase(current_it
);
68 CallbackMap callbacks_
;
70 DISALLOW_COPY_AND_ASSIGN(FakeNotificationManager
);
73 // Logs calls of the success and error callbacks on requests.
78 explicit ExecuteEvent(int request_id
) : request_id_(request_id
) {}
79 virtual ~ExecuteEvent() {}
81 int request_id() { return request_id_
; }
89 SuccessEvent(int request_id
, scoped_ptr
<RequestValue
> result
, bool has_more
)
90 : request_id_(request_id
),
91 result_(result
.Pass()),
92 has_more_(has_more
) {}
93 virtual ~SuccessEvent() {}
95 int request_id() { return request_id_
; }
96 RequestValue
* result() { return result_
.get(); }
97 bool has_more() { return has_more_
; }
101 scoped_ptr
<RequestValue
> result_
;
107 ErrorEvent(int request_id
,
108 scoped_ptr
<RequestValue
> result
,
109 base::File::Error error
)
110 : request_id_(request_id
), result_(result
.Pass()), error_(error
) {}
111 virtual ~ErrorEvent() {}
113 int request_id() { return request_id_
; }
114 RequestValue
* result() { return result_
.get(); }
115 base::File::Error
error() { return error_
; }
119 scoped_ptr
<RequestValue
> result_
;
120 base::File::Error error_
;
123 EventLogger() : weak_ptr_factory_(this) {}
124 virtual ~EventLogger() {}
126 void OnExecute(int request_id
) {
127 execute_events_
.push_back(new ExecuteEvent(request_id
));
130 void OnSuccess(int request_id
,
131 scoped_ptr
<RequestValue
> result
,
133 success_events_
.push_back(
134 new SuccessEvent(request_id
, result
.Pass(), has_more
));
137 void OnError(int request_id
,
138 scoped_ptr
<RequestValue
> result
,
139 base::File::Error error
) {
140 error_events_
.push_back(new ErrorEvent(request_id
, result
.Pass(), error
));
143 ScopedVector
<ExecuteEvent
>& execute_events() { return execute_events_
; }
144 ScopedVector
<SuccessEvent
>& success_events() { return success_events_
; }
145 ScopedVector
<ErrorEvent
>& error_events() { return error_events_
; }
147 base::WeakPtr
<EventLogger
> GetWeakPtr() {
148 return weak_ptr_factory_
.GetWeakPtr();
152 ScopedVector
<ExecuteEvent
> execute_events_
;
153 ScopedVector
<SuccessEvent
> success_events_
;
154 ScopedVector
<ErrorEvent
> error_events_
;
155 base::WeakPtrFactory
<EventLogger
> weak_ptr_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(EventLogger
);
160 // Fake handler, which forwards callbacks to the logger. The handler is owned
161 // by a request manager, however the logger is owned by tests.
162 class FakeHandler
: public RequestManager::HandlerInterface
{
164 // The handler can outlive the passed logger, so using a weak pointer. The
165 // |execute_reply| value will be returned for the Execute() call.
166 FakeHandler(base::WeakPtr
<EventLogger
> logger
, bool execute_reply
)
167 : logger_(logger
), execute_reply_(execute_reply
) {}
169 // RequestManager::Handler overrides.
170 bool Execute(int request_id
) override
{
172 logger_
->OnExecute(request_id
);
174 return execute_reply_
;
177 // RequestManager::Handler overrides.
178 void OnSuccess(int request_id
,
179 scoped_ptr
<RequestValue
> result
,
180 bool has_more
) override
{
182 logger_
->OnSuccess(request_id
, result
.Pass(), has_more
);
185 // RequestManager::Handler overrides.
186 void OnError(int request_id
,
187 scoped_ptr
<RequestValue
> result
,
188 base::File::Error error
) override
{
190 logger_
->OnError(request_id
, result
.Pass(), error
);
193 ~FakeHandler() override
{}
196 base::WeakPtr
<EventLogger
> logger_
;
198 DISALLOW_COPY_AND_ASSIGN(FakeHandler
);
201 // Observer the request manager for request events.
202 class RequestObserver
: public RequestManager::Observer
{
206 explicit Event(int request_id
) : request_id_(request_id
) {}
208 int request_id() const { return request_id_
; }
214 class CreatedEvent
: public Event
{
216 CreatedEvent(int request_id
, RequestType type
)
217 : Event(request_id
), type_(type
) {}
218 ~CreatedEvent() override
{}
220 RequestType
type() const { return type_
; }
226 class FulfilledEvent
: public Event
{
228 FulfilledEvent(int request_id
, bool has_more
)
229 : Event(request_id
), has_more_(has_more
) {}
230 ~FulfilledEvent() override
{}
232 bool has_more() const { return has_more_
; }
238 class RejectedEvent
: public Event
{
240 RejectedEvent(int request_id
, base::File::Error error
)
241 : Event(request_id
), error_(error
) {}
242 ~RejectedEvent() override
{}
244 base::File::Error
error() const { return error_
; }
247 base::File::Error error_
;
251 ~RequestObserver() override
{}
253 // RequestManager::Observer overrides.
254 void OnRequestCreated(int request_id
, RequestType type
) override
{
255 created_
.push_back(CreatedEvent(request_id
, type
));
258 // RequestManager::Observer overrides.
259 void OnRequestDestroyed(int request_id
) override
{
260 destroyed_
.push_back(Event(request_id
));
263 // RequestManager::Observer overrides.
264 void OnRequestExecuted(int request_id
) override
{
265 executed_
.push_back(Event(request_id
));
268 // RequestManager::Observer overrides.
269 void OnRequestFulfilled(int request_id
,
270 const RequestValue
& result
,
271 bool has_more
) override
{
272 fulfilled_
.push_back(FulfilledEvent(request_id
, has_more
));
275 // RequestManager::Observer overrides.
276 void OnRequestRejected(int request_id
,
277 const RequestValue
& result
,
278 base::File::Error error
) override
{
279 rejected_
.push_back(RejectedEvent(request_id
, error
));
282 // RequestManager::Observer overrides.
283 void OnRequestTimeouted(int request_id
) override
{
284 timeouted_
.push_back(Event(request_id
));
287 const std::vector
<CreatedEvent
>& created() const { return created_
; }
288 const std::vector
<Event
>& destroyed() const { return destroyed_
; }
289 const std::vector
<Event
>& executed() const { return executed_
; }
290 const std::vector
<FulfilledEvent
>& fulfilled() const { return fulfilled_
; }
291 const std::vector
<RejectedEvent
>& rejected() const { return rejected_
; }
292 const std::vector
<Event
>& timeouted() const { return timeouted_
; }
295 std::vector
<CreatedEvent
> created_
;
296 std::vector
<Event
> destroyed_
;
297 std::vector
<Event
> executed_
;
298 std::vector
<FulfilledEvent
> fulfilled_
;
299 std::vector
<RejectedEvent
> rejected_
;
300 std::vector
<Event
> timeouted_
;
302 DISALLOW_COPY_AND_ASSIGN(RequestObserver
);
307 class FileSystemProviderRequestManagerTest
: public testing::Test
{
309 FileSystemProviderRequestManagerTest() {}
310 ~FileSystemProviderRequestManagerTest() override
{}
312 void SetUp() override
{
313 notification_manager_
.reset(new FakeNotificationManager
);
314 request_manager_
.reset(new RequestManager(notification_manager_
.get()));
317 content::TestBrowserThreadBundle thread_bundle_
;
318 scoped_ptr
<FakeNotificationManager
> notification_manager_
;
319 scoped_ptr
<RequestManager
> request_manager_
;
322 TEST_F(FileSystemProviderRequestManagerTest
, CreateFailure
) {
324 RequestObserver observer
;
325 request_manager_
->AddObserver(&observer
);
327 const int request_id
= request_manager_
->CreateRequest(
329 make_scoped_ptr
<RequestManager::HandlerInterface
>(
330 new FakeHandler(logger
.GetWeakPtr(), false /* execute_reply */)));
332 EXPECT_EQ(0, request_id
);
333 EXPECT_EQ(0u, logger
.success_events().size());
334 EXPECT_EQ(0u, logger
.error_events().size());
336 EXPECT_EQ(1u, observer
.created().size());
337 EXPECT_EQ(TESTING
, observer
.created()[0].type());
338 EXPECT_EQ(1u, observer
.destroyed().size());
339 EXPECT_EQ(0u, observer
.executed().size());
341 const std::vector
<int> active_request_ids
=
342 request_manager_
->GetActiveRequestIds();
343 EXPECT_EQ(0u, active_request_ids
.size());
345 request_manager_
->RemoveObserver(&observer
);
348 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndFulFill
) {
350 RequestObserver observer
;
351 request_manager_
->AddObserver(&observer
);
353 const int request_id
= request_manager_
->CreateRequest(
355 make_scoped_ptr
<RequestManager::HandlerInterface
>(
356 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
358 EXPECT_EQ(1, request_id
);
359 EXPECT_EQ(0u, logger
.success_events().size());
360 EXPECT_EQ(0u, logger
.error_events().size());
362 ASSERT_EQ(1u, observer
.created().size());
363 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
364 EXPECT_EQ(TESTING
, observer
.created()[0].type());
366 ASSERT_EQ(1u, observer
.executed().size());
367 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
369 const std::vector
<int> active_request_ids
=
370 request_manager_
->GetActiveRequestIds();
371 ASSERT_EQ(1u, active_request_ids
.size());
372 EXPECT_EQ(request_id
, active_request_ids
[0]);
374 scoped_ptr
<RequestValue
> response(
375 RequestValue::CreateForTesting("i-like-vanilla"));
376 const bool has_more
= false;
378 const base::File::Error result
=
379 request_manager_
->FulfillRequest(request_id
, response
.Pass(), has_more
);
380 EXPECT_EQ(base::File::FILE_OK
, result
);
382 ASSERT_EQ(1u, observer
.fulfilled().size());
383 EXPECT_EQ(request_id
, observer
.fulfilled()[0].request_id());
384 EXPECT_FALSE(observer
.fulfilled()[0].has_more());
386 // Validate if the callback has correct arguments.
387 ASSERT_EQ(1u, logger
.success_events().size());
388 EXPECT_EQ(0u, logger
.error_events().size());
389 EventLogger::SuccessEvent
* event
= logger
.success_events()[0];
390 ASSERT_TRUE(event
->result());
391 const std::string
* response_test_string
= event
->result()->testing_params();
392 ASSERT_TRUE(response_test_string
);
393 EXPECT_EQ("i-like-vanilla", *response_test_string
);
394 EXPECT_FALSE(event
->has_more());
396 // Confirm, that the request is removed. Basically, fulfilling again for the
397 // same request, should fail.
399 const std::vector
<int> active_request_ids
=
400 request_manager_
->GetActiveRequestIds();
401 EXPECT_EQ(0u, active_request_ids
.size());
403 const base::File::Error retry
= request_manager_
->FulfillRequest(
404 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
405 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
406 EXPECT_EQ(1u, observer
.fulfilled().size());
409 // Rejecting should also fail.
411 const base::File::Error retry
= request_manager_
->RejectRequest(
412 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()),
413 base::File::FILE_ERROR_FAILED
);
414 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
415 EXPECT_EQ(0u, observer
.rejected().size());
418 ASSERT_EQ(1u, observer
.destroyed().size());
419 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
420 EXPECT_EQ(0u, observer
.timeouted().size());
422 request_manager_
->RemoveObserver(&observer
);
425 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndFulFill_WithHasNext
) {
427 RequestObserver observer
;
428 request_manager_
->AddObserver(&observer
);
430 const int request_id
= request_manager_
->CreateRequest(
432 make_scoped_ptr
<RequestManager::HandlerInterface
>(
433 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
435 EXPECT_EQ(1, request_id
);
436 EXPECT_EQ(0u, logger
.success_events().size());
437 EXPECT_EQ(0u, logger
.error_events().size());
439 ASSERT_EQ(1u, observer
.created().size());
440 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
441 EXPECT_EQ(TESTING
, observer
.created()[0].type());
443 ASSERT_EQ(1u, observer
.executed().size());
444 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
446 const bool has_more
= true;
448 const base::File::Error result
= request_manager_
->FulfillRequest(
449 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
450 EXPECT_EQ(base::File::FILE_OK
, result
);
452 // Validate if the callback has correct arguments.
453 ASSERT_EQ(1u, logger
.success_events().size());
454 EXPECT_EQ(0u, logger
.error_events().size());
455 EventLogger::SuccessEvent
* event
= logger
.success_events()[0];
456 EXPECT_TRUE(event
->result());
457 EXPECT_TRUE(event
->has_more());
459 ASSERT_EQ(1u, observer
.fulfilled().size());
460 EXPECT_EQ(request_id
, observer
.fulfilled()[0].request_id());
461 EXPECT_TRUE(observer
.fulfilled()[0].has_more());
463 // Confirm, that the request is not removed (since it has has_more == true).
464 // Basically, fulfilling again for the same request, should not fail.
466 const std::vector
<int> active_request_ids
=
467 request_manager_
->GetActiveRequestIds();
468 ASSERT_EQ(1u, active_request_ids
.size());
469 EXPECT_EQ(request_id
, active_request_ids
[0]);
471 const bool new_has_more
= false;
472 const base::File::Error retry
= request_manager_
->FulfillRequest(
473 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), new_has_more
);
474 EXPECT_EQ(base::File::FILE_OK
, retry
);
476 ASSERT_EQ(2u, observer
.fulfilled().size());
477 EXPECT_EQ(request_id
, observer
.fulfilled()[1].request_id());
478 EXPECT_FALSE(observer
.fulfilled()[1].has_more());
481 // Since |new_has_more| is false, then the request should be removed. To check
482 // it, try to fulfill again, what should fail.
484 const std::vector
<int> active_request_ids
=
485 request_manager_
->GetActiveRequestIds();
486 EXPECT_EQ(0u, active_request_ids
.size());
488 const bool new_has_more
= false;
489 const base::File::Error retry
= request_manager_
->FulfillRequest(
490 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), new_has_more
);
491 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
492 EXPECT_EQ(0u, observer
.rejected().size());
495 ASSERT_EQ(1u, observer
.destroyed().size());
496 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
497 EXPECT_EQ(0u, observer
.timeouted().size());
499 request_manager_
->RemoveObserver(&observer
);
502 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndReject
) {
504 RequestObserver observer
;
505 request_manager_
->AddObserver(&observer
);
507 const int request_id
= request_manager_
->CreateRequest(
509 make_scoped_ptr
<RequestManager::HandlerInterface
>(
510 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
512 EXPECT_EQ(1, request_id
);
513 EXPECT_EQ(0u, logger
.success_events().size());
514 EXPECT_EQ(0u, logger
.error_events().size());
516 ASSERT_EQ(1u, observer
.created().size());
517 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
518 EXPECT_EQ(TESTING
, observer
.created()[0].type());
520 ASSERT_EQ(1u, observer
.executed().size());
521 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
523 const base::File::Error error
= base::File::FILE_ERROR_NO_MEMORY
;
524 const base::File::Error result
= request_manager_
->RejectRequest(
525 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
526 EXPECT_EQ(base::File::FILE_OK
, result
);
528 // Validate if the callback has correct arguments.
529 ASSERT_EQ(1u, logger
.error_events().size());
530 EXPECT_EQ(0u, logger
.success_events().size());
531 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
532 EXPECT_EQ(error
, event
->error());
534 ASSERT_EQ(1u, observer
.rejected().size());
535 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
536 EXPECT_EQ(error
, observer
.rejected()[0].error());
538 // Confirm, that the request is removed. Basically, fulfilling again for the
539 // same request, should fail.
541 const bool has_more
= false;
542 const base::File::Error retry
= request_manager_
->FulfillRequest(
543 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
544 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
545 EXPECT_EQ(0u, observer
.fulfilled().size());
548 // Rejecting should also fail.
550 const base::File::Error retry
= request_manager_
->RejectRequest(
551 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
552 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
553 EXPECT_EQ(1u, observer
.rejected().size());
556 ASSERT_EQ(1u, observer
.destroyed().size());
557 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
558 EXPECT_EQ(0u, observer
.timeouted().size());
560 request_manager_
->RemoveObserver(&observer
);
563 TEST_F(FileSystemProviderRequestManagerTest
,
564 CreateAndFulfillWithWrongRequestId
) {
566 RequestObserver observer
;
567 request_manager_
->AddObserver(&observer
);
569 const int request_id
= request_manager_
->CreateRequest(
571 make_scoped_ptr
<RequestManager::HandlerInterface
>(
572 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
574 EXPECT_EQ(1, request_id
);
575 EXPECT_EQ(0u, logger
.success_events().size());
576 EXPECT_EQ(0u, logger
.error_events().size());
578 ASSERT_EQ(1u, observer
.created().size());
579 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
580 EXPECT_EQ(TESTING
, observer
.created()[0].type());
582 ASSERT_EQ(1u, observer
.executed().size());
583 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
585 const bool has_more
= true;
587 const base::File::Error result
= request_manager_
->FulfillRequest(
588 request_id
+ 1, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
589 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, result
);
591 // Callbacks should not be called.
592 EXPECT_EQ(0u, logger
.error_events().size());
593 EXPECT_EQ(0u, logger
.success_events().size());
595 EXPECT_EQ(0u, observer
.fulfilled().size());
596 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
598 // Confirm, that the request hasn't been removed, by fulfilling it correctly.
600 const base::File::Error retry
= request_manager_
->FulfillRequest(
601 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
602 EXPECT_EQ(base::File::FILE_OK
, retry
);
603 EXPECT_EQ(1u, observer
.fulfilled().size());
606 request_manager_
->RemoveObserver(&observer
);
609 TEST_F(FileSystemProviderRequestManagerTest
,
610 CreateAndRejectWithWrongRequestId
) {
612 RequestObserver observer
;
613 request_manager_
->AddObserver(&observer
);
615 const int request_id
= request_manager_
->CreateRequest(
617 make_scoped_ptr
<RequestManager::HandlerInterface
>(
618 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
620 EXPECT_EQ(1, request_id
);
621 EXPECT_EQ(0u, logger
.success_events().size());
622 EXPECT_EQ(0u, logger
.error_events().size());
624 ASSERT_EQ(1u, observer
.created().size());
625 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
626 EXPECT_EQ(TESTING
, observer
.created()[0].type());
628 ASSERT_EQ(1u, observer
.executed().size());
629 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
631 const base::File::Error error
= base::File::FILE_ERROR_NO_MEMORY
;
632 const base::File::Error result
= request_manager_
->RejectRequest(
633 request_id
+ 1, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
634 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, result
);
636 // Callbacks should not be called.
637 EXPECT_EQ(0u, logger
.error_events().size());
638 EXPECT_EQ(0u, logger
.success_events().size());
640 EXPECT_EQ(0u, observer
.rejected().size());
642 // Confirm, that the request hasn't been removed, by rejecting it correctly.
644 const base::File::Error retry
= request_manager_
->RejectRequest(
645 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
646 EXPECT_EQ(base::File::FILE_OK
, retry
);
647 EXPECT_EQ(1u, observer
.rejected().size());
650 request_manager_
->RemoveObserver(&observer
);
653 TEST_F(FileSystemProviderRequestManagerTest
, UniqueIds
) {
656 const int first_request_id
= request_manager_
->CreateRequest(
658 make_scoped_ptr
<RequestManager::HandlerInterface
>(
659 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
661 const int second_request_id
= request_manager_
->CreateRequest(
663 make_scoped_ptr
<RequestManager::HandlerInterface
>(
664 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
666 EXPECT_EQ(1, first_request_id
);
667 EXPECT_EQ(2, second_request_id
);
670 TEST_F(FileSystemProviderRequestManagerTest
, AbortOnDestroy
) {
672 RequestObserver observer
;
676 RequestManager
request_manager(NULL
);
677 request_manager
.AddObserver(&observer
);
679 request_id
= request_manager
.CreateRequest(
681 make_scoped_ptr
<RequestManager::HandlerInterface
>(
682 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
684 EXPECT_EQ(1, request_id
);
685 EXPECT_EQ(0u, logger
.success_events().size());
686 EXPECT_EQ(0u, logger
.error_events().size());
688 ASSERT_EQ(1u, observer
.created().size());
689 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
690 EXPECT_EQ(TESTING
, observer
.created()[0].type());
692 ASSERT_EQ(1u, observer
.executed().size());
693 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
695 EXPECT_EQ(0u, observer
.fulfilled().size());
696 EXPECT_EQ(0u, observer
.rejected().size());
697 EXPECT_EQ(0u, observer
.destroyed().size());
698 EXPECT_EQ(0u, observer
.timeouted().size());
700 // Do not remove the observer, to catch events while destructing.
703 // All active requests should be aborted in the destructor of RequestManager.
704 ASSERT_EQ(1u, logger
.error_events().size());
705 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
706 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, event
->error());
708 EXPECT_EQ(0u, logger
.success_events().size());
710 EXPECT_EQ(0u, observer
.fulfilled().size());
711 EXPECT_EQ(0u, observer
.timeouted().size());
712 ASSERT_EQ(1u, observer
.rejected().size());
713 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
714 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, observer
.rejected()[0].error());
715 ASSERT_EQ(1u, observer
.destroyed().size());
718 TEST_F(FileSystemProviderRequestManagerTest
, AbortOnTimeout
) {
720 RequestObserver observer
;
721 request_manager_
->AddObserver(&observer
);
723 request_manager_
->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0));
724 const int request_id
= request_manager_
->CreateRequest(
726 make_scoped_ptr
<RequestManager::HandlerInterface
>(
727 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
728 EXPECT_EQ(1, request_id
);
729 EXPECT_EQ(0u, logger
.success_events().size());
730 EXPECT_EQ(0u, logger
.error_events().size());
731 EXPECT_EQ(0u, notification_manager_
->size());
733 ASSERT_EQ(1u, observer
.created().size());
734 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
735 EXPECT_EQ(TESTING
, observer
.created()[0].type());
737 ASSERT_EQ(1u, observer
.executed().size());
738 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
740 // Wait until the request is timeouted.
741 base::RunLoop().RunUntilIdle();
743 // Abort the request.
744 EXPECT_EQ(1u, notification_manager_
->size());
745 notification_manager_
->Abort();
746 EXPECT_EQ(0u, notification_manager_
->size());
748 ASSERT_EQ(1u, logger
.error_events().size());
749 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
750 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, event
->error());
752 ASSERT_EQ(1u, observer
.rejected().size());
753 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
754 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, observer
.rejected()[0].error());
755 ASSERT_EQ(1u, observer
.timeouted().size());
756 EXPECT_EQ(request_id
, observer
.timeouted()[0].request_id());
757 ASSERT_EQ(1u, observer
.destroyed().size());
758 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
760 request_manager_
->RemoveObserver(&observer
);
763 TEST_F(FileSystemProviderRequestManagerTest
, ContinueOnTimeout
) {
765 RequestObserver observer
;
766 request_manager_
->AddObserver(&observer
);
768 request_manager_
->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0));
769 const int request_id
= request_manager_
->CreateRequest(
771 make_scoped_ptr
<RequestManager::HandlerInterface
>(
772 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
773 EXPECT_EQ(1, request_id
);
774 EXPECT_EQ(0u, logger
.success_events().size());
775 EXPECT_EQ(0u, logger
.error_events().size());
776 EXPECT_EQ(0u, notification_manager_
->size());
778 ASSERT_EQ(1u, observer
.created().size());
779 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
780 EXPECT_EQ(TESTING
, observer
.created()[0].type());
782 ASSERT_EQ(1u, observer
.executed().size());
783 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
785 // Wait until the request is timeouted.
786 base::RunLoop().RunUntilIdle();
788 // Let the extension more time by closing the notification.
789 EXPECT_EQ(1u, notification_manager_
->size());
790 notification_manager_
->Continue();
791 EXPECT_EQ(0u, notification_manager_
->size());
793 // The request is still active.
794 EXPECT_EQ(0u, logger
.success_events().size());
795 EXPECT_EQ(0u, logger
.error_events().size());
797 // Wait until the request is timeouted again.
798 base::RunLoop().RunUntilIdle();
799 EXPECT_EQ(1u, notification_manager_
->size());
801 request_manager_
->RemoveObserver(&observer
);
804 } // namespace file_system_provider
805 } // namespace chromeos