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/common/message_pump_mojo.h" | 5 #include "mojo/common/message_pump_mojo.h" |
6 | 6 |
7 #include "base/message_loop/message_loop_test.h" | 7 #include "base/message_loop/message_loop_test.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "mojo/common/message_pump_mojo_handler.h" | 9 #include "mojo/common/message_pump_mojo_handler.h" |
10 #include "mojo/public/cpp/system/core.h" | 10 #include "mojo/public/cpp/system/core.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 int success_count() { return success_count_; } | 40 int success_count() { return success_count_; } |
41 int error_count() { return error_count_; } | 41 int error_count() { return error_count_; } |
42 | 42 |
43 private: | 43 private: |
44 int success_count_; | 44 int success_count_; |
45 int error_count_; | 45 int error_count_; |
46 | 46 |
47 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler); | 47 DISALLOW_COPY_AND_ASSIGN(CountingMojoHandler); |
48 }; | 48 }; |
49 | 49 |
| 50 class CountingObserver : public MessagePumpMojo::Observer { |
| 51 public: |
| 52 virtual void WillSignalHandler() override { |
| 53 will_signal_handler_count++; |
| 54 } |
| 55 virtual void DidSignalHandler() override { |
| 56 did_signal_handler_count++; |
| 57 } |
| 58 |
| 59 int will_signal_handler_count = 0; |
| 60 int did_signal_handler_count = 0; |
| 61 }; |
| 62 |
50 TEST(MessagePumpMojo, RunUntilIdle) { | 63 TEST(MessagePumpMojo, RunUntilIdle) { |
51 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 64 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
52 CountingMojoHandler handler; | 65 CountingMojoHandler handler; |
53 MessagePipe handles; | 66 MessagePipe handles; |
54 MessagePumpMojo::current()->AddHandler(&handler, | 67 MessagePumpMojo::current()->AddHandler(&handler, |
55 handles.handle0.get(), | 68 handles.handle0.get(), |
56 MOJO_HANDLE_SIGNAL_READABLE, | 69 MOJO_HANDLE_SIGNAL_READABLE, |
57 base::TimeTicks()); | 70 base::TimeTicks()); |
58 WriteMessageRaw( | 71 WriteMessageRaw( |
59 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 72 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
60 WriteMessageRaw( | 73 WriteMessageRaw( |
61 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); | 74 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
62 base::RunLoop run_loop; | 75 base::RunLoop run_loop; |
63 run_loop.RunUntilIdle(); | 76 run_loop.RunUntilIdle(); |
64 EXPECT_EQ(2, handler.success_count()); | 77 EXPECT_EQ(2, handler.success_count()); |
65 } | 78 } |
66 | 79 |
| 80 TEST(MessagePumpMojo, Observer) { |
| 81 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
| 82 |
| 83 CountingObserver observer; |
| 84 MessagePumpMojo::current()->AddObserver(&observer); |
| 85 |
| 86 CountingMojoHandler handler; |
| 87 MessagePipe handles; |
| 88 MessagePumpMojo::current()->AddHandler(&handler, |
| 89 handles.handle0.get(), |
| 90 MOJO_HANDLE_SIGNAL_READABLE, |
| 91 base::TimeTicks()); |
| 92 WriteMessageRaw( |
| 93 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 94 base::RunLoop run_loop; |
| 95 run_loop.RunUntilIdle(); |
| 96 EXPECT_EQ(1, handler.success_count()); |
| 97 EXPECT_EQ(1, observer.will_signal_handler_count); |
| 98 EXPECT_EQ(1, observer.did_signal_handler_count); |
| 99 MessagePumpMojo::current()->RemoveObserver(&observer); |
| 100 |
| 101 WriteMessageRaw( |
| 102 handles.handle1.get(), NULL, 0, NULL, 0, MOJO_WRITE_MESSAGE_FLAG_NONE); |
| 103 base::RunLoop run_loop2; |
| 104 run_loop2.RunUntilIdle(); |
| 105 EXPECT_EQ(2, handler.success_count()); |
| 106 EXPECT_EQ(1, observer.will_signal_handler_count); |
| 107 EXPECT_EQ(1, observer.did_signal_handler_count); |
| 108 } |
| 109 |
67 TEST(MessagePumpMojo, UnregisterAfterDeadline) { | 110 TEST(MessagePumpMojo, UnregisterAfterDeadline) { |
68 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 111 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
69 CountingMojoHandler handler; | 112 CountingMojoHandler handler; |
70 MessagePipe handles; | 113 MessagePipe handles; |
71 MessagePumpMojo::current()->AddHandler( | 114 MessagePumpMojo::current()->AddHandler( |
72 &handler, | 115 &handler, |
73 handles.handle0.get(), | 116 handles.handle0.get(), |
74 MOJO_HANDLE_SIGNAL_READABLE, | 117 MOJO_HANDLE_SIGNAL_READABLE, |
75 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1)); | 118 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(1)); |
76 for (int i = 0; i < 2; ++i) { | 119 for (int i = 0; i < 2; ++i) { |
77 base::RunLoop run_loop; | 120 base::RunLoop run_loop; |
78 run_loop.RunUntilIdle(); | 121 run_loop.RunUntilIdle(); |
79 } | 122 } |
80 EXPECT_EQ(1, handler.error_count()); | 123 EXPECT_EQ(1, handler.error_count()); |
81 } | 124 } |
82 | 125 |
83 } // namespace test | 126 } // namespace test |
84 } // namespace common | 127 } // namespace common |
85 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |