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