OLD | NEW |
(Empty) | |
| 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 "mojo/public/bindings/allocation_scope.h" |
| 6 #include "mojo/public/bindings/remote_ptr.h" |
| 7 #include "mojo/public/bindings/tests/sample_import.mojom.h" |
| 8 #include "mojo/public/bindings/tests/sample_interfaces.mojom.h" |
| 9 #include "mojo/public/environment/environment.h" |
| 10 #include "mojo/public/tests/test_utils.h" |
| 11 #include "mojo/public/utility/run_loop.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace test { |
| 16 namespace { |
| 17 |
| 18 class ProviderImpl : public sample::Provider { |
| 19 public: |
| 20 explicit ProviderImpl(sample::ScopedProviderClientHandle handle) |
| 21 : client_(handle.Pass(), this) { |
| 22 } |
| 23 |
| 24 virtual void EchoString( |
| 25 const String& a, |
| 26 const Callback<void(String)>& callback) MOJO_OVERRIDE { |
| 27 AllocationScope scope; |
| 28 callback.Run(a); |
| 29 } |
| 30 |
| 31 virtual void EchoStrings( |
| 32 const String& a, |
| 33 const String& b, |
| 34 const Callback<void(String, String)>& callback) MOJO_OVERRIDE { |
| 35 AllocationScope scope; |
| 36 callback.Run(a, b); |
| 37 } |
| 38 |
| 39 virtual void EchoMessagePipeHandle( |
| 40 ScopedMessagePipeHandle a, |
| 41 const Callback<void(ScopedMessagePipeHandle)>& callback) MOJO_OVERRIDE { |
| 42 AllocationScope scope; |
| 43 callback.Run(a.Pass()); |
| 44 } |
| 45 |
| 46 private: |
| 47 RemotePtr<sample::ProviderClient> client_; |
| 48 }; |
| 49 |
| 50 class StringRecorder { |
| 51 public: |
| 52 StringRecorder(std::string* buf) : buf_(buf) { |
| 53 } |
| 54 void Run(const String& a) const { |
| 55 *buf_ = a.To<std::string>(); |
| 56 } |
| 57 void Run(const String& a, const String& b) const { |
| 58 *buf_ = a.To<std::string>() + b.To<std::string>(); |
| 59 } |
| 60 private: |
| 61 std::string* buf_; |
| 62 }; |
| 63 |
| 64 class MessagePipeWriter { |
| 65 public: |
| 66 explicit MessagePipeWriter(const char* text) : text_(text) { |
| 67 } |
| 68 void Run(ScopedMessagePipeHandle handle) const { |
| 69 WriteTextMessage(handle.get(), text_); |
| 70 } |
| 71 private: |
| 72 std::string text_; |
| 73 }; |
| 74 |
| 75 class RequestResponseTest : public testing::Test { |
| 76 public: |
| 77 void PumpMessages() { |
| 78 loop_.RunUntilIdle(); |
| 79 } |
| 80 |
| 81 private: |
| 82 Environment env_; |
| 83 RunLoop loop_; |
| 84 }; |
| 85 |
| 86 TEST_F(RequestResponseTest, EchoString) { |
| 87 InterfacePipe<sample::Provider> pipe; |
| 88 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); |
| 89 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass()); |
| 90 |
| 91 std::string buf; |
| 92 { |
| 93 AllocationScope scope; |
| 94 provider->EchoString("hello", StringRecorder(&buf)); |
| 95 } |
| 96 |
| 97 PumpMessages(); |
| 98 |
| 99 EXPECT_EQ(std::string("hello"), buf); |
| 100 } |
| 101 |
| 102 TEST_F(RequestResponseTest, EchoStrings) { |
| 103 InterfacePipe<sample::Provider> pipe; |
| 104 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); |
| 105 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass()); |
| 106 |
| 107 std::string buf; |
| 108 { |
| 109 AllocationScope scope; |
| 110 provider->EchoStrings("hello", " world", StringRecorder(&buf)); |
| 111 } |
| 112 |
| 113 PumpMessages(); |
| 114 |
| 115 EXPECT_EQ(std::string("hello world"), buf); |
| 116 } |
| 117 |
| 118 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { |
| 119 InterfacePipe<sample::Provider> pipe; |
| 120 ProviderImpl provider_impl(pipe.handle_to_peer.Pass()); |
| 121 RemotePtr<sample::Provider> provider(pipe.handle_to_self.Pass()); |
| 122 |
| 123 MessagePipe pipe2; |
| 124 { |
| 125 AllocationScope scope; |
| 126 provider->EchoMessagePipeHandle(pipe2.handle1.Pass(), |
| 127 MessagePipeWriter("hello")); |
| 128 } |
| 129 |
| 130 PumpMessages(); |
| 131 |
| 132 std::string value; |
| 133 ReadTextMessage(pipe2.handle0.get(), &value); |
| 134 |
| 135 EXPECT_EQ(std::string("hello"), value); |
| 136 } |
| 137 |
| 138 } // namespace |
| 139 } // namespace test |
| 140 } // namespace mojo |
OLD | NEW |