Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
Jason Monk | 340b0e5 | 2017-03-08 14:57:56 -0500 | [diff] [blame] | 15 | package android.testing; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 16 | |
| 17 | import android.os.Handler; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 18 | import android.os.HandlerThread; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 19 | import android.os.Looper; |
| 20 | import android.os.Message; |
| 21 | import android.os.MessageQueue; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 22 | import android.os.TestLooperManager; |
| 23 | import android.support.test.InstrumentationRegistry; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 24 | import android.util.ArrayMap; |
| 25 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 26 | import org.junit.runners.model.FrameworkMethod; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 27 | |
| 28 | import java.lang.annotation.ElementType; |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | import java.lang.annotation.Target; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 32 | import java.util.Map; |
| 33 | |
| 34 | /** |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 35 | * This is a wrapper around {@link TestLooperManager} to make it easier to manage |
| 36 | * and provide an easy annotation for use with tests. |
| 37 | * |
| 38 | * @see TestableLooperTest TestableLooperTest for examples. |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 39 | */ |
| 40 | public class TestableLooper { |
| 41 | |
Jason Monk | 759e912 | 2018-07-20 14:52:22 -0400 | [diff] [blame] | 42 | /** |
| 43 | * Whether to hold onto the main thread through all tests in an attempt to |
| 44 | * catch crashes. |
| 45 | */ |
| 46 | public static final boolean HOLD_MAIN_THREAD = false; |
| 47 | |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 48 | private Looper mLooper; |
| 49 | private MessageQueue mQueue; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 50 | private MessageHandler mMessageHandler; |
| 51 | |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 52 | private Handler mHandler; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 53 | private Runnable mEmptyMessage; |
| 54 | private TestLooperManager mQueueWrapper; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 55 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 56 | public TestableLooper(Looper l) throws Exception { |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 57 | this(acquireLooperManager(l), l); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 60 | private TestableLooper(TestLooperManager wrapper, Looper l) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 61 | mQueueWrapper = wrapper; |
| 62 | setupQueue(l); |
| 63 | } |
| 64 | |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 65 | private TestableLooper(Looper looper, boolean b) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 66 | setupQueue(looper); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public Looper getLooper() { |
| 70 | return mLooper; |
| 71 | } |
| 72 | |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 73 | private void setupQueue(Looper l) { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 74 | mLooper = l; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 75 | mQueue = mLooper.getQueue(); |
| 76 | mHandler = new Handler(mLooper); |
| 77 | } |
| 78 | |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 79 | /** |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 80 | * Must be called to release the looper when the test is complete, otherwise |
| 81 | * the looper will not be available for any subsequent tests. This is |
| 82 | * automatically handled for tests using {@link RunWithLooper}. |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 83 | */ |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 84 | public void destroy() { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 85 | mQueueWrapper.release(); |
Jason Monk | 759e912 | 2018-07-20 14:52:22 -0400 | [diff] [blame] | 86 | if (HOLD_MAIN_THREAD && mLooper == Looper.getMainLooper()) { |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 87 | TestableInstrumentation.releaseMain(); |
| 88 | } |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 91 | /** |
| 92 | * Sets a callback for all messages processed on this TestableLooper. |
| 93 | * |
| 94 | * @see {@link MessageHandler} |
| 95 | */ |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 96 | public void setMessageHandler(MessageHandler handler) { |
| 97 | mMessageHandler = handler; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Parse num messages from the message queue. |
| 102 | * |
| 103 | * @param num Number of messages to parse |
| 104 | */ |
| 105 | public int processMessages(int num) { |
| 106 | for (int i = 0; i < num; i++) { |
| 107 | if (!parseMessageInt()) { |
| 108 | return i + 1; |
| 109 | } |
| 110 | } |
| 111 | return num; |
| 112 | } |
| 113 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 114 | /** |
| 115 | * Process messages in the queue until no more are found. |
| 116 | */ |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 117 | public void processAllMessages() { |
| 118 | while (processQueuedMessages() != 0) ; |
| 119 | } |
| 120 | |
| 121 | private int processQueuedMessages() { |
| 122 | int count = 0; |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 123 | mEmptyMessage = () -> { }; |
| 124 | mHandler.post(mEmptyMessage); |
| 125 | waitForMessage(mQueueWrapper, mHandler, mEmptyMessage); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 126 | while (parseMessageInt()) count++; |
| 127 | return count; |
| 128 | } |
| 129 | |
| 130 | private boolean parseMessageInt() { |
| 131 | try { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 132 | Message result = mQueueWrapper.next(); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 133 | if (result != null) { |
| 134 | // This is a break message. |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 135 | if (result.getCallback() == mEmptyMessage) { |
| 136 | mQueueWrapper.recycle(result); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | if (mMessageHandler != null) { |
| 141 | if (mMessageHandler.onMessageHandled(result)) { |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 142 | mQueueWrapper.execute(result); |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 143 | mQueueWrapper.recycle(result); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 144 | } else { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 145 | mQueueWrapper.recycle(result); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 146 | // Message handler indicated it doesn't want us to continue. |
| 147 | return false; |
| 148 | } |
| 149 | } else { |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 150 | mQueueWrapper.execute(result); |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 151 | mQueueWrapper.recycle(result); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 152 | } |
| 153 | } else { |
| 154 | // No messages, don't continue parsing |
| 155 | return false; |
| 156 | } |
| 157 | } catch (Exception e) { |
| 158 | throw new RuntimeException(e); |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Runs an executable with myLooper set and processes all messages added. |
| 165 | */ |
| 166 | public void runWithLooper(RunnableWithException runnable) throws Exception { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 167 | new Handler(getLooper()).post(() -> { |
| 168 | try { |
| 169 | runnable.run(); |
| 170 | } catch (Exception e) { |
| 171 | throw new RuntimeException(e); |
| 172 | } |
| 173 | }); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 174 | processAllMessages(); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | public interface RunnableWithException { |
| 178 | void run() throws Exception; |
| 179 | } |
| 180 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 181 | /** |
| 182 | * Annotation that tells the {@link AndroidTestingRunner} to create a TestableLooper and |
| 183 | * run this test/class on that thread. The {@link TestableLooper} can be acquired using |
| 184 | * {@link #get(Object)}. |
| 185 | */ |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 186 | @Retention(RetentionPolicy.RUNTIME) |
| 187 | @Target({ElementType.METHOD, ElementType.TYPE}) |
| 188 | public @interface RunWithLooper { |
| 189 | boolean setAsMainLooper() default false; |
| 190 | } |
| 191 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 192 | private static void waitForMessage(TestLooperManager queueWrapper, Handler handler, |
| 193 | Runnable execute) { |
| 194 | for (int i = 0; i < 10; i++) { |
| 195 | if (!queueWrapper.hasMessages(handler, null, execute)) { |
| 196 | try { |
| 197 | Thread.sleep(1); |
| 198 | } catch (InterruptedException e) { |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if (!queueWrapper.hasMessages(handler, null, execute)) { |
| 203 | throw new RuntimeException("Message didn't queue..."); |
| 204 | } |
| 205 | } |
| 206 | |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 207 | private static TestLooperManager acquireLooperManager(Looper l) { |
Jason Monk | 759e912 | 2018-07-20 14:52:22 -0400 | [diff] [blame] | 208 | if (HOLD_MAIN_THREAD && l == Looper.getMainLooper()) { |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 209 | TestableInstrumentation.acquireMain(); |
| 210 | } |
| 211 | return InstrumentationRegistry.getInstrumentation().acquireLooperManager(l); |
| 212 | } |
| 213 | |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 214 | private static final Map<Object, TestableLooper> sLoopers = new ArrayMap<>(); |
| 215 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 216 | /** |
| 217 | * For use with {@link RunWithLooper}, used to get the TestableLooper that was |
| 218 | * automatically created for this test. |
| 219 | */ |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 220 | public static TestableLooper get(Object test) { |
| 221 | return sLoopers.get(test); |
| 222 | } |
| 223 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 224 | static class LooperFrameworkMethod extends FrameworkMethod { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 225 | private HandlerThread mHandlerThread; |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 226 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 227 | private final TestableLooper mTestableLooper; |
| 228 | private final Looper mLooper; |
| 229 | private final Handler mHandler; |
| 230 | |
| 231 | public LooperFrameworkMethod(FrameworkMethod base, boolean setAsMain, Object test) { |
| 232 | super(base.getMethod()); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 233 | try { |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 234 | mLooper = setAsMain ? Looper.getMainLooper() : createLooper(); |
| 235 | mTestableLooper = new TestableLooper(mLooper, false); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 236 | } catch (Exception e) { |
| 237 | throw new RuntimeException(e); |
| 238 | } |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 239 | sLoopers.put(test, mTestableLooper); |
| 240 | mHandler = new Handler(mLooper); |
| 241 | } |
| 242 | |
| 243 | public LooperFrameworkMethod(TestableLooper other, FrameworkMethod base) { |
| 244 | super(base.getMethod()); |
| 245 | mLooper = other.mLooper; |
| 246 | mTestableLooper = other; |
Jason Monk | 1e352f4 | 2018-05-16 10:15:33 -0400 | [diff] [blame] | 247 | mHandler = Handler.createAsync(mLooper); |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) { |
| 251 | if (sLoopers.containsKey(test)) { |
| 252 | return new LooperFrameworkMethod(sLoopers.get(test), base); |
| 253 | } |
| 254 | return new LooperFrameworkMethod(base, setAsMain, test); |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 258 | public Object invokeExplosively(Object target, Object... params) throws Throwable { |
| 259 | if (Looper.myLooper() == mLooper) { |
| 260 | // Already on the right thread from another statement, just execute then. |
| 261 | return super.invokeExplosively(target, params); |
| 262 | } |
| 263 | boolean set = mTestableLooper.mQueueWrapper == null; |
| 264 | if (set) { |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 265 | mTestableLooper.mQueueWrapper = acquireLooperManager(mLooper); |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 266 | } |
| 267 | try { |
| 268 | Object[] ret = new Object[1]; |
| 269 | // Run the execution on the looper thread. |
| 270 | Runnable execute = () -> { |
| 271 | try { |
| 272 | ret[0] = super.invokeExplosively(target, params); |
| 273 | } catch (Throwable throwable) { |
| 274 | throw new LooperException(throwable); |
| 275 | } |
| 276 | }; |
| 277 | Message m = Message.obtain(mHandler, execute); |
| 278 | |
| 279 | // Dispatch our message. |
| 280 | try { |
| 281 | mTestableLooper.mQueueWrapper.execute(m); |
| 282 | } catch (LooperException e) { |
| 283 | throw e.getSource(); |
| 284 | } catch (RuntimeException re) { |
| 285 | // If the TestLooperManager has to post, it will wrap what it throws in a |
| 286 | // RuntimeException, make sure we grab the actual source. |
| 287 | if (re.getCause() instanceof LooperException) { |
| 288 | throw ((LooperException) re.getCause()).getSource(); |
| 289 | } else { |
| 290 | throw re.getCause(); |
| 291 | } |
| 292 | } finally { |
| 293 | m.recycle(); |
| 294 | } |
| 295 | return ret[0]; |
| 296 | } finally { |
| 297 | if (set) { |
| 298 | mTestableLooper.mQueueWrapper.release(); |
| 299 | mTestableLooper.mQueueWrapper = null; |
Jason Monk | 759e912 | 2018-07-20 14:52:22 -0400 | [diff] [blame] | 300 | if (HOLD_MAIN_THREAD && mLooper == Looper.getMainLooper()) { |
Jason Monk | c429f69 | 2017-06-27 13:13:49 -0400 | [diff] [blame] | 301 | TestableInstrumentation.releaseMain(); |
| 302 | } |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | private Looper createLooper() { |
| 308 | // TODO: Find way to share these. |
| 309 | mHandlerThread = new HandlerThread(TestableLooper.class.getSimpleName()); |
| 310 | mHandlerThread.start(); |
| 311 | return mHandlerThread.getLooper(); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | protected void finalize() throws Throwable { |
| 316 | super.finalize(); |
| 317 | if (mHandlerThread != null) { |
| 318 | mHandlerThread.quit(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | private static class LooperException extends RuntimeException { |
| 323 | private final Throwable mSource; |
| 324 | |
| 325 | public LooperException(Throwable t) { |
| 326 | mSource = t; |
Jason Monk | f715f41 | 2017-03-14 17:16:56 -0400 | [diff] [blame] | 327 | } |
Jason Monk | fd8f615 | 2017-03-24 12:44:34 +0000 | [diff] [blame] | 328 | |
Jason Monk | 745d0a8 | 2017-04-17 11:34:22 -0400 | [diff] [blame] | 329 | public Throwable getSource() { |
| 330 | return mSource; |
Jason Monk | 9ec0f1e | 2017-02-23 11:33:54 -0500 | [diff] [blame] | 331 | } |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Jason Monk | 0c40800 | 2017-05-03 15:43:52 -0400 | [diff] [blame] | 335 | /** |
| 336 | * Callback to control the execution of messages on the looper, when set with |
| 337 | * {@link #setMessageHandler(MessageHandler)} then {@link #onMessageHandled(Message)} |
| 338 | * will get called back for every message processed on the {@link TestableLooper}. |
| 339 | */ |
Jason Monk | e750748 | 2017-02-02 13:00:05 -0500 | [diff] [blame] | 340 | public interface MessageHandler { |
| 341 | /** |
| 342 | * Return true to have the message executed and delivered to target. |
| 343 | * Return false to not execute the message and stop executing messages. |
| 344 | */ |
| 345 | boolean onMessageHandled(Message m); |
| 346 | } |
| 347 | } |