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.
7 #include "mojo/public/cpp/environment/default_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"
12 #include "mojo/public/cpp/utility/run_loop.h"
13 #include "testing/gtest/include/gtest/gtest.h"
18 class TestAsyncWaitCallback
{
20 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK
) {
22 virtual ~TestAsyncWaitCallback() {}
24 int result_count() const { return result_count_
; }
26 MojoResult
last_result() const { return last_result_
; }
28 // MojoAsyncWaitCallback:
29 static void OnHandleReady(void* closure
, MojoResult result
) {
30 TestAsyncWaitCallback
* self
= static_cast<TestAsyncWaitCallback
*>(closure
);
31 self
->result_count_
++;
32 self
->last_result_
= result
;
37 MojoResult last_result_
;
39 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback
);
42 MojoAsyncWaitID
CallAsyncWait(const Handle
& handle
,
44 TestAsyncWaitCallback
* callback
) {
45 MojoAsyncWaiter
* waiter
= GetDefaultAsyncWaiter();
46 return waiter
->AsyncWait(waiter
,
49 MOJO_DEADLINE_INDEFINITE
,
50 &TestAsyncWaitCallback::OnHandleReady
,
54 void CallCancelWait(MojoAsyncWaitID wait_id
) {
55 MojoAsyncWaiter
* waiter
= GetDefaultAsyncWaiter();
56 waiter
->CancelWait(waiter
, wait_id
);
59 class AsyncWaiterTest
: public testing::Test
{
64 Environment environment_
;
67 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest
);
70 // Verifies AsyncWaitCallback is notified when pipe is ready.
71 TEST_F(AsyncWaiterTest
, CallbackNotified
) {
72 TestAsyncWaitCallback callback
;
73 MessagePipe test_pipe
;
74 EXPECT_TRUE(test::WriteTextMessage(test_pipe
.handle1
.get(), std::string()));
76 CallAsyncWait(test_pipe
.handle0
.get(),
77 MOJO_WAIT_FLAG_READABLE
,
79 RunLoop::current()->Run();
80 EXPECT_EQ(1, callback
.result_count());
81 EXPECT_EQ(MOJO_RESULT_OK
, callback
.last_result());
84 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready.
85 TEST_F(AsyncWaiterTest
, TwoCallbacksNotified
) {
86 TestAsyncWaitCallback callback1
;
87 TestAsyncWaitCallback callback2
;
88 MessagePipe test_pipe1
;
89 MessagePipe test_pipe2
;
90 EXPECT_TRUE(test::WriteTextMessage(test_pipe1
.handle1
.get(), std::string()));
91 EXPECT_TRUE(test::WriteTextMessage(test_pipe2
.handle1
.get(), std::string()));
93 CallAsyncWait(test_pipe1
.handle0
.get(), MOJO_WAIT_FLAG_READABLE
, &callback1
);
94 CallAsyncWait(test_pipe2
.handle0
.get(), MOJO_WAIT_FLAG_READABLE
, &callback2
);
96 RunLoop::current()->Run();
97 EXPECT_EQ(1, callback1
.result_count());
98 EXPECT_EQ(MOJO_RESULT_OK
, callback1
.last_result());
99 EXPECT_EQ(1, callback2
.result_count());
100 EXPECT_EQ(MOJO_RESULT_OK
, callback2
.last_result());
103 // Verifies cancel works.
104 TEST_F(AsyncWaiterTest
, CancelCallback
) {
105 TestAsyncWaitCallback callback
;
106 MessagePipe test_pipe
;
107 EXPECT_TRUE(test::WriteTextMessage(test_pipe
.handle1
.get(), std::string()));
110 CallAsyncWait(test_pipe
.handle0
.get(),
111 MOJO_WAIT_FLAG_READABLE
,
113 RunLoop::current()->Run();
114 EXPECT_EQ(0, callback
.result_count());