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.settings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doNothing; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.os.Looper; 30 import android.os.Process; 31 import android.view.View; 32 import android.widget.TextView; 33 34 import androidx.test.annotation.UiThreadTest; 35 import androidx.test.core.app.ApplicationProvider; 36 import androidx.test.ext.junit.runners.AndroidJUnit4; 37 38 import com.android.settings.testutils.ResourcesUtils; 39 import com.android.settings.wifi.helper.SavedWifiHelper; 40 41 import org.junit.Before; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.Spy; 47 import org.mockito.junit.MockitoJUnit; 48 import org.mockito.junit.MockitoRule; 49 50 import java.util.ArrayList; 51 import java.util.Arrays; 52 import java.util.List; 53 54 @RunWith(AndroidJUnit4.class) 55 public class UserCredentialsSettingsTest { 56 static final String TEST_ALIAS = "test_alias"; 57 static final String TEST_USER_BY_NAME = "test_used_by_name"; 58 59 static final String TEXT_PURPOSE_SYSTEM = "credential_for_vpn_and_apps"; 60 static final String TEXT_PURPOSE_WIFI = "credential_for_wifi"; 61 static final String TEXT_PURPOSE_WIFI_IN_USE = "credential_for_wifi_in_use"; 62 63 @Rule 64 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 65 @Spy 66 final Context mContext = ApplicationProvider.getApplicationContext(); 67 @Mock 68 SavedWifiHelper mSavedWifiHelper; 69 @Mock 70 View mView; 71 72 UserCredentialsSettings mSettings; 73 UserCredentialsSettings.Credential mSysCredential = 74 new UserCredentialsSettings.Credential(TEST_ALIAS, Process.SYSTEM_UID); 75 UserCredentialsSettings.Credential mWifiCredential = 76 new UserCredentialsSettings.Credential(TEST_ALIAS, Process.WIFI_UID); 77 List<String> mUsedByNames = Arrays.asList(TEST_USER_BY_NAME); 78 TextView mPurposeView = new TextView(ApplicationProvider.getApplicationContext()); 79 TextView mUsedByTitleView = new TextView(ApplicationProvider.getApplicationContext()); 80 TextView mUsedByContentView = new TextView(ApplicationProvider.getApplicationContext()); 81 82 @Before 83 @UiThreadTest setUp()84 public void setUp() { 85 when(mSavedWifiHelper.isCertificateInUse(any(String.class))).thenReturn(false); 86 when(mSavedWifiHelper.getCertificateNetworkNames(any(String.class))) 87 .thenReturn(new ArrayList<>()); 88 when(mView.getTag()).thenReturn(mWifiCredential); 89 90 if (Looper.myLooper() == null) { 91 Looper.prepare(); 92 } 93 mSettings = spy(new UserCredentialsSettings()); 94 when(mSettings.getContext()).thenReturn(mContext); 95 mSettings.mSavedWifiHelper = mSavedWifiHelper; 96 doNothing().when(mSettings) 97 .showCredentialDialogFragment(any(UserCredentialsSettings.Credential.class)); 98 } 99 100 @Test 101 @UiThreadTest onClick_noCredentialInTag_doNothing()102 public void onClick_noCredentialInTag_doNothing() { 103 when(mView.getTag()).thenReturn(null); 104 105 mSettings.onClick(mView); 106 107 verify(mSavedWifiHelper, never()).getCertificateNetworkNames(any(String.class)); 108 verify(mSettings, never()) 109 .showCredentialDialogFragment(any(UserCredentialsSettings.Credential.class)); 110 } 111 112 @Test 113 @UiThreadTest onClick_credentialInNotUse_notSetUsedByNamesThenShowDialog()114 public void onClick_credentialInNotUse_notSetUsedByNamesThenShowDialog() { 115 mWifiCredential.setInUse(false); 116 when(mView.getTag()).thenReturn(mWifiCredential); 117 118 mSettings.onClick(mView); 119 120 verify(mSavedWifiHelper, never()).getCertificateNetworkNames(any(String.class)); 121 verify(mSettings) 122 .showCredentialDialogFragment(any(UserCredentialsSettings.Credential.class)); 123 } 124 125 @Test 126 @UiThreadTest onClick_credentialInUse_setUsedByNamesThenShowDialog()127 public void onClick_credentialInUse_setUsedByNamesThenShowDialog() { 128 mWifiCredential.setInUse(true); 129 when(mView.getTag()).thenReturn(mWifiCredential); 130 when(mSavedWifiHelper.getCertificateNetworkNames(any(String.class))) 131 .thenReturn(mUsedByNames); 132 133 mSettings.onClick(mView); 134 135 verify(mSavedWifiHelper).getCertificateNetworkNames(any(String.class)); 136 assertThat(mWifiCredential.getUsedByNames()).isEqualTo(mUsedByNames); 137 verify(mSettings) 138 .showCredentialDialogFragment(any(UserCredentialsSettings.Credential.class)); 139 } 140 141 @Test 142 @UiThreadTest updatePurposeView_getSystemCert_setTextCorrectly()143 public void updatePurposeView_getSystemCert_setTextCorrectly() { 144 mSettings.updatePurposeView(mPurposeView, mSysCredential); 145 146 assertThat(mPurposeView.getText()).isEqualTo(getResString(TEXT_PURPOSE_SYSTEM)); 147 } 148 149 @Test 150 @UiThreadTest updatePurposeView_getWifiCert_setTextCorrectly()151 public void updatePurposeView_getWifiCert_setTextCorrectly() { 152 mWifiCredential.setInUse(false); 153 154 mSettings.updatePurposeView(mPurposeView, mWifiCredential); 155 156 assertThat(mPurposeView.getText()).isEqualTo(getResString(TEXT_PURPOSE_WIFI)); 157 } 158 159 @Test 160 @UiThreadTest updatePurposeView_isWifiCertInUse_setTextCorrectly()161 public void updatePurposeView_isWifiCertInUse_setTextCorrectly() { 162 mWifiCredential.setInUse(true); 163 164 mSettings.updatePurposeView(mPurposeView, mWifiCredential); 165 166 assertThat(mPurposeView.getText()).isEqualTo(getResString(TEXT_PURPOSE_WIFI_IN_USE)); 167 } 168 169 @Test 170 @UiThreadTest updateUsedByViews_noUsedByName_hideViews()171 public void updateUsedByViews_noUsedByName_hideViews() { 172 mWifiCredential.setUsedByNames(new ArrayList<>()); 173 174 mSettings.updateUsedByViews(mUsedByTitleView, mUsedByContentView, mWifiCredential); 175 176 assertThat(mUsedByTitleView.getVisibility()).isEqualTo(View.GONE); 177 assertThat(mUsedByContentView.getVisibility()).isEqualTo(View.GONE); 178 } 179 180 @Test 181 @UiThreadTest updateUsedByViews_hasUsedByName_showViews()182 public void updateUsedByViews_hasUsedByName_showViews() { 183 mWifiCredential.setUsedByNames(mUsedByNames); 184 185 mSettings.updateUsedByViews(mUsedByTitleView, mUsedByContentView, mWifiCredential); 186 187 assertThat(mUsedByTitleView.getVisibility()).isEqualTo(View.VISIBLE); 188 assertThat(mUsedByContentView.getVisibility()).isEqualTo(View.VISIBLE); 189 assertThat(mUsedByContentView.getText().toString().contains(TEST_USER_BY_NAME)).isTrue(); 190 } 191 getResString(String name)192 static String getResString(String name) { 193 return ResourcesUtils.getResourcesString(ApplicationProvider.getApplicationContext(), name); 194 } 195 } 196