Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: third_party/mojo/src/mojo/public/cpp/bindings/tests/callback_unittest.cc

Issue 1410053006: Move third_party/mojo/src/mojo/public to mojo/public (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "testing/gtest/include/gtest/gtest.h"
6 #include "third_party/mojo/src/mojo/public/cpp/bindings/callback.h"
7 #include "third_party/mojo/src/mojo/public/cpp/bindings/map.h"
8 #include "third_party/mojo/src/mojo/public/cpp/bindings/string.h"
9
10 namespace mojo {
11 namespace test {
12 namespace {
13
14 struct RunnableNoArgs {
15 explicit RunnableNoArgs(int* calls) : calls(calls) {}
16
17 void Run() const { (*calls)++; }
18 int* calls;
19 };
20
21 struct RunnableOneArg {
22 explicit RunnableOneArg(int* calls) : calls(calls) {}
23
24 void Run(int increment) const { (*calls) += increment; }
25
26 int* calls;
27 };
28
29 struct RunnableStringArgByConstRef {
30 explicit RunnableStringArgByConstRef(int* calls) : calls(calls) {}
31
32 void Run(const String& s) const { (*calls)++; }
33
34 int* calls;
35 };
36
37 using ExampleMoveOnlyType = Map<int, int>;
38
39 struct RunnableMoveOnlyParam {
40 explicit RunnableMoveOnlyParam(int* calls) : calls(calls) {}
41
42 void Run(ExampleMoveOnlyType m) const { (*calls)++; }
43
44 int* calls;
45 };
46
47 int* g_calls = nullptr;
48
49 void FunctionNoArgs() {
50 (*g_calls)++;
51 }
52
53 void FunctionOneArg(int increment) {
54 (*g_calls) += increment;
55 }
56
57 void FunctionStringArgByConstRef(const String& s) {
58 (*g_calls)++;
59 }
60
61 void FunctionMoveOnlyType(ExampleMoveOnlyType m) {
62 (*g_calls)++;
63 }
64
65 static_assert(!internal::HasCompatibleCallOperator<RunnableNoArgs>::value,
66 "HasCompatibleCallOperator<Runnable>");
67 static_assert(!internal::HasCompatibleCallOperator<RunnableOneArg, int>::value,
68 "HasCompatibleCallOperator<RunnableOneArg, int>");
69 static_assert(!internal::HasCompatibleCallOperator<RunnableStringArgByConstRef,
70 String>::value,
71 "HasCompatibleCallOperator<RunnableStringArgByConstRef, String>");
72 static_assert(!internal::HasCompatibleCallOperator<RunnableMoveOnlyParam,
73 ExampleMoveOnlyType>::value,
74 "HasCompatibleCallOperator<RunnableMoveOnlyParam, String>");
75
76 auto lambda_one = []() {};
77 static_assert(internal::HasCompatibleCallOperator<decltype(lambda_one)>::value,
78 "HasCompatibleCallOperator<lambda []() {}>");
79
80 auto lambda_two = [](int x) {};
81 static_assert(
82 internal::HasCompatibleCallOperator<decltype(lambda_two), int>::value,
83 "HasCompatibleCallOperator<lambda [](int x) {}, int>");
84
85 auto lambda_three = [](const String& s) {};
86 static_assert(
87 internal::HasCompatibleCallOperator<decltype(lambda_three), String>::value,
88 "HasCompatibleCallOperator<lambda [](const String& s) {}, String>");
89
90 auto lambda_four = [](ExampleMoveOnlyType m) {};
91 static_assert(internal::HasCompatibleCallOperator<decltype(lambda_four),
92 ExampleMoveOnlyType>::value,
93 "HasCompatibleCallOperator<lambda [](ExampleMoveOnlyType) {}, "
94 "ExampleMoveOnlyType>");
95
96 // Tests constructing and invoking a mojo::Callback from objects with a
97 // compatible Run() method (called 'runnables'), from lambdas, and from function
98 // pointers.
99 TEST(Callback, Create) {
100 int calls = 0;
101
102 RunnableNoArgs f(&calls);
103 // Construct from a runnable object.
104 mojo::Callback<void()> cb = f;
105 cb.Run();
106 EXPECT_EQ(1, calls);
107
108 // Construct from a parameterless lambda that captures one variable.
109 cb = [&calls]() { calls++; };
110 cb.Run();
111 EXPECT_EQ(2, calls);
112
113 // Construct from a runnable object with one primitive parameter.
114 mojo::Callback<void(int)> cb_with_param = RunnableOneArg(&calls);
115 cb_with_param.Run(1);
116 EXPECT_EQ(3, calls);
117
118 // Construct from a lambda that takes one parameter and captures one variable.
119 cb_with_param = [&calls](int increment) { calls += increment; };
120 cb_with_param.Run(1);
121 EXPECT_EQ(4, calls);
122
123 // Construct from a runnable object with one string parameter.
124 mojo::Callback<void(String)> cb_with_string_param =
125 RunnableStringArgByConstRef(&calls);
126 cb_with_string_param.Run(String("hello world"));
127 EXPECT_EQ(5, calls);
128
129 // Construct from a lambda that takes one string parameter.
130 cb_with_string_param = [&calls](const String& s) { calls++; };
131 cb_with_string_param.Run(String("world"));
132 EXPECT_EQ(6, calls);
133
134 ExampleMoveOnlyType m;
135 mojo::Callback<void(ExampleMoveOnlyType)> cb_with_move_only_param =
136 RunnableMoveOnlyParam(&calls);
137 cb_with_move_only_param.Run(m.Clone());
138 EXPECT_EQ(7, calls);
139
140 cb_with_move_only_param = [&calls](ExampleMoveOnlyType m) { calls++; };
141 cb_with_move_only_param.Run(m.Clone());
142 EXPECT_EQ(8, calls);
143
144 // Construct from a function pointer.
145 g_calls = &calls;
146
147 cb = &FunctionNoArgs;
148 cb.Run();
149 EXPECT_EQ(9, calls);
150
151 cb_with_param = &FunctionOneArg;
152 cb_with_param.Run(1);
153 EXPECT_EQ(10, calls);
154
155 cb_with_string_param = &FunctionStringArgByConstRef;
156 cb_with_string_param.Run(String("hello"));
157 EXPECT_EQ(11, calls);
158
159 cb_with_move_only_param = &FunctionMoveOnlyType;
160 cb_with_move_only_param.Run(m.Clone());
161 EXPECT_EQ(12, calls);
162
163 g_calls = nullptr;
164 }
165
166 bool g_overloaded_function_with_int_param_called = false;
167
168 void OverloadedFunction(int param) {
169 g_overloaded_function_with_int_param_called = true;
170 }
171
172 bool g_overloaded_function_with_double_param_called = false;
173
174 void OverloadedFunction(double param) {
175 g_overloaded_function_with_double_param_called = true;
176 }
177
178 // Tests constructing and invoking a mojo::Callback from pointers to overloaded
179 // functions.
180 TEST(Callback, CreateFromOverloadedFunctionPtr) {
181 g_overloaded_function_with_int_param_called = false;
182 mojo::Callback<void(int)> cb_with_int_param = &OverloadedFunction;
183 cb_with_int_param.Run(123);
184 EXPECT_TRUE(g_overloaded_function_with_int_param_called);
185 g_overloaded_function_with_int_param_called = false;
186
187 g_overloaded_function_with_double_param_called = false;
188 mojo::Callback<void(double)> cb_with_double_param = &OverloadedFunction;
189 cb_with_double_param.Run(123);
190 EXPECT_TRUE(g_overloaded_function_with_double_param_called);
191 g_overloaded_function_with_double_param_called = false;
192 }
193
194 } // namespace
195 } // namespace test
196 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698