1 /*
2  * Copyright (C) 2019 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 com.android.server.wifi.util;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doAnswer;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.pm.ApplicationInfo;
28 import android.content.res.Resources;
29 import android.net.wifi.IWifiManager;
30 import android.net.wifi.WifiManager;
31 import android.os.Build;
32 import android.os.Looper;
33 
34 import androidx.test.filters.SmallTest;
35 
36 import com.android.server.wifi.WifiBaseTest;
37 import com.android.wifi.resources.R;
38 
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 
45 /** Unit tests for {@link RssiUtil}. */
46 @SmallTest
47 public class RssiUtilTest extends WifiBaseTest {
48 
49     public static final int[] RSSI_THRESHOLDS = {-88, -77, -66, -55};
50 
51     @Mock private Context mContext;
52     @Mock private Resources mResources;
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         MockitoAnnotations.initMocks(this);
57         when(mContext.getResources()).thenReturn(mResources);
58         when(mResources.getIntArray(R.array.config_wifiRssiLevelThresholds))
59                 .thenReturn(RSSI_THRESHOLDS);
60     }
61 
62     /**
63      * Tests that our default RSSI thresholds matches what would be calculated by
64      * {@link WifiManager#calculateSignalLevel(int, int)}.
65      */
66     @Test
testCalculateSignalLevelImplementationsConsistent()67     public void testCalculateSignalLevelImplementationsConsistent() {
68         for (int rssi = -100; rssi <= 0; rssi++) {
69             int expected = WifiManager.calculateSignalLevel(rssi, WifiManager.RSSI_LEVELS);
70             int actual = RssiUtil.calculateSignalLevel(mContext, rssi);
71             assertWithMessage("RSSI: %s", rssi).that(actual).isEqualTo(expected);
72         }
73     }
74 
75     /** Tests the signal level at RSSIs at around the thresholds. */
76     @Test
testCalculateSignalLevel()77     public void testCalculateSignalLevel() {
78         assertThat(RssiUtil.calculateSignalLevel(mContext, -100)).isEqualTo(0);
79         assertThat(RssiUtil.calculateSignalLevel(mContext, -89)).isEqualTo(0);
80         assertThat(RssiUtil.calculateSignalLevel(mContext, -88)).isEqualTo(1);
81         assertThat(RssiUtil.calculateSignalLevel(mContext, -78)).isEqualTo(1);
82         assertThat(RssiUtil.calculateSignalLevel(mContext, -77)).isEqualTo(2);
83         assertThat(RssiUtil.calculateSignalLevel(mContext, -67)).isEqualTo(2);
84         assertThat(RssiUtil.calculateSignalLevel(mContext, -66)).isEqualTo(3);
85         assertThat(RssiUtil.calculateSignalLevel(mContext, -56)).isEqualTo(3);
86         assertThat(RssiUtil.calculateSignalLevel(mContext, -55)).isEqualTo(4);
87         assertThat(RssiUtil.calculateSignalLevel(mContext, 0)).isEqualTo(4);
88         assertThat(RssiUtil.calculateSignalLevel(mContext, Integer.MAX_VALUE)).isEqualTo(4);
89     }
90 
91     /** Tests that an empty thresholds array always returns level 0. */
92     @Test
testCalculateSignalLevelEmptyThresholdsArray()93     public void testCalculateSignalLevelEmptyThresholdsArray() {
94         when(mResources.getIntArray(R.array.config_wifiRssiLevelThresholds))
95                 .thenReturn(new int[0]);
96         assertThat(RssiUtil.calculateSignalLevel(mContext, -100)).isEqualTo(0);
97         assertThat(RssiUtil.calculateSignalLevel(mContext, -50)).isEqualTo(0);
98         assertThat(RssiUtil.calculateSignalLevel(mContext, 0)).isEqualTo(0);
99 
100         assertThat(RssiUtil.calculateSignalLevel(mContext, Integer.MAX_VALUE)).isEqualTo(0);
101     }
102 
103     /** Tests that the expected default max signal level is returned. */
104     @Test
testGetMaxSignalLevel()105     public void testGetMaxSignalLevel() throws Exception {
106         IWifiManager iWifiManager = mock(IWifiManager.class);
107 
108         doAnswer(invocation -> {
109             int rssi = invocation.getArgument(0);
110             return RssiUtil.calculateSignalLevel(mContext, rssi);
111         }).when(iWifiManager).calculateSignalLevel(anyInt());
112 
113         ApplicationInfo applicationInfo = mock(ApplicationInfo.class);
114         applicationInfo.targetSdkVersion = Build.VERSION_CODES.Q;
115         Context context = mock(Context.class);
116         when(context.getApplicationInfo()).thenReturn(applicationInfo);
117         when(context.getOpPackageName()).thenReturn("TestPackage");
118         WifiManager wifiManager = new WifiManager(
119                 context, iWifiManager, mock(Looper.class));
120 
121         int level = wifiManager.getMaxSignalLevel();
122         assertThat(level).isEqualTo(4);
123     }
124 }
125