Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: base/message_loop/message_loop_unittest.cc

Issue 2774363003: android: Java-based launcher thread (Closed)
Patch Set: comment on test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/ptr_util.h"
15 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
16 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
17 #include "base/message_loop/message_loop_test.h" 18 #include "base/message_loop/message_loop_test.h"
18 #include "base/pending_task.h" 19 #include "base/pending_task.h"
19 #include "base/posix/eintr_wrapper.h" 20 #include "base/posix/eintr_wrapper.h"
20 #include "base/run_loop.h" 21 #include "base/run_loop.h"
21 #include "base/single_thread_task_runner.h" 22 #include "base/single_thread_task_runner.h"
22 #include "base/synchronization/waitable_event.h" 23 #include "base/synchronization/waitable_event.h"
23 #include "base/test/test_simple_task_runner.h" 24 #include "base/test/test_simple_task_runner.h"
24 #include "base/threading/platform_thread.h" 25 #include "base/threading/platform_thread.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 JNIEnv* env = base::android::AttachCurrentThread(); 87 JNIEnv* env = base::android::AttachCurrentThread();
87 jclass exception = env->FindClass( 88 jclass exception = env->FindClass(
88 "org/chromium/base/TestSystemMessageHandler$TestException"); 89 "org/chromium/base/TestSystemMessageHandler$TestException");
89 90
90 env->ThrowNew(exception, 91 env->ThrowNew(exception,
91 "This is a test exception that should be caught in " 92 "This is a test exception that should be caught in "
92 "TestSystemMessageHandler.handleMessage"); 93 "TestSystemMessageHandler.handleMessage");
93 static_cast<base::MessageLoopForUI*>(base::MessageLoop::current())->Abort(); 94 static_cast<base::MessageLoopForUI*>(base::MessageLoop::current())->Abort();
94 } 95 }
95 96
96 void RunTest_AbortDontRunMoreTasks(bool delayed) { 97 void RunTest_AbortDontRunMoreTasks(bool delayed, bool init_java_first) {
97 MessageLoop loop(MessageLoop::TYPE_JAVA);
98
99 WaitableEvent test_done_event(WaitableEvent::ResetPolicy::MANUAL, 98 WaitableEvent test_done_event(WaitableEvent::ResetPolicy::MANUAL,
100 WaitableEvent::InitialState::NOT_SIGNALED); 99 WaitableEvent::InitialState::NOT_SIGNALED);
101 100
102 std::unique_ptr<android::JavaHandlerThreadForTesting> java_thread; 101 std::unique_ptr<android::JavaHandlerThread> java_thread;
103 java_thread.reset(new android::JavaHandlerThreadForTesting( 102 if (init_java_first) {
gab 2017/03/29 17:19:53 I'm not clear on difference? The "JavaHandlerThrea
boliu 2017/03/29 18:02:39 It calls a different constructor, which constructs
gab 2017/03/29 18:14:04 Ah I see, yeah two constructors doing different th
104 "JavaHandlerThreadForTesting from AbortDontRunMoreTasks", 103 java_thread.reset(
105 &test_done_event)); 104 new android::JavaHandlerThreadForTesting(&test_done_event));
105 } else {
106 java_thread.reset(new android::JavaHandlerThreadForTesting(
107 "JavaHandlerThreadForTesting from AbortDontRunMoreTasks",
108 &test_done_event));
109 }
106 java_thread->Start(); 110 java_thread->Start();
107 111
108 if (delayed) { 112 if (delayed) {
109 java_thread->message_loop()->task_runner()->PostDelayedTask( 113 java_thread->message_loop()->task_runner()->PostDelayedTask(
110 FROM_HERE, Bind(&AbortMessagePump), TimeDelta::FromMilliseconds(10)); 114 FROM_HERE, Bind(&AbortMessagePump), TimeDelta::FromMilliseconds(10));
111 } else { 115 } else {
112 java_thread->message_loop()->task_runner()->PostTask( 116 java_thread->message_loop()->task_runner()->PostTask(
113 FROM_HERE, Bind(&AbortMessagePump)); 117 FROM_HERE, Bind(&AbortMessagePump));
114 } 118 }
115 119
116 // Wait to ensure we catch the correct exception (and don't crash) 120 // Wait to ensure we catch the correct exception (and don't crash)
117 test_done_event.Wait(); 121 test_done_event.Wait();
118 122
119 java_thread->Stop(); 123 java_thread->Stop();
120 java_thread.reset(); 124 java_thread.reset();
121 } 125 }
122 126
123 TEST(MessageLoopTest, JavaExceptionAbort) { 127 TEST(MessageLoopTest, JavaExceptionAbort) {
124 RunTest_AbortDontRunMoreTasks(false); 128 constexpr bool delayed = false;
129 constexpr bool init_java_first = false;
130 RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
125 } 131 }
126 TEST(MessageLoopTest, DelayedJavaExceptionAbort) { 132 TEST(MessageLoopTest, DelayedJavaExceptionAbort) {
127 RunTest_AbortDontRunMoreTasks(true); 133 constexpr bool delayed = true;
134 constexpr bool init_java_first = false;
135 RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
136 }
137 TEST(MessageLoopTest, JavaExceptionAbortInitJavaFirst) {
138 constexpr bool delayed = false;
139 constexpr bool init_java_first = true;
140 RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
128 } 141 }
129 #endif // defined(OS_ANDROID) 142 #endif // defined(OS_ANDROID)
130 143
131 #if defined(OS_WIN) 144 #if defined(OS_WIN)
132 145
133 // This function runs slowly to simulate a large amount of work being done. 146 // This function runs slowly to simulate a large amount of work being done.
134 static void SlowFunc(TimeDelta pause, int* quit_counter) { 147 static void SlowFunc(TimeDelta pause, int* quit_counter) {
135 PlatformThread::Sleep(pause); 148 PlatformThread::Sleep(pause);
136 if (--(*quit_counter) == 0) 149 if (--(*quit_counter) == 0)
137 MessageLoop::current()->QuitWhenIdle(); 150 MessageLoop::current()->QuitWhenIdle();
(...skipping 883 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 1034
1022 { 1035 {
1023 std::string kThreadName("bar"); 1036 std::string kThreadName("bar");
1024 base::Thread thread(kThreadName); 1037 base::Thread thread(kThreadName);
1025 ASSERT_TRUE(thread.StartAndWaitForTesting()); 1038 ASSERT_TRUE(thread.StartAndWaitForTesting());
1026 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName()); 1039 EXPECT_EQ(kThreadName, thread.message_loop()->GetThreadName());
1027 } 1040 }
1028 } 1041 }
1029 1042
1030 } // namespace base 1043 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698