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/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/sequenced_task_runner_helpers.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_
.PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
26 virtual void SetUp() {
27 ui_thread_
.reset(new BrowserThreadImpl(BrowserThread::UI
));
28 file_thread_
.reset(new BrowserThreadImpl(BrowserThread::FILE));
30 file_thread_
->Start();
33 virtual void TearDown() {
38 static void BasicFunction(base::MessageLoop
* message_loop
) {
39 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
40 message_loop
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
44 : public base::RefCountedThreadSafe
<
45 DeletedOnFile
, BrowserThread::DeleteOnFileThread
> {
47 explicit DeletedOnFile(base::MessageLoop
* message_loop
)
48 : message_loop_(message_loop
) {}
51 friend struct BrowserThread::DeleteOnThread
<BrowserThread::FILE>;
52 friend class base::DeleteHelper
<DeletedOnFile
>;
55 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
56 message_loop_
->PostTask(FROM_HERE
, base::MessageLoop::QuitClosure());
59 base::MessageLoop
* message_loop_
;
63 scoped_ptr
<BrowserThreadImpl
> ui_thread_
;
64 scoped_ptr
<BrowserThreadImpl
> file_thread_
;
65 // It's kind of ugly to make this mutable - solely so we can post the Quit
66 // Task from Release(). This should be fixed.
67 mutable base::MessageLoop loop_
;
70 TEST_F(BrowserThreadTest
, PostTask
) {
71 BrowserThread::PostTask(
74 base::Bind(&BasicFunction
, base::MessageLoop::current()));
75 base::MessageLoop::current()->Run();
78 TEST_F(BrowserThreadTest
, Release
) {
79 BrowserThread::ReleaseSoon(BrowserThread::UI
, FROM_HERE
, this);
80 base::MessageLoop::current()->Run();
83 TEST_F(BrowserThreadTest
, ReleasedOnCorrectThread
) {
85 scoped_refptr
<DeletedOnFile
> test(
86 new DeletedOnFile(base::MessageLoop::current()));
88 base::MessageLoop::current()->Run();
91 TEST_F(BrowserThreadTest
, PostTaskViaMessageLoopProxy
) {
92 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
=
93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
94 message_loop_proxy
->PostTask(
95 FROM_HERE
, base::Bind(&BasicFunction
, base::MessageLoop::current()));
96 base::MessageLoop::current()->Run();
99 TEST_F(BrowserThreadTest
, ReleaseViaMessageLoopProxy
) {
100 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
=
101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
);
102 message_loop_proxy
->ReleaseSoon(FROM_HERE
, this);
103 base::MessageLoop::current()->Run();
106 TEST_F(BrowserThreadTest
, PostTaskAndReply
) {
107 // Most of the heavy testing for PostTaskAndReply() is done inside the
108 // MessageLoopProxy test. This just makes sure we get piped through at all.
109 ASSERT_TRUE(BrowserThread::PostTaskAndReply(
112 base::Bind(&base::DoNothing
),
113 base::Bind(&base::MessageLoop::Quit
,
114 base::Unretained(base::MessageLoop::current()->current()))));
115 base::MessageLoop::current()->Run();
118 } // namespace content