OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/public/utility/bindings_support_impl.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "mojo/public/system/core_cpp.h" | |
9 #include "mojo/public/tests/test_support.h" | |
10 #include "mojo/public/utility/environment.h" | |
11 #include "mojo/public/utility/run_loop.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace mojo { | |
15 namespace { | |
16 | |
17 class TestAsyncWaitCallback : public BindingsSupport::AsyncWaitCallback { | |
18 public: | |
19 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) { | |
20 } | |
21 virtual ~TestAsyncWaitCallback() {} | |
22 | |
23 int result_count() const { return result_count_; } | |
24 | |
25 MojoResult last_result() const { return last_result_; } | |
26 | |
27 // RunLoopHandler: | |
28 virtual void OnHandleReady(MojoResult result) OVERRIDE { | |
29 result_count_++; | |
30 last_result_ = result; | |
31 } | |
32 | |
33 private: | |
34 int result_count_; | |
35 MojoResult last_result_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback); | |
38 }; | |
39 | |
40 class BindingsSupportImplTest : public testing::Test { | |
41 public: | |
42 BindingsSupportImplTest() {} | |
43 | |
44 virtual void SetUp() OVERRIDE { | |
45 Test::SetUp(); | |
46 environment_.reset(new Environment); | |
47 run_loop_.reset(new RunLoop); | |
48 } | |
49 virtual void TearDown() OVERRIDE { | |
50 run_loop_.reset(); | |
51 environment_.reset(NULL); | |
52 Test::TearDown(); | |
53 } | |
54 | |
55 private: | |
56 scoped_ptr<Environment> environment_; | |
57 scoped_ptr<RunLoop> run_loop_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(BindingsSupportImplTest); | |
60 }; | |
61 | |
62 // Verifies AsyncWaitCallback is notified when pipe is ready. | |
63 TEST_F(BindingsSupportImplTest, CallbackNotified) { | |
64 TestAsyncWaitCallback callback; | |
65 MessagePipe test_pipe; | |
66 EXPECT_EQ(MOJO_RESULT_OK, | |
67 test::WriteEmptyMessage(test_pipe.handle1.get())); | |
68 | |
69 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | |
70 MOJO_WAIT_FLAG_READABLE, &callback); | |
71 RunLoop::current()->Run(); | |
72 EXPECT_EQ(1, callback.result_count()); | |
73 EXPECT_EQ(MOJO_RESULT_OK, callback.last_result()); | |
74 } | |
75 | |
76 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready. | |
77 TEST_F(BindingsSupportImplTest, TwoCallbacksNotified) { | |
78 TestAsyncWaitCallback callback1; | |
79 TestAsyncWaitCallback callback2; | |
80 MessagePipe test_pipe1; | |
81 MessagePipe test_pipe2; | |
82 EXPECT_EQ(MOJO_RESULT_OK, | |
83 test::WriteEmptyMessage(test_pipe1.handle1.get())); | |
84 EXPECT_EQ(MOJO_RESULT_OK, | |
85 test::WriteEmptyMessage(test_pipe2.handle1.get())); | |
86 | |
87 BindingsSupport::Get()->AsyncWait(test_pipe1.handle0.get(), | |
88 MOJO_WAIT_FLAG_READABLE, &callback1); | |
89 BindingsSupport::Get()->AsyncWait(test_pipe2.handle0.get(), | |
90 MOJO_WAIT_FLAG_READABLE, &callback2); | |
91 RunLoop::current()->Run(); | |
92 EXPECT_EQ(1, callback1.result_count()); | |
93 EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result()); | |
94 EXPECT_EQ(1, callback2.result_count()); | |
95 EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result()); | |
96 } | |
97 | |
98 // Verifies cancel works. | |
99 TEST_F(BindingsSupportImplTest, CancelCallback) { | |
100 TestAsyncWaitCallback callback; | |
101 MessagePipe test_pipe; | |
102 EXPECT_EQ(MOJO_RESULT_OK, test::WriteEmptyMessage(test_pipe.handle1.get())); | |
103 | |
104 BindingsSupport::Get()->CancelWait( | |
105 BindingsSupport::Get()->AsyncWait(test_pipe.handle0.get(), | |
106 MOJO_WAIT_FLAG_READABLE, &callback)); | |
107 RunLoop::current()->Run(); | |
108 EXPECT_EQ(0, callback.result_count()); | |
109 } | |
110 | |
111 } // namespace | |
112 } // namespace mojo | |
OLD | NEW |