Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / mojo / public / cpp / environment / tests / async_waiter_unittest.cc
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/cpp/environment/default_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                               MojoWaitFlags flags,
44                               TestAsyncWaitCallback* callback) {
45   MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter();
46   return waiter->AsyncWait(waiter,
47                            handle.value(),
48                            flags,
49                            MOJO_DEADLINE_INDEFINITE,
50                            &TestAsyncWaitCallback::OnHandleReady,
51                            callback);
52 }
53
54 void CallCancelWait(MojoAsyncWaitID wait_id) {
55   MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter();
56   waiter->CancelWait(waiter, wait_id);
57 }
58
59 class AsyncWaiterTest : public testing::Test {
60  public:
61   AsyncWaiterTest() {}
62
63  private:
64   Environment environment_;
65   RunLoop run_loop_;
66
67   MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest);
68 };
69
70 // Verifies AsyncWaitCallback is notified when pipe is ready.
71 TEST_F(AsyncWaiterTest, CallbackNotified) {
72   TestAsyncWaitCallback callback;
73   MessagePipe test_pipe;
74   EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
75
76   CallAsyncWait(test_pipe.handle0.get(),
77                 MOJO_WAIT_FLAG_READABLE,
78                 &callback);
79   RunLoop::current()->Run();
80   EXPECT_EQ(1, callback.result_count());
81   EXPECT_EQ(MOJO_RESULT_OK, callback.last_result());
82 }
83
84 // Verifies 2 AsyncWaitCallbacks are notified when there pipes are ready.
85 TEST_F(AsyncWaiterTest, TwoCallbacksNotified) {
86   TestAsyncWaitCallback callback1;
87   TestAsyncWaitCallback callback2;
88   MessagePipe test_pipe1;
89   MessagePipe test_pipe2;
90   EXPECT_TRUE(test::WriteTextMessage(test_pipe1.handle1.get(), std::string()));
91   EXPECT_TRUE(test::WriteTextMessage(test_pipe2.handle1.get(), std::string()));
92
93   CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1);
94   CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2);
95
96   RunLoop::current()->Run();
97   EXPECT_EQ(1, callback1.result_count());
98   EXPECT_EQ(MOJO_RESULT_OK, callback1.last_result());
99   EXPECT_EQ(1, callback2.result_count());
100   EXPECT_EQ(MOJO_RESULT_OK, callback2.last_result());
101 }
102
103 // Verifies cancel works.
104 TEST_F(AsyncWaiterTest, CancelCallback) {
105   TestAsyncWaitCallback callback;
106   MessagePipe test_pipe;
107   EXPECT_TRUE(test::WriteTextMessage(test_pipe.handle1.get(), std::string()));
108
109   CallCancelWait(
110       CallAsyncWait(test_pipe.handle0.get(),
111                     MOJO_WAIT_FLAG_READABLE,
112                     &callback));
113   RunLoop::current()->Run();
114   EXPECT_EQ(0, callback.result_count());
115 }
116
117 }  // namespace
118 }  // namespace mojo