1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.util; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.fail; 21 22 import android.os.ConditionVariable; 23 import android.os.Handler; 24 import android.os.HandlerThread; 25 import android.os.Looper; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 29 import androidx.annotation.NonNull; 30 31 import java.util.concurrent.Executor; 32 33 public final class TestUtils { TestUtils()34 private TestUtils() { } 35 36 /** 37 * Block until the given Handler thread becomes idle, or until timeoutMs has passed. 38 */ waitForIdleHandler(HandlerThread handlerThread, long timeoutMs)39 public static void waitForIdleHandler(HandlerThread handlerThread, long timeoutMs) { 40 waitForIdleLooper(handlerThread.getLooper(), timeoutMs); 41 } 42 43 /** 44 * Block until the given Looper becomes idle, or until timeoutMs has passed. 45 */ waitForIdleLooper(Looper looper, long timeoutMs)46 public static void waitForIdleLooper(Looper looper, long timeoutMs) { 47 waitForIdleHandler(new Handler(looper), timeoutMs); 48 } 49 50 /** 51 * Block until the given Handler becomes idle, or until timeoutMs has passed. 52 */ waitForIdleHandler(Handler handler, long timeoutMs)53 public static void waitForIdleHandler(Handler handler, long timeoutMs) { 54 final ConditionVariable cv = new ConditionVariable(); 55 handler.post(() -> cv.open()); 56 if (!cv.block(timeoutMs)) { 57 fail(handler.toString() + " did not become idle after " + timeoutMs + " ms"); 58 } 59 } 60 61 /** 62 * Block until the given Serial Executor becomes idle, or until timeoutMs has passed. 63 */ waitForIdleSerialExecutor(@onNull Executor executor, long timeoutMs)64 public static void waitForIdleSerialExecutor(@NonNull Executor executor, long timeoutMs) { 65 final ConditionVariable cv = new ConditionVariable(); 66 executor.execute(() -> cv.open()); 67 if (!cv.block(timeoutMs)) { 68 fail(executor.toString() + " did not become idle after " + timeoutMs + " ms"); 69 } 70 } 71 72 /** 73 * Return a new instance of {@code T} after being parceled then unparceled. 74 */ parcelingRoundTrip(T source)75 public static <T extends Parcelable> T parcelingRoundTrip(T source) { 76 final Parcelable.Creator<T> creator; 77 try { 78 creator = (Parcelable.Creator<T>) source.getClass().getField("CREATOR").get(null); 79 } catch (IllegalAccessException | NoSuchFieldException e) { 80 fail("Missing CREATOR field: " + e.getMessage()); 81 return null; 82 } 83 Parcel p = Parcel.obtain(); 84 source.writeToParcel(p, /* flags */ 0); 85 p.setDataPosition(0); 86 final byte[] marshalled = p.marshall(); 87 p = Parcel.obtain(); 88 p.unmarshall(marshalled, 0, marshalled.length); 89 p.setDataPosition(0); 90 return creator.createFromParcel(p); 91 } 92 93 /** 94 * Assert that after being parceled then unparceled, {@code source} is equal to the original 95 * object. 96 */ assertParcelingIsLossless(T source)97 public static <T extends Parcelable> void assertParcelingIsLossless(T source) { 98 assertEquals(source, parcelingRoundTrip(source)); 99 } 100 } 101