1 // Copyright 2018 The Chromium Authors 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 "base/fuchsia/test_interface_impl.h" 6 7 #include <utility> 8 9 #include "base/memory/weak_ptr.h" 10 #include "base/run_loop.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace base { 14 15 TestInterfaceImpl::TestInterfaceImpl() = default; 16 TestInterfaceImpl::~TestInterfaceImpl() = default; 17 Add(int32_t a,int32_t b,AddCallback callback)18void TestInterfaceImpl::Add(int32_t a, int32_t b, AddCallback callback) { 19 callback(a + b); 20 } 21 VerifyTestInterface(fidl::InterfacePtr<testfidl::TestInterface> & ptr)22zx_status_t VerifyTestInterface( 23 fidl::InterfacePtr<testfidl::TestInterface>& ptr) { 24 // Call the service and wait for response. 25 RunLoop run_loop; 26 zx_status_t result = ZX_ERR_INTERNAL; 27 base::WeakPtrFactory<zx_status_t> weak_result(&result); 28 29 ptr.set_error_handler( 30 [quit = run_loop.QuitClosure(), 31 weak_result = weak_result.GetWeakPtr()](zx_status_t status) { 32 if (weak_result) 33 *weak_result = status; 34 std::move(quit).Run(); 35 }); 36 37 ptr->Add(2, 2, 38 [quit = run_loop.QuitClosure(), 39 weak_result = weak_result.GetWeakPtr()](int32_t value) { 40 EXPECT_EQ(value, 4); 41 if (weak_result) 42 *weak_result = ZX_OK; 43 std::move(quit).Run(); 44 }); 45 46 run_loop.Run(); 47 48 // Reset error handler because the current one captures |run_loop| and 49 // |error| references which are about to be destroyed. 50 ptr.set_error_handler(nullptr); 51 52 return result; 53 } 54 55 } // namespace base 56