1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
media / base / callback_holder_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/callback_holder.h"
#include "base/functional/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static void SetBool(bool* var) {
DCHECK(!*var);
*var = true;
}
static void CopyVar(int var1, int* var2) {
DCHECK_NE(var1, *var2);
*var2 = var1;
}
TEST(CallbackHolderTest, SetAfterHold_Closure) {
CallbackHolder<base::OnceClosure> cb;
EXPECT_TRUE(cb.IsNull());
cb.HoldCallback();
bool closure_called = false;
cb.SetCallback(base::BindOnce(&SetBool, &closure_called));
EXPECT_FALSE(cb.IsNull());
cb.RunOrHold();
EXPECT_FALSE(closure_called);
EXPECT_FALSE(cb.IsNull());
cb.RunHeldCallback();
EXPECT_TRUE(cb.IsNull());
EXPECT_TRUE(closure_called);
}
TEST(CallbackHolderTest, HoldAfterSet_Closure) {
CallbackHolder<base::OnceClosure> cb;
EXPECT_TRUE(cb.IsNull());
bool closure_called = false;
cb.SetCallback(base::BindOnce(&SetBool, &closure_called));
EXPECT_FALSE(cb.IsNull());
cb.HoldCallback();
cb.RunOrHold();
EXPECT_FALSE(closure_called);
EXPECT_FALSE(cb.IsNull());
cb.RunHeldCallback();
EXPECT_TRUE(cb.IsNull());
EXPECT_TRUE(closure_called);
}
TEST(CallbackHolderTest, NotHold_Closure) {
CallbackHolder<base::OnceClosure> cb;
EXPECT_TRUE(cb.IsNull());
bool closure_called = false;
cb.SetCallback(base::BindOnce(&SetBool, &closure_called));
EXPECT_FALSE(cb.IsNull());
cb.RunOrHold();
EXPECT_TRUE(cb.IsNull());
EXPECT_TRUE(closure_called);
}
TEST(CallbackHolderTest, SetAfterHold_Callback) {
CallbackHolder<base::OnceCallback<void(int, int*)>> cb;
EXPECT_TRUE(cb.IsNull());
cb.HoldCallback();
cb.SetCallback(base::BindOnce(&CopyVar));
EXPECT_FALSE(cb.IsNull());
int var1 = 100;
int var2 = 0;
cb.RunOrHold(var1, &var2);
EXPECT_FALSE(cb.IsNull());
EXPECT_NE(var1, var2);
cb.RunHeldCallback();
EXPECT_TRUE(cb.IsNull());
EXPECT_EQ(var1, var2);
}
TEST(CallbackHolderTest, HoldAfterSet_Callback) {
CallbackHolder<base::OnceCallback<void(int, int*)>> cb;
EXPECT_TRUE(cb.IsNull());
cb.SetCallback(base::BindOnce(&CopyVar));
EXPECT_FALSE(cb.IsNull());
cb.HoldCallback();
int var1 = 100;
int var2 = 0;
cb.RunOrHold(var1, &var2);
EXPECT_FALSE(cb.IsNull());
EXPECT_NE(var1, var2);
cb.RunHeldCallback();
EXPECT_TRUE(cb.IsNull());
EXPECT_EQ(var1, var2);
}
TEST(CallbackHolderTest, NotHold_Callback) {
CallbackHolder<base::OnceCallback<void(int, int*)>> cb;
EXPECT_TRUE(cb.IsNull());
cb.SetCallback(base::BindOnce(&CopyVar));
EXPECT_FALSE(cb.IsNull());
int var1 = 100;
int var2 = 0;
cb.RunOrHold(var1, &var2);
EXPECT_TRUE(cb.IsNull());
EXPECT_EQ(var1, var2);
}
} // namespace media