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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 } | 55 } |
56 | 56 |
57 void FunctionStringArgByConstRef(const String& s) { | 57 void FunctionStringArgByConstRef(const String& s) { |
58 (*g_calls)++; | 58 (*g_calls)++; |
59 } | 59 } |
60 | 60 |
61 void FunctionMoveOnlyType(ExampleMoveOnlyType m) { | 61 void FunctionMoveOnlyType(ExampleMoveOnlyType m) { |
62 (*g_calls)++; | 62 (*g_calls)++; |
63 } | 63 } |
64 | 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 | 65 // Tests constructing and invoking a mojo::Callback from objects with a |
97 // compatible Run() method (called 'runnables'), from lambdas, and from function | 66 // compatible Run() method (called 'runnables'), from lambdas, and from function |
98 // pointers. | 67 // pointers. |
99 TEST(Callback, Create) { | 68 TEST(Callback, Create) { |
100 int calls = 0; | 69 int calls = 0; |
101 | 70 |
102 RunnableNoArgs f(&calls); | 71 RunnableNoArgs f(&calls); |
103 // Construct from a runnable object. | 72 // Construct from a runnable object. |
104 mojo::Callback<void()> cb = f; | 73 mojo::Callback<void()> cb = f; |
105 cb.Run(); | 74 cb.Run(); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 g_overloaded_function_with_double_param_called = false; | 156 g_overloaded_function_with_double_param_called = false; |
188 mojo::Callback<void(double)> cb_with_double_param = &OverloadedFunction; | 157 mojo::Callback<void(double)> cb_with_double_param = &OverloadedFunction; |
189 cb_with_double_param.Run(123); | 158 cb_with_double_param.Run(123); |
190 EXPECT_TRUE(g_overloaded_function_with_double_param_called); | 159 EXPECT_TRUE(g_overloaded_function_with_double_param_called); |
191 g_overloaded_function_with_double_param_called = false; | 160 g_overloaded_function_with_double_param_called = false; |
192 } | 161 } |
193 | 162 |
194 } // namespace | 163 } // namespace |
195 } // namespace test | 164 } // namespace test |
196 } // namespace mojo | 165 } // namespace mojo |
OLD | NEW |