Enable password generation only if sync for passwords is enabled.
[chromium-blink-merge.git] / base / callback_unittest.cc
blobf0f3915a2cb8ce2e5c84e7c9ce13bd56a3839d78
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.
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/callback_helpers.h"
8 #include "base/callback_internal.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
14 namespace {
16 struct FakeInvoker {
17 typedef void(RunType)(internal::BindStateBase*);
18 static void Run(internal::BindStateBase*) {
22 } // namespace
24 namespace internal {
25 template <typename Runnable, typename RunType, typename BoundArgsType>
26 struct BindState;
28 // White-box testpoints to inject into a Callback<> object for checking
29 // comparators and emptiness APIs. Use a BindState that is specialized
30 // based on a type we declared in the anonymous namespace above to remove any
31 // chance of colliding with another instantiation and breaking the
32 // one-definition-rule.
33 template <>
34 struct BindState<void(void), void(void), void(FakeInvoker)>
35 : public BindStateBase {
36 public:
37 typedef FakeInvoker InvokerType;
40 template <>
41 struct BindState<void(void), void(void),
42 void(FakeInvoker, FakeInvoker)>
43 : public BindStateBase {
44 public:
45 typedef FakeInvoker InvokerType;
47 } // namespace internal
49 namespace {
51 typedef internal::BindState<void(void), void(void), void(FakeInvoker)>
52 FakeBindState1;
53 typedef internal::BindState<void(void), void(void),
54 void(FakeInvoker, FakeInvoker)>
55 FakeBindState2;
57 class CallbackTest : public ::testing::Test {
58 public:
59 CallbackTest()
60 : callback_a_(new FakeBindState1()),
61 callback_b_(new FakeBindState2()) {
64 virtual ~CallbackTest() {
67 protected:
68 Callback<void(void)> callback_a_;
69 const Callback<void(void)> callback_b_; // Ensure APIs work with const.
70 Callback<void(void)> null_callback_;
73 // Ensure we can create unbound callbacks. We need this to be able to store
74 // them in class members that can be initialized later.
75 TEST_F(CallbackTest, DefaultConstruction) {
76 Callback<void(void)> c0;
77 Callback<void(int)> c1;
78 Callback<void(int,int)> c2;
79 Callback<void(int,int,int)> c3;
80 Callback<void(int,int,int,int)> c4;
81 Callback<void(int,int,int,int,int)> c5;
82 Callback<void(int,int,int,int,int,int)> c6;
84 EXPECT_TRUE(c0.is_null());
85 EXPECT_TRUE(c1.is_null());
86 EXPECT_TRUE(c2.is_null());
87 EXPECT_TRUE(c3.is_null());
88 EXPECT_TRUE(c4.is_null());
89 EXPECT_TRUE(c5.is_null());
90 EXPECT_TRUE(c6.is_null());
93 TEST_F(CallbackTest, IsNull) {
94 EXPECT_TRUE(null_callback_.is_null());
95 EXPECT_FALSE(callback_a_.is_null());
96 EXPECT_FALSE(callback_b_.is_null());
99 TEST_F(CallbackTest, Equals) {
100 EXPECT_TRUE(callback_a_.Equals(callback_a_));
101 EXPECT_FALSE(callback_a_.Equals(callback_b_));
102 EXPECT_FALSE(callback_b_.Equals(callback_a_));
104 // We should compare based on instance, not type.
105 Callback<void(void)> callback_c(new FakeBindState1());
106 Callback<void(void)> callback_a2 = callback_a_;
107 EXPECT_TRUE(callback_a_.Equals(callback_a2));
108 EXPECT_FALSE(callback_a_.Equals(callback_c));
110 // Empty, however, is always equal to empty.
111 Callback<void(void)> empty2;
112 EXPECT_TRUE(null_callback_.Equals(empty2));
115 TEST_F(CallbackTest, Reset) {
116 // Resetting should bring us back to empty.
117 ASSERT_FALSE(callback_a_.is_null());
118 ASSERT_FALSE(callback_a_.Equals(null_callback_));
120 callback_a_.Reset();
122 EXPECT_TRUE(callback_a_.is_null());
123 EXPECT_TRUE(callback_a_.Equals(null_callback_));
126 struct TestForReentrancy {
127 TestForReentrancy()
128 : cb_already_run(false),
129 cb(Bind(&TestForReentrancy::AssertCBIsNull, Unretained(this))) {
131 void AssertCBIsNull() {
132 ASSERT_TRUE(cb.is_null());
133 cb_already_run = true;
135 bool cb_already_run;
136 Closure cb;
139 TEST_F(CallbackTest, ResetAndReturn) {
140 TestForReentrancy tfr;
141 ASSERT_FALSE(tfr.cb.is_null());
142 ASSERT_FALSE(tfr.cb_already_run);
143 ResetAndReturn(&tfr.cb).Run();
144 ASSERT_TRUE(tfr.cb.is_null());
145 ASSERT_TRUE(tfr.cb_already_run);
148 } // namespace
149 } // namespace base