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
3 // found in the LICENSE file.
6 #include "base/bind_helpers.h"
7 #include "base/location.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/sequenced_task_runner_helpers.h"
10 #include "base/single_thread_task_runner.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/platform_test.h"
18 class BrowserThreadTest
: public testing::Test
{
20 void Release() const {
21 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
22 loop_
.task_runner()->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
26 void SetUp() override
{
27 ui_thread_
.reset(new BrowserThreadImpl(BrowserThread::UI
));
28 file_thread_
.reset(new BrowserThreadImpl(BrowserThread::FILE));
30 file_thread_
->Start();
33 void TearDown() override
{
38 static void BasicFunction(base::MessageLoop
* message_loop
) {
39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
40 message_loop
->task_runner()->PostTask(FROM_HERE
,
41 base::MessageLoop::QuitClosure());
45 : public base::RefCountedThreadSafe
<
46 DeletedOnFile
, BrowserThread::DeleteOnFileThread
> {
48 explicit DeletedOnFile(base::MessageLoop
* message_loop
)
49 : message_loop_(message_loop
) {}
52 friend struct BrowserThread::DeleteOnThread
<BrowserThread::FILE>;
53 friend class base::DeleteHelper
<DeletedOnFile
>;
56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
57 message_loop_
->task_runner()->PostTask(FROM_HERE
,
58 base::MessageLoop::QuitClosure());
61 base::MessageLoop
* message_loop_
;
65 scoped_ptr
<BrowserThreadImpl
> ui_thread_
;
66 scoped_ptr
<BrowserThreadImpl
> file_thread_
;
67 // It's kind of ugly to make this mutable - solely so we can post the Quit
68 // Task from Release(). This should be fixed.
69 mutable base::MessageLoop loop_
;
72 TEST_F(BrowserThreadTest
, PostTask
) {
73 BrowserThread::PostTask(
76 base::Bind(&BasicFunction
, base::MessageLoop::current()));
77 base::MessageLoop::current()->Run();
80 TEST_F(BrowserThreadTest
, Release
) {
81 BrowserThread::ReleaseSoon(BrowserThread::UI
, FROM_HERE
, this);
82 base::MessageLoop::current()->Run();
85 TEST_F(BrowserThreadTest
, ReleasedOnCorrectThread
) {
87 scoped_refptr
<DeletedOnFile
> test(
88 new DeletedOnFile(base::MessageLoop::current()));
90 base::MessageLoop::current()->Run();
93 TEST_F(BrowserThreadTest
, PostTaskViaTaskRunner
) {
94 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
=
95 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
96 task_runner
->PostTask(
97 FROM_HERE
, base::Bind(&BasicFunction
, base::MessageLoop::current()));
98 base::MessageLoop::current()->Run();
101 TEST_F(BrowserThreadTest
, ReleaseViaTaskRunner
) {
102 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
=
103 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
);
104 task_runner
->ReleaseSoon(FROM_HERE
, this);
105 base::MessageLoop::current()->Run();
108 TEST_F(BrowserThreadTest
, PostTaskAndReply
) {
109 // Most of the heavy testing for PostTaskAndReply() is done inside the
110 // task runner test. This just makes sure we get piped through at all.
111 ASSERT_TRUE(BrowserThread::PostTaskAndReply(
114 base::Bind(&base::DoNothing
),
115 base::Bind(&base::MessageLoop::Quit
,
116 base::Unretained(base::MessageLoop::current()->current()))));
117 base::MessageLoop::current()->Run();
120 } // namespace content