OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 "mojo/system/channel.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "mojo/embedder/platform_channel_pair.h" | |
11 #include "mojo/system/raw_channel.h" | |
12 #include "mojo/system/test_utils.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace mojo { | |
16 namespace system { | |
17 namespace { | |
18 | |
19 enum Tristate { | |
20 TRISTATE_UNKNOWN = -1, | |
21 TRISTATE_FALSE = 0, | |
22 TRISTATE_TRUE = 1 | |
23 }; | |
24 | |
25 Tristate BoolToTristate(bool b) { | |
yzshen1
2014/04/25 05:14:55
optional: It seems more intuitive to use two boole
| |
26 return b ? TRISTATE_TRUE : TRISTATE_FALSE; | |
27 } | |
28 | |
29 class ChannelTest : public testing::Test { | |
30 public: | |
31 ChannelTest() | |
32 : io_thread_(test::TestIOThread::kAutoStart), | |
33 init_result_(TRISTATE_UNKNOWN) {} | |
34 virtual ~ChannelTest() {} | |
35 | |
36 virtual void SetUp() OVERRIDE { | |
37 io_thread_.PostTaskAndWait( | |
38 FROM_HERE, | |
39 base::Bind(&ChannelTest::SetUpOnIOThread, base::Unretained(this))); | |
40 } | |
41 | |
42 void CreateChannelOnIOThread() { | |
43 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
44 channel_ = new Channel(); | |
45 } | |
46 | |
47 void InitChannelOnIOThread() { | |
48 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
49 | |
50 CHECK(raw_channel_); | |
51 CHECK(channel_); | |
52 CHECK_EQ(init_result_, TRISTATE_UNKNOWN); | |
53 | |
54 init_result_ = BoolToTristate(channel_->Init(raw_channel_.Pass())); | |
55 } | |
56 | |
57 void ShutdownChannelOnIOThread() { | |
58 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
59 | |
60 CHECK(channel_); | |
61 channel_->Shutdown(); | |
62 } | |
63 | |
64 test::TestIOThread* io_thread() { return &io_thread_; } | |
65 RawChannel* raw_channel() { return raw_channel_.get(); } | |
66 scoped_ptr<RawChannel>* mutable_raw_channel() { return &raw_channel_; } | |
67 Channel* channel() { return channel_.get(); } | |
68 scoped_refptr<Channel>* mutable_channel() { return &channel_; } | |
69 Tristate init_result() const { return init_result_; } | |
70 | |
71 private: | |
72 void SetUpOnIOThread() { | |
73 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
74 | |
75 embedder::PlatformChannelPair channel_pair; | |
76 raw_channel_ = RawChannel::Create(channel_pair.PassServerHandle()).Pass(); | |
77 other_platform_handle_ = channel_pair.PassClientHandle(); | |
78 } | |
79 | |
80 test::TestIOThread io_thread_; | |
81 scoped_ptr<RawChannel> raw_channel_; | |
82 embedder::ScopedPlatformHandle other_platform_handle_; | |
83 scoped_refptr<Channel> channel_; | |
84 | |
85 Tristate init_result_; | |
86 | |
87 DISALLOW_COPY_AND_ASSIGN(ChannelTest); | |
88 }; | |
89 | |
90 // ChannelTest.InitShutdown ---------------------------------------------------- | |
91 | |
92 TEST_F(ChannelTest, InitShutdown) { | |
93 io_thread()->PostTaskAndWait( | |
94 FROM_HERE, | |
95 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
96 base::Unretained(this))); | |
97 ASSERT_TRUE(channel()); | |
98 | |
99 io_thread()->PostTaskAndWait( | |
100 FROM_HERE, | |
101 base::Bind(&ChannelTest::InitChannelOnIOThread, | |
102 base::Unretained(this))); | |
103 EXPECT_EQ(TRISTATE_TRUE, init_result()); | |
104 | |
105 io_thread()->PostTaskAndWait( | |
106 FROM_HERE, | |
107 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | |
108 base::Unretained(this))); | |
109 | |
110 // Okay to destroy |Channel| on not-the-I/O-thread. | |
111 EXPECT_TRUE(channel()->HasOneRef()); | |
112 *mutable_channel() = NULL; | |
113 } | |
114 | |
115 // ChannelTest.InitFails ------------------------------------------------------- | |
116 | |
117 class MockRawChannelOnInitFails : public RawChannel { | |
118 public: | |
119 MockRawChannelOnInitFails() : on_init_called_(false) {} | |
120 virtual ~MockRawChannelOnInitFails() {} | |
121 | |
122 virtual IOResult Read(size_t*) OVERRIDE { | |
123 CHECK(false); | |
124 return IO_FAILED; | |
125 } | |
126 virtual IOResult ScheduleRead() OVERRIDE { | |
127 CHECK(false); | |
128 return IO_FAILED; | |
129 } | |
130 virtual IOResult WriteNoLock(size_t*) OVERRIDE { | |
131 CHECK(false); | |
132 return IO_FAILED; | |
133 } | |
134 virtual IOResult ScheduleWriteNoLock() OVERRIDE { | |
135 CHECK(false); | |
136 return IO_FAILED; | |
137 } | |
138 virtual bool OnInit() OVERRIDE { | |
139 EXPECT_FALSE(on_init_called_); | |
140 on_init_called_ = true; | |
141 return false; | |
142 } | |
143 virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer>, | |
144 scoped_ptr<WriteBuffer>) OVERRIDE { | |
145 CHECK(false); | |
146 } | |
147 | |
148 private: | |
149 bool on_init_called_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(MockRawChannelOnInitFails); | |
152 }; | |
153 | |
154 TEST_F(ChannelTest, InitFails) { | |
155 io_thread()->PostTaskAndWait( | |
156 FROM_HERE, | |
157 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
158 base::Unretained(this))); | |
159 ASSERT_TRUE(channel()); | |
160 | |
161 ASSERT_TRUE(raw_channel()); | |
162 mutable_raw_channel()->reset(new MockRawChannelOnInitFails()); | |
163 | |
164 io_thread()->PostTaskAndWait( | |
165 FROM_HERE, | |
166 base::Bind(&ChannelTest::InitChannelOnIOThread, | |
167 base::Unretained(this))); | |
168 EXPECT_EQ(TRISTATE_FALSE, init_result()); | |
169 | |
170 // Should destroy |Channel| with no |Shutdown()| (on not-the-I/O-thread). | |
171 EXPECT_TRUE(channel()->HasOneRef()); | |
172 *mutable_channel() = NULL; | |
173 } | |
174 | |
175 // TODO(vtl): More. | |
176 | |
177 } // namespace | |
178 } // namespace system | |
179 } // namespace mojo | |
OLD | NEW |