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

Side by Side Diff: base/task_unittest.cc

Issue 9034032: And now NewRunnableMethod(), you die. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix chrome_frame_automation Created 8 years, 11 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/task.h ('k') | chrome/browser/browsing_data_remover.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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/bind.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/task.h" 7 #include "base/task.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace { 10 namespace {
11 11
12 class CancelInDestructor : public base::RefCounted<CancelInDestructor> {
13 public:
14 CancelInDestructor() : cancelable_task_(NULL) {}
15
16 void Start() {
17 if (cancelable_task_) {
18 ADD_FAILURE();
19 return;
20 }
21 AddRef();
22 cancelable_task_ = NewRunnableMethod(
23 this, &CancelInDestructor::NeverIssuedCallback);
24 Release();
25 }
26
27 CancelableTask* cancelable_task() {
28 return cancelable_task_;
29 }
30
31 private:
32 friend class base::RefCounted<CancelInDestructor>;
33
34 ~CancelInDestructor() {
35 if (cancelable_task_)
36 cancelable_task_->Cancel();
37 }
38
39 void NeverIssuedCallback() { NOTREACHED(); }
40
41 CancelableTask* cancelable_task_;
42 };
43
44 TEST(TaskTest, TestCancelInDestructor) {
45 // Intentionally not using a scoped_refptr for cancel_in_destructor.
46 CancelInDestructor* cancel_in_destructor = new CancelInDestructor();
47 cancel_in_destructor->Start();
48 CancelableTask* cancelable_task = cancel_in_destructor->cancelable_task();
49 ASSERT_TRUE(cancelable_task);
50 delete cancelable_task;
51 }
52
53 class DoneTask : public Task { 12 class DoneTask : public Task {
54 public: 13 public:
55 DoneTask(int* run_count, bool* was_deleted) 14 DoneTask(int* run_count, bool* was_deleted)
56 : run_count_(run_count), was_deleted_(was_deleted) { 15 : run_count_(run_count), was_deleted_(was_deleted) {
57 } 16 }
58 virtual ~DoneTask() { 17 virtual ~DoneTask() {
59 *was_deleted_ = true; 18 *was_deleted_ = true;
60 } 19 }
61 20
62 virtual void Run() { 21 virtual void Run() {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count)); 87 base::ScopedClosureRunner runner(base::Bind(&Increment, &run_count));
129 c = runner.Release(); 88 c = runner.Release();
130 EXPECT_EQ(0, run_count); 89 EXPECT_EQ(0, run_count);
131 } 90 }
132 EXPECT_EQ(0, run_count); 91 EXPECT_EQ(0, run_count);
133 c.Run(); 92 c.Run();
134 EXPECT_EQ(1, run_count); 93 EXPECT_EQ(1, run_count);
135 } 94 }
136 95
137 } // namespace 96 } // namespace
OLDNEW
« no previous file with comments | « base/task.h ('k') | chrome/browser/browsing_data_remover.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698