| 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" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace common { | 14 namespace common { |
| 15 namespace test { | 15 namespace test { |
| 16 | 16 |
| 17 scoped_ptr<base::MessagePump> CreateMojoMessagePump() { | 17 scoped_ptr<base::MessagePump> CreateMojoMessagePump() { |
| 18 return scoped_ptr<base::MessagePump>(new MessagePumpMojo()); | 18 return scoped_ptr<base::MessagePump>(new MessagePumpMojo()); |
| 19 } | 19 } |
| 20 | 20 |
| 21 RUN_MESSAGE_LOOP_TESTS(Mojo, &CreateMojoMessagePump); | 21 RUN_MESSAGE_LOOP_TESTS(Mojo, &CreateMojoMessagePump); |
| 22 | 22 |
| 23 class CountingMojoHandler : public MessagePumpMojoHandler { | 23 class CountingMojoHandler : public MessagePumpMojoHandler { |
| 24 public: | 24 public: |
| 25 CountingMojoHandler() : success_count_(0), error_count_(0) {} | 25 CountingMojoHandler() : success_count_(0), error_count_(0) {} |
| 26 | 26 |
| 27 virtual void OnHandleReady(const Handle& handle) override { | 27 void OnHandleReady(const Handle& handle) override { |
| 28 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle), | 28 ReadMessageRaw(static_cast<const MessagePipeHandle&>(handle), |
| 29 NULL, | 29 NULL, |
| 30 NULL, | 30 NULL, |
| 31 NULL, | 31 NULL, |
| 32 NULL, | 32 NULL, |
| 33 MOJO_READ_MESSAGE_FLAG_NONE); | 33 MOJO_READ_MESSAGE_FLAG_NONE); |
| 34 ++success_count_; | 34 ++success_count_; |
| 35 } | 35 } |
| 36 virtual void OnHandleError(const Handle& handle, MojoResult result) override { | 36 void OnHandleError(const Handle& handle, MojoResult result) override { |
| 37 ++error_count_; | 37 ++error_count_; |
| 38 } | 38 } |
| 39 | 39 |
| 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 { | 50 class CountingObserver : public MessagePumpMojo::Observer { |
| 51 public: | 51 public: |
| 52 virtual void WillSignalHandler() override { | 52 void WillSignalHandler() override { will_signal_handler_count++; } |
| 53 will_signal_handler_count++; | 53 void DidSignalHandler() override { did_signal_handler_count++; } |
| 54 } | |
| 55 virtual void DidSignalHandler() override { | |
| 56 did_signal_handler_count++; | |
| 57 } | |
| 58 | 54 |
| 59 int will_signal_handler_count = 0; | 55 int will_signal_handler_count = 0; |
| 60 int did_signal_handler_count = 0; | 56 int did_signal_handler_count = 0; |
| 61 }; | 57 }; |
| 62 | 58 |
| 63 TEST(MessagePumpMojo, RunUntilIdle) { | 59 TEST(MessagePumpMojo, RunUntilIdle) { |
| 64 base::MessageLoop message_loop(MessagePumpMojo::Create()); | 60 base::MessageLoop message_loop(MessagePumpMojo::Create()); |
| 65 CountingMojoHandler handler; | 61 CountingMojoHandler handler; |
| 66 MessagePipe handles; | 62 MessagePipe handles; |
| 67 MessagePumpMojo::current()->AddHandler(&handler, | 63 MessagePumpMojo::current()->AddHandler(&handler, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 for (int i = 0; i < 2; ++i) { | 115 for (int i = 0; i < 2; ++i) { |
| 120 base::RunLoop run_loop; | 116 base::RunLoop run_loop; |
| 121 run_loop.RunUntilIdle(); | 117 run_loop.RunUntilIdle(); |
| 122 } | 118 } |
| 123 EXPECT_EQ(1, handler.error_count()); | 119 EXPECT_EQ(1, handler.error_count()); |
| 124 } | 120 } |
| 125 | 121 |
| 126 } // namespace test | 122 } // namespace test |
| 127 } // namespace common | 123 } // namespace common |
| 128 } // namespace mojo | 124 } // namespace mojo |
| OLD | NEW |