• 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 
17 package android.car.test.mocks;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doAnswer;
22 import static org.mockito.Mockito.when;
23 
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.atomic.AtomicBoolean;
32 
33 @RunWith(MockitoJUnitRunner.class)
34 public final class BlockingAnswerTest {
35 
36     private static final long TIMEOUT_MS = 2_000L;
37 
38     @Mock private SomeService mService;
39 
40     @Test
testForReturn()41     public void testForReturn() throws Exception {
42         CountDownLatch latch = new CountDownLatch(1);
43         int returnValue = 1;
44         when(mService.intMethod()).thenReturn(returnValue);
45         final AtomicBoolean invoked = new AtomicBoolean();
46         BlockingAnswer<Integer> blockingAnswer = BlockingAnswer.forReturn(TIMEOUT_MS, inv -> {
47             invoked.set(true);
48             latch.countDown();
49         }, returnValue);
50         when(mService.intMethod()).thenAnswer(blockingAnswer);
51 
52         mService.intMethod();
53 
54         // By unblocking we release the code triggered by blocking answer to run on a
55         // separated thread.
56         blockingAnswer.unblock();
57 
58         // Since code from BlockingAnswer is triggered in a separated thread, will use the
59         // countdown latch to sync up.
60         latch.await(1, TimeUnit.SECONDS);
61         assertThat(invoked.get()).isTrue();
62     }
63 
64     @Test
testForVoidReturn()65     public void testForVoidReturn() throws Exception {
66         CountDownLatch latch = new CountDownLatch(1);
67         final AtomicBoolean invoked = new AtomicBoolean();
68         BlockingAnswer<Void> blockingAnswer = BlockingAnswer.forVoidReturn(TIMEOUT_MS, inv -> {
69             invoked.set(true);
70             latch.countDown();
71         });
72         doAnswer(blockingAnswer).when(mService).voidMethod();
73 
74         mService.voidMethod();
75 
76         // By unblocking we release the code triggered by blocking answer to run on a
77         // separated thread.
78         blockingAnswer.unblock();
79 
80         // Since code from BlockingAnswer is triggered in a separated thread, will use the
81         // countdown latch to sync up.
82         latch.await(1, TimeUnit.SECONDS);
83         assertThat(invoked.get()).isTrue();
84     }
85 
86     private interface SomeService {
87 
voidMethod()88         void voidMethod();
89 
intMethod()90         int intMethod();
91     }
92 }
93