OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/public/cpp/bindings/callback.h" | 5 #include "mojo/public/cpp/bindings/callback.h" |
6 #include "mojo/public/cpp/bindings/map.h" | 6 #include "mojo/public/cpp/bindings/map.h" |
7 #include "mojo/public/cpp/bindings/string.h" | 7 #include "mojo/public/cpp/bindings/string.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace mojo { | 10 namespace mojo { |
(...skipping 26 matching lines...) Expand all Loading... |
37 using ExampleMoveOnlyType = Map<int, int>; | 37 using ExampleMoveOnlyType = Map<int, int>; |
38 | 38 |
39 struct RunnableMoveOnlyParam { | 39 struct RunnableMoveOnlyParam { |
40 explicit RunnableMoveOnlyParam(int* calls) : calls(calls) {} | 40 explicit RunnableMoveOnlyParam(int* calls) : calls(calls) {} |
41 | 41 |
42 void Run(ExampleMoveOnlyType m) const { (*calls)++; } | 42 void Run(ExampleMoveOnlyType m) const { (*calls)++; } |
43 | 43 |
44 int* calls; | 44 int* calls; |
45 }; | 45 }; |
46 | 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, | 47 static_assert(!internal::HasCompatibleCallOperator<RunnableNoArgs>::value, |
66 "HasCompatibleCallOperator<Runnable>"); | 48 "HasCompatibleCallOperator<Runnable>"); |
67 static_assert(!internal::HasCompatibleCallOperator<RunnableOneArg, int>::value, | 49 static_assert(!internal::HasCompatibleCallOperator<RunnableOneArg, int>::value, |
68 "HasCompatibleCallOperator<RunnableOneArg, int>"); | 50 "HasCompatibleCallOperator<RunnableOneArg, int>"); |
69 static_assert(!internal::HasCompatibleCallOperator<RunnableStringArgByConstRef, | 51 static_assert(!internal::HasCompatibleCallOperator<RunnableStringArgByConstRef, |
70 String>::value, | 52 String>::value, |
71 "HasCompatibleCallOperator<RunnableStringArgByConstRef, String>"); | 53 "HasCompatibleCallOperator<RunnableStringArgByConstRef, String>"); |
72 static_assert(!internal::HasCompatibleCallOperator<RunnableMoveOnlyParam, | 54 static_assert(!internal::HasCompatibleCallOperator<RunnableMoveOnlyParam, |
73 ExampleMoveOnlyType>::value, | 55 ExampleMoveOnlyType>::value, |
74 "HasCompatibleCallOperator<RunnableMoveOnlyParam, String>"); | 56 "HasCompatibleCallOperator<RunnableMoveOnlyParam, String>"); |
(...skipping 12 matching lines...) Expand all Loading... |
87 internal::HasCompatibleCallOperator<decltype(lambda_three), String>::value, | 69 internal::HasCompatibleCallOperator<decltype(lambda_three), String>::value, |
88 "HasCompatibleCallOperator<lambda [](const String& s) {}, String>"); | 70 "HasCompatibleCallOperator<lambda [](const String& s) {}, String>"); |
89 | 71 |
90 auto lambda_four = [](ExampleMoveOnlyType m) {}; | 72 auto lambda_four = [](ExampleMoveOnlyType m) {}; |
91 static_assert(internal::HasCompatibleCallOperator<decltype(lambda_four), | 73 static_assert(internal::HasCompatibleCallOperator<decltype(lambda_four), |
92 ExampleMoveOnlyType>::value, | 74 ExampleMoveOnlyType>::value, |
93 "HasCompatibleCallOperator<lambda [](ExampleMoveOnlyType) {}, " | 75 "HasCompatibleCallOperator<lambda [](ExampleMoveOnlyType) {}, " |
94 "ExampleMoveOnlyType>"); | 76 "ExampleMoveOnlyType>"); |
95 | 77 |
96 // Tests constructing and invoking a mojo::Callback from objects with a | 78 // Tests constructing and invoking a mojo::Callback from objects with a |
97 // compatible Run() method (called 'runnables'), from lambdas, and from function | 79 // compatible Run() method (called 'runnables') and from lambdas. |
98 // pointers. | 80 TEST(CallbackFromLambda, Create) { |
99 TEST(Callback, Create) { | |
100 int calls = 0; | 81 int calls = 0; |
101 | 82 |
102 RunnableNoArgs f(&calls); | 83 RunnableNoArgs f(&calls); |
103 // Construct from a runnable object. | 84 // Construct from a runnable object. |
104 mojo::Callback<void()> cb = f; | 85 mojo::Callback<void()> cb = f; |
105 cb.Run(); | 86 cb.Run(); |
106 EXPECT_EQ(1, calls); | 87 EXPECT_EQ(1, calls); |
107 | 88 |
108 // Construct from a parameterless lambda that captures one variable. | 89 // Construct from a parameterless lambda that captures one variable. |
109 cb = [&calls]() { calls++; }; | 90 cb = [&calls]() { calls++; }; |
(...skipping 23 matching lines...) Expand all Loading... |
133 | 114 |
134 ExampleMoveOnlyType m; | 115 ExampleMoveOnlyType m; |
135 mojo::Callback<void(ExampleMoveOnlyType)> cb_with_move_only_param = | 116 mojo::Callback<void(ExampleMoveOnlyType)> cb_with_move_only_param = |
136 RunnableMoveOnlyParam(&calls); | 117 RunnableMoveOnlyParam(&calls); |
137 cb_with_move_only_param.Run(m.Clone()); | 118 cb_with_move_only_param.Run(m.Clone()); |
138 EXPECT_EQ(7, calls); | 119 EXPECT_EQ(7, calls); |
139 | 120 |
140 cb_with_move_only_param = [&calls](ExampleMoveOnlyType m) { calls++; }; | 121 cb_with_move_only_param = [&calls](ExampleMoveOnlyType m) { calls++; }; |
141 cb_with_move_only_param.Run(m.Clone()); | 122 cb_with_move_only_param.Run(m.Clone()); |
142 EXPECT_EQ(8, calls); | 123 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 } | 124 } |
193 | 125 |
194 } // namespace | 126 } // namespace |
195 } // namespace test | 127 } // namespace test |
196 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |