mock_callback_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2017 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 "base/test/mock_callback.h"
  5. #include "base/callback.h"
  6. #include "testing/gmock/include/gmock/gmock.h"
  7. using testing::InSequence;
  8. using testing::Return;
  9. namespace base {
  10. namespace {
  11. TEST(MockCallbackTest, ZeroArgs) {
  12. MockCallback<RepeatingClosure> mock_closure;
  13. EXPECT_CALL(mock_closure, Run());
  14. mock_closure.Get().Run();
  15. MockCallback<RepeatingCallback<int()>> mock_int_callback;
  16. {
  17. InSequence sequence;
  18. EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(42));
  19. EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(88));
  20. }
  21. EXPECT_EQ(42, mock_int_callback.Get().Run());
  22. EXPECT_EQ(88, mock_int_callback.Get().Run());
  23. }
  24. TEST(MockCallbackTest, WithArgs) {
  25. MockCallback<RepeatingCallback<int(int, int)>> mock_two_int_callback;
  26. EXPECT_CALL(mock_two_int_callback, Run(1, 2)).WillOnce(Return(42));
  27. EXPECT_CALL(mock_two_int_callback, Run(0, 0)).WillRepeatedly(Return(-1));
  28. RepeatingCallback<int(int, int)> two_int_callback =
  29. mock_two_int_callback.Get();
  30. EXPECT_EQ(-1, two_int_callback.Run(0, 0));
  31. EXPECT_EQ(42, two_int_callback.Run(1, 2));
  32. EXPECT_EQ(-1, two_int_callback.Run(0, 0));
  33. }
  34. TEST(MockCallbackTest, ZeroArgsOnce) {
  35. MockCallback<OnceClosure> mock_closure;
  36. EXPECT_CALL(mock_closure, Run());
  37. mock_closure.Get().Run();
  38. MockCallback<OnceCallback<int()>> mock_int_callback;
  39. EXPECT_CALL(mock_int_callback, Run()).WillOnce(Return(88));
  40. EXPECT_EQ(88, mock_int_callback.Get().Run());
  41. }
  42. TEST(MockCallbackTest, WithArgsOnce) {
  43. MockCallback<OnceCallback<int(int, int)>> mock_two_int_callback;
  44. EXPECT_CALL(mock_two_int_callback, Run(1, 2)).WillOnce(Return(42));
  45. OnceCallback<int(int, int)> two_int_callback = mock_two_int_callback.Get();
  46. EXPECT_EQ(42, std::move(two_int_callback).Run(1, 2));
  47. }
  48. TEST(MockCallbackTest, Typedefs) {
  49. static_assert(std::is_same<MockCallback<RepeatingCallback<int()>>,
  50. MockRepeatingCallback<int()>>::value,
  51. "Repeating typedef differs for zero args");
  52. static_assert(std::is_same<MockCallback<RepeatingCallback<int(int, int)>>,
  53. MockRepeatingCallback<int(int, int)>>::value,
  54. "Repeating typedef differs for multiple args");
  55. static_assert(std::is_same<MockCallback<RepeatingCallback<void()>>,
  56. MockRepeatingClosure>::value,
  57. "Repeating typedef differs for closure");
  58. static_assert(std::is_same<MockCallback<OnceCallback<int()>>,
  59. MockOnceCallback<int()>>::value,
  60. "Once typedef differs for zero args");
  61. static_assert(std::is_same<MockCallback<OnceCallback<int(int, int)>>,
  62. MockOnceCallback<int(int, int)>>::value,
  63. "Once typedef differs for multiple args");
  64. static_assert(std::is_same<MockCallback<RepeatingCallback<void()>>,
  65. MockRepeatingClosure>::value,
  66. "Once typedef differs for closure");
  67. }
  68. } // namespace
  69. } // namespace base