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 <stdint.h> 6 #include <utility> 7 8 #include "base/message_loop/message_loop.h" 9 #include "base/run_loop.h" 10 #include "mojo/public/cpp/bindings/binding.h" 11 #include "mojo/public/cpp/test_support/test_utils.h" 12 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" 13 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace mojo { 17 namespace test { 18 namespace { 19 20 class ProviderImpl : public sample::Provider { 21 public: 22 explicit ProviderImpl(InterfaceRequest<sample::Provider> request) 23 : binding_(this, std::move(request)) {} 24 25 void EchoString(const std::string& a, 26 const EchoStringCallback& callback) override { 27 EchoStringCallback callback_copy; 28 // Make sure operator= is used. 29 callback_copy = callback; 30 callback_copy.Run(a); 31 } 32 33 void EchoStrings(const std::string& a, 34 const std::string& b, 35 const EchoStringsCallback& callback) override { 36 callback.Run(a, b); 37 } 38 39 void EchoMessagePipeHandle( 40 ScopedMessagePipeHandle a, 41 const EchoMessagePipeHandleCallback& callback) override { 42 callback.Run(std::move(a)); 43 } 44 45 void EchoEnum(sample::Enum a, const EchoEnumCallback& callback) override { 46 callback.Run(a); 47 } 48 49 void EchoInt(int32_t a, const EchoIntCallback& callback) override { 50 callback.Run(a); 51 } 52 53 Binding<sample::Provider> binding_; 54 }; 55 56 void RecordString(std::string* storage, 57 const base::Closure& closure, 58 const std::string& str) { 59 *storage = str; 60 closure.Run(); 61 } 62 63 void RecordStrings(std::string* storage, 64 const base::Closure& closure, 65 const std::string& a, 66 const std::string& b) { 67 *storage = a + b; 68 closure.Run(); 69 } 70 71 void WriteToMessagePipe(const char* text, 72 const base::Closure& closure, 73 ScopedMessagePipeHandle handle) { 74 WriteTextMessage(handle.get(), text); 75 closure.Run(); 76 } 77 78 void RecordEnum(sample::Enum* storage, 79 const base::Closure& closure, 80 sample::Enum value) { 81 *storage = value; 82 closure.Run(); 83 } 84 85 class RequestResponseTest : public testing::Test { 86 public: 87 RequestResponseTest() {} 88 ~RequestResponseTest() override { base::RunLoop().RunUntilIdle(); } 89 90 void PumpMessages() { base::RunLoop().RunUntilIdle(); } 91 92 private: 93 base::MessageLoop loop_; 94 }; 95 96 TEST_F(RequestResponseTest, EchoString) { 97 sample::ProviderPtr provider; 98 ProviderImpl provider_impl(GetProxy(&provider)); 99 100 std::string buf; 101 base::RunLoop run_loop; 102 provider->EchoString("hello", 103 base::Bind(&RecordString, &buf, run_loop.QuitClosure())); 104 105 run_loop.Run(); 106 107 EXPECT_EQ(std::string("hello"), buf); 108 } 109 110 TEST_F(RequestResponseTest, EchoStrings) { 111 sample::ProviderPtr provider; 112 ProviderImpl provider_impl(GetProxy(&provider)); 113 114 std::string buf; 115 base::RunLoop run_loop; 116 provider->EchoStrings("hello", " world", base::Bind(&RecordStrings, &buf, 117 run_loop.QuitClosure())); 118 119 run_loop.Run(); 120 121 EXPECT_EQ(std::string("hello world"), buf); 122 } 123 124 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { 125 sample::ProviderPtr provider; 126 ProviderImpl provider_impl(GetProxy(&provider)); 127 128 MessagePipe pipe2; 129 base::RunLoop run_loop; 130 provider->EchoMessagePipeHandle( 131 std::move(pipe2.handle1), 132 base::Bind(&WriteToMessagePipe, "hello", run_loop.QuitClosure())); 133 134 run_loop.Run(); 135 136 std::string value; 137 ReadTextMessage(pipe2.handle0.get(), &value); 138 139 EXPECT_EQ(std::string("hello"), value); 140 } 141 142 TEST_F(RequestResponseTest, EchoEnum) { 143 sample::ProviderPtr provider; 144 ProviderImpl provider_impl(GetProxy(&provider)); 145 146 sample::Enum value; 147 base::RunLoop run_loop; 148 provider->EchoEnum(sample::Enum::VALUE, 149 base::Bind(&RecordEnum, &value, run_loop.QuitClosure())); 150 run_loop.Run(); 151 152 EXPECT_EQ(sample::Enum::VALUE, value); 153 } 154 155 } // namespace 156 } // namespace test 157 } // namespace mojo 158