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 com.android.server.uwb; 18 19 import static org.mockito.Mockito.*; 20 21 import android.content.BroadcastReceiver; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageManager; 25 import android.net.wifi.WifiManager; 26 import android.net.wifi.WifiManager.ActiveCountryCodeChangedCallback; 27 import android.os.Handler; 28 import android.os.test.TestLooper; 29 import android.telephony.TelephonyManager; 30 31 import androidx.test.filters.SmallTest; 32 33 import com.android.server.uwb.data.UwbUciConstants; 34 import com.android.server.uwb.jni.NativeUwbManager; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Captor; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 import java.nio.charset.StandardCharsets; 44 45 /** 46 * Unit tests for {@link com.android.server.uwb.UwbCountryCode}. 47 */ 48 @SmallTest 49 public class UwbCountryCodeTest { 50 private static final String TEST_COUNTRY_CODE = "US"; 51 private static final String TEST_COUNTRY_CODE_OTHER = "JP"; 52 53 @Mock Context mContext; 54 @Mock TelephonyManager mTelephonyManager; 55 @Mock WifiManager mWifiManager; 56 @Mock NativeUwbManager mNativeUwbManager; 57 @Mock UwbInjector mUwbInjector; 58 @Mock PackageManager mPackageManager; 59 private TestLooper mTestLooper; 60 private UwbCountryCode mUwbCountryCode; 61 62 @Captor 63 private ArgumentCaptor<BroadcastReceiver> mTelephonyCountryCodeReceiverCaptor; 64 @Captor 65 private ArgumentCaptor<ActiveCountryCodeChangedCallback> mWifiCountryCodeReceiverCaptor; 66 67 /** 68 * Setup test. 69 */ 70 @Before setUp()71 public void setUp() throws Exception { 72 MockitoAnnotations.initMocks(this); 73 mTestLooper = new TestLooper(); 74 75 when(mContext.getSystemService(TelephonyManager.class)) 76 .thenReturn(mTelephonyManager); 77 when(mContext.getSystemService(WifiManager.class)) 78 .thenReturn(mWifiManager); 79 when(mContext.getPackageManager()).thenReturn(mPackageManager); 80 when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI)).thenReturn(true); 81 when(mNativeUwbManager.setCountryCode(any())).thenReturn( 82 (byte) UwbUciConstants.STATUS_CODE_OK); 83 mUwbCountryCode = new UwbCountryCode( 84 mContext, mNativeUwbManager, new Handler(mTestLooper.getLooper()), mUwbInjector); 85 } 86 87 @Test testSetDefaultCountryCodeWhenNoCountryCodeAvailable()88 public void testSetDefaultCountryCodeWhenNoCountryCodeAvailable() { 89 mUwbCountryCode.initialize(); 90 verify(mNativeUwbManager).setCountryCode( 91 UwbCountryCode.DEFAULT_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 92 } 93 94 @Test testInitializeCountryCodeFromTelephony()95 public void testInitializeCountryCodeFromTelephony() { 96 when(mTelephonyManager.getNetworkCountryIso()).thenReturn(TEST_COUNTRY_CODE); 97 mUwbCountryCode.initialize(); 98 verify(mNativeUwbManager).setCountryCode( 99 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 100 } 101 102 @Test testInitializeCountryCodeFromTelephonyVerifyListener()103 public void testInitializeCountryCodeFromTelephonyVerifyListener() { 104 UwbCountryCode.CountryCodeChangedListener listener = mock( 105 UwbCountryCode.CountryCodeChangedListener.class); 106 mUwbCountryCode.addListener(listener); 107 when(mTelephonyManager.getNetworkCountryIso()).thenReturn(TEST_COUNTRY_CODE); 108 mUwbCountryCode.initialize(); 109 verify(mNativeUwbManager).setCountryCode( 110 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 111 verify(listener).onCountryCodeChanged(TEST_COUNTRY_CODE); 112 } 113 114 @Test testSetCountryCodeFromTelephony()115 public void testSetCountryCodeFromTelephony() { 116 when(mTelephonyManager.getNetworkCountryIso()).thenReturn(TEST_COUNTRY_CODE); 117 mUwbCountryCode.initialize(); 118 clearInvocations(mNativeUwbManager); 119 120 mUwbCountryCode.setCountryCode(false); 121 // already set. 122 verify(mNativeUwbManager, never()).setCountryCode( 123 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 124 } 125 126 @Test testSetCountryCodeWithForceUpdateFromTelephony()127 public void testSetCountryCodeWithForceUpdateFromTelephony() { 128 when(mTelephonyManager.getNetworkCountryIso()).thenReturn(TEST_COUNTRY_CODE); 129 mUwbCountryCode.initialize(); 130 clearInvocations(mNativeUwbManager); 131 132 mUwbCountryCode.setCountryCode(true); 133 // set again 134 verify(mNativeUwbManager).setCountryCode( 135 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 136 } 137 138 @Test testSetCountryCodeFromOemWhenTelephonyAndWifiNotAvailable()139 public void testSetCountryCodeFromOemWhenTelephonyAndWifiNotAvailable() { 140 when(mUwbInjector.getOemDefaultCountryCode()).thenReturn(TEST_COUNTRY_CODE); 141 mUwbCountryCode.initialize(); 142 clearInvocations(mNativeUwbManager); 143 144 mUwbCountryCode.setCountryCode(false); 145 // already set. 146 verify(mNativeUwbManager, never()).setCountryCode( 147 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 148 } 149 150 @Test testChangeInTelephonyCountryCode()151 public void testChangeInTelephonyCountryCode() { 152 mUwbCountryCode.initialize(); 153 verify(mContext).registerReceiver( 154 mTelephonyCountryCodeReceiverCaptor.capture(), any(), any(), any()); 155 Intent intent = new Intent(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED) 156 .putExtra(TelephonyManager.EXTRA_NETWORK_COUNTRY, TEST_COUNTRY_CODE); 157 mTelephonyCountryCodeReceiverCaptor.getValue().onReceive(mock(Context.class), intent); 158 verify(mNativeUwbManager).setCountryCode( 159 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 160 } 161 162 @Test testChangeInWifiCountryCode()163 public void testChangeInWifiCountryCode() { 164 mUwbCountryCode.initialize(); 165 verify(mWifiManager).registerActiveCountryCodeChangedCallback( 166 any(), mWifiCountryCodeReceiverCaptor.capture()); 167 mWifiCountryCodeReceiverCaptor.getValue().onActiveCountryCodeChanged(TEST_COUNTRY_CODE); 168 verify(mNativeUwbManager).setCountryCode( 169 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 170 } 171 172 @Test testChangeInTelephonyCountryCodeWhenWifiCountryCodeAvailable()173 public void testChangeInTelephonyCountryCodeWhenWifiCountryCodeAvailable() { 174 mUwbCountryCode.initialize(); 175 verify(mWifiManager).registerActiveCountryCodeChangedCallback( 176 any(), mWifiCountryCodeReceiverCaptor.capture()); 177 mWifiCountryCodeReceiverCaptor.getValue().onActiveCountryCodeChanged(TEST_COUNTRY_CODE); 178 verify(mContext).registerReceiver( 179 mTelephonyCountryCodeReceiverCaptor.capture(), any(), any(), any()); 180 verify(mNativeUwbManager).setCountryCode( 181 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 182 183 Intent intent = new Intent(TelephonyManager.ACTION_NETWORK_COUNTRY_CHANGED) 184 .putExtra(TelephonyManager.EXTRA_NETWORK_COUNTRY, TEST_COUNTRY_CODE_OTHER); 185 mTelephonyCountryCodeReceiverCaptor.getValue().onReceive(mock(Context.class), intent); 186 verify(mNativeUwbManager).setCountryCode( 187 TEST_COUNTRY_CODE_OTHER.getBytes(StandardCharsets.UTF_8)); 188 } 189 190 @Test testForceOverrideCodeWhenTelephonyAndWifiAvailable()191 public void testForceOverrideCodeWhenTelephonyAndWifiAvailable() { 192 when(mTelephonyManager.getNetworkCountryIso()).thenReturn(TEST_COUNTRY_CODE); 193 mUwbCountryCode.initialize(); 194 195 verify(mWifiManager).registerActiveCountryCodeChangedCallback( 196 any(), mWifiCountryCodeReceiverCaptor.capture()); 197 mWifiCountryCodeReceiverCaptor.getValue().onActiveCountryCodeChanged(TEST_COUNTRY_CODE); 198 clearInvocations(mNativeUwbManager); 199 200 mUwbCountryCode.setOverrideCountryCode(TEST_COUNTRY_CODE_OTHER); 201 verify(mNativeUwbManager).setCountryCode( 202 TEST_COUNTRY_CODE_OTHER.getBytes(StandardCharsets.UTF_8)); 203 clearInvocations(mNativeUwbManager); 204 205 mUwbCountryCode.clearOverrideCountryCode(); 206 verify(mNativeUwbManager).setCountryCode( 207 TEST_COUNTRY_CODE.getBytes(StandardCharsets.UTF_8)); 208 } 209 } 210