11package com .qiniu .android ;
22
3+ import android .os .Looper ;
4+
35import com .qiniu .android .utils .AsyncRun ;
46import com .qiniu .android .utils .LogUtil ;
57
@@ -11,7 +13,7 @@ private class TestParam {
1113 int completeCount ;
1214 int successCount ;
1315 }
14- public void testAsyncMain (){
16+ public void testAsyncMainOnMainThread (){
1517
1618 final TestParam testParam = new TestParam ();
1719 testParam .maxCount = 100 ;
@@ -22,11 +24,14 @@ public void testAsyncMain(){
2224 AsyncRun .runInMain (new Runnable () {
2325 @ Override
2426 public void run () {
25- String threadName = Thread . currentThread (). getName ();
26- if (threadName . equals ( "main" )){
27+
28+ if (Thread . currentThread () == Looper . getMainLooper (). getThread ( )){
2729 testParam .successCount += 1 ;
2830 }
2931
32+ String threadName = Thread .currentThread ().getName ();
33+ LogUtil .d ("thread name:" + threadName );
34+
3035 testParam .completeCount += 1 ;
3136 }
3237 });
@@ -49,6 +54,53 @@ public boolean shouldWait() {
4954 assertTrue ((testParam .successCount == testParam .maxCount ));
5055 }
5156
57+ public void testAsyncMainOnOtherThread (){
58+
59+ final TestParam testParam = new TestParam ();
60+ testParam .maxCount = 10 ;
61+ testParam .completeCount = 0 ;
62+ testParam .successCount = 0 ;
63+ for (int i = 0 ; i < testParam .maxCount ; i ++) {
64+ final int i_p = i ;
65+
66+ new Thread (new Runnable () {
67+ @ Override
68+ public void run () {
69+ AsyncRun .runInMain (new Runnable () {
70+ @ Override
71+ public void run () {
72+
73+ if (Thread .currentThread ().getName ().equals ("main" )){
74+ testParam .successCount += 1 ;
75+ }
76+
77+ String threadName = Thread .currentThread ().getName ();
78+ LogUtil .d ("thread name:" + threadName );
79+
80+ testParam .completeCount += 1 ;
81+ }
82+ });
83+ }
84+ }).start ();
85+ }
86+
87+ WaitConditional waitConditional = new WaitConditional () {
88+ @ Override
89+ public boolean shouldWait () {
90+ if (testParam .completeCount == testParam .maxCount ){
91+ return false ;
92+ } else {
93+ return true ;
94+ }
95+ }
96+ };
97+
98+ wait (waitConditional , 5 );
99+
100+ LogUtil .i (String .format ("success count: %d" , testParam .successCount ));
101+ assertTrue ((testParam .successCount == testParam .maxCount ));
102+ }
103+
52104 public void testAsyncBg (){
53105 final TestParam testParam = new TestParam ();
54106 testParam .maxCount = 100 ;
0 commit comments