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/message_loop_proxy_impl.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "testing/platform_test.h"
17 class MessageLoopProxyImplTest
: public testing::Test
{
19 void Release() const {
25 loop_
.PostTask(FROM_HERE
, MessageLoop::QuitWhenIdleClosure());
28 void AssertOnIOThread() const {
29 ASSERT_TRUE(io_thread_
->message_loop_proxy()->BelongsToCurrentThread());
30 ASSERT_EQ(io_thread_
->message_loop_proxy(),
31 MessageLoopProxy::current());
34 void AssertOnFileThread() const {
35 ASSERT_TRUE(file_thread_
->message_loop_proxy()->BelongsToCurrentThread());
36 ASSERT_EQ(file_thread_
->message_loop_proxy(),
37 MessageLoopProxy::current());
41 virtual void SetUp() OVERRIDE
{
42 io_thread_
.reset(new Thread("MessageLoopProxyImplTest_IO"));
43 file_thread_
.reset(new Thread("MessageLoopProxyImplTest_File"));
45 file_thread_
->Start();
48 virtual void TearDown() OVERRIDE
{
53 static void BasicFunction(MessageLoopProxyImplTest
* test
) {
54 test
->AssertOnFileThread();
58 static void AssertNotRun() {
59 FAIL() << "Callback Should not get executed.";
64 explicit DeletedOnFile(MessageLoopProxyImplTest
* test
) : test_(test
) {}
67 test_
->AssertOnFileThread();
72 MessageLoopProxyImplTest
* test_
;
75 scoped_ptr
<Thread
> io_thread_
;
76 scoped_ptr
<Thread
> file_thread_
;
79 mutable MessageLoop loop_
;
82 TEST_F(MessageLoopProxyImplTest
, Release
) {
83 EXPECT_TRUE(io_thread_
->message_loop_proxy()->ReleaseSoon(FROM_HERE
, this));
84 MessageLoop::current()->Run();
87 TEST_F(MessageLoopProxyImplTest
, Delete
) {
88 DeletedOnFile
* deleted_on_file
= new DeletedOnFile(this);
89 EXPECT_TRUE(file_thread_
->message_loop_proxy()->DeleteSoon(
90 FROM_HERE
, deleted_on_file
));
91 MessageLoop::current()->Run();
94 TEST_F(MessageLoopProxyImplTest
, PostTask
) {
95 EXPECT_TRUE(file_thread_
->message_loop_proxy()->PostTask(
96 FROM_HERE
, Bind(&MessageLoopProxyImplTest::BasicFunction
,
98 MessageLoop::current()->Run();
101 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadExits
) {
102 scoped_ptr
<Thread
> test_thread(
103 new Thread("MessageLoopProxyImplTest_Dummy"));
104 test_thread
->Start();
105 scoped_refptr
<MessageLoopProxy
> message_loop_proxy
=
106 test_thread
->message_loop_proxy();
109 bool ret
= message_loop_proxy
->PostTask(
111 Bind(&MessageLoopProxyImplTest::AssertNotRun
));
115 TEST_F(MessageLoopProxyImplTest
, PostTaskAfterThreadIsDeleted
) {
116 scoped_refptr
<MessageLoopProxy
> message_loop_proxy
;
118 scoped_ptr
<Thread
> test_thread(
119 new Thread("MessageLoopProxyImplTest_Dummy"));
120 test_thread
->Start();
121 message_loop_proxy
= test_thread
->message_loop_proxy();
123 bool ret
= message_loop_proxy
->PostTask(
125 Bind(&MessageLoopProxyImplTest::AssertNotRun
));