1 // Copyright (c) 2010 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.h"
6 #include "base/message_loop_proxy_impl.h"
7 #include "base/scoped_ptr.h"
8 #include "base/thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
13 class MessageLoopProxyImplTest
: public testing::Test
{
21 loop_
.PostTask(FROM_HERE
, new MessageLoop::QuitTask
);
24 void AssertOnIOThread() {
25 ASSERT_TRUE(io_thread_
->message_loop_proxy()->BelongsToCurrentThread());
28 void AssertOnFileThread() {
29 ASSERT_TRUE(file_thread_
->message_loop_proxy()->BelongsToCurrentThread());
33 virtual void SetUp() {
34 io_thread_
.reset(new base::Thread("MessageLoopProxyImplTest_IO"));
35 file_thread_
.reset(new base::Thread("MessageLoopProxyImplTest_File"));
37 file_thread_
->Start();
40 virtual void TearDown() {
45 static void BasicFunction(MessageLoopProxyImplTest
* test
) {
46 test
->AssertOnFileThread();
50 class DummyTask
: public Task
{
52 explicit DummyTask(bool* deleted
) : deleted_(deleted
) { }
67 explicit DeletedOnFile(MessageLoopProxyImplTest
* test
) : test_(test
) {}
70 test_
->AssertOnFileThread();
75 MessageLoopProxyImplTest
* test_
;
78 scoped_ptr
<base::Thread
> io_thread_
;
79 scoped_ptr
<base::Thread
> file_thread_
;
86 TEST_F(MessageLoopProxyImplTest
, PostTask
) {
87 EXPECT_TRUE(file_thread_
->message_loop_proxy()->PostTask(
88 FROM_HERE
, NewRunnableFunction(&BasicFunction
, this)));
89 MessageLoop::current()->Run();
92 TEST_F(MessageLoopProxyImplTest
, Release
) {
93 EXPECT_TRUE(io_thread_
->message_loop_proxy()->ReleaseSoon(FROM_HERE
, this));
94 MessageLoop::current()->Run();
97 TEST_F(MessageLoopProxyImplTest
, Delete
) {
98 DeletedOnFile
* deleted_on_file
= new DeletedOnFile(this);
99 EXPECT_TRUE(file_thread_
->message_loop_proxy()->DeleteSoon(
100 FROM_HERE
, deleted_on_file
));
101 MessageLoop::current()->Run();
104 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadExits
) {
105 scoped_ptr
<base::Thread
> test_thread(
106 new base::Thread("MessageLoopProxyImplTest_Dummy"));
107 test_thread
->Start();
108 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
=
109 test_thread
->message_loop_proxy();
112 bool deleted
= false;
113 bool ret
= message_loop_proxy
->PostTask(
114 FROM_HERE
, new DummyTask(&deleted
));
116 EXPECT_TRUE(deleted
);
119 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadIsDeleted
) {
120 scoped_refptr
<base::MessageLoopProxy
> message_loop_proxy
;
122 scoped_ptr
<base::Thread
> test_thread(
123 new base::Thread("MessageLoopProxyImplTest_Dummy"));
124 test_thread
->Start();
125 message_loop_proxy
= test_thread
->message_loop_proxy();
127 bool deleted
= false;
128 bool ret
= message_loop_proxy
->PostTask(FROM_HERE
, new DummyTask(&deleted
));
130 EXPECT_TRUE(deleted
);