Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: base/message_loop_proxy_impl_unittest.cc

Issue 1837003: Created a stock implementation of the MessageLoopProxy interface than can be ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Rearranged header files Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/message_loop_proxy_impl.cc ('k') | base/thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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.
4
5 #include "base/message_loop.h"
6 #include "base/message_loop_proxy_impl.h"
7 #include "base/thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "testing/platform_test.h"
10
11
12 class MessageLoopProxyImplTest : public testing::Test {
13 public:
14 void Release() {
15 AssertOnIOThread();
16 Quit();
17 }
18
19 void Quit() {
20 loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
21 }
22
23 void AssertOnIOThread() {
24 ASSERT_TRUE(io_thread_->message_loop_proxy()->BelongsToCurrentThread());
25 }
26
27 void AssertOnFileThread() {
28 ASSERT_TRUE(file_thread_->message_loop_proxy()->BelongsToCurrentThread());
29 }
30
31 protected:
32 virtual void SetUp() {
33 io_thread_.reset(new base::Thread("MessageLoopProxyImplTest_IO"));
34 file_thread_.reset(new base::Thread("MessageLoopProxyImplTest_File"));
35 io_thread_->Start();
36 file_thread_->Start();
37 }
38
39 virtual void TearDown() {
40 io_thread_->Stop();
41 file_thread_->Stop();
42 }
43
44 static void BasicFunction(MessageLoopProxyImplTest* test) {
45 test->AssertOnFileThread();
46 test->Quit();
47 }
48
49 class DummyTask : public Task {
50 public:
51 explicit DummyTask(bool* deleted) : deleted_(deleted) { }
52 ~DummyTask() {
53 *deleted_ = true;
54 }
55
56 void Run() {
57 FAIL();
58 }
59
60 private:
61 bool* deleted_;
62 };
63
64 class DeletedOnFile {
65 public:
66 explicit DeletedOnFile(MessageLoopProxyImplTest* test) : test_(test) {}
67
68 ~DeletedOnFile() {
69 test_->AssertOnFileThread();
70 test_->Quit();
71 }
72
73 private:
74 MessageLoopProxyImplTest* test_;
75 };
76
77 scoped_ptr<base::Thread> io_thread_;
78 scoped_ptr<base::Thread> file_thread_;
79
80 private:
81 MessageLoop loop_;
82 };
83
84
85 TEST_F(MessageLoopProxyImplTest, PostTask) {
86 EXPECT_TRUE(file_thread_->message_loop_proxy()->PostTask(
87 FROM_HERE, NewRunnableFunction(&BasicFunction, this)));
88 MessageLoop::current()->Run();
89 }
90
91 TEST_F(MessageLoopProxyImplTest, Release) {
92 EXPECT_TRUE(io_thread_->message_loop_proxy()->ReleaseSoon(FROM_HERE, this));
93 MessageLoop::current()->Run();
94 }
95
96 TEST_F(MessageLoopProxyImplTest, Delete) {
97 DeletedOnFile* deleted_on_file = new DeletedOnFile(this);
98 EXPECT_TRUE(file_thread_->message_loop_proxy()->DeleteSoon(
99 FROM_HERE, deleted_on_file));
100 MessageLoop::current()->Run();
101 }
102
103 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadExits) {
104 scoped_ptr<base::Thread> test_thread(
105 new base::Thread("MessageLoopProxyImplTest_Dummy"));
106 test_thread->Start();
107 scoped_refptr<base::MessageLoopProxy> message_loop_proxy =
108 test_thread->message_loop_proxy();
109 test_thread->Stop();
110
111 bool deleted = false;
112 bool ret = message_loop_proxy->PostTask(
113 FROM_HERE, new DummyTask(&deleted));
114 EXPECT_FALSE(ret);
115 EXPECT_TRUE(deleted);
116 }
117
118 TEST_F(MessageLoopProxyImplTest, PostTaskAfterThreadIsDeleted) {
119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy;
120 {
121 scoped_ptr<base::Thread> test_thread(
122 new base::Thread("MessageLoopProxyImplTest_Dummy"));
123 test_thread->Start();
124 message_loop_proxy = test_thread->message_loop_proxy();
125 }
126 bool deleted = false;
127 bool ret = message_loop_proxy->PostTask(FROM_HERE, new DummyTask(&deleted));
128 EXPECT_FALSE(ret);
129 EXPECT_TRUE(deleted);
130 }
131
OLDNEW
« no previous file with comments | « base/message_loop_proxy_impl.cc ('k') | base/thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698