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.
5 #include "mojo/public/environment/default_async_waiter.h"
7 #include "mojo/public/environment/environment.h"
8 #include "mojo/public/system/core_cpp.h"
9 #include "mojo/public/system/macros.h"
10 #include "mojo/public/tests/test_support.h"
11 #include "mojo/public/utility/run_loop.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 class TestAsyncWaitCallback {
19 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {
21 virtual ~TestAsyncWaitCallback() {}
23 int result_count() const { return result_count_; }
25 MojoResult last_result() const { return last_result_; }
27 // MojoAsyncWaitCallback:
28 static void OnHandleReady(void* closure, MojoResult result) {
29 TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure);
30 self->result_count_++;
31 self->last_result_ = result;
36 MojoResult last_result_;
38 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback);
41 MojoAsyncWaitID CallAsyncWait(const Handle& handle,
43 TestAsyncWaitCallback* callback) {
44 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter();
45 return waiter->AsyncWait(waiter,
48 MOJO_DEADLINE_INDEFINITE,
49 &TestAsyncWaitCallback::OnHandleReady,
53 void CallCancelWait(MojoAsyncWaitID wait_id) {
54 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter();
55 waiter->CancelWait(waiter, wait_id);
58 class AsyncWaiterTest : public testing::Test {
63 Environment environment_;
66 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest);
69 // Verifies AsyncWaitCallback is notified when pipe is ready.
70 TEST_F(AsyncWaiterTest, CallbackNotified) {
71 TestAsyncWaitCallback callback;
72 MessagePipe test_pipe;
73 EXPECT_EQ(MOJO_RESULT_OK,
74 test::WriteEmptyMessage(test_pipe.handle1.get()));
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_EQ(MOJO_RESULT_OK,
91 test::WriteEmptyMessage(test_pipe1.handle1.get()));
92 EXPECT_EQ(MOJO_RESULT_OK,
93 test::WriteEmptyMessage(test_pipe2.handle1.get()));
95 CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1);
96 CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2);
98 RunLoop::current()->Run();
99 EXPECT_EQ(1, callback1.result_count());
100 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result());
101 EXPECT_EQ(1, callback2.result_count());
102 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result());
105 // Verifies cancel works.
106 TEST_F(AsyncWaiterTest, CancelCallback) {
107 TestAsyncWaitCallback callback;
108 MessagePipe test_pipe;
109 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get()));
112 CallAsyncWait(test_pipe.handle0.get(),
113 MOJO_WAIT_FLAG_READABLE,
115 RunLoop::current()->Run();
116 EXPECT_EQ(0, callback.result_count());