James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 5 | #include "mojo/message_pump/message_pump_mojo.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 6 | |
| 7 | #include "base/message_loop/message_loop_test.h" |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 8 | #include "base/run_loop.h" |
James Robinson | 94ade6b | 2015-08-25 13:02:06 -0700 | [diff] [blame] | 9 | #include "mojo/message_pump/message_pump_mojo_handler.h" |
Viet-Trung Luu | adf6aa8 | 2016-03-15 11:28:19 -0700 | [diff] [blame] | 10 | #include "mojo/public/cpp/system/message_pipe.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace mojo { |
| 14 | namespace common { |
| 15 | namespace test { |
| 16 | |
| 17 | scoped_ptr<base::MessagePump> CreateMojoMessagePump() { |
| 18 | return scoped_ptr<base::MessagePump>(new MessagePumpMojo()); |
| 19 | } |
| 20 | |
| 21 | RUN_MESSAGE_LOOP_TESTS(Mojo, &CreateMojoMessagePump); |
| 22 | |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 23 | class CountingMojoHandler : public MessagePumpMojoHandler { |
| 24 | public: |
| 25 | CountingMojoHandler() : success_count_(0), error_count_(0) {} |
| 26 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 27 | void OnHandleReady(const Handle& handle) override { |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 28 | 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 Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 36 | void OnHandleError(const Handle& handle, MojoResult result) override { |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 37 | ++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 Robinson | d453188 | 2014-10-17 16:14:32 -0700 | [diff] [blame] | 50 | class CountingObserver : public MessagePumpMojo::Observer { |
| 51 | public: |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 52 | void WillSignalHandler() override { will_signal_handler_count++; } |
| 53 | void DidSignalHandler() override { did_signal_handler_count++; } |
James Robinson | d453188 | 2014-10-17 16:14:32 -0700 | [diff] [blame] | 54 | |
| 55 | int will_signal_handler_count = 0; |
| 56 | int did_signal_handler_count = 0; |
| 57 | }; |
| 58 | |
James Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 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()); |
| 74 | } |
| 75 | |
James Robinson | d453188 | 2014-10-17 16:14:32 -0700 | [diff] [blame] | 76 | TEST(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 Robinson | a976313 | 2014-10-06 11:18:13 -0700 | [diff] [blame] | 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(); |
| 118 | } |
| 119 | EXPECT_EQ(1, handler.error_count()); |
| 120 | } |
| 121 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 122 | } // namespace test |
| 123 | } // namespace common |
| 124 | } // namespace mojo |