| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/public/utility/bindings_support_impl.h" | 5 #include "mojo/public/environment/async_waiter.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "mojo/public/environment/environment.h" |
| 8 #include "mojo/public/system/core_cpp.h" | 8 #include "mojo/public/system/core_cpp.h" |
| 9 #include "mojo/public/tests/test_support.h" | 9 #include "mojo/public/tests/test_support.h" |
| 10 #include "mojo/public/utility/environment.h" | |
| 11 #include "mojo/public/utility/run_loop.h" | 10 #include "mojo/public/utility/run_loop.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 namespace { | 14 namespace { |
| 16 | 15 |
| 17 class TestAsyncWaitCallback : public BindingsSupport::AsyncWaitCallback { | 16 class TestAsyncWaitCallback { |
| 18 public: | 17 public: |
| 19 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { | 18 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { |
| 20 } | 19 } |
| 21 virtual ~TestAsyncWaitCallback() {} | 20 virtual ~TestAsyncWaitCallback() {} |
| 22 | 21 |
| 23 int result_count() const { return result_count_; } | 22 int result_count() const { return result_count_; } |
| 24 | 23 |
| 25 MojoResult last_result() const { return last_result_; } | 24 MojoResult last_result() const { return last_result_; } |
| 26 | 25 |
| 27 // RunLoopHandler: | 26 // MojoAsyncWaitCallback: |
| 28 virtual void OnHandleReady(MojoResult result) OVERRIDE { | 27 static void OnHandleReady(uintptr_t user_data, MojoResult result) { |
| 29 result_count_++; | 28 TestAsyncWaitCallback* self = |
| 30 last_result_ = result; | 29 reinterpret_cast<TestAsyncWaitCallback*>(user_data); |
| 30 self->result_count_++; |
| 31 self->last_result_ = result; |
| 31 } | 32 } |
| 32 | 33 |
| 33 private: | 34 private: |
| 34 int result_count_; | 35 int result_count_; |
| 35 MojoResult last_result_; | 36 MojoResult last_result_; |
| 36 | 37 |
| 37 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | 38 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 class BindingsSupportImplTest : public testing::Test { | 41 MojoAsyncWaitID CallAsyncWait(const Handle& handle, |
| 42 MojoWaitFlags flags, |
| 43 TestAsyncWaitCallback* callback) { |
| 44 MojoAsyncWaiter* waiter = MojoGetDefaultAsyncWaiter(); |
| 45 return waiter->AsyncWait(waiter, |
| 46 handle.value(), |
| 47 flags, |
| 48 MOJO_DEADLINE_INDEFINITE, |
| 49 &TestAsyncWaitCallback::OnHandleReady, |
| 50 reinterpret_cast<uintptr_t>(callback)); |
| 51 } |
| 52 |
| 53 void CallCancelWait(MojoAsyncWaitID wait_id) { |
| 54 MojoAsyncWaiter* waiter = MojoGetDefaultAsyncWaiter(); |
| 55 waiter->CancelWait(waiter, wait_id); |
| 56 } |
| 57 |
| 58 class AsyncWaiterTest : public testing::Test { |
| 41 public: | 59 public: |
| 42 BindingsSupportImplTest() {} | 60 AsyncWaiterTest() {} |
| 43 | 61 |
| 44 virtual void SetUp() OVERRIDE { | 62 virtual void SetUp() OVERRIDE { |
| 45 Test::SetUp(); | 63 Test::SetUp(); |
| 46 environment_.reset(new Environment); | 64 environment_.reset(new Environment); |
| 47 run_loop_.reset(new RunLoop); | 65 run_loop_.reset(new RunLoop); |
| 48 } | 66 } |
| 49 virtual void TearDown() OVERRIDE { | 67 virtual void TearDown() OVERRIDE { |
| 50 run_loop_.reset(); | 68 run_loop_.reset(); |
| 51 environment_.reset(NULL); | 69 environment_.reset(NULL); |
| 52 Test::TearDown(); | 70 Test::TearDown(); |
| 53 } | 71 } |
| 54 | 72 |
| 55 private: | 73 private: |
| 56 scoped_ptr<Environment> environment_; | 74 scoped_ptr<Environment> environment_; |
| 57 scoped_ptr<RunLoop> run_loop_; | 75 scoped_ptr<RunLoop> run_loop_; |
| 58 | 76 |
| 59 DISALLOW_COPY_AND_ASSIGN(BindingsSupportImplTest); | 77 DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); |
| 60 }; | 78 }; |
| 61 | 79 |
| 62 // Verifies AsyncWaitCallback is notified when pipe is ready. | 80 // Verifies AsyncWaitCallback is notified when pipe is ready. |
| 63 TEST_F(BindingsSupportImplTest, CallbackNotified) { | 81 TEST_F(AsyncWaiterTest, CallbackNotified) { |
| 64 TestAsyncWaitCallback callback; | 82 TestAsyncWaitCallback callback; |
| 65 MessagePipe test_pipe; | 83 MessagePipe test_pipe; |
| 66 EXPECT_EQ(MOJO_RESULT_OK, | 84 EXPECT_EQ(MOJO_RESULT_OK, |
| 67 test::WriteEmptyMessage(test_pipe.handle1.get())); | 85 test::WriteEmptyMessage(test_pipe.handle1.get())); |
| 68 | 86 |
| 69 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | 87 CallAsyncWait(test_pipe.handle0.get(), |
| 70 MOJO_WAIT_FLAG_READABLE, &callback); | 88 MOJO_WAIT_FLAG_READABLE, |
| 89 &callback); |
| 71 RunLoop::current()->Run(); | 90 RunLoop::current()->Run(); |
| 72 EXPECT_EQ(1, callback.result_count()); | 91 EXPECT_EQ(1, callback.result_count()); |
| 73 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | 92 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); |
| 74 } | 93 } |
| 75 | 94 |
| 76 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | 95 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. |
| 77 TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { | 96 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { |
| 78 TestAsyncWaitCallback callback1; | 97 TestAsyncWaitCallback callback1; |
| 79 TestAsyncWaitCallback callback2; | 98 TestAsyncWaitCallback callback2; |
| 80 MessagePipe test_pipe1; | 99 MessagePipe test_pipe1; |
| 81 MessagePipe test_pipe2; | 100 MessagePipe test_pipe2; |
| 82 EXPECT_EQ(MOJO_RESULT_OK, | 101 EXPECT_EQ(MOJO_RESULT_OK, |
| 83 test::WriteEmptyMessage(test_pipe1.handle1.get())); | 102 test::WriteEmptyMessage(test_pipe1.handle1.get())); |
| 84 EXPECT_EQ(MOJO_RESULT_OK, | 103 EXPECT_EQ(MOJO_RESULT_OK, |
| 85 test::WriteEmptyMessage(test_pipe2.handle1.get())); | 104 test::WriteEmptyMessage(test_pipe2.handle1.get())); |
| 86 | 105 |
| 87 BindingsSupport::Get()->AsyncWait(test_pipe1.handle0.get(), | 106 CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1); |
| 88 MOJO_WAIT_FLAG_READABLE, &callback1); | 107 CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2); |
| 89 BindingsSupport::Get()->AsyncWait(test_pipe2.handle0.get(), | 108 |
| 90 MOJO_WAIT_FLAG_READABLE, &callback2); | |
| 91 RunLoop::current()->Run(); | 109 RunLoop::current()->Run(); |
| 92 EXPECT_EQ(1, callback1.result_count()); | 110 EXPECT_EQ(1, callback1.result_count()); |
| 93 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | 111 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); |
| 94 EXPECT_EQ(1, callback2.result_count()); | 112 EXPECT_EQ(1, callback2.result_count()); |
| 95 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | 113 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); |
| 96 } | 114 } |
| 97 | 115 |
| 98 // Verifies cancel works. | 116 // Verifies cancel works. |
| 99 TEST_F(BindingsSupportImplTest, CancelCallback) { | 117 TEST_F(AsyncWaiterTest, CancelCallback) { |
| 100 TestAsyncWaitCallback callback; | 118 TestAsyncWaitCallback callback; |
| 101 MessagePipe test_pipe; | 119 MessagePipe test_pipe; |
| 102 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | 120 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); |
| 103 | 121 |
| 104 BindingsSupport::Get()->CancelWait( | 122 CallCancelWait( |
| 105 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | 123 CallAsyncWait(test_pipe.handle0.get(), |
| 106 MOJO_WAIT_FLAG_READABLE, &callback)); | 124 MOJO_WAIT_FLAG_READABLE, |
| 125 &callback)); |
| 107 RunLoop::current()->Run(); | 126 RunLoop::current()->Run(); |
| 108 EXPECT_EQ(0, callback.result_count()); | 127 EXPECT_EQ(0, callback.result_count()); |
| 109 } | 128 } |
| 110 | 129 |
| 111 } // namespace | 130 } // namespace |
| 112 } // namespace mojo | 131 } // namespace mojo |
| OLD | NEW |