OLD | NEW |
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/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
10 #include "content/test/test_browser_thread.h" | 10 #include "content/test/test_browser_thread.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 } | 34 } |
35 | 35 |
36 static void BasicFunction(MessageLoop* message_loop) { | 36 static void BasicFunction(MessageLoop* message_loop) { |
37 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 37 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
38 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 38 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
39 } | 39 } |
40 | 40 |
41 static void DoNothing() { | 41 static void DoNothing() { |
42 } | 42 } |
43 | 43 |
44 class DummyTask : public Task { | |
45 public: | |
46 explicit DummyTask(bool* deleted) : deleted_(deleted) { } | |
47 ~DummyTask() { | |
48 *deleted_ = true; | |
49 } | |
50 | |
51 void Run() { | |
52 CHECK(false); | |
53 } | |
54 | |
55 private: | |
56 bool* deleted_; | |
57 }; | |
58 | |
59 class DeletedOnFile | 44 class DeletedOnFile |
60 : public base::RefCountedThreadSafe< | 45 : public base::RefCountedThreadSafe< |
61 DeletedOnFile, BrowserThread::DeleteOnFileThread> { | 46 DeletedOnFile, BrowserThread::DeleteOnFileThread> { |
62 public: | 47 public: |
63 explicit DeletedOnFile(MessageLoop* message_loop) | 48 explicit DeletedOnFile(MessageLoop* message_loop) |
64 : message_loop_(message_loop) { } | 49 : message_loop_(message_loop) { } |
65 | 50 |
66 ~DeletedOnFile() { | 51 ~DeletedOnFile() { |
67 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 52 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
68 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 53 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
(...skipping 25 matching lines...) Expand all Loading... |
94 BrowserThread::FILE, FROM_HERE, | 79 BrowserThread::FILE, FROM_HERE, |
95 base::Bind(&BasicFunction, MessageLoop::current())); | 80 base::Bind(&BasicFunction, MessageLoop::current())); |
96 MessageLoop::current()->Run(); | 81 MessageLoop::current()->Run(); |
97 } | 82 } |
98 | 83 |
99 TEST_F(BrowserThreadTest, Release) { | 84 TEST_F(BrowserThreadTest, Release) { |
100 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); | 85 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); |
101 MessageLoop::current()->Run(); | 86 MessageLoop::current()->Run(); |
102 } | 87 } |
103 | 88 |
104 TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeleted) { | |
105 bool deleted = false; | |
106 BrowserThread::PostTask( | |
107 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, | |
108 new DummyTask(&deleted)); | |
109 EXPECT_TRUE(deleted); | |
110 } | |
111 | |
112 TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { | 89 TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { |
113 { | 90 { |
114 scoped_refptr<DeletedOnFile> test( | 91 scoped_refptr<DeletedOnFile> test( |
115 new DeletedOnFile(MessageLoop::current())); | 92 new DeletedOnFile(MessageLoop::current())); |
116 } | 93 } |
117 MessageLoop::current()->Run(); | 94 MessageLoop::current()->Run(); |
118 } | 95 } |
119 | 96 |
120 TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) { | 97 TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) { |
121 scoped_refptr<NeverDeleted> test(new NeverDeleted()); | 98 scoped_refptr<NeverDeleted> test(new NeverDeleted()); |
(...skipping 19 matching lines...) Expand all Loading... |
141 // MessageLoopProxy test. This just makes sure we get piped through at all. | 118 // MessageLoopProxy test. This just makes sure we get piped through at all. |
142 ASSERT_TRUE(BrowserThread::PostTaskAndReply( | 119 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
143 BrowserThread::FILE, | 120 BrowserThread::FILE, |
144 FROM_HERE, | 121 FROM_HERE, |
145 base::Bind(&BrowserThreadTest::DoNothing), | 122 base::Bind(&BrowserThreadTest::DoNothing), |
146 base::Bind(&MessageLoop::Quit, | 123 base::Bind(&MessageLoop::Quit, |
147 base::Unretained(MessageLoop::current()->current())))); | 124 base::Unretained(MessageLoop::current()->current())))); |
148 MessageLoop::current()->Run(); | 125 MessageLoop::current()->Run(); |
149 } | 126 } |
150 | 127 |
151 | |
152 TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { | |
153 bool deleted = false; | |
154 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | |
155 BrowserThread::GetMessageLoopProxyForThread( | |
156 BrowserThread::WEBKIT_DEPRECATED); | |
157 message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); | |
158 EXPECT_TRUE(deleted); | |
159 } | 128 } |
160 | |
161 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { | |
162 scoped_ptr<BrowserThreadImpl> io_thread( | |
163 new BrowserThreadImpl(BrowserThread::IO)); | |
164 io_thread->Start(); | |
165 io_thread->Stop(); | |
166 | |
167 bool deleted = false; | |
168 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | |
169 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
170 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); | |
171 EXPECT_FALSE(ret); | |
172 EXPECT_TRUE(deleted); | |
173 } | |
174 | |
175 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) { | |
176 { | |
177 scoped_ptr<BrowserThreadImpl> io_thread( | |
178 new BrowserThreadImpl(BrowserThread::IO)); | |
179 io_thread->Start(); | |
180 } | |
181 bool deleted = false; | |
182 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | |
183 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | |
184 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); | |
185 EXPECT_FALSE(ret); | |
186 EXPECT_TRUE(deleted); | |
187 } | |
188 | |
189 } | |
OLD | NEW |