OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/message_pump/message_pump_mojo.h" | 5 #include "mojo/message_pump/message_pump_mojo.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop_test.h" | 8 #include "base/message_loop/message_loop_test.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "mojo/message_pump/message_pump_mojo_handler.h" | 10 #include "mojo/message_pump/message_pump_mojo_handler.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 CountingMojoHandler() : success_count_(0), error_count_(0) {} | 26 CountingMojoHandler() : success_count_(0), error_count_(0) {} |
27 | 27 |
28 void OnHandleReady(const Handle& handle) override { | 28 void OnHandleReady(const Handle& handle) override { |
29 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle), | 29 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle), |
30 NULL, | 30 NULL, |
31 NULL, | 31 NULL, |
32 NULL, | 32 NULL, |
33 NULL, | 33 NULL, |
34 MOJO_READ_MESSAGE_FLAG_NONE); | 34 MOJO_READ_MESSAGE_FLAG_NONE); |
35 ++success_count_; | 35 ++success_count_; |
| 36 if (success_count_ == success_callback_count_ && |
| 37 !success_callback_.is_null()) { |
| 38 success_callback_.Run(); |
| 39 success_callback_.Reset(); |
| 40 } |
36 } | 41 } |
| 42 |
| 43 void set_success_callback(const base::Closure& callback, |
| 44 int success_count) { |
| 45 success_callback_ = callback; |
| 46 success_callback_count_ = success_count; |
| 47 } |
| 48 |
37 void OnHandleError(const Handle& handle, MojoResult result) override { | 49 void OnHandleError(const Handle& handle, MojoResult result) override { |
38 ++error_count_; | 50 ++error_count_; |
| 51 if (!error_callback_.is_null()) { |
| 52 error_callback_.Run(); |
| 53 error_callback_.Reset(); |
| 54 } |
| 55 } |
| 56 |
| 57 void set_error_callback(const base::Closure& callback) { |
| 58 error_callback_ = callback; |
39 } | 59 } |
40 | 60 |
41 int success_count() { return success_count_; } | 61 int success_count() { return success_count_; } |
42 int error_count() { return error_count_; } | 62 int error_count() { return error_count_; } |
43 | 63 |
44 private: | 64 private: |
45 int success_count_; | 65 int success_count_; |
46 int error_count_; | 66 int error_count_; |
47 | 67 |
| 68 base::Closure error_callback_; |
| 69 int success_callback_count_; |
| 70 |
| 71 base::Closure success_callback_; |
| 72 |
48 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler); | 73 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler); |
49 }; | 74 }; |
50 | 75 |
51 class CountingObserver : public MessagePumpMojo::Observer { | 76 class CountingObserver : public MessagePumpMojo::Observer { |
52 public: | 77 public: |
53 void WillSignalHandler() override { will_signal_handler_count++; } | 78 void WillSignalHandler() override { will_signal_handler_count++; } |
54 void DidSignalHandler() override { did_signal_handler_count++; } | 79 void DidSignalHandler() override { did_signal_handler_count++; } |
55 | 80 |
56 int will_signal_handler_count = 0; | 81 int will_signal_handler_count = 0; |
57 int did_signal_handler_count = 0; | 82 int did_signal_handler_count = 0; |
58 }; | 83 }; |
59 | 84 |
60 TEST(MessagePumpMojo, RunUntilIdle) { | 85 TEST(MessagePumpMojo, RunUntilIdle) { |
61 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 86 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
62 CountingMojoHandler handler; | 87 CountingMojoHandler handler; |
| 88 base::RunLoop run_loop; |
| 89 handler.set_success_callback(run_loop.QuitClosure(), 2); |
63 MessagePipe handles; | 90 MessagePipe handles; |
64 MessagePumpMojo::current()->AddHandler(&handler, | 91 MessagePumpMojo::current()->AddHandler(&handler, |
65 handles.handle0.get(), | 92 handles.handle0.get(), |
66 MOJO_HANDLE_SIGNAL_READABLE, | 93 MOJO_HANDLE_SIGNAL_READABLE, |
67 base::TimeTicks()); | 94 base::TimeTicks()); |
68 WriteMessageRaw( | 95 WriteMessageRaw( |
69 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 96 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
70 WriteMessageRaw( | 97 WriteMessageRaw( |
71 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 98 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
72 base::RunLoop run_loop; | 99 MojoHandleSignalsState hss; |
73 run_loop.RunUntilIdle(); | 100 ASSERT_EQ(MOJO_RESULT_OK, |
| 101 MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 102 MOJO_DEADLINE_INDEFINITE, &hss)); |
| 103 run_loop.Run(); |
74 EXPECT_EQ(2, handler.success_count()); | 104 EXPECT_EQ(2, handler.success_count()); |
75 } | 105 } |
76 | 106 |
77 TEST(MessagePumpMojo, Observer) { | 107 TEST(MessagePumpMojo, Observer) { |
78 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 108 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
79 | 109 |
80 CountingObserver observer; | 110 CountingObserver observer; |
81 MessagePumpMojo::current()->AddObserver(&observer); | 111 MessagePumpMojo::current()->AddObserver(&observer); |
82 | 112 |
83 CountingMojoHandler handler; | 113 CountingMojoHandler handler; |
| 114 base::RunLoop run_loop; |
| 115 handler.set_success_callback(run_loop.QuitClosure(), 1); |
84 MessagePipe handles; | 116 MessagePipe handles; |
85 MessagePumpMojo::current()->AddHandler(&handler, | 117 MessagePumpMojo::current()->AddHandler(&handler, |
86 handles.handle0.get(), | 118 handles.handle0.get(), |
87 MOJO_HANDLE_SIGNAL_READABLE, | 119 MOJO_HANDLE_SIGNAL_READABLE, |
88 base::TimeTicks()); | 120 base::TimeTicks()); |
89 WriteMessageRaw( | 121 WriteMessageRaw( |
90 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 122 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
91 base::RunLoop run_loop; | 123 |
92 run_loop.RunUntilIdle(); | 124 MojoHandleSignalsState hss; |
| 125 ASSERT_EQ(MOJO_RESULT_OK, |
| 126 MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 127 MOJO_DEADLINE_INDEFINITE, &hss)); |
| 128 run_loop.Run(); |
93 EXPECT_EQ(1, handler.success_count()); | 129 EXPECT_EQ(1, handler.success_count()); |
94 EXPECT_EQ(1, observer.will_signal_handler_count); | 130 EXPECT_EQ(1, observer.will_signal_handler_count); |
95 EXPECT_EQ(1, observer.did_signal_handler_count); | 131 EXPECT_EQ(1, observer.did_signal_handler_count); |
96 MessagePumpMojo::current()->RemoveObserver(&observer); | 132 MessagePumpMojo::current()->RemoveObserver(&observer); |
97 | 133 |
| 134 base::RunLoop run_loop2; |
| 135 handler.set_success_callback(run_loop2.QuitClosure(), 2); |
98 WriteMessageRaw( | 136 WriteMessageRaw( |
99 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 137 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
100 base::RunLoop run_loop2; | 138 ASSERT_EQ(MOJO_RESULT_OK, |
101 run_loop2.RunUntilIdle(); | 139 MojoWait(handles.handle0.get().value(), MOJO_HANDLE_SIGNAL_READABLE, |
| 140 MOJO_DEADLINE_INDEFINITE, &hss)); |
| 141 run_loop2.Run(); |
102 EXPECT_EQ(2, handler.success_count()); | 142 EXPECT_EQ(2, handler.success_count()); |
103 EXPECT_EQ(1, observer.will_signal_handler_count); | 143 EXPECT_EQ(1, observer.will_signal_handler_count); |
104 EXPECT_EQ(1, observer.did_signal_handler_count); | 144 EXPECT_EQ(1, observer.did_signal_handler_count); |
105 } | 145 } |
106 | 146 |
107 TEST(MessagePumpMojo, UnregisterAfterDeadline) { | 147 TEST(MessagePumpMojo, UnregisterAfterDeadline) { |
108 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 148 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
109 CountingMojoHandler handler; | 149 CountingMojoHandler handler; |
| 150 base::RunLoop run_loop; |
| 151 handler.set_error_callback(run_loop.QuitClosure()); |
110 MessagePipe handles; | 152 MessagePipe handles; |
111 MessagePumpMojo::current()->AddHandler( | 153 MessagePumpMojo::current()->AddHandler( |
112 &handler, | 154 &handler, |
113 handles.handle0.get(), | 155 handles.handle0.get(), |
114 MOJO_HANDLE_SIGNAL_READABLE, | 156 MOJO_HANDLE_SIGNAL_READABLE, |
115 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1)); | 157 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1)); |
116 for (int i = 0; i < 2; ++i) { | 158 run_loop.Run(); |
117 base::RunLoop run_loop; | |
118 run_loop.RunUntilIdle(); | |
119 } | |
120 EXPECT_EQ(1, handler.error_count()); | 159 EXPECT_EQ(1, handler.error_count()); |
121 } | 160 } |
122 | 161 |
123 } // namespace test | 162 } // namespace test |
124 } // namespace common | 163 } // namespace common |
125 } // namespace mojo | 164 } // namespace mojo |
OLD | NEW |