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/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"
14 class TestAsyncWaitCallback {
16 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {}
17 ~TestAsyncWaitCallback() {}
19 int result_count() const { return result_count_; }
21 MojoResult last_result() const { return last_result_; }
23 void OnHandleReady(MojoResult result) {
25 last_result_ = result;
30 MojoResult last_result_;
32 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback);
35 // Manual code to create a callback since we don't have mojo::Bind yet.
36 class ManualCallback {
38 explicit ManualCallback(TestAsyncWaitCallback* callback)
39 : callback_(callback) {}
41 void Run(MojoResult result) const { callback_->OnHandleReady(result); }
44 TestAsyncWaitCallback* callback_;
47 class AsyncWaiterTest : public testing::Test {
52 Environment environment_;
55 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest);
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()));
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());
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()));
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));
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());
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()));
99 AsyncWaiter waiter(test_pipe.handle0.get(), MOJO_HANDLE_SIGNAL_READABLE,
100 ManualCallback(&callback));
102 RunLoop::current()->Run();
103 EXPECT_EQ(0, callback.result_count());