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 "chrome/browser/chromeos/file_system_provider/request_manager.h" 6 7 #include <map> 8 #include <string> 9 #include <vector> 10 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" 23 24 namespace chromeos { 25 namespace file_system_provider { 26 namespace { 27 28 // Fake implementation for the notification manager. Simulates user action on 29 // a notification. 30 class FakeNotificationManager : public NotificationManagerInterface { 31 public: 32 FakeNotificationManager() {} 33 virtual ~FakeNotificationManager() {} 34 35 // NotificationManagerInterface overrides: 36 virtual void ShowUnresponsiveNotification( 37 int id, 38 const NotificationCallback& callback) OVERRIDE { 39 callbacks_[id] = callback; 40 } 41 42 virtual void HideUnresponsiveNotification(int id) OVERRIDE { 43 callbacks_.erase(id); 44 } 45 46 // Aborts all of the virtually shown notifications. 47 void Abort() { OnNotificationResult(ABORT); } 48 49 // Discards all of the virtually shown notifications. 50 void Continue() { OnNotificationResult(CONTINUE); } 51 52 // Returns number of currently shown notifications. 53 size_t size() { return callbacks_.size(); } 54 55 private: 56 typedef std::map<int, NotificationCallback> CallbackMap; 57 58 // Handles a notification result by calling all registered callbacks and 59 // clearing the list. 60 void OnNotificationResult(NotificationResult result) { 61 CallbackMap::iterator it = callbacks_.begin(); 62 while (it != callbacks_.end()) { 63 CallbackMap::iterator current_it = it++; 64 NotificationCallback callback = current_it->second; 65 callbacks_.erase(current_it); 66 callback.Run(result); 67 } 68 } 69 70 CallbackMap callbacks_; 71 72 DISALLOW_COPY_AND_ASSIGN(FakeNotificationManager); 73 }; 74 75 // Logs calls of the success and error callbacks on requests. 76 class EventLogger { 77 public: 78 class ExecuteEvent { 79 public: 80 explicit ExecuteEvent(int request_id) : request_id_(request_id) {} 81 virtual ~ExecuteEvent() {} 82 83 int request_id() { return request_id_; } 84 85 private: 86 int request_id_; 87 }; 88 89 class SuccessEvent { 90 public: 91 SuccessEvent(int request_id, scoped_ptr<RequestValue> result, bool has_more) 92 : request_id_(request_id), 93 result_(result.Pass()), 94 has_more_(has_more) {} 95 virtual ~SuccessEvent() {} 96 97 int request_id() { return request_id_; } 98 RequestValue* result() { return result_.get(); } 99 bool has_more() { return has_more_; } 100 101 private: 102 int request_id_; 103 scoped_ptr<RequestValue> result_; 104 bool has_more_; 105 }; 106 107 class ErrorEvent { 108 public: 109 ErrorEvent(int request_id, 110 scoped_ptr<RequestValue> result, 111 base::File::Error error) 112 : request_id_(request_id), result_(result.Pass()), error_(error) {} 113 virtual ~ErrorEvent() {} 114 115 int request_id() { return request_id_; } 116 RequestValue* result() { return result_.get(); } 117 base::File::Error error() { return error_; } 118 119 private: 120 int request_id_; 121 scoped_ptr<RequestValue> result_; 122 base::File::Error error_; 123 }; 124 125 EventLogger() : weak_ptr_factory_(this) {} 126 virtual ~EventLogger() {} 127 128 void OnExecute(int request_id) { 129 execute_events_.push_back(new ExecuteEvent(request_id)); 130 } 131 132 void OnSuccess(int request_id, 133 scoped_ptr<RequestValue> result, 134 bool has_more) { 135 success_events_.push_back( 136 new SuccessEvent(request_id, result.Pass(), has_more)); 137 } 138 139 void OnError(int request_id, 140 scoped_ptr<RequestValue> result, 141 base::File::Error error) { 142 error_events_.push_back(new ErrorEvent(request_id, result.Pass(), error)); 143 } 144 145 ScopedVector<ExecuteEvent>& execute_events() { return execute_events_; } 146 ScopedVector<SuccessEvent>& success_events() { return success_events_; } 147 ScopedVector<ErrorEvent>& error_events() { return error_events_; } 148 149 base::WeakPtr<EventLogger> GetWeakPtr() { 150 return weak_ptr_factory_.GetWeakPtr(); 151 } 152 153 private: 154 ScopedVector<ExecuteEvent> execute_events_; 155 ScopedVector<SuccessEvent> success_events_; 156 ScopedVector<ErrorEvent> error_events_; 157 base::WeakPtrFactory<EventLogger> weak_ptr_factory_; 158 159 DISALLOW_COPY_AND_ASSIGN(EventLogger); 160 }; 161 162 // Fake handler, which forwards callbacks to the logger. The handler is owned 163 // by a request manager, however the logger is owned by tests. 164 class FakeHandler : public RequestManager::HandlerInterface { 165 public: 166 // The handler can outlive the passed logger, so using a weak pointer. The 167 // |execute_reply| value will be returned for the Execute() call. 168 FakeHandler(base::WeakPtr<EventLogger> logger, bool execute_reply) 169 : logger_(logger), execute_reply_(execute_reply) {} 170 171 // RequestManager::Handler overrides. 172 virtual bool Execute(int request_id) OVERRIDE { 173 if (logger_.get()) 174 logger_->OnExecute(request_id); 175 176 return execute_reply_; 177 } 178 179 // RequestManager::Handler overrides. 180 virtual void OnSuccess(int request_id, 181 scoped_ptr<RequestValue> result, 182 bool has_more) OVERRIDE { 183 if (logger_.get()) 184 logger_->OnSuccess(request_id, result.Pass(), has_more); 185 } 186 187 // RequestManager::Handler overrides. 188 virtual void OnError(int request_id, 189 scoped_ptr<RequestValue> result, 190 base::File::Error error) OVERRIDE { 191 if (logger_.get()) 192 logger_->OnError(request_id, result.Pass(), error); 193 } 194 195 virtual ~FakeHandler() {} 196 197 private: 198 base::WeakPtr<EventLogger> logger_; 199 bool execute_reply_; 200 DISALLOW_COPY_AND_ASSIGN(FakeHandler); 201 }; 202 203 // Observer the request manager for request events. 204 class RequestObserver : public RequestManager::Observer { 205 public: 206 class Event { 207 public: 208 explicit Event(int request_id) : request_id_(request_id) {} 209 virtual ~Event() {} 210 int request_id() const { return request_id_; } 211 212 private: 213 int request_id_; 214 }; 215 216 class CreatedEvent : public Event { 217 public: 218 CreatedEvent(int request_id, RequestType type) 219 : Event(request_id), type_(type) {} 220 virtual ~CreatedEvent() {} 221 222 RequestType type() const { return type_; } 223 224 private: 225 RequestType type_; 226 }; 227 228 class FulfilledEvent : public Event { 229 public: 230 FulfilledEvent(int request_id, bool has_more) 231 : Event(request_id), has_more_(has_more) {} 232 virtual ~FulfilledEvent() {} 233 234 bool has_more() const { return has_more_; } 235 236 private: 237 bool has_more_; 238 }; 239 240 class RejectedEvent : public Event { 241 public: 242 RejectedEvent(int request_id, base::File::Error error) 243 : Event(request_id), error_(error) {} 244 virtual ~RejectedEvent() {} 245 246 base::File::Error error() const { return error_; } 247 248 private: 249 base::File::Error error_; 250 }; 251 252 RequestObserver() {} 253 virtual ~RequestObserver() {} 254 255 // RequestManager::Observer overrides. 256 virtual void OnRequestCreated(int request_id, RequestType type) OVERRIDE { 257 created_.push_back(CreatedEvent(request_id, type)); 258 } 259 260 // RequestManager::Observer overrides. 261 virtual void OnRequestDestroyed(int request_id) OVERRIDE { 262 destroyed_.push_back(Event(request_id)); 263 } 264 265 // RequestManager::Observer overrides. 266 virtual void OnRequestExecuted(int request_id) OVERRIDE { 267 executed_.push_back(Event(request_id)); 268 } 269 270 // RequestManager::Observer overrides. 271 virtual void OnRequestFulfilled(int request_id, 272 const RequestValue& result, 273 bool has_more) OVERRIDE { 274 fulfilled_.push_back(FulfilledEvent(request_id, has_more)); 275 } 276 277 // RequestManager::Observer overrides. 278 virtual void OnRequestRejected(int request_id, 279 const RequestValue& result, 280 base::File::Error error) OVERRIDE { 281 rejected_.push_back(RejectedEvent(request_id, error)); 282 } 283 284 // RequestManager::Observer overrides. 285 virtual void OnRequestTimeouted(int request_id) OVERRIDE { 286 timeouted_.push_back(Event(request_id)); 287 } 288 289 const std::vector<CreatedEvent>& created() const { return created_; } 290 const std::vector<Event>& destroyed() const { return destroyed_; } 291 const std::vector<Event>& executed() const { return executed_; } 292 const std::vector<FulfilledEvent>& fulfilled() const { return fulfilled_; } 293 const std::vector<RejectedEvent>& rejected() const { return rejected_; } 294 const std::vector<Event>& timeouted() const { return timeouted_; } 295 296 private: 297 std::vector<CreatedEvent> created_; 298 std::vector<Event> destroyed_; 299 std::vector<Event> executed_; 300 std::vector<FulfilledEvent> fulfilled_; 301 std::vector<RejectedEvent> rejected_; 302 std::vector<Event> timeouted_; 303 304 DISALLOW_COPY_AND_ASSIGN(RequestObserver); 305 }; 306 307 } // namespace 308 309 class FileSystemProviderRequestManagerTest : public testing::Test { 310 protected: 311 FileSystemProviderRequestManagerTest() {} 312 virtual ~FileSystemProviderRequestManagerTest() {} 313 314 virtual void SetUp() OVERRIDE { 315 notification_manager_.reset(new FakeNotificationManager); 316 request_manager_.reset(new RequestManager(notification_manager_.get())); 317 } 318 319 content::TestBrowserThreadBundle thread_bundle_; 320 scoped_ptr<FakeNotificationManager> notification_manager_; 321 scoped_ptr<RequestManager> request_manager_; 322 }; 323 324 TEST_F(FileSystemProviderRequestManagerTest, CreateFailure) { 325 EventLogger logger; 326 RequestObserver observer; 327 request_manager_->AddObserver(&observer); 328 329 const int request_id = request_manager_->CreateRequest( 330 TESTING, 331 make_scoped_ptr<RequestManager::HandlerInterface>( 332 new FakeHandler(logger.GetWeakPtr(), false /* execute_reply */))); 333 334 EXPECT_EQ(0, request_id); 335 EXPECT_EQ(0u, logger.success_events().size()); 336 EXPECT_EQ(0u, logger.error_events().size()); 337 338 EXPECT_EQ(1u, observer.created().size()); 339 EXPECT_EQ(TESTING, observer.created()[0].type()); 340 EXPECT_EQ(1u, observer.destroyed().size()); 341 EXPECT_EQ(0u, observer.executed().size()); 342 343 const std::vector<int> active_request_ids = 344 request_manager_->GetActiveRequestIds(); 345 EXPECT_EQ(0u, active_request_ids.size()); 346 347 request_manager_->RemoveObserver(&observer); 348 } 349 350 TEST_F(FileSystemProviderRequestManagerTest, CreateAndFulFill) { 351 EventLogger logger; 352 RequestObserver observer; 353 request_manager_->AddObserver(&observer); 354 355 const int request_id = request_manager_->CreateRequest( 356 TESTING, 357 make_scoped_ptr<RequestManager::HandlerInterface>( 358 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 359 360 EXPECT_EQ(1, request_id); 361 EXPECT_EQ(0u, logger.success_events().size()); 362 EXPECT_EQ(0u, logger.error_events().size()); 363 364 ASSERT_EQ(1u, observer.created().size()); 365 EXPECT_EQ(request_id, observer.created()[0].request_id()); 366 EXPECT_EQ(TESTING, observer.created()[0].type()); 367 368 ASSERT_EQ(1u, observer.executed().size()); 369 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 370 371 const std::vector<int> active_request_ids = 372 request_manager_->GetActiveRequestIds(); 373 ASSERT_EQ(1u, active_request_ids.size()); 374 EXPECT_EQ(request_id, active_request_ids[0]); 375 376 scoped_ptr<RequestValue> response( 377 RequestValue::CreateForTesting("i-like-vanilla")); 378 const bool has_more = false; 379 380 bool result = 381 request_manager_->FulfillRequest(request_id, response.Pass(), has_more); 382 EXPECT_TRUE(result); 383 384 ASSERT_EQ(1u, observer.fulfilled().size()); 385 EXPECT_EQ(request_id, observer.fulfilled()[0].request_id()); 386 EXPECT_FALSE(observer.fulfilled()[0].has_more()); 387 388 // Validate if the callback has correct arguments. 389 ASSERT_EQ(1u, logger.success_events().size()); 390 EXPECT_EQ(0u, logger.error_events().size()); 391 EventLogger::SuccessEvent* event = logger.success_events()[0]; 392 ASSERT_TRUE(event->result()); 393 const std::string* response_test_string = event->result()->testing_params(); 394 ASSERT_TRUE(response_test_string); 395 EXPECT_EQ("i-like-vanilla", *response_test_string); 396 EXPECT_FALSE(event->has_more()); 397 398 // Confirm, that the request is removed. Basically, fulfilling again for the 399 // same request, should fail. 400 { 401 const std::vector<int> active_request_ids = 402 request_manager_->GetActiveRequestIds(); 403 EXPECT_EQ(0u, active_request_ids.size()); 404 405 bool retry = request_manager_->FulfillRequest( 406 request_id, scoped_ptr<RequestValue>(new RequestValue), has_more); 407 EXPECT_FALSE(retry); 408 EXPECT_EQ(1u, observer.fulfilled().size()); 409 } 410 411 // Rejecting should also fail. 412 { 413 bool retry = request_manager_->RejectRequest( 414 request_id, 415 scoped_ptr<RequestValue>(new RequestValue()), 416 base::File::FILE_ERROR_FAILED); 417 EXPECT_FALSE(retry); 418 EXPECT_EQ(0u, observer.rejected().size()); 419 } 420 421 ASSERT_EQ(1u, observer.destroyed().size()); 422 EXPECT_EQ(request_id, observer.destroyed()[0].request_id()); 423 EXPECT_EQ(0u, observer.timeouted().size()); 424 425 request_manager_->RemoveObserver(&observer); 426 } 427 428 TEST_F(FileSystemProviderRequestManagerTest, CreateAndFulFill_WithHasNext) { 429 EventLogger logger; 430 RequestObserver observer; 431 request_manager_->AddObserver(&observer); 432 433 const int request_id = request_manager_->CreateRequest( 434 TESTING, 435 make_scoped_ptr<RequestManager::HandlerInterface>( 436 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 437 438 EXPECT_EQ(1, request_id); 439 EXPECT_EQ(0u, logger.success_events().size()); 440 EXPECT_EQ(0u, logger.error_events().size()); 441 442 ASSERT_EQ(1u, observer.created().size()); 443 EXPECT_EQ(request_id, observer.created()[0].request_id()); 444 EXPECT_EQ(TESTING, observer.created()[0].type()); 445 446 ASSERT_EQ(1u, observer.executed().size()); 447 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 448 449 const bool has_more = true; 450 451 bool result = request_manager_->FulfillRequest( 452 request_id, scoped_ptr<RequestValue>(new RequestValue), has_more); 453 EXPECT_TRUE(result); 454 455 // Validate if the callback has correct arguments. 456 ASSERT_EQ(1u, logger.success_events().size()); 457 EXPECT_EQ(0u, logger.error_events().size()); 458 EventLogger::SuccessEvent* event = logger.success_events()[0]; 459 EXPECT_TRUE(event->result()); 460 EXPECT_TRUE(event->has_more()); 461 462 ASSERT_EQ(1u, observer.fulfilled().size()); 463 EXPECT_EQ(request_id, observer.fulfilled()[0].request_id()); 464 EXPECT_TRUE(observer.fulfilled()[0].has_more()); 465 466 // Confirm, that the request is not removed (since it has has_more == true). 467 // Basically, fulfilling again for the same request, should not fail. 468 { 469 const std::vector<int> active_request_ids = 470 request_manager_->GetActiveRequestIds(); 471 ASSERT_EQ(1u, active_request_ids.size()); 472 EXPECT_EQ(request_id, active_request_ids[0]); 473 474 bool new_has_more = false; 475 bool retry = request_manager_->FulfillRequest( 476 request_id, scoped_ptr<RequestValue>(new RequestValue), new_has_more); 477 EXPECT_TRUE(retry); 478 479 ASSERT_EQ(2u, observer.fulfilled().size()); 480 EXPECT_EQ(request_id, observer.fulfilled()[1].request_id()); 481 EXPECT_FALSE(observer.fulfilled()[1].has_more()); 482 } 483 484 // Since |new_has_more| is false, then the request should be removed. To check 485 // it, try to fulfill again, what should fail. 486 { 487 const std::vector<int> active_request_ids = 488 request_manager_->GetActiveRequestIds(); 489 EXPECT_EQ(0u, active_request_ids.size()); 490 491 bool new_has_more = false; 492 bool retry = request_manager_->FulfillRequest( 493 request_id, scoped_ptr<RequestValue>(new RequestValue), new_has_more); 494 EXPECT_FALSE(retry); 495 EXPECT_EQ(0u, observer.rejected().size()); 496 } 497 498 ASSERT_EQ(1u, observer.destroyed().size()); 499 EXPECT_EQ(request_id, observer.destroyed()[0].request_id()); 500 EXPECT_EQ(0u, observer.timeouted().size()); 501 502 request_manager_->RemoveObserver(&observer); 503 } 504 505 TEST_F(FileSystemProviderRequestManagerTest, CreateAndReject) { 506 EventLogger logger; 507 RequestObserver observer; 508 request_manager_->AddObserver(&observer); 509 510 const int request_id = request_manager_->CreateRequest( 511 TESTING, 512 make_scoped_ptr<RequestManager::HandlerInterface>( 513 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 514 515 EXPECT_EQ(1, request_id); 516 EXPECT_EQ(0u, logger.success_events().size()); 517 EXPECT_EQ(0u, logger.error_events().size()); 518 519 ASSERT_EQ(1u, observer.created().size()); 520 EXPECT_EQ(request_id, observer.created()[0].request_id()); 521 EXPECT_EQ(TESTING, observer.created()[0].type()); 522 523 ASSERT_EQ(1u, observer.executed().size()); 524 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 525 526 base::File::Error error = base::File::FILE_ERROR_NO_MEMORY; 527 bool result = request_manager_->RejectRequest( 528 request_id, scoped_ptr<RequestValue>(new RequestValue()), error); 529 EXPECT_TRUE(result); 530 531 // Validate if the callback has correct arguments. 532 ASSERT_EQ(1u, logger.error_events().size()); 533 EXPECT_EQ(0u, logger.success_events().size()); 534 EventLogger::ErrorEvent* event = logger.error_events()[0]; 535 EXPECT_EQ(error, event->error()); 536 537 ASSERT_EQ(1u, observer.rejected().size()); 538 EXPECT_EQ(request_id, observer.rejected()[0].request_id()); 539 EXPECT_EQ(error, observer.rejected()[0].error()); 540 541 // Confirm, that the request is removed. Basically, fulfilling again for the 542 // same request, should fail. 543 { 544 bool has_more = false; 545 bool retry = request_manager_->FulfillRequest( 546 request_id, scoped_ptr<RequestValue>(new RequestValue), has_more); 547 EXPECT_FALSE(retry); 548 EXPECT_EQ(0u, observer.fulfilled().size()); 549 } 550 551 // Rejecting should also fail. 552 { 553 bool retry = request_manager_->RejectRequest( 554 request_id, scoped_ptr<RequestValue>(new RequestValue()), error); 555 EXPECT_FALSE(retry); 556 EXPECT_EQ(1u, observer.rejected().size()); 557 } 558 559 ASSERT_EQ(1u, observer.destroyed().size()); 560 EXPECT_EQ(request_id, observer.destroyed()[0].request_id()); 561 EXPECT_EQ(0u, observer.timeouted().size()); 562 563 request_manager_->RemoveObserver(&observer); 564 } 565 566 TEST_F(FileSystemProviderRequestManagerTest, 567 CreateAndFulfillWithWrongRequestId) { 568 EventLogger logger; 569 RequestObserver observer; 570 request_manager_->AddObserver(&observer); 571 572 const int request_id = request_manager_->CreateRequest( 573 TESTING, 574 make_scoped_ptr<RequestManager::HandlerInterface>( 575 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 576 577 EXPECT_EQ(1, request_id); 578 EXPECT_EQ(0u, logger.success_events().size()); 579 EXPECT_EQ(0u, logger.error_events().size()); 580 581 ASSERT_EQ(1u, observer.created().size()); 582 EXPECT_EQ(request_id, observer.created()[0].request_id()); 583 EXPECT_EQ(TESTING, observer.created()[0].type()); 584 585 ASSERT_EQ(1u, observer.executed().size()); 586 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 587 588 const bool has_more = true; 589 590 const bool result = request_manager_->FulfillRequest( 591 request_id + 1, scoped_ptr<RequestValue>(new RequestValue), has_more); 592 EXPECT_FALSE(result); 593 594 // Callbacks should not be called. 595 EXPECT_EQ(0u, logger.error_events().size()); 596 EXPECT_EQ(0u, logger.success_events().size()); 597 598 EXPECT_EQ(0u, observer.fulfilled().size()); 599 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 600 601 // Confirm, that the request hasn't been removed, by fulfilling it correctly. 602 { 603 const bool retry = request_manager_->FulfillRequest( 604 request_id, scoped_ptr<RequestValue>(new RequestValue), has_more); 605 EXPECT_TRUE(retry); 606 EXPECT_EQ(1u, observer.fulfilled().size()); 607 } 608 609 request_manager_->RemoveObserver(&observer); 610 } 611 612 TEST_F(FileSystemProviderRequestManagerTest, 613 CreateAndRejectWithWrongRequestId) { 614 EventLogger logger; 615 RequestObserver observer; 616 request_manager_->AddObserver(&observer); 617 618 const int request_id = request_manager_->CreateRequest( 619 TESTING, 620 make_scoped_ptr<RequestManager::HandlerInterface>( 621 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 622 623 EXPECT_EQ(1, request_id); 624 EXPECT_EQ(0u, logger.success_events().size()); 625 EXPECT_EQ(0u, logger.error_events().size()); 626 627 ASSERT_EQ(1u, observer.created().size()); 628 EXPECT_EQ(request_id, observer.created()[0].request_id()); 629 EXPECT_EQ(TESTING, observer.created()[0].type()); 630 631 ASSERT_EQ(1u, observer.executed().size()); 632 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 633 634 base::File::Error error = base::File::FILE_ERROR_NO_MEMORY; 635 bool result = request_manager_->RejectRequest( 636 request_id + 1, scoped_ptr<RequestValue>(new RequestValue()), error); 637 EXPECT_FALSE(result); 638 639 // Callbacks should not be called. 640 EXPECT_EQ(0u, logger.error_events().size()); 641 EXPECT_EQ(0u, logger.success_events().size()); 642 643 EXPECT_EQ(0u, observer.rejected().size()); 644 645 // Confirm, that the request hasn't been removed, by rejecting it correctly. 646 { 647 bool retry = request_manager_->RejectRequest( 648 request_id, scoped_ptr<RequestValue>(new RequestValue()), error); 649 EXPECT_TRUE(retry); 650 EXPECT_EQ(1u, observer.rejected().size()); 651 } 652 653 request_manager_->RemoveObserver(&observer); 654 } 655 656 TEST_F(FileSystemProviderRequestManagerTest, UniqueIds) { 657 EventLogger logger; 658 659 const int first_request_id = request_manager_->CreateRequest( 660 TESTING, 661 make_scoped_ptr<RequestManager::HandlerInterface>( 662 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 663 664 const int second_request_id = request_manager_->CreateRequest( 665 TESTING, 666 make_scoped_ptr<RequestManager::HandlerInterface>( 667 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 668 669 EXPECT_EQ(1, first_request_id); 670 EXPECT_EQ(2, second_request_id); 671 } 672 673 TEST_F(FileSystemProviderRequestManagerTest, AbortOnDestroy) { 674 EventLogger logger; 675 RequestObserver observer; 676 int request_id; 677 678 { 679 RequestManager request_manager(NULL); 680 request_manager.AddObserver(&observer); 681 682 request_id = request_manager.CreateRequest( 683 TESTING, 684 make_scoped_ptr<RequestManager::HandlerInterface>( 685 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 686 687 EXPECT_EQ(1, request_id); 688 EXPECT_EQ(0u, logger.success_events().size()); 689 EXPECT_EQ(0u, logger.error_events().size()); 690 691 ASSERT_EQ(1u, observer.created().size()); 692 EXPECT_EQ(request_id, observer.created()[0].request_id()); 693 EXPECT_EQ(TESTING, observer.created()[0].type()); 694 695 ASSERT_EQ(1u, observer.executed().size()); 696 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 697 698 EXPECT_EQ(0u, observer.fulfilled().size()); 699 EXPECT_EQ(0u, observer.rejected().size()); 700 EXPECT_EQ(0u, observer.destroyed().size()); 701 EXPECT_EQ(0u, observer.timeouted().size()); 702 703 // Do not remove the observer, to catch events while destructing. 704 } 705 706 // All active requests should be aborted in the destructor of RequestManager. 707 ASSERT_EQ(1u, logger.error_events().size()); 708 EventLogger::ErrorEvent* event = logger.error_events()[0]; 709 EXPECT_EQ(base::File::FILE_ERROR_ABORT, event->error()); 710 711 EXPECT_EQ(0u, logger.success_events().size()); 712 713 EXPECT_EQ(0u, observer.fulfilled().size()); 714 EXPECT_EQ(0u, observer.timeouted().size()); 715 ASSERT_EQ(1u, observer.rejected().size()); 716 EXPECT_EQ(request_id, observer.rejected()[0].request_id()); 717 EXPECT_EQ(base::File::FILE_ERROR_ABORT, observer.rejected()[0].error()); 718 ASSERT_EQ(1u, observer.destroyed().size()); 719 } 720 721 TEST_F(FileSystemProviderRequestManagerTest, AbortOnTimeout) { 722 EventLogger logger; 723 RequestObserver observer; 724 request_manager_->AddObserver(&observer); 725 726 request_manager_->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0)); 727 const int request_id = request_manager_->CreateRequest( 728 TESTING, 729 make_scoped_ptr<RequestManager::HandlerInterface>( 730 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 731 EXPECT_EQ(1, request_id); 732 EXPECT_EQ(0u, logger.success_events().size()); 733 EXPECT_EQ(0u, logger.error_events().size()); 734 EXPECT_EQ(0u, notification_manager_->size()); 735 736 ASSERT_EQ(1u, observer.created().size()); 737 EXPECT_EQ(request_id, observer.created()[0].request_id()); 738 EXPECT_EQ(TESTING, observer.created()[0].type()); 739 740 ASSERT_EQ(1u, observer.executed().size()); 741 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 742 743 // Wait until the request is timeouted. 744 base::RunLoop().RunUntilIdle(); 745 746 // Abort the request. 747 EXPECT_EQ(1u, notification_manager_->size()); 748 notification_manager_->Abort(); 749 EXPECT_EQ(0u, notification_manager_->size()); 750 751 ASSERT_EQ(1u, logger.error_events().size()); 752 EventLogger::ErrorEvent* event = logger.error_events()[0]; 753 EXPECT_EQ(base::File::FILE_ERROR_ABORT, event->error()); 754 755 ASSERT_EQ(1u, observer.rejected().size()); 756 EXPECT_EQ(request_id, observer.rejected()[0].request_id()); 757 EXPECT_EQ(base::File::FILE_ERROR_ABORT, observer.rejected()[0].error()); 758 ASSERT_EQ(1u, observer.timeouted().size()); 759 EXPECT_EQ(request_id, observer.timeouted()[0].request_id()); 760 ASSERT_EQ(1u, observer.destroyed().size()); 761 EXPECT_EQ(request_id, observer.destroyed()[0].request_id()); 762 763 request_manager_->RemoveObserver(&observer); 764 } 765 766 TEST_F(FileSystemProviderRequestManagerTest, ContinueOnTimeout) { 767 EventLogger logger; 768 RequestObserver observer; 769 request_manager_->AddObserver(&observer); 770 771 request_manager_->SetTimeoutForTesting(base::TimeDelta::FromSeconds(0)); 772 const int request_id = request_manager_->CreateRequest( 773 TESTING, 774 make_scoped_ptr<RequestManager::HandlerInterface>( 775 new FakeHandler(logger.GetWeakPtr(), true /* execute_reply */))); 776 EXPECT_EQ(1, request_id); 777 EXPECT_EQ(0u, logger.success_events().size()); 778 EXPECT_EQ(0u, logger.error_events().size()); 779 EXPECT_EQ(0u, notification_manager_->size()); 780 781 ASSERT_EQ(1u, observer.created().size()); 782 EXPECT_EQ(request_id, observer.created()[0].request_id()); 783 EXPECT_EQ(TESTING, observer.created()[0].type()); 784 785 ASSERT_EQ(1u, observer.executed().size()); 786 EXPECT_EQ(request_id, observer.executed()[0].request_id()); 787 788 // Wait until the request is timeouted. 789 base::RunLoop().RunUntilIdle(); 790 791 // Let the extension more time by closing the notification. 792 EXPECT_EQ(1u, notification_manager_->size()); 793 notification_manager_->Continue(); 794 EXPECT_EQ(0u, notification_manager_->size()); 795 796 // The request is still active. 797 EXPECT_EQ(0u, logger.success_events().size()); 798 EXPECT_EQ(0u, logger.error_events().size()); 799 800 // Wait until the request is timeouted again. 801 base::RunLoop().RunUntilIdle(); 802 EXPECT_EQ(1u, notification_manager_->size()); 803 804 request_manager_->RemoveObserver(&observer); 805 } 806 807 } // namespace file_system_provider 808 } // namespace chromeos 809