| 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/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.h" | 8 #include "base/message_loop_proxy.h" |
| 8 #include "content/browser/browser_thread.h" | 9 #include "content/browser/browser_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 class BrowserThreadTest : public testing::Test { | 13 class BrowserThreadTest : public testing::Test { |
| 13 public: | 14 public: |
| 14 void Release() const { | 15 void Release() const { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 27 virtual void TearDown() { | 28 virtual void TearDown() { |
| 28 ui_thread_->Stop(); | 29 ui_thread_->Stop(); |
| 29 file_thread_->Stop(); | 30 file_thread_->Stop(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 static void BasicFunction(MessageLoop* message_loop) { | 33 static void BasicFunction(MessageLoop* message_loop) { |
| 33 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 34 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 34 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); | 35 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 35 } | 36 } |
| 36 | 37 |
| 38 static void DoNothing() { |
| 39 } |
| 40 |
| 37 class DummyTask : public Task { | 41 class DummyTask : public Task { |
| 38 public: | 42 public: |
| 39 explicit DummyTask(bool* deleted) : deleted_(deleted) { } | 43 explicit DummyTask(bool* deleted) : deleted_(deleted) { } |
| 40 ~DummyTask() { | 44 ~DummyTask() { |
| 41 *deleted_ = true; | 45 *deleted_ = true; |
| 42 } | 46 } |
| 43 | 47 |
| 44 void Run() { | 48 void Run() { |
| 45 CHECK(false); | 49 CHECK(false); |
| 46 } | 50 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 MessageLoop::current()->Run(); | 127 MessageLoop::current()->Run(); |
| 124 } | 128 } |
| 125 | 129 |
| 126 TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { | 130 TEST_F(BrowserThreadTest, ReleaseViaMessageLoopProxy) { |
| 127 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 131 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); | 132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI); |
| 129 message_loop_proxy->ReleaseSoon(FROM_HERE, this); | 133 message_loop_proxy->ReleaseSoon(FROM_HERE, this); |
| 130 MessageLoop::current()->Run(); | 134 MessageLoop::current()->Run(); |
| 131 } | 135 } |
| 132 | 136 |
| 137 TEST_F(BrowserThreadTest, PostTaskAndReply) { |
| 138 // Most of the heavy testing for PostTaskAndReply() is done inside the |
| 139 // MessageLoopProxy test. This just makes sure we get piped through at all. |
| 140 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
| 141 BrowserThread::FILE, |
| 142 FROM_HERE, |
| 143 base::Bind(&BrowserThreadTest::DoNothing), |
| 144 base::Bind(&MessageLoop::Quit, |
| 145 base::Unretained(MessageLoop::current()->current())))); |
| 146 MessageLoop::current()->Run(); |
| 147 } |
| 148 |
| 149 |
| 133 TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { | 150 TEST_F(BrowserThreadTest, TaskToNonExistentThreadIsDeletedViaMessageLoopProxy) { |
| 134 bool deleted = false; | 151 bool deleted = false; |
| 135 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 152 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 136 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT); | 153 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT); |
| 137 message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); | 154 message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 138 EXPECT_TRUE(deleted); | 155 EXPECT_TRUE(deleted); |
| 139 } | 156 } |
| 140 | 157 |
| 141 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { | 158 TEST_F(BrowserThreadTest, PostTaskViaMessageLoopProxyAfterThreadExits) { |
| 142 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); | 159 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 156 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); | 173 scoped_ptr<BrowserThread> io_thread(new BrowserThread(BrowserThread::IO)); |
| 157 io_thread->Start(); | 174 io_thread->Start(); |
| 158 } | 175 } |
| 159 bool deleted = false; | 176 bool deleted = false; |
| 160 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = | 177 scoped_refptr<base::MessageLoopProxy> message_loop_proxy = |
| 161 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | 178 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 162 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); | 179 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted)); |
| 163 EXPECT_FALSE(ret); | 180 EXPECT_FALSE(ret); |
| 164 EXPECT_TRUE(deleted); | 181 EXPECT_TRUE(deleted); |
| 165 } | 182 } |
| 166 | |
| OLD | NEW |