| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <utility> | 6 #include <utility> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 EXPECT_EQ("greetings", description); | 788 EXPECT_EQ("greetings", description); |
| 789 quit_closure.Run(); | 789 quit_closure.Run(); |
| 790 }, | 790 }, |
| 791 run_loop.QuitClosure())); | 791 run_loop.QuitClosure())); |
| 792 | 792 |
| 793 request.ResetWithReason(88u, "greetings"); | 793 request.ResetWithReason(88u, "greetings"); |
| 794 | 794 |
| 795 run_loop.Run(); | 795 run_loop.Run(); |
| 796 } | 796 } |
| 797 | 797 |
| 798 TEST_F(InterfacePtrTest, CallbackOwnsInterfacePtr) { | 798 TEST_F(InterfacePtrTest, CallbackIsPassedInterfacePtr) { |
| 799 sample::PingTestPtr ptr; | 799 sample::PingTestPtr ptr; |
| 800 sample::PingTestRequest request(&ptr); | 800 sample::PingTestRequest request(&ptr); |
| 801 | 801 |
| 802 base::RunLoop run_loop; | 802 base::RunLoop run_loop; |
| 803 | 803 |
| 804 // Make a call with the proxy's lifetime bound to the response callback. | 804 // Make a call with the proxy's lifetime bound to the response callback. |
| 805 sample::PingTest* raw_proxy = ptr.get(); | 805 sample::PingTest* raw_proxy = ptr.get(); |
| 806 ptr.set_connection_error_handler(run_loop.QuitClosure()); | 806 ptr.set_connection_error_handler(run_loop.QuitClosure()); |
| 807 raw_proxy->Ping( | 807 raw_proxy->Ping( |
| 808 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); | 808 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); |
| 809 | 809 |
| 810 // Trigger an error on |ptr|. This will ultimately lead to the proxy's | 810 // Trigger an error on |ptr|. This will ultimately lead to the proxy's |
| 811 // response callbacks being destroyed, which will in turn lead to the proxy | 811 // response callbacks being destroyed, which will in turn lead to the proxy |
| 812 // being destroyed. This should not crash. | 812 // being destroyed. This should not crash. |
| 813 request.PassMessagePipe(); | 813 request.PassMessagePipe(); |
| 814 run_loop.Run(); | 814 run_loop.Run(); |
| 815 } | 815 } |
| 816 | 816 |
| 817 TEST_F(InterfacePtrTest, ConnectionErrorHandlerOwnsInterfacePtr) { |
| 818 sample::PingTestPtr* ptr = new sample::PingTestPtr; |
| 819 sample::PingTestRequest request(ptr); |
| 820 |
| 821 base::RunLoop run_loop; |
| 822 |
| 823 // Make a call with |ptr|'s lifetime bound to the connection error handler |
| 824 // callback. |
| 825 ptr->set_connection_error_handler(base::Bind( |
| 826 [](const base::Closure& quit, sample::PingTestPtr* ptr) { |
| 827 ptr->reset(); |
| 828 quit.Run(); |
| 829 }, |
| 830 run_loop.QuitClosure(), base::Owned(ptr))); |
| 831 |
| 832 // Trigger an error on |ptr|. In the error handler |ptr| is reset. This |
| 833 // shouldn't immediately destroy the callback (and |ptr| that it owns), before |
| 834 // the callback is completed. |
| 835 request.PassMessagePipe(); |
| 836 run_loop.Run(); |
| 837 } |
| 838 |
| 817 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) { | 839 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) { |
| 818 math::CalculatorPtr ptr; | 840 math::CalculatorPtr ptr; |
| 819 MathCalculatorImpl calc_impl(MakeRequest(&ptr)); | 841 MathCalculatorImpl calc_impl(MakeRequest(&ptr)); |
| 820 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = | 842 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = |
| 821 math::ThreadSafeCalculatorPtr::Create(std::move(ptr)); | 843 math::ThreadSafeCalculatorPtr::Create(std::move(ptr)); |
| 822 | 844 |
| 823 base::RunLoop run_loop; | 845 base::RunLoop run_loop; |
| 824 | 846 |
| 825 // Create and start the thread from where we'll call the interface pointer. | 847 // Create and start the thread from where we'll call the interface pointer. |
| 826 base::Thread other_thread("service test thread"); | 848 base::Thread other_thread("service test thread"); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 906 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); | 928 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); |
| 907 | 929 |
| 908 // Reset the pointer now so the InterfacePtr associated resources can be | 930 // Reset the pointer now so the InterfacePtr associated resources can be |
| 909 // deleted before the background thread's message loop is invalidated. | 931 // deleted before the background thread's message loop is invalidated. |
| 910 thread_safe_ptr = nullptr; | 932 thread_safe_ptr = nullptr; |
| 911 } | 933 } |
| 912 | 934 |
| 913 } // namespace | 935 } // namespace |
| 914 } // namespace test | 936 } // namespace test |
| 915 } // namespace mojo | 937 } // namespace mojo |
| OLD | NEW |