Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [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 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 17 | TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 18 | int run_count = 0; |
| 19 | { |
| 20 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count)); |
| 21 | EXPECT_EQ(0, run_count); |
| 22 | } |
| 23 | EXPECT_EQ(1, run_count); |
| 24 | } |
| 25 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 26 | TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 27 | int run_count = 0; |
| 28 | base::Closure c; |
| 29 | { |
| 30 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count)); |
| 31 | c = runner.Release(); |
| 32 | EXPECT_EQ(0, run_count); |
| 33 | } |
| 34 | EXPECT_EQ(0, run_count); |
| 35 | c.Run(); |
| 36 | EXPECT_EQ(1, run_count); |
| 37 | } |
| 38 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 39 | TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 40 | int run_count_1 = 0; |
| 41 | int run_count_2 = 0; |
| 42 | { |
| 43 | base::ScopedClosureRunner runner; |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 44 | runner.ReplaceClosure(base::Bind(&Increment, &run_count_1)); |
| 45 | runner.ReplaceClosure(base::Bind(&Increment, &run_count_2)); |
| 46 | EXPECT_EQ(0, run_count_1); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 47 | EXPECT_EQ(0, run_count_2); |
| 48 | } |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 49 | EXPECT_EQ(0, run_count_1); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 50 | EXPECT_EQ(1, run_count_2); |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 51 | } |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 52 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 53 | TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) { |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 54 | int run_count_3 = 0; |
| 55 | { |
| 56 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3)); |
| 57 | EXPECT_EQ(0, run_count_3); |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 58 | runner.RunAndReset(); |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 59 | EXPECT_EQ(1, run_count_3); |
| 60 | } |
| 61 | EXPECT_EQ(1, run_count_3); |
| 62 | } |
| 63 | |
Luis Hector Chavez | 0c4f26a | 2016-07-15 16:23:21 -0700 | [diff] [blame] | 64 | TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveConstructor) { |
| 65 | int run_count = 0; |
| 66 | { |
| 67 | std::unique_ptr<base::ScopedClosureRunner> runner( |
| 68 | new base::ScopedClosureRunner(base::Bind(&Increment, &run_count))); |
| 69 | base::ScopedClosureRunner runner2(std::move(*runner)); |
| 70 | runner.reset(); |
| 71 | EXPECT_EQ(0, run_count); |
| 72 | } |
| 73 | EXPECT_EQ(1, run_count); |
| 74 | } |
| 75 | |
| 76 | TEST(CallbackHelpersTest, TestScopedClosureRunnerMoveAssignment) { |
| 77 | int run_count_1 = 0; |
| 78 | int run_count_2 = 0; |
| 79 | { |
| 80 | base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_1)); |
| 81 | { |
| 82 | base::ScopedClosureRunner runner2(base::Bind(&Increment, &run_count_2)); |
| 83 | runner = std::move(runner2); |
| 84 | EXPECT_EQ(0, run_count_1); |
| 85 | EXPECT_EQ(0, run_count_2); |
| 86 | } |
| 87 | EXPECT_EQ(0, run_count_1); |
| 88 | EXPECT_EQ(0, run_count_2); |
| 89 | } |
| 90 | EXPECT_EQ(0, run_count_1); |
| 91 | EXPECT_EQ(1, run_count_2); |
| 92 | } |
| 93 | |
Daniel Erat | b8cf949 | 2015-07-06 13:18:13 -0600 | [diff] [blame] | 94 | } // namespace |