aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/tests/request_response_unittest.cc
blob: 43b8f0dc90e9b75fd49e70374ffc753897764376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>
#include <utility>

#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/test_support/test_utils.h"
#include "mojo/public/interfaces/bindings/tests/sample_import.mojom.h"
#include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace test {
namespace {

class ProviderImpl : public sample::Provider {
 public:
  explicit ProviderImpl(InterfaceRequest<sample::Provider> request)
      : binding_(this, std::move(request)) {}

  void EchoString(const std::string& a,
                  const EchoStringCallback& callback) override {
    EchoStringCallback callback_copy;
    // Make sure operator= is used.
    callback_copy = callback;
    callback_copy.Run(a);
  }

  void EchoStrings(const std::string& a,
                   const std::string& b,
                   const EchoStringsCallback& callback) override {
    callback.Run(a, b);
  }

  void EchoMessagePipeHandle(
      ScopedMessagePipeHandle a,
      const EchoMessagePipeHandleCallback& callback) override {
    callback.Run(std::move(a));
  }

  void EchoEnum(sample::Enum a, const EchoEnumCallback& callback) override {
    callback.Run(a);
  }

  void EchoInt(int32_t a, const EchoIntCallback& callback) override {
    callback.Run(a);
  }

  Binding<sample::Provider> binding_;
};

void RecordString(std::string* storage,
                  const base::Closure& closure,
                  const std::string& str) {
  *storage = str;
  closure.Run();
}

void RecordStrings(std::string* storage,
                   const base::Closure& closure,
                   const std::string& a,
                   const std::string& b) {
  *storage = a + b;
  closure.Run();
}

void WriteToMessagePipe(const char* text,
                        const base::Closure& closure,
                        ScopedMessagePipeHandle handle) {
  WriteTextMessage(handle.get(), text);
  closure.Run();
}

void RecordEnum(sample::Enum* storage,
                const base::Closure& closure,
                sample::Enum value) {
  *storage = value;
  closure.Run();
}

class RequestResponseTest : public testing::Test {
 public:
  RequestResponseTest() {}
  ~RequestResponseTest() override { base::RunLoop().RunUntilIdle(); }

  void PumpMessages() { base::RunLoop().RunUntilIdle(); }

 private:
  base::MessageLoop loop_;
};

TEST_F(RequestResponseTest, EchoString) {
  sample::ProviderPtr provider;
  ProviderImpl provider_impl(MakeRequest(&provider));

  std::string buf;
  base::RunLoop run_loop;
  provider->EchoString("hello",
                       base::Bind(&RecordString, &buf, run_loop.QuitClosure()));

  run_loop.Run();

  EXPECT_EQ(std::string("hello"), buf);
}

TEST_F(RequestResponseTest, EchoStrings) {
  sample::ProviderPtr provider;
  ProviderImpl provider_impl(MakeRequest(&provider));

  std::string buf;
  base::RunLoop run_loop;
  provider->EchoStrings("hello", " world", base::Bind(&RecordStrings, &buf,
                                                      run_loop.QuitClosure()));

  run_loop.Run();

  EXPECT_EQ(std::string("hello world"), buf);
}

TEST_F(RequestResponseTest, EchoMessagePipeHandle) {
  sample::ProviderPtr provider;
  ProviderImpl provider_impl(MakeRequest(&provider));

  MessagePipe pipe2;
  base::RunLoop run_loop;
  provider->EchoMessagePipeHandle(
      std::move(pipe2.handle1),
      base::Bind(&WriteToMessagePipe, "hello", run_loop.QuitClosure()));

  run_loop.Run();

  std::string value;
  ReadTextMessage(pipe2.handle0.get(), &value);

  EXPECT_EQ(std::string("hello"), value);
}

TEST_F(RequestResponseTest, EchoEnum) {
  sample::ProviderPtr provider;
  ProviderImpl provider_impl(MakeRequest(&provider));

  sample::Enum value;
  base::RunLoop run_loop;
  provider->EchoEnum(sample::Enum::VALUE,
                     base::Bind(&RecordEnum, &value, run_loop.QuitClosure()));
  run_loop.Run();

  EXPECT_EQ(sample::Enum::VALUE, value);
}

}  // namespace
}  // namespace test
}  // namespace mojo