Fix clang error caused by lack of OVERRIDE in r161270.
[chromium-blink-merge.git] / base / message_loop_proxy_impl_unittest.cc
blobe97d8f17a200dde58031abf110a95236b4b775db
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"
7 #include "base/bind.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 {
17 public:
18 void Release() const {
19 AssertOnIOThread();
20 Quit();
23 void Quit() 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());
39 protected:
40 virtual void SetUp() OVERRIDE {
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();
47 virtual void TearDown() OVERRIDE {
48 io_thread_->Stop();
49 file_thread_->Stop();
52 static void BasicFunction(MessageLoopProxyImplTest* test) {
53 test->AssertOnFileThread();
54 test->Quit();
57 static void AssertNotRun() {
58 FAIL() << "Callback Should not get executed.";
61 class DeletedOnFile {
62 public:
63 explicit DeletedOnFile(MessageLoopProxyImplTest* test) : test_(test) {}
65 ~DeletedOnFile() {
66 test_->AssertOnFileThread();
67 test_->Quit();
70 private:
71 MessageLoopProxyImplTest* test_;
74 scoped_ptr<base::Thread> io_thread_;
75 scoped_ptr<base::Thread> file_thread_;
77 private:
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();
106 test_thread->Stop();
108 bool ret = message_loop_proxy->PostTask(
109 FROM_HERE,
110 base::Bind(&MessageLoopProxyImplTest::AssertNotRun));
111 EXPECT_FALSE(ret);
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(
123 FROM_HERE,
124 base::Bind(&MessageLoopProxyImplTest::AssertNotRun));
125 EXPECT_FALSE(ret);