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     /**
48      * A default {@link BlockingCallback} used when a specific interface is not required.
49      */
50     public static class DefaultBlockingCallback<E> extends BlockingCallback<E> {
triggerCallback(E e)51         public void triggerCallback(E e) {
52             callbackTriggered(e);
53         }
54     }
55 
56     /**
57      * A default {@link BlockingCallback} used when there is no data passed to the callback.
58      */
59     public static class VoidBlockingCallback extends BlockingCallback<Void> {
triggerCallback()60         public void triggerCallback() {
61             callbackTriggered(null);
62         }
63     }
64 
65     /**
66      * Blocking version of {@link Runnable}.
67      */
68     public static class BlockingRunnable extends VoidBlockingCallback implements Runnable {
69         @Override
run()70         public void run() {
71             triggerCallback();
72         }
73     }
74 
75     /** Call this method from the callback method to mark the response as received. */
callbackTriggered(E value)76     protected void callbackTriggered(E value) {
77         mValue.set(value);
78         mLatch.countDown();
79     }
80 
81     /**
82      * Fetch the value passed into the callback.
83      *
84      * <p>Throws an {@link AssertionError} if the callback is not triggered in
85      * {@link #DEFAULT_TIMEOUT_SECONDS} seconds.
86      */
await()87     public E await() throws InterruptedException {
88         return await(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
89     }
90 
91     /**
92      * Fetch the value passed into the callback.
93      *
94      * <p>Throws an {@link AssertionError} if the callback is not triggered before the timeout
95      * elapses.
96      */
await(long timeout, TimeUnit unit)97     public E await(long timeout, TimeUnit unit) throws InterruptedException {
98         if (!mLatch.await(timeout, unit)) {
99             fail("Callback was not received");
100         }
101         return mValue.get();
102     }
103 }
104