Merge "Added test for AndroidAsyncFuture" into sc-dev
diff --git a/tests/carservice_unit_test/src/android/car/util/concurrent/AndroidAsyncFutureTest.java b/tests/carservice_unit_test/src/android/car/util/concurrent/AndroidAsyncFutureTest.java
new file mode 100644
index 0000000..85b8d5e
--- /dev/null
+++ b/tests/carservice_unit_test/src/android/car/util/concurrent/AndroidAsyncFutureTest.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.car.util.concurrent;
+
+import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.android.internal.infra.AndroidFuture;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoSession;
+import org.mockito.quality.Strictness;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.TimeUnit;
+import java.util.function.BiConsumer;
+
+public final class AndroidAsyncFutureTest {
+
+ @Mock
+ private AndroidFuture<Integer> mFuture;
+
+ private MockitoSession mMockSession;
+
+ private AndroidAsyncFuture<Integer> mAndroidAsyncFuture;
+
+ @Before
+ public void setupMocks() {
+ mMockSession = mockitoSession()
+ .initMocks(this)
+ .strictness(Strictness.LENIENT)
+ .startMocking();
+ mAndroidAsyncFuture = new AndroidAsyncFuture<Integer>(mFuture);
+ }
+
+ @After
+ public void tearDown() {
+ mMockSession.finishMocking();
+ }
+
+ @Test
+ public void testGet() throws Exception {
+ Integer input = 0;
+ when(mFuture.get()).thenReturn(input);
+
+ Integer result = mAndroidAsyncFuture.get();
+
+ assertThat(result).isEqualTo(input);
+ verify(mFuture).get();
+ }
+
+ @Test
+ public void testGetWithTimeout() throws Exception {
+ long timeout = 100;
+ TimeUnit timeUnit = TimeUnit.SECONDS;
+ Integer input = 0;
+ when(mFuture.get(timeout, timeUnit)).thenReturn(input);
+
+ Integer result = mAndroidAsyncFuture.get(timeout, timeUnit);
+
+ assertThat(result).isEqualTo(input);
+ verify(mFuture).get(timeout, timeUnit);
+ }
+
+ @Test
+ public void testWhenCompleteAsync() throws Exception {
+ Executor executor = (a) -> {};
+ BiConsumer<Integer, Throwable> biConsumer = (l, m) -> {};
+
+ AsyncFuture asyncFuture = mAndroidAsyncFuture.whenCompleteAsync(biConsumer, executor);
+
+ assertThat(asyncFuture).isEqualTo(mAndroidAsyncFuture);
+ verify(mFuture).whenCompleteAsync(biConsumer, executor);
+ }
+}