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.
5 #include "mojo/common/message_pump_mojo.h"
7 #include "base/message_loop/message_loop_test.h"
8 #include "base/run_loop.h"
9 #include "mojo/common/message_pump_mojo_handler.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
17 scoped_ptr
<base::MessagePump
> CreateMojoMessagePump() {
18 return scoped_ptr
<base::MessagePump
>(new MessagePumpMojo());
21 RUN_MESSAGE_LOOP_TESTS(Mojo
, &CreateMojoMessagePump
);
23 class CountingMojoHandler
: public MessagePumpMojoHandler
{
25 CountingMojoHandler() : success_count_(0), error_count_(0) {}
27 void OnHandleReady(const Handle
& handle
) override
{
28 ReadMessageRaw(static_cast<const MessagePipeHandle
&>(handle
),
33 MOJO_READ_MESSAGE_FLAG_NONE
);
36 void OnHandleError(const Handle
& handle
, MojoResult result
) override
{
40 int success_count() { return success_count_
; }
41 int error_count() { return error_count_
; }
47 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler
);
50 class CountingObserver
: public MessagePumpMojo::Observer
{
52 void WillSignalHandler() override
{ will_signal_handler_count
++; }
53 void DidSignalHandler() override
{ did_signal_handler_count
++; }
55 int will_signal_handler_count
= 0;
56 int did_signal_handler_count
= 0;
59 TEST(MessagePumpMojo
, RunUntilIdle
) {
60 base::MessageLoop
message_loop(MessagePumpMojo::Create());
61 CountingMojoHandler handler
;
63 MessagePumpMojo::current()->AddHandler(&handler
,
64 handles
.handle0
.get(),
65 MOJO_HANDLE_SIGNAL_READABLE
,
68 handles
.handle1
.get(), NULL
, 0, NULL
, 0, MOJO_WRITE_MESSAGE_FLAG_NONE
);
70 handles
.handle1
.get(), NULL
, 0, NULL
, 0, MOJO_WRITE_MESSAGE_FLAG_NONE
);
71 base::RunLoop run_loop
;
72 run_loop
.RunUntilIdle();
73 EXPECT_EQ(2, handler
.success_count());
76 TEST(MessagePumpMojo
, Observer
) {
77 base::MessageLoop
message_loop(MessagePumpMojo::Create());
79 CountingObserver observer
;
80 MessagePumpMojo::current()->AddObserver(&observer
);
82 CountingMojoHandler handler
;
84 MessagePumpMojo::current()->AddHandler(&handler
,
85 handles
.handle0
.get(),
86 MOJO_HANDLE_SIGNAL_READABLE
,
89 handles
.handle1
.get(), NULL
, 0, NULL
, 0, MOJO_WRITE_MESSAGE_FLAG_NONE
);
90 base::RunLoop run_loop
;
91 run_loop
.RunUntilIdle();
92 EXPECT_EQ(1, handler
.success_count());
93 EXPECT_EQ(1, observer
.will_signal_handler_count
);
94 EXPECT_EQ(1, observer
.did_signal_handler_count
);
95 MessagePumpMojo::current()->RemoveObserver(&observer
);
98 handles
.handle1
.get(), NULL
, 0, NULL
, 0, MOJO_WRITE_MESSAGE_FLAG_NONE
);
99 base::RunLoop run_loop2
;
100 run_loop2
.RunUntilIdle();
101 EXPECT_EQ(2, handler
.success_count());
102 EXPECT_EQ(1, observer
.will_signal_handler_count
);
103 EXPECT_EQ(1, observer
.did_signal_handler_count
);
106 TEST(MessagePumpMojo
, UnregisterAfterDeadline
) {
107 base::MessageLoop
message_loop(MessagePumpMojo::Create());
108 CountingMojoHandler handler
;
110 MessagePumpMojo::current()->AddHandler(
112 handles
.handle0
.get(),
113 MOJO_HANDLE_SIGNAL_READABLE
,
114 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1));
115 for (int i
= 0; i
< 2; ++i
) {
116 base::RunLoop run_loop
;
117 run_loop
.RunUntilIdle();
119 EXPECT_EQ(1, handler
.error_count());
123 } // namespace common