|
1 | 1 | package androidx.test.espresso.base;
|
2 | 2 |
|
| 3 | +import static androidx.test.internal.util.Checks.checkNotNull; |
| 4 | + |
| 5 | +import android.os.Build.VERSION; |
| 6 | +import android.os.Build.VERSION_CODES; |
3 | 7 | import android.os.Looper;
|
4 | 8 | import android.os.Message;
|
5 | 9 | import android.os.MessageQueue;
|
| 10 | +import android.os.TestLooperManager; |
6 | 11 | import androidx.annotation.Nullable;
|
| 12 | +import androidx.test.platform.app.InstrumentationRegistry; |
7 | 13 | import java.lang.reflect.Field;
|
8 | 14 | import java.lang.reflect.Method;
|
9 | 15 |
|
|
13 | 19 | *
|
14 | 20 | * <p>Unlike the real TestLooperManager this only supports being used from the Looper's thread.
|
15 | 21 | */
|
16 |
| -class TestLooperManagerCompat { |
| 22 | +@SuppressWarnings("NonFinalStaticField") |
| 23 | +final class TestLooperManagerCompat { |
17 | 24 |
|
18 |
| - private static final Method messageQueueNextMethod; |
19 |
| - private static final Field messageQueueHeadField; |
20 |
| - private static final Method recycleUncheckedMethod; |
| 25 | + private static Method messageQueueNextMethod; |
| 26 | + private static Field messageQueueHeadField; |
| 27 | + private static Method recycleUncheckedMethod; |
| 28 | + private static Method peekWhenMethod; |
| 29 | + private static Method blockedOnBarrierMethod; |
| 30 | + |
| 31 | + private static boolean initTestLooperManager() { |
| 32 | + // TODO(b/112000181): update this check and remove reflection when compiling against Baklava |
| 33 | + if (VERSION.SDK_INT >= VERSION_CODES.VANILLA_ICE_CREAM) { |
| 34 | + try { |
| 35 | + peekWhenMethod = TestLooperManager.class.getDeclaredMethod("peekWhen"); |
| 36 | + blockedOnBarrierMethod = |
| 37 | + TestLooperManager.class.getDeclaredMethod("isBlockedOnSyncBarrier"); |
| 38 | + return true; |
| 39 | + } catch (ReflectiveOperationException e) { |
| 40 | + // fall through |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | + } |
21 | 45 |
|
22 | 46 | static {
|
23 | 47 | try {
|
24 |
| - messageQueueNextMethod = MessageQueue.class.getDeclaredMethod("next"); |
25 |
| - messageQueueNextMethod.setAccessible(true); |
26 |
| - messageQueueHeadField = MessageQueue.class.getDeclaredField("mMessages"); |
27 |
| - messageQueueHeadField.setAccessible(true); |
28 |
| - recycleUncheckedMethod = Message.class.getDeclaredMethod("recycleUnchecked"); |
29 |
| - recycleUncheckedMethod.setAccessible(true); |
| 48 | + if (!initTestLooperManager()) { |
| 49 | + messageQueueNextMethod = MessageQueue.class.getDeclaredMethod("next"); |
| 50 | + messageQueueNextMethod.setAccessible(true); |
| 51 | + messageQueueHeadField = MessageQueue.class.getDeclaredField("mMessages"); |
| 52 | + messageQueueHeadField.setAccessible(true); |
| 53 | + recycleUncheckedMethod = Message.class.getDeclaredMethod("recycleUnchecked"); |
| 54 | + recycleUncheckedMethod.setAccessible(true); |
| 55 | + } |
30 | 56 | } catch (ReflectiveOperationException e) {
|
31 | 57 | throw new RuntimeException(e);
|
32 | 58 | }
|
33 | 59 | }
|
34 | 60 |
|
35 | 61 | private final MessageQueue queue;
|
36 | 62 |
|
| 63 | + // the TestLooperManager to defer to. Will only be non-null if running |
| 64 | + // on an Android API level that supports it |
| 65 | + private final TestLooperManager delegate; |
| 66 | + |
37 | 67 | private TestLooperManagerCompat(MessageQueue queue) {
|
38 | 68 | this.queue = queue;
|
| 69 | + this.delegate = null; |
| 70 | + } |
| 71 | + |
| 72 | + private TestLooperManagerCompat(TestLooperManager testLooperManager) { |
| 73 | + this.queue = null; |
| 74 | + this.delegate = testLooperManager; |
39 | 75 | }
|
40 | 76 |
|
41 | 77 | static TestLooperManagerCompat acquire(Looper looper) {
|
42 |
| - return new TestLooperManagerCompat(looper.getQueue()); |
| 78 | + if (peekWhenMethod != null) { |
| 79 | + // running on a newer Android version that has the supported TestLooperManagerCompat changes |
| 80 | + TestLooperManager testLooperManager = |
| 81 | + InstrumentationRegistry.getInstrumentation().acquireLooperManager(looper); |
| 82 | + return new TestLooperManagerCompat(testLooperManager); |
| 83 | + } else { |
| 84 | + return new TestLooperManagerCompat(looper.getQueue()); |
| 85 | + } |
43 | 86 | }
|
44 | 87 |
|
45 | 88 | @Nullable
|
46 | 89 | Long peekWhen() {
|
47 |
| - Message msg = legacyPeek(); |
48 |
| - if (msg != null && msg.getTarget() == null) { |
49 |
| - return null; |
| 90 | + try { |
| 91 | + if (delegate != null) { |
| 92 | + return (Long) peekWhenMethod.invoke(delegate); |
| 93 | + } else { |
| 94 | + Message msg = legacyPeek(); |
| 95 | + if (msg != null && msg.getTarget() == null) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + return msg == null ? null : msg.getWhen(); |
| 99 | + } |
| 100 | + } catch (ReflectiveOperationException e) { |
| 101 | + throw new RuntimeException(e); |
50 | 102 | }
|
51 |
| - return msg == null ? null : msg.getWhen(); |
52 | 103 | }
|
53 | 104 |
|
54 | 105 | @Nullable
|
55 |
| - private Message legacyPeek() { |
| 106 | + private Message legacyPeek() throws IllegalAccessException { |
| 107 | + checkNotNull(queue); |
56 | 108 | // the legacy MessageQueue implementation synchronizes on itself,
|
57 | 109 | // so this uses the same lock
|
58 | 110 | synchronized (queue) {
|
59 |
| - try { |
60 |
| - return (Message) messageQueueHeadField.get(queue); |
61 |
| - } catch (IllegalAccessException e) { |
62 |
| - throw new RuntimeException(e); |
63 |
| - } |
| 111 | + return (Message) messageQueueHeadField.get(queue); |
64 | 112 | }
|
65 | 113 | }
|
66 | 114 |
|
67 | 115 | void execute(Message message) {
|
68 |
| - message.getTarget().dispatchMessage(message); |
| 116 | + if (delegate != null) { |
| 117 | + delegate.execute(message); |
| 118 | + } else { |
| 119 | + message.getTarget().dispatchMessage(message); |
| 120 | + } |
69 | 121 | }
|
70 | 122 |
|
71 | 123 | void release() {
|
72 |
| - // ignore for now |
| 124 | + if (delegate != null) { |
| 125 | + delegate.release(); |
| 126 | + } |
73 | 127 | }
|
74 | 128 |
|
75 | 129 | boolean isBlockedOnSyncBarrier() {
|
76 |
| - Message msg = legacyPeek(); |
77 |
| - return msg != null && msg.getTarget() == null; |
| 130 | + try { |
| 131 | + if (delegate != null) { |
| 132 | + return (boolean) blockedOnBarrierMethod.invoke(delegate); |
| 133 | + } else { |
| 134 | + Message msg = legacyPeek(); |
| 135 | + return msg != null && msg.getTarget() == null; |
| 136 | + } |
| 137 | + } catch (ReflectiveOperationException e) { |
| 138 | + throw new RuntimeException(e); |
| 139 | + } |
78 | 140 | }
|
79 | 141 |
|
80 | 142 | Message next() {
|
81 | 143 | try {
|
82 |
| - return (Message) messageQueueNextMethod.invoke(queue); |
| 144 | + if (delegate != null) { |
| 145 | + return delegate.next(); |
| 146 | + } else { |
| 147 | + return (Message) messageQueueNextMethod.invoke(queue); |
| 148 | + } |
83 | 149 | } catch (ReflectiveOperationException e) {
|
84 | 150 | throw new RuntimeException(e);
|
85 | 151 | }
|
86 | 152 | }
|
87 | 153 |
|
88 | 154 | void recycle(Message m) {
|
89 | 155 | try {
|
90 |
| - recycleUncheckedMethod.invoke(m); |
| 156 | + if (delegate != null) { |
| 157 | + delegate.recycle(m); |
| 158 | + } else { |
| 159 | + recycleUncheckedMethod.invoke(m); |
| 160 | + } |
91 | 161 | } catch (ReflectiveOperationException e) {
|
92 | 162 | throw new RuntimeException(e);
|
93 | 163 | }
|
|
0 commit comments