OLD | NEW |
| (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 "gtest/gtest.h" | |
6 #include "mojo/public/cpp/bindings/callback.h" | |
7 #include "mojo/public/cpp/bindings/map.h" | |
8 #include "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 // Tests constructing and invoking a mojo::Callback from objects with a | |
66 // compatible Run() method (called 'runnables'), from lambdas, and from function | |
67 // pointers. | |
68 TEST(Callback, Create) { | |
69 int calls = 0; | |
70 | |
71 RunnableNoArgs f(&calls); | |
72 // Construct from a runnable object. | |
73 mojo::Callback<void()> cb = f; | |
74 cb.Run(); | |
75 EXPECT_EQ(1, calls); | |
76 | |
77 // Construct from a parameterless lambda that captures one variable. | |
78 cb = [&calls]() { calls++; }; | |
79 cb.Run(); | |
80 EXPECT_EQ(2, calls); | |
81 | |
82 // Construct from a runnable object with one primitive parameter. | |
83 mojo::Callback<void(int)> cb_with_param = RunnableOneArg(&calls); | |
84 cb_with_param.Run(1); | |
85 EXPECT_EQ(3, calls); | |
86 | |
87 // Construct from a lambda that takes one parameter and captures one variable. | |
88 cb_with_param = [&calls](int increment) { calls += increment; }; | |
89 cb_with_param.Run(1); | |
90 EXPECT_EQ(4, calls); | |
91 | |
92 // Construct from a runnable object with one string parameter. | |
93 mojo::Callback<void(String)> cb_with_string_param = | |
94 RunnableStringArgByConstRef(&calls); | |
95 cb_with_string_param.Run(String("hello world")); | |
96 EXPECT_EQ(5, calls); | |
97 | |
98 // Construct from a lambda that takes one string parameter. | |
99 cb_with_string_param = [&calls](const String& s) { calls++; }; | |
100 cb_with_string_param.Run(String("world")); | |
101 EXPECT_EQ(6, calls); | |
102 | |
103 ExampleMoveOnlyType m; | |
104 mojo::Callback<void(ExampleMoveOnlyType)> cb_with_move_only_param = | |
105 RunnableMoveOnlyParam(&calls); | |
106 cb_with_move_only_param.Run(m.Clone()); | |
107 EXPECT_EQ(7, calls); | |
108 | |
109 cb_with_move_only_param = [&calls](ExampleMoveOnlyType m) { calls++; }; | |
110 cb_with_move_only_param.Run(m.Clone()); | |
111 EXPECT_EQ(8, calls); | |
112 | |
113 // Construct from a function pointer. | |
114 g_calls = &calls; | |
115 | |
116 cb = &FunctionNoArgs; | |
117 cb.Run(); | |
118 EXPECT_EQ(9, calls); | |
119 | |
120 cb_with_param = &FunctionOneArg; | |
121 cb_with_param.Run(1); | |
122 EXPECT_EQ(10, calls); | |
123 | |
124 cb_with_string_param = &FunctionStringArgByConstRef; | |
125 cb_with_string_param.Run(String("hello")); | |
126 EXPECT_EQ(11, calls); | |
127 | |
128 cb_with_move_only_param = &FunctionMoveOnlyType; | |
129 cb_with_move_only_param.Run(m.Clone()); | |
130 EXPECT_EQ(12, calls); | |
131 | |
132 g_calls = nullptr; | |
133 } | |
134 | |
135 bool g_overloaded_function_with_int_param_called = false; | |
136 | |
137 void OverloadedFunction(int param) { | |
138 g_overloaded_function_with_int_param_called = true; | |
139 } | |
140 | |
141 bool g_overloaded_function_with_double_param_called = false; | |
142 | |
143 void OverloadedFunction(double param) { | |
144 g_overloaded_function_with_double_param_called = true; | |
145 } | |
146 | |
147 // Tests constructing and invoking a mojo::Callback from pointers to overloaded | |
148 // functions. | |
149 TEST(Callback, CreateFromOverloadedFunctionPtr) { | |
150 g_overloaded_function_with_int_param_called = false; | |
151 mojo::Callback<void(int)> cb_with_int_param = &OverloadedFunction; | |
152 cb_with_int_param.Run(123); | |
153 EXPECT_TRUE(g_overloaded_function_with_int_param_called); | |
154 g_overloaded_function_with_int_param_called = false; | |
155 | |
156 g_overloaded_function_with_double_param_called = false; | |
157 mojo::Callback<void(double)> cb_with_double_param = &OverloadedFunction; | |
158 cb_with_double_param.Run(123); | |
159 EXPECT_TRUE(g_overloaded_function_with_double_param_called); | |
160 g_overloaded_function_with_double_param_called = false; | |
161 } | |
162 | |
163 } // namespace | |
164 } // namespace test | |
165 } // namespace mojo | |
OLD | NEW |