• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package android.car.util.concurrent;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.MockitoSession;
28 import org.mockito.junit.MockitoJUnitRunner;
29 
30 import java.util.concurrent.Executor;
31 import java.util.concurrent.TimeUnit;
32 import java.util.function.BiConsumer;
33 
34 @RunWith(MockitoJUnitRunner.class)
35 public final class AndroidAsyncFutureTest {
36 
37     @Mock
38     private AndroidFuture<Integer> mFuture;
39 
40     private MockitoSession mMockSession;
41 
42     private AndroidAsyncFuture<Integer> mAndroidAsyncFuture;
43 
44     @Before
setup()45     public void setup() {
46         mAndroidAsyncFuture = new AndroidAsyncFuture<Integer>(mFuture);
47     }
48 
49     @Test
testGet()50     public void testGet() throws Exception {
51         Integer input = 0;
52         when(mFuture.get()).thenReturn(input);
53 
54         Integer result = mAndroidAsyncFuture.get();
55 
56         assertThat(result).isEqualTo(input);
57         verify(mFuture).get();
58     }
59 
60     @Test
testGetWithTimeout()61     public void testGetWithTimeout() throws Exception {
62         long timeout = 100;
63         TimeUnit timeUnit = TimeUnit.SECONDS;
64         Integer input = 0;
65         when(mFuture.get(timeout, timeUnit)).thenReturn(input);
66 
67         Integer result = mAndroidAsyncFuture.get(timeout, timeUnit);
68 
69         assertThat(result).isEqualTo(input);
70         verify(mFuture).get(timeout, timeUnit);
71     }
72 
73     @Test
testWhenCompleteAsync()74     public void testWhenCompleteAsync() throws Exception {
75         Executor executor = (a) -> {};
76         BiConsumer<Integer, Throwable> biConsumer = (l, m) -> {};
77 
78         AsyncFuture asyncFuture = mAndroidAsyncFuture.whenCompleteAsync(biConsumer, executor);
79 
80         assertThat(asyncFuture).isEqualTo(mAndroidAsyncFuture);
81         verify(mFuture).whenCompleteAsync(biConsumer, executor);
82     }
83 }
84