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 "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "testing/gtest/include/gtest/gtest.h"
26 namespace file_system_provider
{
29 // Fake implementation for the notification manager. Simulates user action on
31 class FakeNotificationManager
: public NotificationManagerInterface
{
33 FakeNotificationManager() {}
34 ~FakeNotificationManager() override
{}
36 // NotificationManagerInterface overrides:
37 void ShowUnresponsiveNotification(
39 const NotificationCallback
& callback
) override
{
40 callbacks_
[id
] = callback
;
43 void HideUnresponsiveNotification(int id
) override
{ callbacks_
.erase(id
); }
45 // Aborts all of the virtually shown notifications.
46 void Abort() { OnNotificationResult(ABORT
); }
48 // Discards all of the virtually shown notifications.
49 void Continue() { OnNotificationResult(CONTINUE
); }
51 // Returns number of currently shown notifications.
52 size_t size() { return callbacks_
.size(); }
55 typedef std::map
<int, NotificationCallback
> CallbackMap
;
57 // Handles a notification result by calling all registered callbacks and
59 void OnNotificationResult(NotificationResult result
) {
60 CallbackMap::iterator it
= callbacks_
.begin();
61 while (it
!= callbacks_
.end()) {
62 CallbackMap::iterator current_it
= it
++;
63 NotificationCallback callback
= current_it
->second
;
64 callbacks_
.erase(current_it
);
69 CallbackMap callbacks_
;
71 DISALLOW_COPY_AND_ASSIGN(FakeNotificationManager
);
74 // Logs calls of the success and error callbacks on requests.
79 explicit ExecuteEvent(int request_id
) : request_id_(request_id
) {}
80 virtual ~ExecuteEvent() {}
82 int request_id() { return request_id_
; }
90 SuccessEvent(int request_id
, scoped_ptr
<RequestValue
> result
, bool has_more
)
91 : request_id_(request_id
),
92 result_(result
.Pass()),
93 has_more_(has_more
) {}
94 virtual ~SuccessEvent() {}
96 int request_id() { return request_id_
; }
97 RequestValue
* result() { return result_
.get(); }
98 bool has_more() { return has_more_
; }
102 scoped_ptr
<RequestValue
> result_
;
108 ErrorEvent(int request_id
,
109 scoped_ptr
<RequestValue
> result
,
110 base::File::Error error
)
111 : request_id_(request_id
), result_(result
.Pass()), error_(error
) {}
112 virtual ~ErrorEvent() {}
114 int request_id() { return request_id_
; }
115 RequestValue
* result() { return result_
.get(); }
116 base::File::Error
error() { return error_
; }
120 scoped_ptr
<RequestValue
> result_
;
121 base::File::Error error_
;
124 EventLogger() : weak_ptr_factory_(this) {}
125 virtual ~EventLogger() {}
127 void OnExecute(int request_id
) {
128 execute_events_
.push_back(new ExecuteEvent(request_id
));
131 void OnSuccess(int request_id
,
132 scoped_ptr
<RequestValue
> result
,
134 success_events_
.push_back(
135 new SuccessEvent(request_id
, result
.Pass(), has_more
));
138 void OnError(int request_id
,
139 scoped_ptr
<RequestValue
> result
,
140 base::File::Error error
) {
141 error_events_
.push_back(new ErrorEvent(request_id
, result
.Pass(), error
));
144 ScopedVector
<ExecuteEvent
>& execute_events() { return execute_events_
; }
145 ScopedVector
<SuccessEvent
>& success_events() { return success_events_
; }
146 ScopedVector
<ErrorEvent
>& error_events() { return error_events_
; }
148 base::WeakPtr
<EventLogger
> GetWeakPtr() {
149 return weak_ptr_factory_
.GetWeakPtr();
153 ScopedVector
<ExecuteEvent
> execute_events_
;
154 ScopedVector
<SuccessEvent
> success_events_
;
155 ScopedVector
<ErrorEvent
> error_events_
;
156 base::WeakPtrFactory
<EventLogger
> weak_ptr_factory_
;
158 DISALLOW_COPY_AND_ASSIGN(EventLogger
);
161 // Fake handler, which forwards callbacks to the logger. The handler is owned
162 // by a request manager, however the logger is owned by tests.
163 class FakeHandler
: public RequestManager::HandlerInterface
{
165 // The handler can outlive the passed logger, so using a weak pointer. The
166 // |execute_reply| value will be returned for the Execute() call.
167 FakeHandler(base::WeakPtr
<EventLogger
> logger
, bool execute_reply
)
168 : logger_(logger
), execute_reply_(execute_reply
) {}
170 // RequestManager::Handler overrides.
171 bool Execute(int request_id
) override
{
173 logger_
->OnExecute(request_id
);
175 return execute_reply_
;
178 // RequestManager::Handler overrides.
179 void OnSuccess(int request_id
,
180 scoped_ptr
<RequestValue
> result
,
181 bool has_more
) override
{
183 logger_
->OnSuccess(request_id
, result
.Pass(), has_more
);
186 // RequestManager::Handler overrides.
187 void OnError(int request_id
,
188 scoped_ptr
<RequestValue
> result
,
189 base::File::Error error
) override
{
191 logger_
->OnError(request_id
, result
.Pass(), error
);
194 ~FakeHandler() override
{}
197 base::WeakPtr
<EventLogger
> logger_
;
199 DISALLOW_COPY_AND_ASSIGN(FakeHandler
);
202 // Observer the request manager for request events.
203 class RequestObserver
: public RequestManager::Observer
{
207 explicit Event(int request_id
) : request_id_(request_id
) {}
209 int request_id() const { return request_id_
; }
215 class CreatedEvent
: public Event
{
217 CreatedEvent(int request_id
, RequestType type
)
218 : Event(request_id
), type_(type
) {}
219 ~CreatedEvent() override
{}
221 RequestType
type() const { return type_
; }
227 class FulfilledEvent
: public Event
{
229 FulfilledEvent(int request_id
, bool has_more
)
230 : Event(request_id
), has_more_(has_more
) {}
231 ~FulfilledEvent() override
{}
233 bool has_more() const { return has_more_
; }
239 class RejectedEvent
: public Event
{
241 RejectedEvent(int request_id
, base::File::Error error
)
242 : Event(request_id
), error_(error
) {}
243 ~RejectedEvent() override
{}
245 base::File::Error
error() const { return error_
; }
248 base::File::Error error_
;
252 ~RequestObserver() override
{}
254 // RequestManager::Observer overrides.
255 void OnRequestCreated(int request_id
, RequestType type
) override
{
256 created_
.push_back(CreatedEvent(request_id
, type
));
259 // RequestManager::Observer overrides.
260 void OnRequestDestroyed(int request_id
) override
{
261 destroyed_
.push_back(Event(request_id
));
264 // RequestManager::Observer overrides.
265 void OnRequestExecuted(int request_id
) override
{
266 executed_
.push_back(Event(request_id
));
269 // RequestManager::Observer overrides.
270 void OnRequestFulfilled(int request_id
,
271 const RequestValue
& result
,
272 bool has_more
) override
{
273 fulfilled_
.push_back(FulfilledEvent(request_id
, has_more
));
276 // RequestManager::Observer overrides.
277 void OnRequestRejected(int request_id
,
278 const RequestValue
& result
,
279 base::File::Error error
) override
{
280 rejected_
.push_back(RejectedEvent(request_id
, error
));
283 // RequestManager::Observer overrides.
284 void OnRequestTimeouted(int request_id
) override
{
285 timeouted_
.push_back(Event(request_id
));
288 const std::vector
<CreatedEvent
>& created() const { return created_
; }
289 const std::vector
<Event
>& destroyed() const { return destroyed_
; }
290 const std::vector
<Event
>& executed() const { return executed_
; }
291 const std::vector
<FulfilledEvent
>& fulfilled() const { return fulfilled_
; }
292 const std::vector
<RejectedEvent
>& rejected() const { return rejected_
; }
293 const std::vector
<Event
>& timeouted() const { return timeouted_
; }
296 std::vector
<CreatedEvent
> created_
;
297 std::vector
<Event
> destroyed_
;
298 std::vector
<Event
> executed_
;
299 std::vector
<FulfilledEvent
> fulfilled_
;
300 std::vector
<RejectedEvent
> rejected_
;
301 std::vector
<Event
> timeouted_
;
303 DISALLOW_COPY_AND_ASSIGN(RequestObserver
);
308 class FileSystemProviderRequestManagerTest
: public testing::Test
{
310 FileSystemProviderRequestManagerTest() {}
311 ~FileSystemProviderRequestManagerTest() override
{}
313 void SetUp() override
{
314 profile_
.reset(new TestingProfile
);
315 notification_manager_
.reset(new FakeNotificationManager
);
316 request_manager_
.reset(new RequestManager(profile_
.get(),
317 std::string() /* extension_id */,
318 notification_manager_
.get()));
321 content::TestBrowserThreadBundle thread_bundle_
;
322 scoped_ptr
<TestingProfile
> profile_
;
323 scoped_ptr
<FakeNotificationManager
> notification_manager_
;
324 scoped_ptr
<RequestManager
> request_manager_
;
327 TEST_F(FileSystemProviderRequestManagerTest
, CreateFailure
) {
329 RequestObserver observer
;
330 request_manager_
->AddObserver(&observer
);
332 const int request_id
= request_manager_
->CreateRequest(
334 make_scoped_ptr
<RequestManager::HandlerInterface
>(
335 new FakeHandler(logger
.GetWeakPtr(), false /* execute_reply */)));
337 EXPECT_EQ(0, request_id
);
338 EXPECT_EQ(0u, logger
.success_events().size());
339 EXPECT_EQ(0u, logger
.error_events().size());
341 EXPECT_EQ(1u, observer
.created().size());
342 EXPECT_EQ(TESTING
, observer
.created()[0].type());
343 EXPECT_EQ(1u, observer
.destroyed().size());
344 EXPECT_EQ(0u, observer
.executed().size());
346 const std::vector
<int> active_request_ids
=
347 request_manager_
->GetActiveRequestIds();
348 EXPECT_EQ(0u, active_request_ids
.size());
350 request_manager_
->RemoveObserver(&observer
);
353 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndFulFill
) {
355 RequestObserver observer
;
356 request_manager_
->AddObserver(&observer
);
358 const int request_id
= request_manager_
->CreateRequest(
360 make_scoped_ptr
<RequestManager::HandlerInterface
>(
361 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
363 EXPECT_EQ(1, request_id
);
364 EXPECT_EQ(0u, logger
.success_events().size());
365 EXPECT_EQ(0u, logger
.error_events().size());
367 ASSERT_EQ(1u, observer
.created().size());
368 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
369 EXPECT_EQ(TESTING
, observer
.created()[0].type());
371 ASSERT_EQ(1u, observer
.executed().size());
372 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
374 const std::vector
<int> active_request_ids
=
375 request_manager_
->GetActiveRequestIds();
376 ASSERT_EQ(1u, active_request_ids
.size());
377 EXPECT_EQ(request_id
, active_request_ids
[0]);
379 scoped_ptr
<RequestValue
> response(
380 RequestValue::CreateForTesting("i-like-vanilla"));
381 const bool has_more
= false;
383 const base::File::Error result
=
384 request_manager_
->FulfillRequest(request_id
, response
.Pass(), has_more
);
385 EXPECT_EQ(base::File::FILE_OK
, result
);
387 ASSERT_EQ(1u, observer
.fulfilled().size());
388 EXPECT_EQ(request_id
, observer
.fulfilled()[0].request_id());
389 EXPECT_FALSE(observer
.fulfilled()[0].has_more());
391 // Validate if the callback has correct arguments.
392 ASSERT_EQ(1u, logger
.success_events().size());
393 EXPECT_EQ(0u, logger
.error_events().size());
394 EventLogger::SuccessEvent
* event
= logger
.success_events()[0];
395 ASSERT_TRUE(event
->result());
396 const std::string
* response_test_string
= event
->result()->testing_params();
397 ASSERT_TRUE(response_test_string
);
398 EXPECT_EQ("i-like-vanilla", *response_test_string
);
399 EXPECT_FALSE(event
->has_more());
401 // Confirm, that the request is removed. Basically, fulfilling again for the
402 // same request, should fail.
404 const std::vector
<int> active_request_ids
=
405 request_manager_
->GetActiveRequestIds();
406 EXPECT_EQ(0u, active_request_ids
.size());
408 const base::File::Error retry
= request_manager_
->FulfillRequest(
409 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
410 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
411 EXPECT_EQ(1u, observer
.fulfilled().size());
414 // Rejecting should also fail.
416 const base::File::Error retry
= request_manager_
->RejectRequest(
417 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()),
418 base::File::FILE_ERROR_FAILED
);
419 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
420 EXPECT_EQ(0u, observer
.rejected().size());
423 ASSERT_EQ(1u, observer
.destroyed().size());
424 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
425 EXPECT_EQ(0u, observer
.timeouted().size());
427 request_manager_
->RemoveObserver(&observer
);
430 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndFulFill_WithHasNext
) {
432 RequestObserver observer
;
433 request_manager_
->AddObserver(&observer
);
435 const int request_id
= request_manager_
->CreateRequest(
437 make_scoped_ptr
<RequestManager::HandlerInterface
>(
438 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
440 EXPECT_EQ(1, request_id
);
441 EXPECT_EQ(0u, logger
.success_events().size());
442 EXPECT_EQ(0u, logger
.error_events().size());
444 ASSERT_EQ(1u, observer
.created().size());
445 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
446 EXPECT_EQ(TESTING
, observer
.created()[0].type());
448 ASSERT_EQ(1u, observer
.executed().size());
449 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
451 const bool has_more
= true;
453 const base::File::Error result
= request_manager_
->FulfillRequest(
454 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
455 EXPECT_EQ(base::File::FILE_OK
, result
);
457 // Validate if the callback has correct arguments.
458 ASSERT_EQ(1u, logger
.success_events().size());
459 EXPECT_EQ(0u, logger
.error_events().size());
460 EventLogger::SuccessEvent
* event
= logger
.success_events()[0];
461 EXPECT_TRUE(event
->result());
462 EXPECT_TRUE(event
->has_more());
464 ASSERT_EQ(1u, observer
.fulfilled().size());
465 EXPECT_EQ(request_id
, observer
.fulfilled()[0].request_id());
466 EXPECT_TRUE(observer
.fulfilled()[0].has_more());
468 // Confirm, that the request is not removed (since it has has_more == true).
469 // Basically, fulfilling again for the same request, should not fail.
471 const std::vector
<int> active_request_ids
=
472 request_manager_
->GetActiveRequestIds();
473 ASSERT_EQ(1u, active_request_ids
.size());
474 EXPECT_EQ(request_id
, active_request_ids
[0]);
476 const bool new_has_more
= false;
477 const base::File::Error retry
= request_manager_
->FulfillRequest(
478 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), new_has_more
);
479 EXPECT_EQ(base::File::FILE_OK
, retry
);
481 ASSERT_EQ(2u, observer
.fulfilled().size());
482 EXPECT_EQ(request_id
, observer
.fulfilled()[1].request_id());
483 EXPECT_FALSE(observer
.fulfilled()[1].has_more());
486 // Since |new_has_more| is false, then the request should be removed. To check
487 // it, try to fulfill again, what should fail.
489 const std::vector
<int> active_request_ids
=
490 request_manager_
->GetActiveRequestIds();
491 EXPECT_EQ(0u, active_request_ids
.size());
493 const bool new_has_more
= false;
494 const base::File::Error retry
= request_manager_
->FulfillRequest(
495 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), new_has_more
);
496 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
497 EXPECT_EQ(0u, observer
.rejected().size());
500 ASSERT_EQ(1u, observer
.destroyed().size());
501 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
502 EXPECT_EQ(0u, observer
.timeouted().size());
504 request_manager_
->RemoveObserver(&observer
);
507 TEST_F(FileSystemProviderRequestManagerTest
, CreateAndReject
) {
509 RequestObserver observer
;
510 request_manager_
->AddObserver(&observer
);
512 const int request_id
= request_manager_
->CreateRequest(
514 make_scoped_ptr
<RequestManager::HandlerInterface
>(
515 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
517 EXPECT_EQ(1, request_id
);
518 EXPECT_EQ(0u, logger
.success_events().size());
519 EXPECT_EQ(0u, logger
.error_events().size());
521 ASSERT_EQ(1u, observer
.created().size());
522 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
523 EXPECT_EQ(TESTING
, observer
.created()[0].type());
525 ASSERT_EQ(1u, observer
.executed().size());
526 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
528 const base::File::Error error
= base::File::FILE_ERROR_NO_MEMORY
;
529 const base::File::Error result
= request_manager_
->RejectRequest(
530 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
531 EXPECT_EQ(base::File::FILE_OK
, result
);
533 // Validate if the callback has correct arguments.
534 ASSERT_EQ(1u, logger
.error_events().size());
535 EXPECT_EQ(0u, logger
.success_events().size());
536 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
537 EXPECT_EQ(error
, event
->error());
539 ASSERT_EQ(1u, observer
.rejected().size());
540 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
541 EXPECT_EQ(error
, observer
.rejected()[0].error());
543 // Confirm, that the request is removed. Basically, fulfilling again for the
544 // same request, should fail.
546 const bool has_more
= false;
547 const base::File::Error retry
= request_manager_
->FulfillRequest(
548 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
549 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
550 EXPECT_EQ(0u, observer
.fulfilled().size());
553 // Rejecting should also fail.
555 const base::File::Error retry
= request_manager_
->RejectRequest(
556 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
557 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, retry
);
558 EXPECT_EQ(1u, observer
.rejected().size());
561 ASSERT_EQ(1u, observer
.destroyed().size());
562 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
563 EXPECT_EQ(0u, observer
.timeouted().size());
565 request_manager_
->RemoveObserver(&observer
);
568 TEST_F(FileSystemProviderRequestManagerTest
,
569 CreateAndFulfillWithWrongRequestId
) {
571 RequestObserver observer
;
572 request_manager_
->AddObserver(&observer
);
574 const int request_id
= request_manager_
->CreateRequest(
576 make_scoped_ptr
<RequestManager::HandlerInterface
>(
577 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
579 EXPECT_EQ(1, request_id
);
580 EXPECT_EQ(0u, logger
.success_events().size());
581 EXPECT_EQ(0u, logger
.error_events().size());
583 ASSERT_EQ(1u, observer
.created().size());
584 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
585 EXPECT_EQ(TESTING
, observer
.created()[0].type());
587 ASSERT_EQ(1u, observer
.executed().size());
588 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
590 const bool has_more
= true;
592 const base::File::Error result
= request_manager_
->FulfillRequest(
593 request_id
+ 1, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
594 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, result
);
596 // Callbacks should not be called.
597 EXPECT_EQ(0u, logger
.error_events().size());
598 EXPECT_EQ(0u, logger
.success_events().size());
600 EXPECT_EQ(0u, observer
.fulfilled().size());
601 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
603 // Confirm, that the request hasn't been removed, by fulfilling it correctly.
605 const base::File::Error retry
= request_manager_
->FulfillRequest(
606 request_id
, scoped_ptr
<RequestValue
>(new RequestValue
), has_more
);
607 EXPECT_EQ(base::File::FILE_OK
, retry
);
608 EXPECT_EQ(1u, observer
.fulfilled().size());
611 request_manager_
->RemoveObserver(&observer
);
614 TEST_F(FileSystemProviderRequestManagerTest
,
615 CreateAndRejectWithWrongRequestId
) {
617 RequestObserver observer
;
618 request_manager_
->AddObserver(&observer
);
620 const int request_id
= request_manager_
->CreateRequest(
622 make_scoped_ptr
<RequestManager::HandlerInterface
>(
623 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
625 EXPECT_EQ(1, request_id
);
626 EXPECT_EQ(0u, logger
.success_events().size());
627 EXPECT_EQ(0u, logger
.error_events().size());
629 ASSERT_EQ(1u, observer
.created().size());
630 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
631 EXPECT_EQ(TESTING
, observer
.created()[0].type());
633 ASSERT_EQ(1u, observer
.executed().size());
634 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
636 const base::File::Error error
= base::File::FILE_ERROR_NO_MEMORY
;
637 const base::File::Error result
= request_manager_
->RejectRequest(
638 request_id
+ 1, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
639 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND
, result
);
641 // Callbacks should not be called.
642 EXPECT_EQ(0u, logger
.error_events().size());
643 EXPECT_EQ(0u, logger
.success_events().size());
645 EXPECT_EQ(0u, observer
.rejected().size());
647 // Confirm, that the request hasn't been removed, by rejecting it correctly.
649 const base::File::Error retry
= request_manager_
->RejectRequest(
650 request_id
, scoped_ptr
<RequestValue
>(new RequestValue()), error
);
651 EXPECT_EQ(base::File::FILE_OK
, retry
);
652 EXPECT_EQ(1u, observer
.rejected().size());
655 request_manager_
->RemoveObserver(&observer
);
658 TEST_F(FileSystemProviderRequestManagerTest
, UniqueIds
) {
661 const int first_request_id
= request_manager_
->CreateRequest(
663 make_scoped_ptr
<RequestManager::HandlerInterface
>(
664 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
666 const int second_request_id
= request_manager_
->CreateRequest(
668 make_scoped_ptr
<RequestManager::HandlerInterface
>(
669 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
671 EXPECT_EQ(1, first_request_id
);
672 EXPECT_EQ(2, second_request_id
);
675 TEST_F(FileSystemProviderRequestManagerTest
, AbortOnDestroy
) {
677 RequestObserver observer
;
681 RequestManager
request_manager(profile_
.get(),
682 std::string() /* extension_id */, nullptr);
683 request_manager
.AddObserver(&observer
);
685 request_id
= request_manager
.CreateRequest(
687 make_scoped_ptr
<RequestManager::HandlerInterface
>(
688 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
690 EXPECT_EQ(1, request_id
);
691 EXPECT_EQ(0u, logger
.success_events().size());
692 EXPECT_EQ(0u, logger
.error_events().size());
694 ASSERT_EQ(1u, observer
.created().size());
695 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
696 EXPECT_EQ(TESTING
, observer
.created()[0].type());
698 ASSERT_EQ(1u, observer
.executed().size());
699 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
701 EXPECT_EQ(0u, observer
.fulfilled().size());
702 EXPECT_EQ(0u, observer
.rejected().size());
703 EXPECT_EQ(0u, observer
.destroyed().size());
704 EXPECT_EQ(0u, observer
.timeouted().size());
706 // Do not remove the observer, to catch events while destructing.
709 // All active requests should be aborted in the destructor of RequestManager.
710 ASSERT_EQ(1u, logger
.error_events().size());
711 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
712 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, event
->error());
714 EXPECT_EQ(0u, logger
.success_events().size());
716 EXPECT_EQ(0u, observer
.fulfilled().size());
717 EXPECT_EQ(0u, observer
.timeouted().size());
718 ASSERT_EQ(1u, observer
.rejected().size());
719 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
720 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, observer
.rejected()[0].error());
721 ASSERT_EQ(1u, observer
.destroyed().size());
724 TEST_F(FileSystemProviderRequestManagerTest
, AbortOnTimeout
) {
726 RequestObserver observer
;
727 request_manager_
->AddObserver(&observer
);
729 request_manager_
->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0));
730 const int request_id
= request_manager_
->CreateRequest(
732 make_scoped_ptr
<RequestManager::HandlerInterface
>(
733 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
734 EXPECT_EQ(1, request_id
);
735 EXPECT_EQ(0u, logger
.success_events().size());
736 EXPECT_EQ(0u, logger
.error_events().size());
737 EXPECT_EQ(0u, notification_manager_
->size());
739 ASSERT_EQ(1u, observer
.created().size());
740 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
741 EXPECT_EQ(TESTING
, observer
.created()[0].type());
743 ASSERT_EQ(1u, observer
.executed().size());
744 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
746 // Wait until the request is timeouted.
747 base::RunLoop().RunUntilIdle();
749 // Abort the request.
750 EXPECT_EQ(1u, notification_manager_
->size());
751 notification_manager_
->Abort();
752 EXPECT_EQ(0u, notification_manager_
->size());
754 ASSERT_EQ(1u, logger
.error_events().size());
755 EventLogger::ErrorEvent
* event
= logger
.error_events()[0];
756 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, event
->error());
758 ASSERT_EQ(1u, observer
.rejected().size());
759 EXPECT_EQ(request_id
, observer
.rejected()[0].request_id());
760 EXPECT_EQ(base::File::FILE_ERROR_ABORT
, observer
.rejected()[0].error());
761 ASSERT_EQ(1u, observer
.timeouted().size());
762 EXPECT_EQ(request_id
, observer
.timeouted()[0].request_id());
763 ASSERT_EQ(1u, observer
.destroyed().size());
764 EXPECT_EQ(request_id
, observer
.destroyed()[0].request_id());
766 request_manager_
->RemoveObserver(&observer
);
769 TEST_F(FileSystemProviderRequestManagerTest
, ContinueOnTimeout
) {
771 RequestObserver observer
;
772 request_manager_
->AddObserver(&observer
);
774 request_manager_
->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0));
775 const int request_id
= request_manager_
->CreateRequest(
777 make_scoped_ptr
<RequestManager::HandlerInterface
>(
778 new FakeHandler(logger
.GetWeakPtr(), true /* execute_reply */)));
779 EXPECT_EQ(1, request_id
);
780 EXPECT_EQ(0u, logger
.success_events().size());
781 EXPECT_EQ(0u, logger
.error_events().size());
782 EXPECT_EQ(0u, notification_manager_
->size());
784 ASSERT_EQ(1u, observer
.created().size());
785 EXPECT_EQ(request_id
, observer
.created()[0].request_id());
786 EXPECT_EQ(TESTING
, observer
.created()[0].type());
788 ASSERT_EQ(1u, observer
.executed().size());
789 EXPECT_EQ(request_id
, observer
.executed()[0].request_id());
791 // Wait until the request is timeouted.
792 base::RunLoop().RunUntilIdle();
794 // Let the extension more time by closing the notification.
795 EXPECT_EQ(1u, notification_manager_
->size());
796 notification_manager_
->Continue();
797 EXPECT_EQ(0u, notification_manager_
->size());
799 // The request is still active.
800 EXPECT_EQ(0u, logger
.success_events().size());
801 EXPECT_EQ(0u, logger
.error_events().size());
803 // Wait until the request is timeouted again.
804 base::RunLoop().RunUntilIdle();
805 EXPECT_EQ(1u, notification_manager_
->size());
807 request_manager_
->RemoveObserver(&observer
);
810 } // namespace file_system_provider
811 } // namespace chromeos