1 /* 2 * Copyright (C) 2021 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.compatibility.common.util; 18 19 import static junit.framework.TestCase.fail; 20 21 import java.util.concurrent.CountDownLatch; 22 import java.util.concurrent.TimeUnit; 23 import java.util.concurrent.atomic.AtomicReference; 24 25 /** 26 * Subclass this to create a blocking version of a callback. For example: 27 * 28 * {@code 29 * private static class KeyChainAliasCallback extends BlockingCallback<String> implements 30 * android.security.KeyChainAliasCallback { 31 * @Override 32 * public void alias(final String chosenAlias) { 33 * callbackTriggered(chosenAlias); 34 * } 35 * } 36 * } 37 * 38 * <p>an instance of KeyChainAliasCallback can then be passed into a method, and the result can 39 * be fetched using {@code .await()}; 40 */ 41 public abstract class BlockingCallback<E> { 42 private static final int DEFAULT_TIMEOUT_SECONDS = 120; 43 44 private final CountDownLatch mLatch = new CountDownLatch(1); 45 private AtomicReference<E> mValue = new AtomicReference<>(); 46 47 /** Call this method from the callback method to mark the response as received. */ callbackTriggered(E value)48 protected void callbackTriggered(E value) { 49 mValue.set(value); 50 mLatch.countDown(); 51 } 52 53 /** 54 * Fetch the value passed into the callback. 55 * 56 * <p>Throws an {@link AssertionError} if the callback is not triggered in 57 * {@link #DEFAULT_TIMEOUT_SECONDS} seconds. 58 */ await()59 public E await() throws InterruptedException { 60 return await(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS); 61 } 62 63 /** 64 * Fetch the value passed into the callback. 65 * 66 * <p>Throws an {@link AssertionError} if the callback is not triggered before the timeout 67 * elapses. 68 */ await(long timeout, TimeUnit unit)69 public E await(long timeout, TimeUnit unit) throws InterruptedException { 70 if (!mLatch.await(timeout, unit)) { 71 fail("Callback was not received"); 72 } 73 return mValue.get(); 74 } 75 } 76