| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 12 #include "base/sequenced_task_runner_helpers.h" | 11 #include "base/sequenced_task_runner_helpers.h" |
| 13 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "content/browser/browser_thread_impl.h" | 13 #include "content/browser/browser_thread_impl.h" |
| 16 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
| 19 | 17 |
| 20 namespace content { | 18 namespace content { |
| 21 | 19 |
| 22 class BrowserThreadTest : public testing::Test { | 20 class BrowserThreadTest : public testing::Test { |
| 23 public: | 21 public: |
| 24 void Release() const { | 22 void Release() const { |
| 25 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 23 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 26 loop_.task_runner()->PostTask(FROM_HERE, | 24 loop_.task_runner()->PostTask(FROM_HERE, |
| 27 base::MessageLoop::QuitWhenIdleClosure()); | 25 base::MessageLoop::QuitWhenIdleClosure()); |
| 28 } | 26 } |
| 29 | 27 |
| 30 void StopFileThread() { | |
| 31 file_thread_->Stop(); | |
| 32 } | |
| 33 | |
| 34 protected: | 28 protected: |
| 35 void SetUp() override { | 29 void SetUp() override { |
| 36 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); | 30 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI)); |
| 37 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); | 31 file_thread_.reset(new BrowserThreadImpl(BrowserThread::FILE)); |
| 38 ui_thread_->Start(); | 32 ui_thread_->Start(); |
| 39 file_thread_->Start(); | 33 file_thread_->Start(); |
| 40 } | 34 } |
| 41 | 35 |
| 42 void TearDown() override { | 36 void TearDown() override { |
| 43 ui_thread_->Stop(); | 37 ui_thread_->Stop(); |
| 44 StopFileThread(); | 38 file_thread_->Stop(); |
| 45 } | 39 } |
| 46 | 40 |
| 47 static void BasicFunction(base::MessageLoop* message_loop) { | 41 static void BasicFunction(base::MessageLoop* message_loop) { |
| 48 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 42 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 49 message_loop->task_runner()->PostTask( | 43 message_loop->task_runner()->PostTask( |
| 50 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 44 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 51 } | 45 } |
| 52 | 46 |
| 53 class DeletedOnFile | 47 class DeletedOnFile |
| 54 : public base::RefCountedThreadSafe< | 48 : public base::RefCountedThreadSafe< |
| (...skipping 16 matching lines...) Expand all Loading... |
| 71 }; | 65 }; |
| 72 | 66 |
| 73 private: | 67 private: |
| 74 std::unique_ptr<BrowserThreadImpl> ui_thread_; | 68 std::unique_ptr<BrowserThreadImpl> ui_thread_; |
| 75 std::unique_ptr<BrowserThreadImpl> file_thread_; | 69 std::unique_ptr<BrowserThreadImpl> file_thread_; |
| 76 // It's kind of ugly to make this mutable - solely so we can post the Quit | 70 // It's kind of ugly to make this mutable - solely so we can post the Quit |
| 77 // Task from Release(). This should be fixed. | 71 // Task from Release(). This should be fixed. |
| 78 mutable base::MessageLoop loop_; | 72 mutable base::MessageLoop loop_; |
| 79 }; | 73 }; |
| 80 | 74 |
| 81 class FileThreadDestructionObserver | |
| 82 : public base::MessageLoop::DestructionObserver { | |
| 83 public: | |
| 84 explicit FileThreadDestructionObserver(bool* did_shutdown, | |
| 85 const base::Closure& callback) | |
| 86 : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 87 callback_(callback), | |
| 88 file_task_runner_( | |
| 89 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)), | |
| 90 did_shutdown_(did_shutdown) { | |
| 91 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE)->PostTask( | |
| 92 FROM_HERE, base::Bind(&Watch, this)); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 static void Watch(FileThreadDestructionObserver* observer) { | |
| 97 base::MessageLoop::current()->AddDestructionObserver(observer); | |
| 98 } | |
| 99 | |
| 100 // base::MessageLoop::DestructionObserver: | |
| 101 void WillDestroyCurrentMessageLoop() override { | |
| 102 // Ensure that even during MessageLoop teardown the BrowserThread ID is | |
| 103 // correctly associated with this thread and the BrowserThreadTaskRunner | |
| 104 // knows it's on the right thread. | |
| 105 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 106 EXPECT_TRUE(file_task_runner_->BelongsToCurrentThread()); | |
| 107 | |
| 108 base::MessageLoop::current()->RemoveDestructionObserver(this); | |
| 109 *did_shutdown_ = true; | |
| 110 callback_task_runner_->PostTask(FROM_HERE, callback_); | |
| 111 } | |
| 112 | |
| 113 const scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; | |
| 114 const base::Closure callback_; | |
| 115 const scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
| 116 bool* did_shutdown_; | |
| 117 }; | |
| 118 | |
| 119 TEST_F(BrowserThreadTest, PostTask) { | 75 TEST_F(BrowserThreadTest, PostTask) { |
| 120 BrowserThread::PostTask( | 76 BrowserThread::PostTask( |
| 121 BrowserThread::FILE, | 77 BrowserThread::FILE, |
| 122 FROM_HERE, | 78 FROM_HERE, |
| 123 base::Bind(&BasicFunction, base::MessageLoop::current())); | 79 base::Bind(&BasicFunction, base::MessageLoop::current())); |
| 124 base::RunLoop().Run(); | 80 base::RunLoop().Run(); |
| 125 } | 81 } |
| 126 | 82 |
| 127 TEST_F(BrowserThreadTest, Release) { | 83 TEST_F(BrowserThreadTest, Release) { |
| 128 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); | 84 BrowserThread::ReleaseSoon(BrowserThread::UI, FROM_HERE, this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 155 TEST_F(BrowserThreadTest, PostTaskAndReply) { | 111 TEST_F(BrowserThreadTest, PostTaskAndReply) { |
| 156 // Most of the heavy testing for PostTaskAndReply() is done inside the | 112 // Most of the heavy testing for PostTaskAndReply() is done inside the |
| 157 // task runner test. This just makes sure we get piped through at all. | 113 // task runner test. This just makes sure we get piped through at all. |
| 158 ASSERT_TRUE(BrowserThread::PostTaskAndReply( | 114 ASSERT_TRUE(BrowserThread::PostTaskAndReply( |
| 159 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing), | 115 BrowserThread::FILE, FROM_HERE, base::Bind(&base::DoNothing), |
| 160 base::Bind(&base::MessageLoop::QuitWhenIdle, | 116 base::Bind(&base::MessageLoop::QuitWhenIdle, |
| 161 base::Unretained(base::MessageLoop::current()->current())))); | 117 base::Unretained(base::MessageLoop::current()->current())))); |
| 162 base::RunLoop().Run(); | 118 base::RunLoop().Run(); |
| 163 } | 119 } |
| 164 | 120 |
| 165 TEST_F(BrowserThreadTest, RunsTasksOnCurrentThreadDuringShutdown) { | |
| 166 bool did_shutdown = false; | |
| 167 base::RunLoop loop; | |
| 168 FileThreadDestructionObserver observer(&did_shutdown, loop.QuitClosure()); | |
| 169 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 170 FROM_HERE, | |
| 171 base::Bind(&BrowserThreadTest::StopFileThread, base::Unretained(this))); | |
| 172 loop.Run(); | |
| 173 | |
| 174 EXPECT_TRUE(did_shutdown); | |
| 175 } | |
| 176 | |
| 177 } // namespace content | 121 } // namespace content |
| OLD | NEW |