avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 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 | |
| 13 | void Increment(int* value) { |
| 14 | (*value)++; |
| 15 | } |
| 16 | |
tzik | cd0e2ce | 2017-02-22 18:13:19 +0900 | [diff] [blame] | 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 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 35 | TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) { |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 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 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 44 | TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) { |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 45 | int run_count = 0; |
tzik | 05a5294 | 2017-08-08 14:19:40 +0900 | [diff] [blame^] | 46 | base::OnceClosure c; |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 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); |
tzik | 05a5294 | 2017-08-08 14:19:40 +0900 | [diff] [blame^] | 53 | std::move(c).Run(); |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 54 | EXPECT_EQ(1, run_count); |
| 55 | } |
| 56 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 57 | TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) { |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 58 | int run_count_1 = 0; |
| 59 | int run_count_2 = 0; |
| 60 | { |
| 61 | base::ScopedClosureRunner runner; |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 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); |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 65 | EXPECT_EQ(0, run_count_2); |
| 66 | } |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 67 | EXPECT_EQ(0, run_count_1); |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 68 | EXPECT_EQ(1, run_count_2); |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 69 | } |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 70 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 71 | TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) { |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 72 | int run_count_3 = 0; |
| 73 | { |
| 74 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3)); |
| 75 | EXPECT_EQ(0, run_count_3); |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 76 | runner.RunAndReset(); |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 77 | EXPECT_EQ(1, run_count_3); |
| 78 | } |
| 79 | EXPECT_EQ(1, run_count_3); |
| 80 | } |
| 81 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 82 | TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveConstructor) { |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 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 | |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 94 | TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveAssignment) { |
| 95 | int run_count_1 = 0; |
| 96 | int run_count_2 = 0; |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 97 | { |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 98 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_1)); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 99 | { |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 100 | base::ScopedClosureRunner runner2(base::Bind(&Increment, &run_count_2)); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 101 | runner = std::move(runner2); |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 102 | EXPECT_EQ(0, run_count_1); |
| 103 | EXPECT_EQ(0, run_count_2); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 104 | } |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 105 | EXPECT_EQ(0, run_count_1); |
| 106 | EXPECT_EQ(0, run_count_2); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 107 | } |
sergeyu | 93396d1 | 2016-07-08 09:34:27 +0900 | [diff] [blame] | 108 | EXPECT_EQ(0, run_count_1); |
| 109 | EXPECT_EQ(1, run_count_2); |
sergeyu | f9cf281 | 2016-06-25 09:51:09 +0900 | [diff] [blame] | 110 | } |
| 111 | |
tzik | 2c9ac66 | 2017-04-20 21:19:50 +0900 | [diff] [blame] | 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 | |
avi@chromium.org | b74bab8 | 2013-08-30 11:04:04 +0900 | [diff] [blame] | 127 | } // namespace |