[email protected] | e7b3a61 | 2012-01-05 02:18:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 5 | #include "base/message_loop_proxy_impl.h" |
| 6 | |
| 7 | #include "base/bind.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 10 | #include "base/message_loop_proxy.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 11 | #include "base/threading/thread.h" |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | #include "testing/platform_test.h" |
| 14 | |
| 15 | |
| 16 | class MessageLoopProxyImplTest : public testing::Test { |
| 17 | public: |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 18 | void Release() const { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 19 | AssertOnIOThread(); |
| 20 | Quit(); |
| 21 | } |
| 22 | |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 23 | void Quit() const { |
[email protected] | 763839e | 2011-12-09 23:06:02 | [diff] [blame] | 24 | loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 25 | } |
| 26 | |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 27 | void AssertOnIOThread() const { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 28 | ASSERT_TRUE(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 29 | ASSERT_EQ(io_thread_->message_loop_proxy(), |
| 30 | base::MessageLoopProxy::current()); |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 33 | void AssertOnFileThread() const { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 34 | ASSERT_TRUE(file_thread_->message_loop_proxy()->BelongsToCurrentThread()); |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 35 | ASSERT_EQ(file_thread_->message_loop_proxy(), |
| 36 | base::MessageLoopProxy::current()); |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | protected: |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 40 | virtual void SetUp() OVERRIDE { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 41 | io_thread_.reset(new base::Thread("MessageLoopProxyImplTest_IO")); |
| 42 | file_thread_.reset(new base::Thread("MessageLoopProxyImplTest_File")); |
| 43 | io_thread_->Start(); |
| 44 | file_thread_->Start(); |
| 45 | } |
| 46 | |
[email protected] | 4410618 | 2012-04-06 03:53:02 | [diff] [blame] | 47 | virtual void TearDown() OVERRIDE { |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 48 | io_thread_->Stop(); |
| 49 | file_thread_->Stop(); |
| 50 | } |
| 51 | |
| 52 | static void BasicFunction(MessageLoopProxyImplTest* test) { |
| 53 | test->AssertOnFileThread(); |
| 54 | test->Quit(); |
| 55 | } |
| 56 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 57 | static void AssertNotRun() { |
| 58 | FAIL() << "Callback Should not get executed."; |
| 59 | } |
| 60 | |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 61 | class DeletedOnFile { |
| 62 | public: |
| 63 | explicit DeletedOnFile(MessageLoopProxyImplTest* test) : test_(test) {} |
| 64 | |
| 65 | ~DeletedOnFile() { |
| 66 | test_->AssertOnFileThread(); |
| 67 | test_->Quit(); |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | MessageLoopProxyImplTest* test_; |
| 72 | }; |
| 73 | |
| 74 | scoped_ptr<base::Thread> io_thread_; |
| 75 | scoped_ptr<base::Thread> file_thread_; |
| 76 | |
| 77 | private: |
[email protected] | 00ed48f | 2010-10-22 22:19:24 | [diff] [blame] | 78 | mutable MessageLoop loop_; |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 79 | }; |
| 80 | |
[email protected] | 656475d27 | 2010-05-06 18:34:24 | [diff] [blame] | 81 | TEST_F(MessageLoopProxyImplTest, Release) { |
| 82 | EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this)); |
| 83 | MessageLoop::current()->Run(); |
| 84 | } |
| 85 | |
| 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(); |
| 91 | } |
| 92 | |
[email protected] | 180c85e | 2011-07-26 18:25:16 | [diff] [blame] | 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(); |
| 98 | } |
| 99 | |
| 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(); |
| 106 | test_thread->Stop(); |
| 107 | |
| 108 | bool ret = message_loop_proxy->PostTask( |
| 109 | FROM_HERE, |
| 110 | base::Bind(&MessageLoopProxyImplTest::AssertNotRun)); |
| 111 | EXPECT_FALSE(ret); |
| 112 | } |
| 113 | |
| 114 | TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadIsDeleted) { |
| 115 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy; |
| 116 | { |
| 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(); |
| 121 | } |
| 122 | bool ret = message_loop_proxy->PostTask( |
| 123 | FROM_HERE, |
| 124 | base::Bind(&MessageLoopProxyImplTest::AssertNotRun)); |
| 125 | EXPECT_FALSE(ret); |
| 126 | } |