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 com.android.server.uwb;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ResolveInfo;
28 import android.os.BugreportManager;
29 import android.platform.test.annotations.Presubmit;
30 
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 import java.util.List;
41 
42 /**
43  * Unit tests for {@link com.android.server.uwb.UwbDiagnostics}.
44  */
45 @RunWith(AndroidJUnit4.class)
46 @SmallTest
47 @Presubmit
48 public class UwbDiagnosticsTest {
49     @Mock SystemBuildProperties mBuildProperties;
50     @Mock Context mContext;
51     @Mock UwbInjector mUwbInjector;
52     @Mock DeviceConfigFacade mDeviceConfigFacade;
53     @Mock BugreportManager mBugreportManager;
54     @Mock PackageManager mPackageManager;
55     @Mock List<ResolveInfo>  mResolveInfoList;
56     UwbDiagnostics mUwbDiagnostics;
57 
58     private static final int BUG_REPORT_MIN_INTERVAL_MS = 3600_000;
59 
60     @Before
setUp()61     public void setUp() throws Exception {
62         MockitoAnnotations.initMocks(this);
63         when(mUwbInjector.getDeviceConfigFacade()).thenReturn(mDeviceConfigFacade);
64         when(mDeviceConfigFacade.getBugReportMinIntervalMs())
65                 .thenReturn(BUG_REPORT_MIN_INTERVAL_MS);
66         when(mBuildProperties.isUserdebugBuild()).thenReturn(true);
67         when(mContext.getSystemService(BugreportManager.class)).thenReturn(mBugreportManager);
68         when(mContext.getPackageManager()).thenReturn(mPackageManager);
69         when(mPackageManager.queryIntentActivities(any(), anyInt())).thenReturn(mResolveInfoList);
70         mUwbDiagnostics = new UwbDiagnostics(mContext, mUwbInjector, mBuildProperties);
71     }
72 
73     @Test
takeBugReportDoesNothingOnUserBuild()74     public void takeBugReportDoesNothingOnUserBuild() throws Exception {
75         when(mBuildProperties.isUserBuild()).thenReturn(true);
76         mUwbDiagnostics.takeBugReport("");
77         assertThat(mUwbDiagnostics.getLastBugReportTimeMs()).isEqualTo(0);
78     }
79 
80     @Test
takeBugReportTwiceWithInsufficientTimeGapSkipSecondRequest()81     public void takeBugReportTwiceWithInsufficientTimeGapSkipSecondRequest() throws Exception {
82         // 1st attempt should succeed
83         when(mUwbInjector.getElapsedSinceBootMillis()).thenReturn(10L);
84         mUwbDiagnostics.takeBugReport("");
85         assertThat(mUwbDiagnostics.getLastBugReportTimeMs()).isEqualTo(10L);
86         // 2nd attempt should fail
87         when(mUwbInjector.getElapsedSinceBootMillis()).thenReturn(BUG_REPORT_MIN_INTERVAL_MS - 20L);
88         mUwbDiagnostics.takeBugReport("");
89         assertThat(mUwbDiagnostics.getLastBugReportTimeMs()).isEqualTo(10L);
90     }
91 }
92