1 // Copyright 2013 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 "base/callback_helpers.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
Increment(int * value)13 void Increment(int* value) {
14 (*value)++;
15 }
16
TEST(CallbackHelpersTest,TestResetAndReturn)17 TEST(CallbackHelpersTest, TestResetAndReturn) {
18 int run_count = 0;
19
20 base::Closure cb = base::Bind(&Increment, &run_count);
21 EXPECT_EQ(0, run_count);
22 base::ResetAndReturn(&cb).Run();
23 EXPECT_EQ(1, run_count);
24 EXPECT_FALSE(cb);
25
26 run_count = 0;
27
28 base::OnceClosure cb2 = base::BindOnce(&Increment, &run_count);
29 EXPECT_EQ(0, run_count);
30 base::ResetAndReturn(&cb2).Run();
31 EXPECT_EQ(1, run_count);
32 EXPECT_FALSE(cb2);
33 }
34
TEST(CallbackHelpersTest,TestScopedClosureRunnerExitScope)35 TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) {
36 int run_count = 0;
37 {
38 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count));
39 EXPECT_EQ(0, run_count);
40 }
41 EXPECT_EQ(1, run_count);
42 }
43
TEST(CallbackHelpersTest,TestScopedClosureRunnerRelease)44 TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) {
45 int run_count = 0;
46 base::OnceClosure c;
47 {
48 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count));
49 c = runner.Release();
50 EXPECT_EQ(0, run_count);
51 }
52 EXPECT_EQ(0, run_count);
53 std::move(c).Run();
54 EXPECT_EQ(1, run_count);
55 }
56
TEST(CallbackHelpersTest,TestScopedClosureRunnerReplaceClosure)57 TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) {
58 int run_count_1 = 0;
59 int run_count_2 = 0;
60 {
61 base::ScopedClosureRunner runner;
62 runner.ReplaceClosure(base::Bind(&Increment, &run_count_1));
63 runner.ReplaceClosure(base::Bind(&Increment, &run_count_2));
64 EXPECT_EQ(0, run_count_1);
65 EXPECT_EQ(0, run_count_2);
66 }
67 EXPECT_EQ(0, run_count_1);
68 EXPECT_EQ(1, run_count_2);
69 }
70
TEST(CallbackHelpersTest,TestScopedClosureRunnerRunAndReset)71 TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) {
72 int run_count_3 = 0;
73 {
74 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3));
75 EXPECT_EQ(0, run_count_3);
76 runner.RunAndReset();
77 EXPECT_EQ(1, run_count_3);
78 }
79 EXPECT_EQ(1, run_count_3);
80 }
81
TEST(CallbackHelpersTest,TestScopedClosureRunnerMoveConstructor)82 TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveConstructor) {
83 int run_count = 0;
84 {
85 std::unique_ptr<base::ScopedClosureRunner> runner(
86 new base::ScopedClosureRunner(base::Bind(&Increment, &run_count)));
87 base::ScopedClosureRunner runner2(std::move(*runner));
88 runner.reset();
89 EXPECT_EQ(0, run_count);
90 }
91 EXPECT_EQ(1, run_count);
92 }
93
TEST(CallbackHelpersTest,TestScopedClosureRunnerMoveAssignment)94 TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveAssignment) {
95 int run_count_1 = 0;
96 int run_count_2 = 0;
97 {
98 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_1));
99 {
100 base::ScopedClosureRunner runner2(base::Bind(&Increment, &run_count_2));
101 runner = std::move(runner2);
102 EXPECT_EQ(0, run_count_1);
103 EXPECT_EQ(0, run_count_2);
104 }
105 EXPECT_EQ(0, run_count_1);
106 EXPECT_EQ(0, run_count_2);
107 }
108 EXPECT_EQ(0, run_count_1);
109 EXPECT_EQ(1, run_count_2);
110 }
111
TEST(CallbackHelpersTest,TestAdaptCallbackForRepeating)112 TEST(CallbackHelpersTest, TestAdaptCallbackForRepeating) {
113 int count = 0;
114 base::OnceCallback<void(int*)> cb =
115 base::BindOnce([](int* count) { ++*count; });
116
117 base::RepeatingCallback<void(int*)> wrapped =
118 base::AdaptCallbackForRepeating(std::move(cb));
119
120 EXPECT_EQ(0, count);
121 wrapped.Run(&count);
122 EXPECT_EQ(1, count);
123 wrapped.Run(&count);
124 EXPECT_EQ(1, count);
125 }
126
127 } // namespace
128