[email protected] | d9b1478 | 2010-04-15 08:08:07 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d9b1478 | 2010-04-15 08:08:07 | [diff] [blame] | 5 | #include "base/message_loop.h" |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 6 | #include "base/message_loop_proxy.h" |
[email protected] | 5097dc8 | 2010-07-15 17:23:23 | [diff] [blame] | 7 | #include "base/scoped_ptr.h" |
[email protected] | ed7e6dd | 2010-10-12 02:02:45 | [diff] [blame] | 8 | #include "chrome/browser/browser_thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 10 | #include "testing/platform_test.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 12 | class BrowserThreadTest : public testing::Test { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 13 | public: |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame^] | 14 | void Release() const { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 15 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 16 | loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 17 | } |
[email protected] | 64cd0d12 | 2008-10-17 21:16:13 | [diff] [blame] | 18 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 19 | protected: |
| 20 | virtual void SetUp() { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 21 | ui_thread_.reset(new BrowserThread(BrowserThread::UI)); |
| 22 | file_thread_.reset(new BrowserThread(BrowserThread::FILE)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 23 | ui_thread_->Start(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 24 | file_thread_->Start(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 25 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 26 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 27 | virtual void TearDown() { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 28 | ui_thread_->Stop(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 29 | file_thread_->Stop(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 30 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 32 | static void BasicFunction(MessageLoop* message_loop) { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 33 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 34 | message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 35 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 37 | class DummyTask : public Task { |
| 38 | public: |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 39 | explicit DummyTask(bool* deleted) : deleted_(deleted) { } |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 40 | ~DummyTask() { |
| 41 | *deleted_ = true; |
| 42 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 44 | void Run() { |
| 45 | CHECK(false); |
| 46 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 48 | private: |
| 49 | bool* deleted_; |
| 50 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 52 | class DeletedOnFile |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 53 | : public base::RefCountedThreadSafe< |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 54 | DeletedOnFile, BrowserThread::DeleteOnFileThread> { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 55 | public: |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 56 | explicit DeletedOnFile(MessageLoop* message_loop) |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 57 | : message_loop_(message_loop) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 59 | ~DeletedOnFile() { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 60 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 61 | message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 62 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 64 | private: |
| 65 | MessageLoop* message_loop_; |
| 66 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 68 | class NeverDeleted |
| 69 | : public base::RefCountedThreadSafe< |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 70 | NeverDeleted, BrowserThread::DeleteOnWebKitThread> { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 71 | public: |
| 72 | ~NeverDeleted() { |
| 73 | CHECK(false); |
| 74 | } |
| 75 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 77 | private: |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 78 | scoped_ptr<BrowserThread> ui_thread_; |
| 79 | scoped_ptr<BrowserThread> file_thread_; |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame^] | 80 | // It's kind of ugly to make this mutable - solely so we can post the Quit |
| 81 | // Task from Release(). This should be fixed. |
| 82 | mutable MessageLoop loop_; |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 83 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 85 | TEST_F(BrowserThreadTest, PostTask) { |
| 86 | BrowserThread::PostTask( |
| 87 | BrowserThread::FILE, FROM_HERE, |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 88 | NewRunnableFunction(&BasicFunction, MessageLoop::current())); |
| 89 | MessageLoop::current()->Run(); |
| 90 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 92 | TEST_F(BrowserThreadTest, Release) { |
| 93 | BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 94 | MessageLoop::current()->Run(); |
| 95 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 97 | TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeleted) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 98 | bool deleted = false; |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 99 | BrowserThread::PostTask( |
| 100 | BrowserThread::WEBKIT, FROM_HERE, |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 101 | new DummyTask(&deleted)); |
| 102 | EXPECT_TRUE(deleted); |
| 103 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 105 | TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 106 | { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 107 | scoped_refptr<DeletedOnFile> test( |
| 108 | new DeletedOnFile(MessageLoop::current())); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 109 | } |
| 110 | MessageLoop::current()->Run(); |
| 111 | } |
| 112 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 113 | TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 114 | scoped_refptr<NeverDeleted> test(new NeverDeleted()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | } |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 116 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 117 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 118 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 119 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 120 | message_loop_proxy->PostTask(FROM_HERE, |
| 121 | NewRunnableFunction(&BasicFunction, |
| 122 | MessageLoop::current())); |
| 123 | MessageLoop::current()->Run(); |
| 124 | } |
| 125 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 126 | TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 127 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 128 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 129 | message_loop_proxy->ReleaseSoon(FROM_HERE, this); |
| 130 | MessageLoop::current()->Run(); |
| 131 | } |
| 132 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 133 | TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 134 | bool deleted = false; |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 135 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 136 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 137 | message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 138 | EXPECT_TRUE(deleted); |
| 139 | } |
| 140 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 141 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { |
| 142 | scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 143 | io_thread->Start(); |
| 144 | io_thread->Stop(); |
| 145 | |
| 146 | bool deleted = false; |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 147 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 148 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 149 | bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 150 | EXPECT_FALSE(ret); |
| 151 | EXPECT_TRUE(deleted); |
| 152 | } |
| 153 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 154 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 155 | { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 156 | scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 157 | io_thread->Start(); |
| 158 | } |
| 159 | bool deleted = false; |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 160 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 161 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 162 | bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 163 | EXPECT_FALSE(ret); |
| 164 | EXPECT_TRUE(deleted); |
| 165 | } |
| 166 | |