[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | c31af70db2 | 2011-08-18 23:13:01 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 6 | #include "base/memory/scoped_ptr.h" |
[email protected] | d9b1478 | 2010-04-15 08:08:07 | [diff] [blame] | 7 | #include "base/message_loop.h" |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 8 | #include "base/message_loop_proxy.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 9 | #include "content/browser/browser_thread_impl.h" |
| 10 | #include "content/test/test_browser_thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 12 | #include "testing/platform_test.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 14 | namespace content { |
| 15 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 16 | class BrowserThreadTest : public testing::Test { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 17 | public: |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 18 | void Release() const { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 19 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d111e93 | 2011-12-10 00:22:59 | [diff] [blame] | 20 | loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 21 | } |
[email protected] | 64cd0d12 | 2008-10-17 21:16:13 | [diff] [blame] | 22 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 23 | protected: |
| 24 | virtual void SetUp() { |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 25 | ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
| 26 | file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 27 | ui_thread_->Start(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 28 | file_thread_->Start(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 29 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 31 | virtual void TearDown() { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 32 | ui_thread_->Stop(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 33 | file_thread_->Stop(); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 34 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 36 | static void BasicFunction(MessageLoop* message_loop) { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 37 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | d111e93 | 2011-12-10 00:22:59 | [diff] [blame] | 38 | message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 39 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 40 | |
[email protected] | c31af70db2 | 2011-08-18 23:13:01 | [diff] [blame] | 41 | static void DoNothing() { |
| 42 | } |
| 43 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 44 | class DummyTask : public Task { |
| 45 | public: |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 46 | explicit DummyTask(bool* deleted) : deleted_(deleted) { } |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 47 | ~DummyTask() { |
| 48 | *deleted_ = true; |
| 49 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 51 | void Run() { |
| 52 | CHECK(false); |
| 53 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 55 | private: |
| 56 | bool* deleted_; |
| 57 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 59 | class DeletedOnFile |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 60 | : public base::RefCountedThreadSafe< |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 61 | DeletedOnFile, BrowserThread::DeleteOnFileThread> { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 62 | public: |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 63 | explicit DeletedOnFile(MessageLoop* message_loop) |
[email protected] | 4a3dab2 | 2009-11-11 17:36:50 | [diff] [blame] | 64 | : message_loop_(message_loop) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 66 | ~DeletedOnFile() { |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 67 | CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | d111e93 | 2011-12-10 00:22:59 | [diff] [blame] | 68 | message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 69 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 71 | private: |
| 72 | MessageLoop* message_loop_; |
| 73 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 75 | class NeverDeleted |
| 76 | : public base::RefCountedThreadSafe< |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 77 | NeverDeleted, BrowserThread::DeleteOnWebKitThread> { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 78 | public: |
| 79 | ~NeverDeleted() { |
| 80 | CHECK(false); |
| 81 | } |
| 82 | }; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 84 | private: |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 85 | scoped_ptr<BrowserThreadImpl> ui_thread_; |
| 86 | scoped_ptr<BrowserThreadImpl> file_thread_; |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 87 | // It's kind of ugly to make this mutable - solely so we can post the Quit |
| 88 | // Task from Release(). This should be fixed. |
| 89 | mutable MessageLoop loop_; |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 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, PostTask) { |
| 93 | BrowserThread::PostTask( |
| 94 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 8075105 | 2011-11-12 17:10:58 | [diff] [blame] | 95 | base::Bind(&BasicFunction, MessageLoop::current())); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 96 | MessageLoop::current()->Run(); |
| 97 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 99 | TEST_F(BrowserThreadTest, Release) { |
| 100 | BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 101 | MessageLoop::current()->Run(); |
| 102 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 104 | TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeleted) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 105 | bool deleted = false; |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 106 | BrowserThread::PostTask( |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 107 | BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 108 | new DummyTask(&deleted)); |
| 109 | EXPECT_TRUE(deleted); |
| 110 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 112 | TEST_F(BrowserThreadTest, ReleasedOnCorrectThread) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 113 | { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 114 | scoped_refptr<DeletedOnFile> test( |
| 115 | new DeletedOnFile(MessageLoop::current())); |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 116 | } |
| 117 | MessageLoop::current()->Run(); |
| 118 | } |
| 119 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 120 | TEST_F(BrowserThreadTest, NotReleasedIfTargetThreadNonExistent) { |
[email protected] | f671062 | 2009-11-02 06:10:30 | [diff] [blame] | 121 | scoped_refptr<NeverDeleted> test(new NeverDeleted()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | } |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 123 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 124 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxy) { |
[email protected] | 656475d | 2010-05-06 18:34:24 | [diff] [blame] | 125 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 126 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
[email protected] | 8075105 | 2011-11-12 17:10:58 | [diff] [blame] | 127 | message_loop_proxy->PostTask( |
| 128 | FROM_HERE, base::Bind(&BasicFunction, MessageLoop::current())); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 129 | MessageLoop::current()->Run(); |
| 130 | } |
| 131 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 132 | TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { |
[email protected] | 656475d | 2010-05-06 18:34:24 | [diff] [blame] | 133 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 134 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 135 | message_loop_proxy->ReleaseSoon(FROM_HERE, this); |
| 136 | MessageLoop::current()->Run(); |
| 137 | } |
| 138 | |
[email protected] | c31af70db2 | 2011-08-18 23:13:01 | [diff] [blame] | 139 | TEST_F(BrowserThreadTest, PostTaskAndReply) { |
| 140 | // Most of the heavy testing for PostTaskAndReply() is done inside the |
| 141 | // MessageLoopProxy test. This just makes sure we get piped through at all. |
| 142 | ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
| 143 | BrowserThread::FILE, |
| 144 | FROM_HERE, |
| 145 | base::Bind(&BrowserThreadTest::DoNothing), |
| 146 | base::Bind(&MessageLoop::Quit, |
| 147 | base::Unretained(MessageLoop::current()->current())))); |
| 148 | MessageLoop::current()->Run(); |
| 149 | } |
| 150 | |
| 151 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 152 | TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 153 | bool deleted = false; |
[email protected] | 656475d | 2010-05-06 18:34:24 | [diff] [blame] | 154 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | e1dd562 | 2011-12-20 12:28:58 | [diff] [blame] | 155 | BrowserThread::GetMessageLoopProxyForThread( |
| 156 | BrowserThread::WEBKIT_DEPRECATED); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 157 | message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 158 | EXPECT_TRUE(deleted); |
| 159 | } |
| 160 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 161 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 162 | scoped_ptr<BrowserThreadImpl> io_thread( |
| 163 | new BrowserThreadImpl(BrowserThread::IO)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 164 | io_thread->Start(); |
| 165 | io_thread->Stop(); |
| 166 | |
| 167 | bool deleted = false; |
[email protected] | 656475d | 2010-05-06 18:34:24 | [diff] [blame] | 168 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 169 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 170 | bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 171 | EXPECT_FALSE(ret); |
| 172 | EXPECT_TRUE(deleted); |
| 173 | } |
| 174 | |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 175 | TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadIsDeleted) { |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 176 | { |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 177 | scoped_ptr<BrowserThreadImpl> io_thread( |
| 178 | new BrowserThreadImpl(BrowserThread::IO)); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 179 | io_thread->Start(); |
| 180 | } |
| 181 | bool deleted = false; |
[email protected] | 656475d | 2010-05-06 18:34:24 | [diff] [blame] | 182 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
[email protected] | 092b04e | 2010-10-12 23:23:44 | [diff] [blame] | 183 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
[email protected] | 2cbac9e | 2010-04-29 03:31:34 | [diff] [blame] | 184 | bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 185 | EXPECT_FALSE(ret); |
| 186 | EXPECT_TRUE(deleted); |
| 187 | } |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 188 | |
| 189 | } |