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/default_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(void* closure, MojoResult result) { |
29 result_count_++; | 28 TestAsyncWaitCallback* self = static_cast<TestAsyncWaitCallback*>(closure); |
30 last_result_ = result; | 29 self->result_count_++; |
| 30 self->last_result_ = result; |
31 } | 31 } |
32 | 32 |
33 private: | 33 private: |
34 int result_count_; | 34 int result_count_; |
35 MojoResult last_result_; | 35 MojoResult last_result_; |
36 | 36 |
37 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | 37 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); |
38 }; | 38 }; |
39 | 39 |
40 class BindingsSupportImplTest : public testing::Test { | 40 MojoAsyncWaitID CallAsyncWait(const Handle& handle, |
| 41 MojoWaitFlags flags, |
| 42 TestAsyncWaitCallback* callback) { |
| 43 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); |
| 44 return waiter->AsyncWait(waiter, |
| 45 handle.value(), |
| 46 flags, |
| 47 MOJO_DEADLINE_INDEFINITE, |
| 48 &TestAsyncWaitCallback::OnHandleReady, |
| 49 callback); |
| 50 } |
| 51 |
| 52 void CallCancelWait(MojoAsyncWaitID wait_id) { |
| 53 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter(); |
| 54 waiter->CancelWait(waiter, wait_id); |
| 55 } |
| 56 |
| 57 class AsyncWaiterTest : public testing::Test { |
41 public: | 58 public: |
42 BindingsSupportImplTest() {} | 59 AsyncWaiterTest() {} |
43 | 60 |
44 virtual void SetUp() OVERRIDE { | 61 virtual void SetUp() OVERRIDE { |
45 Test::SetUp(); | 62 Test::SetUp(); |
46 environment_.reset(new Environment); | 63 environment_.reset(new Environment); |
47 run_loop_.reset(new RunLoop); | 64 run_loop_.reset(new RunLoop); |
48 } | 65 } |
49 virtual void TearDown() OVERRIDE { | 66 virtual void TearDown() OVERRIDE { |
50 run_loop_.reset(); | 67 run_loop_.reset(); |
51 environment_.reset(NULL); | 68 environment_.reset(NULL); |
52 Test::TearDown(); | 69 Test::TearDown(); |
53 } | 70 } |
54 | 71 |
55 private: | 72 private: |
56 scoped_ptr<Environment> environment_; | 73 scoped_ptr<Environment> environment_; |
57 scoped_ptr<RunLoop> run_loop_; | 74 scoped_ptr<RunLoop> run_loop_; |
58 | 75 |
59 DISALLOW_COPY_AND_ASSIGN(BindingsSupportImplTest); | 76 DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest); |
60 }; | 77 }; |
61 | 78 |
62 // Verifies AsyncWaitCallback is notified when pipe is ready. | 79 // Verifies AsyncWaitCallback is notified when pipe is ready. |
63 TEST_F(BindingsSupportImplTest, CallbackNotified) { | 80 TEST_F(AsyncWaiterTest, CallbackNotified) { |
64 TestAsyncWaitCallback callback; | 81 TestAsyncWaitCallback callback; |
65 MessagePipe test_pipe; | 82 MessagePipe test_pipe; |
66 EXPECT_EQ(MOJO_RESULT_OK, | 83 EXPECT_EQ(MOJO_RESULT_OK, |
67 test::WriteEmptyMessage(test_pipe.handle1.get())); | 84 test::WriteEmptyMessage(test_pipe.handle1.get())); |
68 | 85 |
69 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | 86 CallAsyncWait(test_pipe.handle0.get(), |
70 MOJO_WAIT_FLAG_READABLE, &callback); | 87 MOJO_WAIT_FLAG_READABLE, |
| 88 &callback); |
71 RunLoop::current()->Run(); | 89 RunLoop::current()->Run(); |
72 EXPECT_EQ(1, callback.result_count()); | 90 EXPECT_EQ(1, callback.result_count()); |
73 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | 91 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); |
74 } | 92 } |
75 | 93 |
76 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | 94 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. |
77 TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { | 95 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) { |
78 TestAsyncWaitCallback callback1; | 96 TestAsyncWaitCallback callback1; |
79 TestAsyncWaitCallback callback2; | 97 TestAsyncWaitCallback callback2; |
80 MessagePipe test_pipe1; | 98 MessagePipe test_pipe1; |
81 MessagePipe test_pipe2; | 99 MessagePipe test_pipe2; |
82 EXPECT_EQ(MOJO_RESULT_OK, | 100 EXPECT_EQ(MOJO_RESULT_OK, |
83 test::WriteEmptyMessage(test_pipe1.handle1.get())); | 101 test::WriteEmptyMessage(test_pipe1.handle1.get())); |
84 EXPECT_EQ(MOJO_RESULT_OK, | 102 EXPECT_EQ(MOJO_RESULT_OK, |
85 test::WriteEmptyMessage(test_pipe2.handle1.get())); | 103 test::WriteEmptyMessage(test_pipe2.handle1.get())); |
86 | 104 |
87 BindingsSupport::Get()->AsyncWait(test_pipe1.handle0.get(), | 105 CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1); |
88 MOJO_WAIT_FLAG_READABLE, &callback1); | 106 CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2); |
89 BindingsSupport::Get()->AsyncWait(test_pipe2.handle0.get(), | 107 |
90 MOJO_WAIT_FLAG_READABLE, &callback2); | |
91 RunLoop::current()->Run(); | 108 RunLoop::current()->Run(); |
92 EXPECT_EQ(1, callback1.result_count()); | 109 EXPECT_EQ(1, callback1.result_count()); |
93 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | 110 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); |
94 EXPECT_EQ(1, callback2.result_count()); | 111 EXPECT_EQ(1, callback2.result_count()); |
95 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | 112 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); |
96 } | 113 } |
97 | 114 |
98 // Verifies cancel works. | 115 // Verifies cancel works. |
99 TEST_F(BindingsSupportImplTest, CancelCallback) { | 116 TEST_F(AsyncWaiterTest, CancelCallback) { |
100 TestAsyncWaitCallback callback; | 117 TestAsyncWaitCallback callback; |
101 MessagePipe test_pipe; | 118 MessagePipe test_pipe; |
102 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | 119 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); |
103 | 120 |
104 BindingsSupport::Get()->CancelWait( | 121 CallCancelWait( |
105 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | 122 CallAsyncWait(test_pipe.handle0.get(), |
106 MOJO_WAIT_FLAG_READABLE, &callback)); | 123 MOJO_WAIT_FLAG_READABLE, |
| 124 &callback)); |
107 RunLoop::current()->Run(); | 125 RunLoop::current()->Run(); |
108 EXPECT_EQ(0, callback.result_count()); | 126 EXPECT_EQ(0, callback.result_count()); |
109 } | 127 } |
110 | 128 |
111 } // namespace | 129 } // namespace |
112 } // namespace mojo | 130 } // namespace mojo |
OLD | NEW |