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 17 TEST(CallbackHelpersTest, TestScopedClosureRunnerExitScope) { 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 26 TEST(CallbackHelpersTest, TestScopedClosureRunnerRelease) { 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 39 TEST(CallbackHelpersTest, TestScopedClosureRunnerReplaceClosure) { 40 int run_count_1 = 0; 41 int run_count_2 = 0; 42 { 43 base::ScopedClosureRunner runner; 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); 47 EXPECT_EQ(0, run_count_2); 48 } 49 EXPECT_EQ(0, run_count_1); 50 EXPECT_EQ(1, run_count_2); 51 } 52 53 TEST(CallbackHelpersTest, TestScopedClosureRunnerRunAndReset) { 54 int run_count_3 = 0; 55 { 56 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count_3)); 57 EXPECT_EQ(0, run_count_3); 58 runner.RunAndReset(); 59 EXPECT_EQ(1, run_count_3); 60 } 61 EXPECT_EQ(1, run_count_3); 62 } 63 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 94 } // namespace 95