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 "gtest/gtest.h" | |
6 #include "mojo/public/cpp/bindings/callback.h" | |
7 #include "mojo/public/cpp/environment/async_waiter.h" | |
8 #include "mojo/public/cpp/test_support/test_utils.h" | |
9 #include "mojo/public/cpp/utility/run_loop.h" | |
10 | |
11 namespace mojo { | |
12 namespace { | |
13 | |
14 class TestAsyncWaitCallback { | |
15 public: | |
16 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {} | |
17 ~TestAsyncWaitCallback() {} | |
18 | |
19 int result_count() const { return result_count_; } | |
20 | |
21 MojoResult last_result() const { return last_result_; } | |
22 | |
23 void OnHandleReady(MojoResult result) { | |
24 result_count_++; | |
25 last_result_ = result; | |
26 } | |
27 | |
28 private: | |
29 int result_count_; | |
30 MojoResult last_result_; | |
31 | |
32 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | |
33 }; | |
34 | |
35 // Manual code to create a callback since we don't have mojo::Bind yet. | |
36 class ManualCallback { | |
37 public: | |
38 explicit ManualCallback(TestAsyncWaitCallback* callback) | |
39 : callback_(callback) {} | |
40 | |
41 void Run(MojoResult result) const { callback_->OnHandleReady(result); } | |
42 | |
43 private: | |
44 TestAsyncWaitCallback* callback_; | |
45 }; | |
46 | |
47 class AsyncWaiterTest : public testing::Test { | |
48 public: | |
49 AsyncWaiterTest() {} | |
50 | |
51 private: | |
52 RunLoop run_loop_; | |
53 | |
54 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); | |
55 }; | |
56 | |
57 // Verifies AsyncWaitCallback is notified when pipe is ready. | |
58 TEST_F(AsyncWaiterTest, CallbackNotified) { | |
59 TestAsyncWaitCallback callback; | |
60 MessagePipe test_pipe; | |
61 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
62 | |
63 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
64 ManualCallback(&callback)); | |
65 RunLoop::current()->Run(); | |
66 EXPECT_EQ(1, callback.result_count()); | |
67 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | |
68 } | |
69 | |
70 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | |
71 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { | |
72 TestAsyncWaitCallback callback1; | |
73 TestAsyncWaitCallback callback2; | |
74 MessagePipe test_pipe1; | |
75 MessagePipe test_pipe2; | |
76 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); | |
77 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); | |
78 | |
79 AsyncWaiter waiter1(test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
80 ManualCallback(&callback1)); | |
81 AsyncWaiter waiter2(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
82 ManualCallback(&callback2)); | |
83 | |
84 RunLoop::current()->Run(); | |
85 EXPECT_EQ(1, callback1.result_count()); | |
86 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | |
87 EXPECT_EQ(1, callback2.result_count()); | |
88 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | |
89 } | |
90 | |
91 // Verifies cancel works. | |
92 TEST_F(AsyncWaiterTest, CancelCallback) { | |
93 TestAsyncWaitCallback callback; | |
94 MessagePipe test_pipe; | |
95 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
96 | |
97 { | |
98 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
99 ManualCallback(&callback)); | |
100 } | |
101 RunLoop::current()->Run(); | |
102 EXPECT_EQ(0, callback.result_count()); | |
103 } | |
104 | |
105 } // namespace | |
106 } // namespace mojo | |
OLD | NEW |