callback_helpers_unittest.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "butil/callback_helpers.h"
  5. #include "butil/bind.h"
  6. #include "butil/callback.h"
  7. #include <gtest/gtest.h>
  8. namespace {
  9. void Increment(int* value) {
  10. (*value)++;
  11. }
  12. TEST(BindHelpersTest, TestScopedClosureRunnerExitScope) {
  13. int run_count = 0;
  14. {
  15. butil::ScopedClosureRunner runner(butil::Bind(&Increment, &run_count));
  16. EXPECT_EQ(0, run_count);
  17. }
  18. EXPECT_EQ(1, run_count);
  19. }
  20. TEST(BindHelpersTest, TestScopedClosureRunnerRelease) {
  21. int run_count = 0;
  22. butil::Closure c;
  23. {
  24. butil::ScopedClosureRunner runner(butil::Bind(&Increment, &run_count));
  25. c = runner.Release();
  26. EXPECT_EQ(0, run_count);
  27. }
  28. EXPECT_EQ(0, run_count);
  29. c.Run();
  30. EXPECT_EQ(1, run_count);
  31. }
  32. TEST(BindHelpersTest, TestScopedClosureRunnerReset) {
  33. int run_count_1 = 0;
  34. int run_count_2 = 0;
  35. {
  36. butil::ScopedClosureRunner runner;
  37. runner.Reset(butil::Bind(&Increment, &run_count_1));
  38. runner.Reset(butil::Bind(&Increment, &run_count_2));
  39. EXPECT_EQ(1, run_count_1);
  40. EXPECT_EQ(0, run_count_2);
  41. }
  42. EXPECT_EQ(1, run_count_2);
  43. int run_count_3 = 0;
  44. {
  45. butil::ScopedClosureRunner runner(butil::Bind(&Increment, &run_count_3));
  46. EXPECT_EQ(0, run_count_3);
  47. runner.Reset();
  48. EXPECT_EQ(1, run_count_3);
  49. }
  50. EXPECT_EQ(1, run_count_3);
  51. }
  52. } // namespace