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/public/cpp/bindings/callback.h" | |
6 #include "mojo/public/cpp/environment/async_waiter.h" | |
7 #include "mojo/public/cpp/test_support/test_utils.h" | |
8 #include "mojo/public/cpp/utility/run_loop.h" | |
9 #include "testing/gtest/include/gtest/gtest.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 Environment environment_; | |
53 RunLoop run_loop_; | |
54 | |
55 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); | |
56 }; | |
57 | |
58 // Verifies AsyncWaitCallback is notified when pipe is ready. | |
59 TEST_F(AsyncWaiterTest, CallbackNotified) { | |
60 TestAsyncWaitCallback callback; | |
61 MessagePipe test_pipe; | |
62 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
63 | |
64 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
65 ManualCallback(&callback)); | |
66 RunLoop::current()->Run(); | |
67 EXPECT_EQ(1, callback.result_count()); | |
68 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | |
69 } | |
70 | |
71 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | |
72 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { | |
73 TestAsyncWaitCallback callback1; | |
74 TestAsyncWaitCallback callback2; | |
75 MessagePipe test_pipe1; | |
76 MessagePipe test_pipe2; | |
77 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); | |
78 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); | |
79 | |
80 AsyncWaiter waiter1(test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
81 ManualCallback(&callback1)); | |
82 AsyncWaiter waiter2(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
83 ManualCallback(&callback2)); | |
84 | |
85 RunLoop::current()->Run(); | |
86 EXPECT_EQ(1, callback1.result_count()); | |
87 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | |
88 EXPECT_EQ(1, callback2.result_count()); | |
89 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | |
90 } | |
91 | |
92 // Verifies cancel works. | |
93 TEST_F(AsyncWaiterTest, CancelCallback) { | |
94 TestAsyncWaitCallback callback; | |
95 MessagePipe test_pipe; | |
96 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | |
97 | |
98 { | |
99 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
100 ManualCallback(&callback)); | |
101 } | |
102 RunLoop::current()->Run(); | |
103 EXPECT_EQ(0, callback.result_count()); | |
104 } | |
105 | |
106 } // namespace | |
107 } // namespace mojo | |
OLD | NEW |