1 /*
2  * Copyright (C) 2023 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.adservices.common;
18 
19 import android.adservices.common.AdServicesOutcomeReceiver;
20 
21 import com.android.adservices.shared.testing.Nullable;
22 import com.android.adservices.shared.testing.concurrency.FailableResultSyncCallback;
23 
24 /**
25  * Stub implementation of {@link AdServicesOutcomeReceiver} for tests.
26  *
27  * <p>See javadoc for individual methods on {@link
28  * com.android.adservices.shared.testing.OutcomeReceiverForTests}.
29  *
30  * @param <T> type of outcome
31  */
32 public final class AdServicesOutcomeReceiverForTests<T>
33         extends FailableResultSyncCallback<T, Exception>
34         implements AdServicesOutcomeReceiver<T, Exception> {
35 
36     @Override
onResult(T result)37     public void onResult(T result) {
38         injectResult(result);
39     }
40 
41     @Override
onError(Exception error)42     public void onError(Exception error) {
43         injectFailure(error);
44     }
45 
46     /** {@link com.android.adservices.shared.testing.OutcomeReceiverForTests#assertSuccess()}. */
assertSuccess()47     public T assertSuccess() throws InterruptedException {
48         return assertResultReceived();
49     }
50 
51     /**
52      * {@link com.android.adservices.shared.testing.OutcomeReceiverForTests#assertFailure(Class)}.
53      */
assertFailure(Class<E> expectedClass)54     public <E extends Exception> E assertFailure(Class<E> expectedClass)
55             throws InterruptedException {
56         return assertFailureReceived(expectedClass);
57     }
58 
59     /** Same as {@link #assertFailureReceived()}. */
assertErrorReceived()60     public Exception assertErrorReceived() throws InterruptedException {
61         return assertFailureReceived();
62     }
63 
64     /** {@link com.android.adservices.shared.testing.OutcomeReceiverForTests#getError()}. */
getError()65     public @Nullable Exception getError() {
66         return getFailure();
67     }
68 }
69