| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/test/android/java_handler_thread_for_testing.h" | 5 #include "base/test/android/java_handler_thread_for_testing.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "jni/JavaHandlerThreadTest_jni.h" |
| 8 | 9 |
| 9 namespace base { | 10 namespace base { |
| 10 namespace android { | 11 namespace android { |
| 11 | 12 |
| 12 base::android::ScopedJavaLocalRef<jobject> | 13 base::android::ScopedJavaLocalRef<jobject> |
| 13 TestJavaMessageHandlerFactory::CreateMessageHandler( | 14 TestJavaMessageHandlerFactory::CreateMessageHandler( |
| 14 JNIEnv* env, | 15 JNIEnv* env, |
| 15 base::MessagePump::Delegate* delegate, | 16 base::MessagePump::Delegate* delegate, |
| 16 MessagePumpForUI* message_pump, | 17 MessagePumpForUI* message_pump, |
| 17 WaitableEvent* test_done_event) { | 18 WaitableEvent* test_done_event) { |
| 18 return TestSystemMessageHandlerLink::CreateTestSystemMessageHandler( | 19 return TestSystemMessageHandlerLink::CreateTestSystemMessageHandler( |
| 19 env, delegate, message_pump, test_done_event); | 20 env, delegate, message_pump, test_done_event); |
| 20 } | 21 } |
| 21 | 22 |
| 22 JavaHandlerThreadForTesting::JavaHandlerThreadForTesting( | 23 JavaHandlerThreadForTesting::JavaHandlerThreadForTesting( |
| 23 const char* name, | 24 const char* name, |
| 24 base::WaitableEvent* test_done_event) | 25 base::WaitableEvent* test_done_event) |
| 25 : JavaHandlerThread(name), | 26 : JavaHandlerThread(name), |
| 26 message_handler_factory_(new TestJavaMessageHandlerFactory()), | 27 message_handler_factory_(new TestJavaMessageHandlerFactory()), |
| 27 test_done_event_(test_done_event) {} | 28 test_done_event_(test_done_event) {} |
| 28 | 29 |
| 30 JavaHandlerThreadForTesting::JavaHandlerThreadForTesting( |
| 31 base::WaitableEvent* test_done_event) |
| 32 : JavaHandlerThread(Java_JavaHandlerThreadTest_testAndGetJavaHandlerThread( |
| 33 base::android::AttachCurrentThread())), |
| 34 message_handler_factory_(new TestJavaMessageHandlerFactory()), |
| 35 test_done_event_(test_done_event) {} |
| 36 |
| 29 JavaHandlerThreadForTesting::~JavaHandlerThreadForTesting() = default; | 37 JavaHandlerThreadForTesting::~JavaHandlerThreadForTesting() = default; |
| 30 | 38 |
| 31 void JavaHandlerThreadForTesting::StartMessageLoop() { | 39 void JavaHandlerThreadForTesting::StartMessageLoop() { |
| 32 static_cast<MessageLoopForUI*>(message_loop_.get()) | 40 static_cast<MessageLoopForUI*>(message_loop_.get()) |
| 33 ->StartForTesting( | 41 ->StartForTesting(message_handler_factory_.get(), test_done_event_); |
| 34 message_handler_factory_.get(), | |
| 35 reinterpret_cast<base::WaitableEvent*>(test_done_event_)); | |
| 36 } | 42 } |
| 37 | 43 |
| 38 void JavaHandlerThreadForTesting::StopMessageLoop() { | 44 void JavaHandlerThreadForTesting::StopMessageLoop() { |
| 39 // Instead of calling MessageLoop::QuitWhenIdle here we call | 45 // Instead of calling MessageLoop::QuitWhenIdle here we call |
| 40 // MessageLoop::QuitNow. This is because QuitWhenIdle will have no effect on | 46 // MessageLoop::QuitNow. This is because QuitWhenIdle will have no effect on |
| 41 // the message loop after MessageLoop::Abort has been called (which should | 47 // the message loop after MessageLoop::Abort has been called (which should |
| 42 // have happened at this point). | 48 // have happened at this point). |
| 43 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitNow(); | 49 static_cast<MessageLoopForUI*>(message_loop_.get())->QuitNow(); |
| 44 // The message loop must be destroyed on the thread it is attached to. | 50 // The message loop must be destroyed on the thread it is attached to. |
| 45 message_loop_.reset(); | 51 message_loop_.reset(); |
| 46 } | 52 } |
| 47 | 53 |
| 48 } // namespace base | 54 } // namespace base |
| 49 } // namespace android | 55 } // namespace android |
| OLD | NEW |