• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.os;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertThrows;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.car.Car;
31 import android.content.Context;
32 import android.os.Handler;
33 import android.os.IBinder;
34 import android.os.Looper;
35 import android.os.RemoteException;
36 import android.os.ServiceSpecificException;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 
44 @RunWith(MockitoJUnitRunner.Silent.class)
45 public final class CarPerformanceManagerUnitTest {
46 
47     private Handler mMainHandler;
48 
49     @Mock private Context mContext;
50     @Mock private Car mCar;
51     @Mock private IBinder mBinder;
52     @Mock private ICarPerformanceService mService;
53 
54     private CarPerformanceManager mCarPerformanceManager;
55 
56     @Before
setUp()57     public void setUp() {
58         mMainHandler = new Handler(Looper.getMainLooper());
59 
60         when(mCar.getContext()).thenReturn(mContext);
61         when(mCar.getEventHandler()).thenReturn(mMainHandler);
62         when(mCar.handleRemoteExceptionFromCarService(any(RemoteException.class), any()))
63                 .thenCallRealMethod();
64         when(mBinder.queryLocalInterface(anyString())).thenReturn(mService);
65         mCarPerformanceManager = new CarPerformanceManager(mCar, mBinder);
66     }
67 
68     @Test
testSetThreadPriority()69     public void testSetThreadPriority() throws Exception {
70         ThreadPolicyWithPriority expected = new ThreadPolicyWithPriority(
71                 ThreadPolicyWithPriority.SCHED_FIFO, /* priority= */ 1);
72 
73         mCarPerformanceManager.setThreadPriority(expected);
74 
75         verify(mService).setThreadPriority(anyInt(), eq(expected));
76     }
77 
78     @Test
testSetThreadPriorityServiceSpecificExceptionFromService()79     public void testSetThreadPriorityServiceSpecificExceptionFromService() throws Exception {
80         ThreadPolicyWithPriority p = new ThreadPolicyWithPriority(
81                 ThreadPolicyWithPriority.SCHED_FIFO, /* priority= */ 1);
82         doThrow(new ServiceSpecificException(0)).when(mService).setThreadPriority(anyInt(), eq(p));
83 
84         assertThrows(CarPerformanceManager.SetSchedulerFailedException.class,
85                 () -> mCarPerformanceManager.setThreadPriority(p));
86     }
87 
88     @Test
testSetThreadPriorityRemoteExceptionFromService()89     public void testSetThreadPriorityRemoteExceptionFromService() throws Exception {
90         ThreadPolicyWithPriority p = new ThreadPolicyWithPriority(
91                 ThreadPolicyWithPriority.SCHED_FIFO, /* priority= */ 1);
92         doThrow(new RemoteException("")).when(mService).setThreadPriority(anyInt(), eq(p));
93 
94         // Nothing should be thrown since {@link RemoteException} should be handled.
95         mCarPerformanceManager.setThreadPriority(p);
96     }
97 
98     @Test
testGetThreadPriority()99     public void testGetThreadPriority() throws Exception {
100         ThreadPolicyWithPriority expected = new ThreadPolicyWithPriority(
101                 ThreadPolicyWithPriority.SCHED_FIFO, /* priority= */ 1);
102         when(mService.getThreadPriority(anyInt())).thenReturn(expected);
103 
104         ThreadPolicyWithPriority got = mCarPerformanceManager.getThreadPriority();
105 
106         assertThat(got.getPolicy()).isEqualTo(expected.getPolicy());
107         assertThat(got.getPriority()).isEqualTo(expected.getPriority());
108     }
109 
110     @Test
testGetThreadPriorityRemoteExceptionFromService()111     public void testGetThreadPriorityRemoteExceptionFromService() throws Exception {
112         when(mService.getThreadPriority(anyInt())).thenThrow(new RemoteException(""));
113 
114         ThreadPolicyWithPriority got = mCarPerformanceManager.getThreadPriority();
115 
116         assertThat(got.getPolicy()).isEqualTo(ThreadPolicyWithPriority.SCHED_DEFAULT);
117         assertThat(got.getPriority()).isEqualTo(0);
118     }
119 }
120