blob: 42e5053aa8d763d2aa407febd80cd54981497965 [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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
James Robinson94ade6b2015-08-25 13:02:06 -07005#include "mojo/message_pump/message_pump_mojo.h"
James Robinson646469d2014-10-03 15:33:28 -07006
7#include "base/message_loop/message_loop_test.h"
James Robinsona9763132014-10-06 11:18:13 -07008#include "base/run_loop.h"
James Robinson94ade6b2015-08-25 13:02:06 -07009#include "mojo/message_pump/message_pump_mojo_handler.h"
Viet-Trung Luuadf6aa82016-03-15 11:28:19 -070010#include "mojo/public/cpp/system/message_pipe.h"
James Robinson646469d2014-10-03 15:33:28 -070011#include "testing/gtest/include/gtest/gtest.h"
12
13namespace mojo {
14namespace common {
15namespace test {
16
17scoped_ptr<base::MessagePump> CreateMojoMessagePump() {
18 return scoped_ptr<base::MessagePump>(new MessagePumpMojo());
19}
20
21RUN_MESSAGE_LOOP_TESTS(Mojo, &CreateMojoMessagePump);
22
James Robinsona9763132014-10-06 11:18:13 -070023class CountingMojoHandler : public MessagePumpMojoHandler {
24 public:
25 CountingMojoHandler() : success_count_(0), error_count_(0) {}
26
James Robinsone1b30cf2014-10-21 12:25:40 -070027 void OnHandleReady(const Handle& handle) override {
James Robinsona9763132014-10-06 11:18:13 -070028 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle),
29 NULL,
30 NULL,
31 NULL,
32 NULL,
33 MOJO_READ_MESSAGE_FLAG_NONE);
34 ++success_count_;
35 }
James Robinsone1b30cf2014-10-21 12:25:40 -070036 void OnHandleError(const Handle& handle, MojoResult result) override {
James Robinsona9763132014-10-06 11:18:13 -070037 ++error_count_;
38 }
39
40 int success_count() { return success_count_; }
41 int error_count() { return error_count_; }
42
43 private:
44 int success_count_;
45 int error_count_;
46
47 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler);
48};
49
James Robinsond4531882014-10-17 16:14:32 -070050class CountingObserver : public MessagePumpMojo::Observer {
51 public:
James Robinsone1b30cf2014-10-21 12:25:40 -070052 void WillSignalHandler() override { will_signal_handler_count++; }
53 void DidSignalHandler() override { did_signal_handler_count++; }
James Robinsond4531882014-10-17 16:14:32 -070054
55 int will_signal_handler_count = 0;
56 int did_signal_handler_count = 0;
57};
58
James Robinsona9763132014-10-06 11:18:13 -070059TEST(MessagePumpMojo, RunUntilIdle) {
60 base::MessageLoop message_loop(MessagePumpMojo::Create());
61 CountingMojoHandler handler;
62 MessagePipe handles;
63 MessagePumpMojo::current()->AddHandler(&handler,
64 handles.handle0.get(),
65 MOJO_HANDLE_SIGNAL_READABLE,
66 base::TimeTicks());
67 WriteMessageRaw(
68 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE);
69 WriteMessageRaw(
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());
74}
75
James Robinsond4531882014-10-17 16:14:32 -070076TEST(MessagePumpMojo, Observer) {
77 base::MessageLoop message_loop(MessagePumpMojo::Create());
78
79 CountingObserver observer;
80 MessagePumpMojo::current()->AddObserver(&observer);
81
82 CountingMojoHandler handler;
83 MessagePipe handles;
84 MessagePumpMojo::current()->AddHandler(&handler,
85 handles.handle0.get(),
86 MOJO_HANDLE_SIGNAL_READABLE,
87 base::TimeTicks());
88 WriteMessageRaw(
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);
96
97 WriteMessageRaw(
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);
104}
105
James Robinsona9763132014-10-06 11:18:13 -0700106TEST(MessagePumpMojo, UnregisterAfterDeadline) {
107 base::MessageLoop message_loop(MessagePumpMojo::Create());
108 CountingMojoHandler handler;
109 MessagePipe handles;
110 MessagePumpMojo::current()->AddHandler(
111 &handler,
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();
118 }
119 EXPECT_EQ(1, handler.error_count());
120}
121
James Robinson646469d2014-10-03 15:33:28 -0700122} // namespace test
123} // namespace common
124} // namespace mojo