blob: 82839963795fcf39e657a3d9b1cd35df5b2ae215 [file] [log] [blame]
Daniel Eratb8cf9492015-07-06 13:18:13 -06001// 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
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070017TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) {
Daniel Eratb8cf9492015-07-06 13:18:13 -060018 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 Chavez0c4f26a2016-07-15 16:23:21 -070026TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) {
Daniel Eratb8cf9492015-07-06 13:18:13 -060027 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 Chavez0c4f26a2016-07-15 16:23:21 -070039TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) {
Daniel Eratb8cf9492015-07-06 13:18:13 -060040 int run_count_1 = 0;
41 int run_count_2 = 0;
42 {
43 base::ScopedClosureRunner runner;
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070044 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 Eratb8cf9492015-07-06 13:18:13 -060047 EXPECT_EQ(0, run_count_2);
48 }
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070049 EXPECT_EQ(0, run_count_1);
Daniel Eratb8cf9492015-07-06 13:18:13 -060050 EXPECT_EQ(1, run_count_2);
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070051}
Daniel Eratb8cf9492015-07-06 13:18:13 -060052
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070053TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) {
Daniel Eratb8cf9492015-07-06 13:18:13 -060054 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 Chavez0c4f26a2016-07-15 16:23:21 -070058 runner.RunAndReset();
Daniel Eratb8cf9492015-07-06 13:18:13 -060059 EXPECT_EQ(1, run_count_3);
60 }
61 EXPECT_EQ(1, run_count_3);
62}
63
Luis Hector Chavez0c4f26a2016-07-15 16:23:21 -070064TEST(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
76TEST(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 Eratb8cf9492015-07-06 13:18:13 -060094} // namespace