| 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 "chrome/test/chromedriver/commands.h" | 5 #include "chrome/test/chromedriver/commands.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 17 #include "base/location.h" | 17 #include "base/location.h" |
| 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 #include "chrome/test/chromedriver/chrome/status.h" | 24 #include "chrome/test/chromedriver/chrome/status.h" |
| 24 #include "chrome/test/chromedriver/chrome/stub_chrome.h" | 25 #include "chrome/test/chromedriver/chrome/stub_chrome.h" |
| 25 #include "chrome/test/chromedriver/chrome/stub_web_view.h" | 26 #include "chrome/test/chromedriver/chrome/stub_web_view.h" |
| 26 #include "chrome/test/chromedriver/chrome/web_view.h" | 27 #include "chrome/test/chromedriver/chrome/web_view.h" |
| 27 #include "chrome/test/chromedriver/command_listener_proxy.h" | 28 #include "chrome/test/chromedriver/command_listener_proxy.h" |
| (...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 | 678 |
| 678 void VerifyNotCalled() { | 679 void VerifyNotCalled() { |
| 679 EXPECT_FALSE(called_); | 680 EXPECT_FALSE(called_); |
| 680 } | 681 } |
| 681 | 682 |
| 682 private: | 683 private: |
| 683 bool called_; | 684 bool called_; |
| 684 }; | 685 }; |
| 685 | 686 |
| 686 Status ExecuteAddListenerToSessionCommand( | 687 Status ExecuteAddListenerToSessionCommand( |
| 687 CommandListener* listener, | 688 std::unique_ptr<CommandListener> listener, |
| 688 Session* session, | 689 Session* session, |
| 689 const base::DictionaryValue& params, | 690 const base::DictionaryValue& params, |
| 690 std::unique_ptr<base::Value>* return_value) { | 691 std::unique_ptr<base::Value>* return_value) { |
| 691 session->command_listeners.push_back(listener); | 692 session->command_listeners.push_back(std::move(listener)); |
| 692 return Status(kOk); | 693 return Status(kOk); |
| 693 } | 694 } |
| 694 | 695 |
| 695 Status ExecuteQuitSessionCommand(Session* session, | 696 Status ExecuteQuitSessionCommand(Session* session, |
| 696 const base::DictionaryValue& params, | 697 const base::DictionaryValue& params, |
| 697 std::unique_ptr<base::Value>* return_value) { | 698 std::unique_ptr<base::Value>* return_value) { |
| 698 session->quit = true; | 699 session->quit = true; |
| 699 return Status(kOk); | 700 return Status(kOk); |
| 700 } | 701 } |
| 701 | 702 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 715 linked_ptr<base::Thread> thread(new base::Thread("1")); | 716 linked_ptr<base::Thread> thread(new base::Thread("1")); |
| 716 ASSERT_TRUE(thread->Start()); | 717 ASSERT_TRUE(thread->Start()); |
| 717 std::string id("id"); | 718 std::string id("id"); |
| 718 thread->task_runner()->PostTask( | 719 thread->task_runner()->PostTask( |
| 719 FROM_HERE, | 720 FROM_HERE, |
| 720 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); | 721 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); |
| 721 | 722 |
| 722 map[id] = thread; | 723 map[id] = thread; |
| 723 | 724 |
| 724 base::DictionaryValue params; | 725 base::DictionaryValue params; |
| 725 std::unique_ptr<MockCommandListener> listener(new MockCommandListener()); | 726 auto listener = base::MakeUnique<MockCommandListener>(); |
| 726 CommandListenerProxy* proxy = new CommandListenerProxy(listener.get()); | 727 auto proxy = base::MakeUnique<CommandListenerProxy>(listener.get()); |
| 727 // We add |proxy| to the session instead of adding |listener| directly so that | 728 // We add |proxy| to the session instead of adding |listener| directly so that |
| 728 // after the session is destroyed by ExecuteQuitSessionCommand, we can still | 729 // after the session is destroyed by ExecuteQuitSessionCommand, we can still |
| 729 // verify the listener was called. The session owns and will destroy |proxy|. | 730 // verify the listener was called. The session owns and will destroy |proxy|. |
| 730 SessionCommand cmd = base::Bind(&ExecuteAddListenerToSessionCommand, proxy); | 731 SessionCommand cmd = |
| 732 base::Bind(&ExecuteAddListenerToSessionCommand, base::Passed(&proxy)); |
| 731 base::MessageLoop loop; | 733 base::MessageLoop loop; |
| 732 base::RunLoop run_loop_addlistener; | 734 base::RunLoop run_loop_addlistener; |
| 733 | 735 |
| 734 // |CommandListener|s are notified immediately before commands are run. | 736 // |CommandListener|s are notified immediately before commands are run. |
| 735 // Here, the command adds |listener| to the session, so |listener| | 737 // Here, the command adds |listener| to the session, so |listener| |
| 736 // should not be notified since it will not have been added yet. | 738 // should not be notified since it will not have been added yet. |
| 737 ExecuteSessionCommand( | 739 ExecuteSessionCommand( |
| 738 &map, | 740 &map, |
| 739 "cmd", | 741 "cmd", |
| 740 cmd, | 742 cmd, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 769 class FailingCommandListener : public CommandListener { | 771 class FailingCommandListener : public CommandListener { |
| 770 public: | 772 public: |
| 771 FailingCommandListener() {} | 773 FailingCommandListener() {} |
| 772 ~FailingCommandListener() override {} | 774 ~FailingCommandListener() override {} |
| 773 | 775 |
| 774 Status BeforeCommand(const std::string& command_name) override { | 776 Status BeforeCommand(const std::string& command_name) override { |
| 775 return Status(kUnknownError); | 777 return Status(kUnknownError); |
| 776 } | 778 } |
| 777 }; | 779 }; |
| 778 | 780 |
| 779 void AddListenerToSessionIfSessionExists(CommandListener* listener) { | 781 void AddListenerToSessionIfSessionExists( |
| 782 std::unique_ptr<CommandListener> listener) { |
| 780 Session* session = GetThreadLocalSession(); | 783 Session* session = GetThreadLocalSession(); |
| 781 if (session) { | 784 if (session) { |
| 782 session->command_listeners.push_back(listener); | 785 session->command_listeners.push_back(std::move(listener)); |
| 783 } | 786 } |
| 784 } | 787 } |
| 785 | 788 |
| 786 void OnFailBecauseErrorNotifyingListeners(base::RunLoop* run_loop, | 789 void OnFailBecauseErrorNotifyingListeners(base::RunLoop* run_loop, |
| 787 const Status& status, | 790 const Status& status, |
| 788 std::unique_ptr<base::Value> value, | 791 std::unique_ptr<base::Value> value, |
| 789 const std::string& session_id, | 792 const std::string& session_id, |
| 790 bool w3c_compliant) { | 793 bool w3c_compliant) { |
| 791 EXPECT_EQ(kUnknownError, status.code()); | 794 EXPECT_EQ(kUnknownError, status.code()); |
| 792 EXPECT_FALSE(value.get()); | 795 EXPECT_FALSE(value.get()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 805 ASSERT_TRUE(thread->Start()); | 808 ASSERT_TRUE(thread->Start()); |
| 806 std::string id("id"); | 809 std::string id("id"); |
| 807 thread->task_runner()->PostTask( | 810 thread->task_runner()->PostTask( |
| 808 FROM_HERE, | 811 FROM_HERE, |
| 809 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); | 812 base::Bind(&internal::CreateSessionOnSessionThreadForTesting, id)); |
| 810 map[id] = thread; | 813 map[id] = thread; |
| 811 | 814 |
| 812 // In SuccessNotifyingCommandListenersBeforeCommand, we verified BeforeCommand | 815 // In SuccessNotifyingCommandListenersBeforeCommand, we verified BeforeCommand |
| 813 // was called before (as opposed to after) command execution. We don't need to | 816 // was called before (as opposed to after) command execution. We don't need to |
| 814 // verify this again, so we can just add |listener| with PostTask. | 817 // verify this again, so we can just add |listener| with PostTask. |
| 815 CommandListener* listener = new FailingCommandListener(); | 818 auto listener = base::MakeUnique<FailingCommandListener>(); |
| 816 thread->task_runner()->PostTask( | 819 thread->task_runner()->PostTask( |
| 817 FROM_HERE, base::Bind(&AddListenerToSessionIfSessionExists, listener)); | 820 FROM_HERE, base::Bind(&AddListenerToSessionIfSessionExists, |
| 821 base::Passed(&listener))); |
| 818 | 822 |
| 819 base::DictionaryValue params; | 823 base::DictionaryValue params; |
| 820 // The command should never be executed if BeforeCommand fails for a listener. | 824 // The command should never be executed if BeforeCommand fails for a listener. |
| 821 SessionCommand cmd = base::Bind(&ShouldNotBeCalled); | 825 SessionCommand cmd = base::Bind(&ShouldNotBeCalled); |
| 822 base::MessageLoop loop; | 826 base::MessageLoop loop; |
| 823 base::RunLoop run_loop; | 827 base::RunLoop run_loop; |
| 824 | 828 |
| 825 ExecuteSessionCommand( | 829 ExecuteSessionCommand( |
| 826 &map, | 830 &map, |
| 827 "cmd", | 831 "cmd", |
| 828 cmd, | 832 cmd, |
| 829 false, | 833 false, |
| 830 params, | 834 params, |
| 831 id, | 835 id, |
| 832 base::Bind(&OnFailBecauseErrorNotifyingListeners, &run_loop)); | 836 base::Bind(&OnFailBecauseErrorNotifyingListeners, &run_loop)); |
| 833 run_loop.Run(); | 837 run_loop.Run(); |
| 834 | 838 |
| 835 thread->task_runner()->PostTask(FROM_HERE, | 839 thread->task_runner()->PostTask(FROM_HERE, |
| 836 base::Bind(&VerifySessionWasDeleted)); | 840 base::Bind(&VerifySessionWasDeleted)); |
| 837 } | 841 } |
| OLD | NEW |