OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include "mojo/public/cpp/bindings/callback.h" |
6 | 6 #include "mojo/public/cpp/environment/async_waiter.h" |
7 #include "mojo/public/c/environment/async_waiter.h" | |
8 #include "mojo/public/cpp/environment/environment.h" | |
9 #include "mojo/public/cpp/system/core.h" | |
10 #include "mojo/public/cpp/system/macros.h" | |
11 #include "mojo/public/cpp/test_support/test_utils.h" | 7 #include "mojo/public/cpp/test_support/test_utils.h" |
12 #include "mojo/public/cpp/utility/run_loop.h" | 8 #include "mojo/public/cpp/utility/run_loop.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
14 | 10 |
15 namespace mojo { | 11 namespace mojo { |
16 namespace { | 12 namespace { |
17 | 13 |
18 class TestAsyncWaitCallback { | 14 class TestAsyncWaitCallback { |
19 public: | 15 public: |
20 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {} | 16 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {} |
21 ~TestAsyncWaitCallback() {} | 17 ~TestAsyncWaitCallback() {} |
22 | 18 |
23 int result_count() const { return result_count_; } | 19 int result_count() const { return result_count_; } |
24 | 20 |
25 MojoResult last_result() const { return last_result_; } | 21 MojoResult last_result() const { return last_result_; } |
26 | 22 |
27 // MojoAsyncWaitCallback: | 23 void OnHandleReady(MojoResult result) { |
28 static void OnHandleReady(void* closure, MojoResult result) { | 24 result_count_++; |
29 TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure); | 25 last_result_ = result; |
30 self->result_count_++; | |
31 self->last_result_ = result; | |
32 } | 26 } |
33 | 27 |
34 private: | 28 private: |
35 int result_count_; | 29 int result_count_; |
36 MojoResult last_result_; | 30 MojoResult last_result_; |
37 | 31 |
38 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | 32 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); |
39 }; | 33 }; |
40 | 34 |
41 MojoAsyncWaitID CallAsyncWait(const Handle& handle, | 35 // Manual code to create a callback since we don't have mojo::Bind yet. |
42 MojoHandleSignals signals, | 36 class ManualCallback { |
43 TestAsyncWaitCallback* callback) { | 37 public: |
44 return Environment::GetDefaultAsyncWaiter()->AsyncWait( | 38 explicit ManualCallback(TestAsyncWaitCallback* callback) |
45 handle.value(), | 39 : callback_(callback) {} |
46 signals, | |
47 MOJO_DEADLINE_INDEFINITE, | |
48 &TestAsyncWaitCallback::OnHandleReady, | |
49 callback); | |
50 } | |
51 | 40 |
52 void CallCancelWait(MojoAsyncWaitID wait_id) { | 41 void Run(MojoResult result) const { callback_->OnHandleReady(result); } |
53 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id); | 42 |
54 } | 43 private: |
| 44 TestAsyncWaitCallback* callback_; |
| 45 }; |
55 | 46 |
56 class AsyncWaiterTest : public testing::Test { | 47 class AsyncWaiterTest : public testing::Test { |
57 public: | 48 public: |
58 AsyncWaiterTest() {} | 49 AsyncWaiterTest() {} |
59 | 50 |
60 private: | 51 private: |
61 Environment environment_; | 52 Environment environment_; |
62 RunLoop run_loop_; | 53 RunLoop run_loop_; |
63 | 54 |
64 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); | 55 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); |
65 }; | 56 }; |
66 | 57 |
67 // Verifies AsyncWaitCallback is notified when pipe is ready. | 58 // Verifies AsyncWaitCallback is notified when pipe is ready. |
68 TEST_F(AsyncWaiterTest, CallbackNotified) { | 59 TEST_F(AsyncWaiterTest, CallbackNotified) { |
69 TestAsyncWaitCallback callback; | 60 TestAsyncWaitCallback callback; |
70 MessagePipe test_pipe; | 61 MessagePipe test_pipe; |
71 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | 62 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); |
72 | 63 |
73 CallAsyncWait( | 64 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, |
74 test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback); | 65 ManualCallback(&callback)); |
75 RunLoop::current()->Run(); | 66 RunLoop::current()->Run(); |
76 EXPECT_EQ(1, callback.result_count()); | 67 EXPECT_EQ(1, callback.result_count()); |
77 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | 68 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); |
78 } | 69 } |
79 | 70 |
80 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | 71 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. |
81 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { | 72 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { |
82 TestAsyncWaitCallback callback1; | 73 TestAsyncWaitCallback callback1; |
83 TestAsyncWaitCallback callback2; | 74 TestAsyncWaitCallback callback2; |
84 MessagePipe test_pipe1; | 75 MessagePipe test_pipe1; |
85 MessagePipe test_pipe2; | 76 MessagePipe test_pipe2; |
86 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); | 77 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); |
87 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); | 78 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); |
88 | 79 |
89 CallAsyncWait( | 80 AsyncWaiter waiter1(test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, |
90 test_pipe1.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback1); | 81 ManualCallback(&callback1)); |
91 CallAsyncWait( | 82 AsyncWaiter waiter2(test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, |
92 test_pipe2.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback2); | 83 ManualCallback(&callback2)); |
93 | 84 |
94 RunLoop::current()->Run(); | 85 RunLoop::current()->Run(); |
95 EXPECT_EQ(1, callback1.result_count()); | 86 EXPECT_EQ(1, callback1.result_count()); |
96 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | 87 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); |
97 EXPECT_EQ(1, callback2.result_count()); | 88 EXPECT_EQ(1, callback2.result_count()); |
98 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | 89 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); |
99 } | 90 } |
100 | 91 |
101 // Verifies cancel works. | 92 // Verifies cancel works. |
102 TEST_F(AsyncWaiterTest, CancelCallback) { | 93 TEST_F(AsyncWaiterTest, CancelCallback) { |
103 TestAsyncWaitCallback callback; | 94 TestAsyncWaitCallback callback; |
104 MessagePipe test_pipe; | 95 MessagePipe test_pipe; |
105 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); | 96 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); |
106 | 97 |
107 CallCancelWait(CallAsyncWait( | 98 { |
108 test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, &callback)); | 99 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 100 ManualCallback(&callback)); |
| 101 } |
109 RunLoop::current()->Run(); | 102 RunLoop::current()->Run(); |
110 EXPECT_EQ(0, callback.result_count()); | 103 EXPECT_EQ(0, callback.result_count()); |
111 } | 104 } |
112 | 105 |
113 } // namespace | 106 } // namespace |
114 } // namespace mojo | 107 } // namespace mojo |
OLD | NEW |