| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package android.testing; |
| 16 |
|
| 17 |
import android.os.Bundle; |
| 18 |
import android.os.Handler; |
| 19 |
import android.os.Looper; |
| 20 |
import android.os.Message; |
| 21 |
import android.os.TestLooperManager; |
| 22 |
import android.util.Log; |
| 23 |
|
| 24 |
import androidx.test.runner.AndroidJUnitRunner; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class TestableInstrumentation extends AndroidJUnitRunner { |
| 34 |
|
| 35 |
private static final String TAG = "TestableInstrumentation"; |
| 36 |
|
| 37 |
private static final int MAX_CRASHES = 5; |
| 38 |
private static MainLooperManager sManager; |
| 39 |
|
| 40 |
@Override |
| 41 |
public void onCreate(Bundle arguments) { |
| 42 |
if (TestableLooper.HOLD_MAIN_THREAD) { |
| 43 |
sManager = new MainLooperManager(); |
| 44 |
Log.setWtfHandler((tag, what, system) -> { |
| 45 |
if (system) { |
| 46 |
Log.e(TAG, "WTF!!", what); |
| 47 |
} else { |
| 48 |
|
| 49 |
|
| 50 |
throw new RuntimeException(what); |
| 51 |
} |
| 52 |
}); |
| 53 |
} |
| 54 |
super.onCreate(arguments); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void finish(int resultCode, Bundle results) { |
| 59 |
if (TestableLooper.HOLD_MAIN_THREAD) { |
| 60 |
sManager.destroy(); |
| 61 |
} |
| 62 |
super.finish(resultCode, results); |
| 63 |
} |
| 64 |
|
| 65 |
public static void acquireMain() { |
| 66 |
if (sManager != null) { |
| 67 |
sManager.acquireMain(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public static void releaseMain() { |
| 72 |
if (sManager != null) { |
| 73 |
sManager.releaseMain(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public class MainLooperManager implements Runnable { |
| 78 |
|
| 79 |
private final ArrayList<Throwable> mExceptions = new ArrayList<>(); |
| 80 |
private Message mStopMessage; |
| 81 |
private final Handler mMainHandler; |
| 82 |
private TestLooperManager mManager; |
| 83 |
|
| 84 |
public MainLooperManager() { |
| 85 |
mMainHandler = Handler.createAsync(Looper.getMainLooper()); |
| 86 |
startManaging(); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void run() { |
| 91 |
try { |
| 92 |
synchronized (this) { |
| 93 |
|
| 94 |
notify(); |
| 95 |
} |
| 96 |
while (true) { |
| 97 |
Message m = mManager.next(); |
| 98 |
if (m == mStopMessage) { |
| 99 |
mManager.recycle(m); |
| 100 |
return; |
| 101 |
} |
| 102 |
try { |
| 103 |
mManager.execute(m); |
| 104 |
} catch (Throwable t) { |
| 105 |
if (!checkStack(t) || (mExceptions.size() == MAX_CRASHES)) { |
| 106 |
throw t; |
| 107 |
} |
| 108 |
mExceptions.add(t); |
| 109 |
Log.d(TAG, "Ignoring exception to run more tests", t); |
| 110 |
} |
| 111 |
mManager.recycle(m); |
| 112 |
} |
| 113 |
} finally { |
| 114 |
mManager.release(); |
| 115 |
synchronized (this) { |
| 116 |
|
| 117 |
notify(); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private boolean checkStack(Throwable t) { |
| 123 |
StackTraceElement topStack = t.getStackTrace()[0]; |
| 124 |
String className = topStack.getClassName(); |
| 125 |
if (className.equals(TestLooperManager.class.getName())) { |
| 126 |
topStack = t.getCause().getStackTrace()[0]; |
| 127 |
className = topStack.getClassName(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
return !className.startsWith("android.") |
| 132 |
&& !className.startsWith("com.android.internal"); |
| 133 |
} |
| 134 |
|
| 135 |
public void destroy() { |
| 136 |
mStopMessage.sendToTarget(); |
| 137 |
if (mExceptions.size() != 0) { |
| 138 |
throw new RuntimeException("Exception caught during tests", mExceptions.get(0)); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public void acquireMain() { |
| 143 |
synchronized (this) { |
| 144 |
mStopMessage.sendToTarget(); |
| 145 |
try { |
| 146 |
wait(); |
| 147 |
} catch (InterruptedException e) { |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public void releaseMain() { |
| 153 |
startManaging(); |
| 154 |
} |
| 155 |
|
| 156 |
private void startManaging() { |
| 157 |
mStopMessage = mMainHandler.obtainMessage(); |
| 158 |
synchronized (this) { |
| 159 |
mManager = acquireLooperManager(Looper.getMainLooper()); |
| 160 |
|
| 161 |
|
| 162 |
new Thread(() -> { |
| 163 |
|
| 164 |
|
| 165 |
mMainHandler.post(this); |
| 166 |
while (!mManager.hasMessages(mMainHandler, null, this)); |
| 167 |
|
| 168 |
Message next = mManager.next(); |
| 169 |
|
| 170 |
while (next.getCallback() != this) { |
| 171 |
mManager.execute(next); |
| 172 |
mManager.recycle(next); |
| 173 |
next = mManager.next(); |
| 174 |
} |
| 175 |
mManager.execute(next); |
| 176 |
}).start(); |
| 177 |
if (Looper.myLooper() != Looper.getMainLooper()) { |
| 178 |
try { |
| 179 |
wait(); |
| 180 |
} catch (InterruptedException e) { |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |