1 /*
2  * Copyright (C) 2020 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 package android.car.test.mocks;
17 
18 import android.annotation.NonNull;
19 import android.util.Log;
20 
21 import org.mockito.invocation.InvocationOnMock;
22 import org.mockito.stubbing.Answer;
23 
24 import java.util.concurrent.CountDownLatch;
25 
26 /**
27  * Custom Mockito {@link Answer} used to block the test until it's invoked.
28  *
29  * <p>Example:
30  *
31  * <pre><code>
32  *
33  * SyncAnswer<UserInfo> syncUserInfo = SyncAnswer.forReturn(user);
34  * when(mMock.preCreateUser(userType)).thenAnswer(syncUserInfo);
35  * objectUnderTest.preCreate();
36  * syncUserInfo.await(100);
37  *
38  * </code></pre>
39  */
40 public final class SyncAnswer<T> implements Answer<T> {
41 
42     private static final String TAG = SyncAnswer.class.getSimpleName();
43 
44     private final CountDownLatch mLatch = new CountDownLatch(1);
45     private final T mAnswer;
46     private final Throwable mException;
47 
SyncAnswer(T answer, Throwable exception)48     private SyncAnswer(T answer, Throwable exception) {
49         mAnswer = answer;
50         mException = exception;
51     }
52 
53     /**
54      * Creates an answer that returns a value.
55      */
56     @NonNull
forReturn(@onNull T value)57     public static <T> SyncAnswer<T> forReturn(@NonNull T value) {
58         return new SyncAnswer<T>(value, /* exception= */ null);
59     }
60 
61     /**
62      * Creates an answer that throws an exception.
63      */
64     @NonNull
forException(@onNull Throwable t)65     public static <T> SyncAnswer<T> forException(@NonNull Throwable t) {
66         return new SyncAnswer<T>(/* value= */ null, t);
67     }
68 
69     /**
70      * Wait until the answer is invoked, or times out.
71      */
await(long timeoutMs)72     public void await(long timeoutMs) throws InterruptedException {
73         JavaMockitoHelper.await(mLatch, timeoutMs);
74     }
75 
76     @Override
answer(InvocationOnMock invocation)77     public T answer(InvocationOnMock invocation) throws Throwable {
78         Log.v(TAG, "answering " + invocation);
79         mLatch.countDown();
80         if (mException != null) {
81             throw mException;
82         }
83         return mAnswer;
84     }
85 
86 }
87