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.
5 #include "base/message_loop_proxy_impl.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/message_loop_proxy.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
16 class MessageLoopProxyImplTest
: public testing::Test
{
18 void Release() const {
24 loop_
.PostTask(FROM_HERE
, MessageLoop::QuitClosure());
27 void AssertOnIOThread() const {
28 ASSERT_TRUE(io_thread_
->message_loop_proxy()->BelongsToCurrentThread());
29 ASSERT_EQ(io_thread_
->message_loop_proxy(),
30 base::MessageLoopProxy::current());
33 void AssertOnFileThread() const {
34 ASSERT_TRUE(file_thread_
->message_loop_proxy()->BelongsToCurrentThread());
35 ASSERT_EQ(file_thread_
->message_loop_proxy(),
36 base::MessageLoopProxy::current());
40 virtual void SetUp() OVERRIDE
{
41 io_thread_
.reset(new base::Thread("MessageLoopProxyImplTest_IO"));
42 file_thread_
.reset(new base::Thread("MessageLoopProxyImplTest_File"));
44 file_thread_
->Start();
47 virtual void TearDown() OVERRIDE
{
52 static void BasicFunction(MessageLoopProxyImplTest
* test
) {
53 test
->AssertOnFileThread();
57 static void AssertNotRun() {
58 FAIL() << "Callback Should not get executed.";
63 explicit DeletedOnFile(MessageLoopProxyImplTest
* test
) : test_(test
) {}
66 test_
->AssertOnFileThread();
71 MessageLoopProxyImplTest
* test_
;
74 scoped_ptr
<base::Thread
> io_thread_
;
75 scoped_ptr
<base::Thread
> file_thread_
;
78 mutable MessageLoop loop_
;
81 TEST_F(MessageLoopProxyImplTest
, Release
) {
82 EXPECT_TRUE(io_thread_
->message_loop_proxy()->ReleaseSoon(FROM_HERE
, this));
83 MessageLoop::current()->Run();
86 TEST_F(MessageLoopProxyImplTest
, Delete
) {
87 DeletedOnFile
* deleted_on_file
= new DeletedOnFile(this);
88 EXPECT_TRUE(file_thread_
->message_loop_proxy()->DeleteSoon(
89 FROM_HERE
, deleted_on_file
));
90 MessageLoop::current()->Run();
93 TEST_F(MessageLoopProxyImplTest
, PostTask
) {
94 EXPECT_TRUE(file_thread_
->message_loop_proxy()->PostTask(
95 FROM_HERE
, base::Bind(&MessageLoopProxyImplTest::BasicFunction
,
96 base::Unretained(this))));
97 MessageLoop::current()->Run();
100 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadExits
) {
101 scoped_ptr
<base::Thread
> test_thread(
102 new base::Thread("MessageLoopProxyImplTest_Dummy"));
103 test_thread
->Start();
104 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
=
105 test_thread
->message_loop_proxy();
108 bool ret
= message_loop_proxy
->PostTask(
110 base::Bind(&MessageLoopProxyImplTest::AssertNotRun
));
114 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadIsDeleted
) {
115 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
;
117 scoped_ptr
<base::Thread
> test_thread(
118 new base::Thread("MessageLoopProxyImplTest_Dummy"));
119 test_thread
->Start();
120 message_loop_proxy
= test_thread
->message_loop_proxy();
122 bool ret
= message_loop_proxy
->PostTask(
124 base::Bind(&MessageLoopProxyImplTest::AssertNotRun
));