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 <string> 6 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" 12 #include "mojo/public/cpp/utility/run_loop.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace mojo { 16 namespace { 17 18 class TestAsyncWaitCallback { 19 public: 20 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { 21 } 22 virtual ~TestAsyncWaitCallback() {} 23 24 int result_count() const { return result_count_; } 25 26 MojoResult last_result() const { return last_result_; } 27 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; 33 } 34 35 private: 36 int result_count_; 37 MojoResult last_result_; 38 39 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); 40 }; 41 42 MojoAsyncWaitID CallAsyncWait(const Handle& handle, 43 MojoHandleSignals signals, 44 TestAsyncWaitCallback* callback) { 45 return Environment::GetDefaultAsyncWaiter()->AsyncWait( 46 handle.value(), 47 signals, 48 MOJO_DEADLINE_INDEFINITE, 49 &TestAsyncWaitCallback::OnHandleReady, 50 callback); 51 } 52 53 void CallCancelWait(MojoAsyncWaitID wait_id) { 54 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id); 55 } 56 57 class AsyncWaiterTest : public testing::Test { 58 public: 59 AsyncWaiterTest() {} 60 61 private: 62 Environment environment_; 63 RunLoop run_loop_; 64 65 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); 66 }; 67 68 // Verifies AsyncWaitCallback is notified when pipe is ready. 69 TEST_F(AsyncWaiterTest, CallbackNotified) { 70 TestAsyncWaitCallback callback; 71 MessagePipe test_pipe; 72 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); 73 74 CallAsyncWait(test_pipe.handle0.get(), 75 MOJO_HANDLE_SIGNAL_READABLE, 76 &callback); 77 RunLoop::current()->Run(); 78 EXPECT_EQ(1, callback.result_count()); 79 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); 80 } 81 82 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. 83 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { 84 TestAsyncWaitCallback callback1; 85 TestAsyncWaitCallback callback2; 86 MessagePipe test_pipe1; 87 MessagePipe test_pipe2; 88 EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string())); 89 EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string())); 90 91 CallAsyncWait(test_pipe1.handle0.get(), 92 MOJO_HANDLE_SIGNAL_READABLE, 93 &callback1); 94 CallAsyncWait(test_pipe2.handle0.get(), 95 MOJO_HANDLE_SIGNAL_READABLE, 96 &callback2); 97 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()); 103 } 104 105 // Verifies cancel works. 106 TEST_F(AsyncWaiterTest, CancelCallback) { 107 TestAsyncWaitCallback callback; 108 MessagePipe test_pipe; 109 EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string())); 110 111 CallCancelWait( 112 CallAsyncWait(test_pipe.handle0.get(), 113 MOJO_HANDLE_SIGNAL_READABLE, 114 &callback)); 115 RunLoop::current()->Run(); 116 EXPECT_EQ(0, callback.result_count()); 117 } 118 119 } // namespace 120 } // namespace mojo 121