blob: 1c1102db9121e6e8f3f59b467a7886fe883c3b6f [file] [log] [blame]
avi@chromium.orgb74bab82013-08-30 11:04:04 +09001// 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
11namespace {
12
13void Increment(int* value) {
14 (*value)++;
15}
16
tzikcd0e2ce2017-02-22 18:13:19 +090017TEST(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
sergeyu93396d12016-07-08 09:34:27 +090035TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) {
avi@chromium.orgb74bab82013-08-30 11:04:04 +090036 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
sergeyu93396d12016-07-08 09:34:27 +090044TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) {
avi@chromium.orgb74bab82013-08-30 11:04:04 +090045 int run_count = 0;
tzik05a52942017-08-08 14:19:40 +090046 base::OnceClosure c;
avi@chromium.orgb74bab82013-08-30 11:04:04 +090047 {
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);
tzik05a52942017-08-08 14:19:40 +090053 std::move(c).Run();
avi@chromium.orgb74bab82013-08-30 11:04:04 +090054 EXPECT_EQ(1, run_count);
55}
56
sergeyu93396d12016-07-08 09:34:27 +090057TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) {
avi@chromium.orgb74bab82013-08-30 11:04:04 +090058 int run_count_1 = 0;
59 int run_count_2 = 0;
60 {
61 base::ScopedClosureRunner runner;
sergeyu93396d12016-07-08 09:34:27 +090062 runner.ReplaceClosure(base::Bind(&Increment, &run_count_1));
63 runner.ReplaceClosure(base::Bind(&Increment, &run_count_2));
64 EXPECT_EQ(0, run_count_1);
avi@chromium.orgb74bab82013-08-30 11:04:04 +090065 EXPECT_EQ(0, run_count_2);
66 }
sergeyu93396d12016-07-08 09:34:27 +090067 EXPECT_EQ(0, run_count_1);
avi@chromium.orgb74bab82013-08-30 11:04:04 +090068 EXPECT_EQ(1, run_count_2);
sergeyu93396d12016-07-08 09:34:27 +090069}
avi@chromium.orgb74bab82013-08-30 11:04:04 +090070
sergeyu93396d12016-07-08 09:34:27 +090071TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) {
avi@chromium.orgb74bab82013-08-30 11:04:04 +090072 int run_count_3 = 0;
73 {
74 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3));
75 EXPECT_EQ(0, run_count_3);
sergeyu93396d12016-07-08 09:34:27 +090076 runner.RunAndReset();
avi@chromium.orgb74bab82013-08-30 11:04:04 +090077 EXPECT_EQ(1, run_count_3);
78 }
79 EXPECT_EQ(1, run_count_3);
80}
81
sergeyu93396d12016-07-08 09:34:27 +090082TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveConstructor) {
sergeyuf9cf2812016-06-25 09:51:09 +090083 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
sergeyu93396d12016-07-08 09:34:27 +090094TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveAssignment) {
95 int run_count_1 = 0;
96 int run_count_2 = 0;
sergeyuf9cf2812016-06-25 09:51:09 +090097 {
sergeyu93396d12016-07-08 09:34:27 +090098 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_1));
sergeyuf9cf2812016-06-25 09:51:09 +090099 {
sergeyu93396d12016-07-08 09:34:27 +0900100 base::ScopedClosureRunner runner2(base::Bind(&Increment, &run_count_2));
sergeyuf9cf2812016-06-25 09:51:09 +0900101 runner = std::move(runner2);
sergeyu93396d12016-07-08 09:34:27 +0900102 EXPECT_EQ(0, run_count_1);
103 EXPECT_EQ(0, run_count_2);
sergeyuf9cf2812016-06-25 09:51:09 +0900104 }
sergeyu93396d12016-07-08 09:34:27 +0900105 EXPECT_EQ(0, run_count_1);
106 EXPECT_EQ(0, run_count_2);
sergeyuf9cf2812016-06-25 09:51:09 +0900107 }
sergeyu93396d12016-07-08 09:34:27 +0900108 EXPECT_EQ(0, run_count_1);
109 EXPECT_EQ(1, run_count_2);
sergeyuf9cf2812016-06-25 09:51:09 +0900110}
111
tzik2c9ac662017-04-20 21:19:50 +0900112TEST(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
avi@chromium.orgb74bab82013-08-30 11:04:04 +0900127} // namespace