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.
5 #include "media/base/callback_holder.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 static void SetBool(bool* var
) {
16 static void CopyVar(int var1
, int* var2
) {
17 DCHECK_NE(var1
, *var2
);
21 TEST(CallbackHolderTest
, SetAfterHold_Closure
) {
22 CallbackHolder
<base::Closure
> cb
;
23 EXPECT_TRUE(cb
.IsNull());
27 bool closure_called
= false;
28 cb
.SetCallback(base::Bind(&SetBool
, &closure_called
));
29 EXPECT_FALSE(cb
.IsNull());
32 EXPECT_FALSE(closure_called
);
34 EXPECT_FALSE(cb
.IsNull());
36 EXPECT_TRUE(cb
.IsNull());
37 EXPECT_TRUE(closure_called
);
40 TEST(CallbackHolderTest
, HoldAfterSet_Closure
) {
41 CallbackHolder
<base::Closure
> cb
;
42 EXPECT_TRUE(cb
.IsNull());
44 bool closure_called
= false;
45 cb
.SetCallback(base::Bind(&SetBool
, &closure_called
));
46 EXPECT_FALSE(cb
.IsNull());
51 EXPECT_FALSE(closure_called
);
52 EXPECT_FALSE(cb
.IsNull());
54 EXPECT_TRUE(cb
.IsNull());
55 EXPECT_TRUE(closure_called
);
58 TEST(CallbackHolderTest
, NotHold_Closure
) {
59 CallbackHolder
<base::Closure
> cb
;
60 EXPECT_TRUE(cb
.IsNull());
62 bool closure_called
= false;
63 cb
.SetCallback(base::Bind(&SetBool
, &closure_called
));
64 EXPECT_FALSE(cb
.IsNull());
67 EXPECT_TRUE(cb
.IsNull());
68 EXPECT_TRUE(closure_called
);
71 TEST(CallbackHolderTest
, SetAfterHold_Callback
) {
72 CallbackHolder
<base::Callback
<void(int, int*)> > cb
;
73 EXPECT_TRUE(cb
.IsNull());
77 cb
.SetCallback(base::Bind(&CopyVar
));
78 EXPECT_FALSE(cb
.IsNull());
82 cb
.RunOrHold(var1
, &var2
);
83 EXPECT_FALSE(cb
.IsNull());
84 EXPECT_NE(var1
, var2
);
87 EXPECT_TRUE(cb
.IsNull());
88 EXPECT_EQ(var1
, var2
);
91 TEST(CallbackHolderTest
, HoldAfterSet_Callback
) {
92 CallbackHolder
<base::Callback
<void(int, int*)> > cb
;
93 EXPECT_TRUE(cb
.IsNull());
95 cb
.SetCallback(base::Bind(&CopyVar
));
96 EXPECT_FALSE(cb
.IsNull());
102 cb
.RunOrHold(var1
, &var2
);
103 EXPECT_FALSE(cb
.IsNull());
104 EXPECT_NE(var1
, var2
);
106 cb
.RunHeldCallback();
107 EXPECT_TRUE(cb
.IsNull());
108 EXPECT_EQ(var1
, var2
);
111 TEST(CallbackHolderTest
, NotHold_Callback
) {
112 CallbackHolder
<base::Callback
<void(int, int*)> > cb
;
113 EXPECT_TRUE(cb
.IsNull());
115 cb
.SetCallback(base::Bind(&CopyVar
));
116 EXPECT_FALSE(cb
.IsNull());
120 cb
.RunOrHold(var1
, &var2
);
121 EXPECT_TRUE(cb
.IsNull());
122 EXPECT_EQ(var1
, var2
);