Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / mojo / public / cpp / environment / tests / async_waiter_unittest.cc
blob75cefb27803f1ea6701105edb01a872c8c8aeceb
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.
5 #include <string>
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"
15 namespace mojo {
16 namespace {
18 class TestAsyncWaitCallback {
19 public:
20 TestAsyncWaitCallback() : result_count_(0), last_result_(MOJO_RESULT_OK) {
22 virtual ~TestAsyncWaitCallback() {}
24 int result_count() const { return result_count_; }
26 MojoResult last_result() const { return last_result_; }
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;
35 private:
36 int result_count_;
37 MojoResult last_result_;
39 MOJO_DISALLOW_COPY_AND_ASSIGN(TestAsyncWaitCallback);
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);
54 void CallCancelWait(MojoAsyncWaitID wait_id) {
55 MojoAsyncWaiter* waiter = GetDefaultAsyncWaiter();
56 waiter->CancelWait(waiter, wait_id);
59 class AsyncWaiterTest : public testing::Test {
60 public:
61 AsyncWaiterTest() {}
63 private:
64 Environment environment_;
65 RunLoop run_loop_;
67 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterTest);
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()));
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());
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()));
93 CallAsyncWait(test_pipe1.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback1);
94 CallAsyncWait(test_pipe2.handle0.get(), MOJO_WAIT_FLAG_READABLE, &callback2);
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());
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()));
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());
117 } // namespace
118 } // namespace mojo