| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 scoped_ptr<base::Value> result; | 539 scoped_ptr<base::Value> result; |
| 540 ASSERT_EQ( | 540 ASSERT_EQ( |
| 541 kStaleElementReference, | 541 kStaleElementReference, |
| 542 ExecuteFindChildElement( | 542 ExecuteFindChildElement( |
| 543 1, &session, &web_view, element_id, params, &result).code()); | 543 1, &session, &web_view, element_id, params, &result).code()); |
| 544 ASSERT_EQ( | 544 ASSERT_EQ( |
| 545 kStaleElementReference, | 545 kStaleElementReference, |
| 546 ExecuteFindChildElements( | 546 ExecuteFindChildElements( |
| 547 1, &session, &web_view, element_id, params, &result).code()); | 547 1, &session, &web_view, element_id, params, &result).code()); |
| 548 } | 548 } |
| 549 | |
| 550 namespace { | |
| 551 | |
| 552 class MockCommandListener : public CommandListener { | |
| 553 public: | |
| 554 MockCommandListener() : called_(false) {} | |
| 555 virtual ~MockCommandListener() {} | |
| 556 | |
| 557 virtual Status BeforeCommand(const std::string& command_name) OVERRIDE { | |
| 558 called_ = true; | |
| 559 EXPECT_STREQ("cmd", command_name.c_str()); | |
| 560 return Status(kOk); | |
| 561 } | |
| 562 | |
| 563 void VerifyCalled() { | |
| 564 EXPECT_TRUE(called_); | |
| 565 } | |
| 566 | |
| 567 void VerifyNotCalled() { | |
| 568 EXPECT_FALSE(called_); | |
| 569 } | |
| 570 | |
| 571 private: | |
| 572 bool called_; | |
| 573 }; | |
| 574 | |
| 575 Status ExecuteAddListenerToSessionCommand( | |
| 576 MockCommandListener* listener, | |
| 577 Session* session, | |
| 578 const base::DictionaryValue& params, | |
| 579 scoped_ptr<base::Value>* return_value) { | |
| 580 session->command_listeners.push_back(listener); | |
| 581 listener->VerifyNotCalled(); | |
| 582 return Status(kOk); | |
| 583 } | |
| 584 | |
| 585 Status ExecuteQuitSessionCommand( | |
| 586 Session* session, | |
| 587 const base::DictionaryValue& params, | |
| 588 scoped_ptr<base::Value>* return_value) { | |
| 589 session->quit = true; | |
| 590 return Status(kOk); | |
| 591 } | |
| 592 | |
| 593 void OnAddListenerToSessionCommand( | |
| 594 MockCommandListener* listener, | |
| 595 base::RunLoop* run_loop, | |
| 596 const Status& status, | |
| 597 scoped_ptr<base::Value> value, | |
| 598 const std::string& session_id) { | |
| 599 ASSERT_EQ(kOk, status.code()); | |
| 600 listener->VerifyNotCalled(); | |
| 601 run_loop->Quit(); | |
| 602 } | |
| 603 | |
| 604 void OnQuitSessionCommand( | |
| 605 MockCommandListener* listener, | |
| 606 base::RunLoop* run_loop, | |
| 607 const Status& status, | |
| 608 scoped_ptr<base::Value> value, | |
| 609 const std::string& session_id) { | |
| 610 ASSERT_EQ(kOk, status.code()); | |
| 611 listener->VerifyCalled(); | |
| 612 run_loop->Quit(); | |
| 613 } | |
| 614 | |
| 615 } // namespace | |
| 616 | |
| 617 TEST(CommandsTest, SessionNotifiedOfCommand) { | |
| 618 SessionThreadMap map; | |
| 619 linked_ptr<base::Thread> thread(new base::Thread("1")); | |
| 620 ASSERT_TRUE(thread->Start()); | |
| 621 std::string id("id"); | |
| 622 thread->message_loop()->PostTask( | |
| 623 FROM_HERE, | |
| 624 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); | |
| 625 map[id] = thread; | |
| 626 | |
| 627 base::DictionaryValue params; | |
| 628 // |listener| is owned by and will be destroyed by |session| | |
| 629 MockCommandListener* listener = new MockCommandListener(); | |
| 630 SessionCommand cmd = base::Bind( | |
| 631 &ExecuteAddListenerToSessionCommand, listener); | |
| 632 | |
| 633 base::MessageLoop loop; | |
| 634 base::RunLoop run_loop_addlistener; | |
| 635 | |
| 636 // |CommandListener|s are notified immediately before commands are run. | |
| 637 // Here, the command adds |listener| to the session, so |listener| | |
| 638 // should not be notified since it will not have been added yet. | |
| 639 ExecuteSessionCommand( | |
| 640 &map, | |
| 641 "cmd", | |
| 642 cmd, | |
| 643 false, | |
| 644 params, | |
| 645 id, | |
| 646 base::Bind(&OnAddListenerToSessionCommand, listener, | |
| 647 &run_loop_addlistener)); | |
| 648 | |
| 649 run_loop_addlistener.Run(); | |
| 650 | |
| 651 base::RunLoop run_loop_testlistener; | |
| 652 cmd = base::Bind( | |
| 653 &ExecuteQuitSessionCommand); | |
| 654 | |
| 655 // |listener| was added to |session| by ExecuteAddListenerToSessionCommand | |
| 656 // and should be notified before the next command, ExecuteQuitSessionCommand. | |
| 657 ExecuteSessionCommand( | |
| 658 &map, | |
| 659 "cmd", | |
| 660 cmd, | |
| 661 false, | |
| 662 params, | |
| 663 id, | |
| 664 base::Bind(&OnQuitSessionCommand, listener, | |
| 665 &run_loop_testlistener)); | |
| 666 | |
| 667 run_loop_testlistener.Run(); | |
| 668 } | |
| OLD | NEW |