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

Side by Side Diff: base/message_loop_proxy_impl_unittest.cc

Issue 7316015: Support Closure in ALL the loops! (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix typo Created 9 years, 5 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
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/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h" 7 #include "base/message_loop.h"
7 #include "base/message_loop_proxy_impl.h" 8 #include "base/message_loop_proxy_impl.h"
willchan no longer on Chromium 2011/07/08 12:04:21 This header should be first.
awong 2011/07/08 18:36:51 I don't think this rule applies to unittests does
willchan no longer on Chromium 2011/07/10 16:47:33 Common misconception for old fartz who first read
awong 2011/07/14 23:03:30 Done. (And I'm not old!)
willchan no longer on Chromium 2011/07/19 12:18:26 In Google years you are pretty old now :)
8 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h" 11 #include "testing/platform_test.h"
11 12
12 13
13 class MessageLoopProxyImplTest : public testing::Test { 14 class MessageLoopProxyImplTest : public testing::Test {
14 public: 15 public:
15 void Release() const { 16 void Release() const {
16 AssertOnIOThread(); 17 AssertOnIOThread();
17 Quit(); 18 Quit();
(...skipping 22 matching lines...) Expand all
40 virtual void TearDown() { 41 virtual void TearDown() {
41 io_thread_->Stop(); 42 io_thread_->Stop();
42 file_thread_->Stop(); 43 file_thread_->Stop();
43 } 44 }
44 45
45 static void BasicFunction(MessageLoopProxyImplTest* test) { 46 static void BasicFunction(MessageLoopProxyImplTest* test) {
46 test->AssertOnFileThread(); 47 test->AssertOnFileThread();
47 test->Quit(); 48 test->Quit();
48 } 49 }
49 50
51 static void AssertNotRun() {
52 FAIL() << "Callback Should not get executed.";
53 }
54
50 class DummyTask : public Task { 55 class DummyTask : public Task {
51 public: 56 public:
52 explicit DummyTask(bool* deleted) : deleted_(deleted) { } 57 explicit DummyTask(bool* deleted) : deleted_(deleted) { }
53 ~DummyTask() { 58 ~DummyTask() {
54 *deleted_ = true; 59 *deleted_ = true;
55 } 60 }
56 61
57 void Run() { 62 void Run() {
58 FAIL(); 63 FAIL();
59 } 64 }
(...skipping 16 matching lines...) Expand all
76 }; 81 };
77 82
78 scoped_ptr<base::Thread> io_thread_; 83 scoped_ptr<base::Thread> io_thread_;
79 scoped_ptr<base::Thread> file_thread_; 84 scoped_ptr<base::Thread> file_thread_;
80 85
81 private: 86 private:
82 mutable MessageLoop loop_; 87 mutable MessageLoop loop_;
83 }; 88 };
84 89
85 90
86 TEST_F(MessageLoopProxyImplTest, PostTask) { 91 TEST_F(MessageLoopProxyImplTest, LegacyPostTask) {
87 EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask( 92 EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask(
88 FROM_HERE, NewRunnableFunction(&BasicFunction, this))); 93 FROM_HERE, NewRunnableFunction(&BasicFunction, this)));
89 MessageLoop::current()->Run(); 94 MessageLoop::current()->Run();
90 } 95 }
91 96
92 TEST_F(MessageLoopProxyImplTest, Release) { 97 TEST_F(MessageLoopProxyImplTest, Release) {
93 EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this)); 98 EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this));
94 MessageLoop::current()->Run(); 99 MessageLoop::current()->Run();
95 } 100 }
96 101
97 TEST_F(MessageLoopProxyImplTest, Delete) { 102 TEST_F(MessageLoopProxyImplTest, Delete) {
98 DeletedOnFile* deleted_on_file = new DeletedOnFile(this); 103 DeletedOnFile* deleted_on_file = new DeletedOnFile(this);
99 EXPECT_TRUE(file_thread_->message_loop_proxy()->DeleteSoon( 104 EXPECT_TRUE(file_thread_->message_loop_proxy()->DeleteSoon(
100 FROM_HERE, deleted_on_file)); 105 FROM_HERE, deleted_on_file));
101 MessageLoop::current()->Run(); 106 MessageLoop::current()->Run();
102 } 107 }
103 108
104 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadExits) { 109 TEST_F(MessageLoopProxyImplTest, LegacyPostTaskAfterThreadExits) {
105 scoped_ptr<base::Thread> test_thread( 110 scoped_ptr<base::Thread> test_thread(
106 new base::Thread("MessageLoopProxyImplTest_Dummy")); 111 new base::Thread("MessageLoopProxyImplTest_Dummy"));
107 test_thread->Start(); 112 test_thread->Start();
108 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = 113 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
109 test_thread->message_loop_proxy(); 114 test_thread->message_loop_proxy();
110 test_thread->Stop(); 115 test_thread->Stop();
111 116
112 bool deleted = false; 117 bool deleted = false;
113 bool ret = message_loop_proxy->PostTask( 118 bool ret = message_loop_proxy->PostTask(
114 FROM_HERE, new DummyTask(&deleted)); 119 FROM_HERE, new DummyTask(&deleted));
115 EXPECT_FALSE(ret); 120 EXPECT_FALSE(ret);
116 EXPECT_TRUE(deleted); 121 EXPECT_TRUE(deleted);
117 } 122 }
118 123
119 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadIsDeleted) { 124 TEST_F(MessageLoopProxyImplTest, LegacyPostTaskAfterThreadIsDeleted) {
120 scoped_refptr<base::MessageLoopProxy> message_loop_proxy; 125 scoped_refptr<base::MessageLoopProxy> message_loop_proxy;
121 { 126 {
122 scoped_ptr<base::Thread> test_thread( 127 scoped_ptr<base::Thread> test_thread(
123 new base::Thread("MessageLoopProxyImplTest_Dummy")); 128 new base::Thread("MessageLoopProxyImplTest_Dummy"));
124 test_thread->Start(); 129 test_thread->Start();
125 message_loop_proxy = test_thread->message_loop_proxy(); 130 message_loop_proxy = test_thread->message_loop_proxy();
126 } 131 }
127 bool deleted = false; 132 bool deleted = false;
128 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); 133 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
129 EXPECT_FALSE(ret); 134 EXPECT_FALSE(ret);
130 EXPECT_TRUE(deleted); 135 EXPECT_TRUE(deleted);
131 } 136 }
132 137
138 TEST_F(MessageLoopProxyImplTest, PostTask) {
139 EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask(
140 FROM_HERE, base::Bind(&MessageLoopProxyImplTest::BasicFunction,
141 base::Unretained(this))));
142 MessageLoop::current()->Run();
143 }
144
145 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadExits) {
146 scoped_ptr<base::Thread> test_thread(
147 new base::Thread("MessageLoopProxyImplTest_Dummy"));
148 test_thread->Start();
149 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
150 test_thread->message_loop_proxy();
151 test_thread->Stop();
152
153 bool ret = message_loop_proxy->PostTask(
154 FROM_HERE,
155 base::Bind(&MessageLoopProxyImplTest::AssertNotRun));
156 EXPECT_FALSE(ret);
157 }
158
159 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadIsDeleted) {
160 scoped_refptr<base::MessageLoopProxy> message_loop_proxy;
161 {
162 scoped_ptr<base::Thread> test_thread(
163 new base::Thread("MessageLoopProxyImplTest_Dummy"));
164 test_thread->Start();
165 message_loop_proxy = test_thread->message_loop_proxy();
166 }
167 bool ret = message_loop_proxy->PostTask(
168 FROM_HERE,
169 base::Bind(&MessageLoopProxyImplTest::AssertNotRun));
170 EXPECT_FALSE(ret);
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698