1// Copyright (c) 2012 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 "media/base/gmock_callback_support.h"
6
7#include "base/bind.h"
8#include "base/callback.h"
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12using testing::ByRef;
13using testing::MockFunction;
14
15namespace media {
16
17typedef base::Callback<void(const bool& src, bool* dst)> TestCallback;
18
19void SetBool(const bool& src, bool* dst) {
20  *dst = src;
21}
22
23TEST(GmockCallbackSupportTest, IsNullCallback) {
24  MockFunction<void(const TestCallback&)> check;
25  EXPECT_CALL(check, Call(IsNullCallback()));
26  check.Call(TestCallback());
27}
28
29TEST(GmockCallbackSupportTest, IsNotNullCallback) {
30  MockFunction<void(const TestCallback&)> check;
31  EXPECT_CALL(check, Call(IsNotNullCallback()));
32  check.Call(base::Bind(&SetBool));
33}
34
35TEST(GmockCallbackSupportTest, RunClosure) {
36  MockFunction<void(const base::Closure&)> check;
37  bool dst = false;
38  EXPECT_CALL(check, Call(IsNotNullCallback()))
39      .WillOnce(RunClosure<0>());
40  check.Call(base::Bind(&SetBool, true, &dst));
41  EXPECT_TRUE(dst);
42}
43
44TEST(GmockCallbackSupportTest, RunCallback0) {
45  MockFunction<void(const TestCallback&)> check;
46  bool dst = false;
47  EXPECT_CALL(check, Call(IsNotNullCallback()))
48      .WillOnce(RunCallback<0>(true, &dst));
49  check.Call(base::Bind(&SetBool));
50  EXPECT_TRUE(dst);
51}
52
53TEST(GmockCallbackSupportTest, RunCallback1) {
54  MockFunction<void(int, const TestCallback&)> check;
55  bool dst = false;
56  EXPECT_CALL(check, Call(0, IsNotNullCallback()))
57      .WillOnce(RunCallback<1>(true, &dst));
58  check.Call(0, base::Bind(&SetBool));
59  EXPECT_TRUE(dst);
60}
61
62TEST(GmockCallbackSupportTest, RunCallbackPassByRef) {
63  MockFunction<void(const TestCallback&)> check;
64  bool dst = false;
65  bool src = false;
66  EXPECT_CALL(check, Call(IsNotNullCallback()))
67      .WillOnce(RunCallback<0>(ByRef(src), &dst));
68  src = true;
69  check.Call(base::Bind(&SetBool));
70  EXPECT_TRUE(dst);
71}
72
73TEST(GmockCallbackSupportTest, RunCallbackPassByValue) {
74  MockFunction<void(const TestCallback&)> check;
75  bool dst = false;
76  bool src = true;
77  EXPECT_CALL(check, Call(IsNotNullCallback()))
78      .WillOnce(RunCallback<0>(src, &dst));
79  src = false;
80  check.Call(base::Bind(&SetBool));
81  EXPECT_TRUE(dst);
82}
83
84}  // namespace media
85