Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: base/callback_unittest.cc

Issue 9717021: Make Callback::Reset() return a copy to support use-cases where Run() ends up modifying |*this|. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base::ResetAndReturn is born. Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/callback_helpers.h ('k') | content/renderer/media/capture_video_decoder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h"
5 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/callback_helpers.h"
6 #include "base/callback_internal.h" 8 #include "base/callback_internal.h"
7 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
9 11
10 namespace base { 12 namespace base {
11 13
12 namespace { 14 namespace {
13 15
14 class HelperObject {
15 public:
16 HelperObject() : next_number_(0) { }
17 int GetNextNumber() { return ++next_number_; }
18 void GetNextNumberArg(int* number) { *number = GetNextNumber(); }
19
20 private:
21 int next_number_;
22 };
23
24 struct FakeInvoker { 16 struct FakeInvoker {
25 typedef void(RunType)(internal::BindStateBase*); 17 typedef void(RunType)(internal::BindStateBase*);
26 static void Run(internal::BindStateBase*) { 18 static void Run(internal::BindStateBase*) {
27 } 19 }
28 }; 20 };
21
29 } // namespace 22 } // namespace
30 23
31 namespace internal { 24 namespace internal {
32 template <typename Runnable, typename RunType, typename BoundArgsType> 25 template <typename Runnable, typename RunType, typename BoundArgsType>
33 struct BindState; 26 struct BindState;
34 27
35 // White-box testpoints to inject into a Callback<> object for checking 28 // White-box testpoints to inject into a Callback<> object for checking
36 // comparators and emptiness APIs. Use a BindState that is specialized 29 // comparators and emptiness APIs. Use a BindState that is specialized
37 // based on a type we declared in the anonymous namespace above to remove any 30 // based on a type we declared in the anonymous namespace above to remove any
38 // chance of colliding with another instantiation and breaking the 31 // chance of colliding with another instantiation and breaking the
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // Resetting should bring us back to empty. 116 // Resetting should bring us back to empty.
124 ASSERT_FALSE(callback_a_.is_null()); 117 ASSERT_FALSE(callback_a_.is_null());
125 ASSERT_FALSE(callback_a_.Equals(null_callback_)); 118 ASSERT_FALSE(callback_a_.Equals(null_callback_));
126 119
127 callback_a_.Reset(); 120 callback_a_.Reset();
128 121
129 EXPECT_TRUE(callback_a_.is_null()); 122 EXPECT_TRUE(callback_a_.is_null());
130 EXPECT_TRUE(callback_a_.Equals(null_callback_)); 123 EXPECT_TRUE(callback_a_.Equals(null_callback_));
131 } 124 }
132 125
126 struct TestForReentrancy {
127 TestForReentrancy()
128 : cb_already_run(false),
129 cb(base::Bind(&TestForReentrancy::AssertCBIsNull,
akalin 2012/03/23 18:42:07 no base::
Ami GONE FROM CHROMIUM 2012/03/24 17:43:35 Done.
130 base::Unretained(this))) {
akalin 2012/03/23 18:42:07 no base::
Ami GONE FROM CHROMIUM 2012/03/24 17:43:35 Done.
131 }
132 void AssertCBIsNull() {
133 ASSERT_TRUE(cb.is_null());
134 cb_already_run = true;
135 }
136 bool cb_already_run;
137 Closure cb;
138 };
139
140 TEST_F(CallbackTest, ResetAndReturn) {
141 TestForReentrancy tfr;
142 ASSERT_FALSE(tfr.cb.is_null());
143 ASSERT_FALSE(tfr.cb_already_run);
144 base::ResetAndReturn(&tfr.cb).Run();
akalin 2012/03/23 18:42:07 no base::
Ami GONE FROM CHROMIUM 2012/03/24 17:43:35 Done.
145 ASSERT_TRUE(tfr.cb.is_null());
146 ASSERT_TRUE(tfr.cb_already_run);
147 }
148
133 } // namespace 149 } // namespace
134 } // namespace base 150 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_helpers.h ('k') | content/renderer/media/capture_video_decoder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698