| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
| 11 #include "mojo/public/cpp/test_support/test_utils.h" | 11 #include "mojo/public/cpp/test_support/test_utils.h" |
| 12 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" | 12 #include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h" |
| 13 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 13 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace mojo { | 16 namespace mojo { |
| 17 namespace test { | 17 namespace test { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class ProviderImpl : public sample::Provider { | 20 class ProviderImpl : public sample::Provider { |
| 21 public: | 21 public: |
| 22 explicit ProviderImpl(InterfaceRequest<sample::Provider> request) | 22 explicit ProviderImpl(InterfaceRequest<sample::Provider> request) |
| 23 : binding_(this, std::move(request)) {} | 23 : binding_(this, std::move(request)) {} |
| 24 | 24 |
| 25 void EchoString(const String& a, | 25 void EchoString(const std::string& a, |
| 26 const EchoStringCallback& callback) override { | 26 const EchoStringCallback& callback) override { |
| 27 EchoStringCallback callback_copy; | 27 EchoStringCallback callback_copy; |
| 28 // Make sure operator= is used. | 28 // Make sure operator= is used. |
| 29 callback_copy = callback; | 29 callback_copy = callback; |
| 30 callback_copy.Run(a); | 30 callback_copy.Run(a); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void EchoStrings(const String& a, | 33 void EchoStrings(const std::string& a, |
| 34 const String& b, | 34 const std::string& b, |
| 35 const EchoStringsCallback& callback) override { | 35 const EchoStringsCallback& callback) override { |
| 36 callback.Run(a, b); | 36 callback.Run(a, b); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void EchoMessagePipeHandle( | 39 void EchoMessagePipeHandle( |
| 40 ScopedMessagePipeHandle a, | 40 ScopedMessagePipeHandle a, |
| 41 const EchoMessagePipeHandleCallback& callback) override { | 41 const EchoMessagePipeHandleCallback& callback) override { |
| 42 callback.Run(std::move(a)); | 42 callback.Run(std::move(a)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void EchoEnum(sample::Enum a, const EchoEnumCallback& callback) override { | 45 void EchoEnum(sample::Enum a, const EchoEnumCallback& callback) override { |
| 46 callback.Run(a); | 46 callback.Run(a); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void EchoInt(int32_t a, const EchoIntCallback& callback) override { | 49 void EchoInt(int32_t a, const EchoIntCallback& callback) override { |
| 50 callback.Run(a); | 50 callback.Run(a); |
| 51 } | 51 } |
| 52 | 52 |
| 53 Binding<sample::Provider> binding_; | 53 Binding<sample::Provider> binding_; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 void RecordString(std::string* storage, | 56 void RecordString(std::string* storage, |
| 57 const base::Closure& closure, | 57 const base::Closure& closure, |
| 58 String str) { | 58 const std::string& str) { |
| 59 *storage = str; | 59 *storage = str; |
| 60 closure.Run(); | 60 closure.Run(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void RecordStrings(std::string* storage, | 63 void RecordStrings(std::string* storage, |
| 64 const base::Closure& closure, | 64 const base::Closure& closure, |
| 65 String a, | 65 const std::string& a, |
| 66 String b) { | 66 const std::string& b) { |
| 67 *storage = a.get() + b.get(); | 67 *storage = a + b; |
| 68 closure.Run(); | 68 closure.Run(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void WriteToMessagePipe(const char* text, | 71 void WriteToMessagePipe(const char* text, |
| 72 const base::Closure& closure, | 72 const base::Closure& closure, |
| 73 ScopedMessagePipeHandle handle) { | 73 ScopedMessagePipeHandle handle) { |
| 74 WriteTextMessage(handle.get(), text); | 74 WriteTextMessage(handle.get(), text); |
| 75 closure.Run(); | 75 closure.Run(); |
| 76 } | 76 } |
| 77 | 77 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 92 private: | 92 private: |
| 93 base::MessageLoop loop_; | 93 base::MessageLoop loop_; |
| 94 }; | 94 }; |
| 95 | 95 |
| 96 TEST_F(RequestResponseTest, EchoString) { | 96 TEST_F(RequestResponseTest, EchoString) { |
| 97 sample::ProviderPtr provider; | 97 sample::ProviderPtr provider; |
| 98 ProviderImpl provider_impl(GetProxy(&provider)); | 98 ProviderImpl provider_impl(GetProxy(&provider)); |
| 99 | 99 |
| 100 std::string buf; | 100 std::string buf; |
| 101 base::RunLoop run_loop; | 101 base::RunLoop run_loop; |
| 102 provider->EchoString(String::From("hello"), | 102 provider->EchoString("hello", |
| 103 base::Bind(&RecordString, &buf, run_loop.QuitClosure())); | 103 base::Bind(&RecordString, &buf, run_loop.QuitClosure())); |
| 104 | 104 |
| 105 run_loop.Run(); | 105 run_loop.Run(); |
| 106 | 106 |
| 107 EXPECT_EQ(std::string("hello"), buf); | 107 EXPECT_EQ(std::string("hello"), buf); |
| 108 } | 108 } |
| 109 | 109 |
| 110 TEST_F(RequestResponseTest, EchoStrings) { | 110 TEST_F(RequestResponseTest, EchoStrings) { |
| 111 sample::ProviderPtr provider; | 111 sample::ProviderPtr provider; |
| 112 ProviderImpl provider_impl(GetProxy(&provider)); | 112 ProviderImpl provider_impl(GetProxy(&provider)); |
| 113 | 113 |
| 114 std::string buf; | 114 std::string buf; |
| 115 base::RunLoop run_loop; | 115 base::RunLoop run_loop; |
| 116 provider->EchoStrings( | 116 provider->EchoStrings("hello", " world", base::Bind(&RecordStrings, &buf, |
| 117 String::From("hello"), String::From(" world"), | 117 run_loop.QuitClosure())); |
| 118 base::Bind(&RecordStrings, &buf, run_loop.QuitClosure())); | |
| 119 | 118 |
| 120 run_loop.Run(); | 119 run_loop.Run(); |
| 121 | 120 |
| 122 EXPECT_EQ(std::string("hello world"), buf); | 121 EXPECT_EQ(std::string("hello world"), buf); |
| 123 } | 122 } |
| 124 | 123 |
| 125 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { | 124 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { |
| 126 sample::ProviderPtr provider; | 125 sample::ProviderPtr provider; |
| 127 ProviderImpl provider_impl(GetProxy(&provider)); | 126 ProviderImpl provider_impl(GetProxy(&provider)); |
| 128 | 127 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 149 provider->EchoEnum(sample::Enum::VALUE, | 148 provider->EchoEnum(sample::Enum::VALUE, |
| 150 base::Bind(&RecordEnum, &value, run_loop.QuitClosure())); | 149 base::Bind(&RecordEnum, &value, run_loop.QuitClosure())); |
| 151 run_loop.Run(); | 150 run_loop.Run(); |
| 152 | 151 |
| 153 EXPECT_EQ(sample::Enum::VALUE, value); | 152 EXPECT_EQ(sample::Enum::VALUE, value); |
| 154 } | 153 } |
| 155 | 154 |
| 156 } // namespace | 155 } // namespace |
| 157 } // namespace test | 156 } // namespace test |
| 158 } // namespace mojo | 157 } // namespace mojo |
| OLD | NEW |