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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/message_loop/message_loop_test.h" | 17 #include "base/message_loop/message_loop_test.h" |
18 #include "base/pending_task.h" | 18 #include "base/pending_task.h" |
19 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
21 #include "base/synchronization/waitable_event.h" | 21 #include "base/synchronization/waitable_event.h" |
22 #include "base/test/test_simple_task_runner.h" | 22 #include "base/test/test_simple_task_runner.h" |
23 #include "base/threading/platform_thread.h" | 23 #include "base/threading/platform_thread.h" |
24 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
25 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
26 #include "build/build_config.h" | 26 #include "build/build_config.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
| 29 #if defined(OS_ANDROID) |
| 30 #include "base/android/jni_android.h" |
| 31 #include "base/test/android/java_handler_thread_for_testing.h" |
| 32 #endif |
| 33 |
29 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
30 #include "base/message_loop/message_pump_win.h" | 35 #include "base/message_loop/message_pump_win.h" |
31 #include "base/process/memory.h" | 36 #include "base/process/memory.h" |
32 #include "base/strings/string16.h" | 37 #include "base/strings/string16.h" |
33 #include "base/win/current_module.h" | 38 #include "base/win/current_module.h" |
34 #include "base/win/scoped_handle.h" | 39 #include "base/win/scoped_handle.h" |
35 #endif | 40 #endif |
36 | 41 |
37 namespace base { | 42 namespace base { |
38 | 43 |
(...skipping 29 matching lines...) Expand all Loading... |
68 | 73 |
69 private: | 74 private: |
70 friend class RefCounted<Foo>; | 75 friend class RefCounted<Foo>; |
71 | 76 |
72 ~Foo() {} | 77 ~Foo() {} |
73 | 78 |
74 int test_count_; | 79 int test_count_; |
75 std::string result_; | 80 std::string result_; |
76 }; | 81 }; |
77 | 82 |
| 83 #if defined(OS_ANDROID) |
| 84 void AbortMessagePump() { |
| 85 JNIEnv* env = base::android::AttachCurrentThread(); |
| 86 jclass exception = env->FindClass( |
| 87 "org/chromium/base/TestSystemMessageHandler$TestException"); |
| 88 |
| 89 env->ThrowNew(exception, |
| 90 "This is a test exception that should be caught in " |
| 91 "TestSystemMessageHandler.handleMessage"); |
| 92 static_cast<base::MessageLoopForUI*>(base::MessageLoop::current())->Abort(); |
| 93 } |
| 94 |
| 95 void RunTest_AbortDontRunMoreTasks(bool delayed) { |
| 96 MessageLoop loop(MessageLoop::TYPE_JAVA); |
| 97 |
| 98 WaitableEvent test_done_event(WaitableEvent::ResetPolicy::MANUAL, |
| 99 WaitableEvent::InitialState::NOT_SIGNALED); |
| 100 |
| 101 std::unique_ptr<android::JavaHandlerThreadForTesting> java_thread; |
| 102 java_thread.reset(new android::JavaHandlerThreadForTesting( |
| 103 "JavaHandlerThreadForTesting from AbortDontRunMoreTasks", |
| 104 &test_done_event)); |
| 105 java_thread->Start(); |
| 106 |
| 107 if (delayed) { |
| 108 java_thread->message_loop()->PostDelayedTask( |
| 109 FROM_HERE, Bind(&AbortMessagePump), TimeDelta::FromMilliseconds(10)); |
| 110 } else { |
| 111 java_thread->message_loop()->PostTask(FROM_HERE, Bind(&AbortMessagePump)); |
| 112 } |
| 113 |
| 114 // Wait to ensure we catch the correct exception (and don't crash) |
| 115 test_done_event.Wait(); |
| 116 |
| 117 java_thread->Stop(); |
| 118 java_thread.reset(); |
| 119 } |
| 120 |
| 121 TEST(MessageLoopTest, JavaExceptionAbort) { |
| 122 RunTest_AbortDontRunMoreTasks(false); |
| 123 } |
| 124 TEST(MessageLoopTest, DelayedJavaExceptionAbort) { |
| 125 RunTest_AbortDontRunMoreTasks(true); |
| 126 } |
| 127 #endif // defined(OS_ANDROID) |
| 128 |
78 #if defined(OS_WIN) | 129 #if defined(OS_WIN) |
79 | 130 |
80 // This function runs slowly to simulate a large amount of work being done. | 131 // This function runs slowly to simulate a large amount of work being done. |
81 static void SlowFunc(TimeDelta pause, int* quit_counter) { | 132 static void SlowFunc(TimeDelta pause, int* quit_counter) { |
82 PlatformThread::Sleep(pause); | 133 PlatformThread::Sleep(pause); |
83 if (--(*quit_counter) == 0) | 134 if (--(*quit_counter) == 0) |
84 MessageLoop::current()->QuitWhenIdle(); | 135 MessageLoop::current()->QuitWhenIdle(); |
85 } | 136 } |
86 | 137 |
87 // This function records the time when Run was called in a Time object, which is | 138 // This function records the time when Run was called in a Time object, which is |
(...skipping 902 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 | 1041 |
991 { | 1042 { |
992 std::string kThreadName("bar"); | 1043 std::string kThreadName("bar"); |
993 base::Thread thread(kThreadName); | 1044 base::Thread thread(kThreadName); |
994 ASSERT_TRUE(thread.StartAndWaitForTesting()); | 1045 ASSERT_TRUE(thread.StartAndWaitForTesting()); |
995 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName()); | 1046 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName()); |
996 } | 1047 } |
997 } | 1048 } |
998 | 1049 |
999 } // namespace base | 1050 } // namespace base |
OLD | NEW |