Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / mojo / common / message_pump_mojo_unittest.cc
blob09cbe54a63a1e7441d96b86373ad94ec029bee13
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 "mojo/public/cpp/system/core.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace mojo {
14 namespace common {
15 namespace test {
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 {
24 public:
25 CountingMojoHandler() : success_count_(0), error_count_(0) {}
27 void OnHandleReady(const Handle& handle) override {
28 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle),
29 NULL,
30 NULL,
31 NULL,
32 NULL,
33 MOJO_READ_MESSAGE_FLAG_NONE);
34 ++success_count_;
36 void OnHandleError(const Handle& handle, MojoResult result) override {
37 ++error_count_;
40 int success_count() { return success_count_; }
41 int error_count() { return error_count_; }
43 private:
44 int success_count_;
45 int error_count_;
47 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler);
50 class CountingObserver : public MessagePumpMojo::Observer {
51 public:
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;
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());
76 TEST(MessagePumpMojo, Observer) {
77 base::MessageLoop message_loop(MessagePumpMojo::Create());
79 CountingObserver observer;
80 MessagePumpMojo::current()->AddObserver(&observer);
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);
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);
106 TEST(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();
119 EXPECT_EQ(1, handler.error_count());
122 } // namespace test
123 } // namespace common
124 } // namespace mojo