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 package com.android.settings.wifi; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.reset; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.graphics.drawable.Drawable; 31 import android.net.wifi.sharedconnectivity.app.NetworkProviderInfo; 32 import android.view.LayoutInflater; 33 import android.view.View; 34 import android.widget.LinearLayout; 35 36 import androidx.preference.PreferenceViewHolder; 37 38 import com.android.settingslib.R; 39 import com.android.settingslib.wifi.WifiUtils; 40 import com.android.wifitrackerlib.HotspotNetworkEntry; 41 import com.android.wifitrackerlib.WifiEntry; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 51 import java.util.ArrayList; 52 import java.util.List; 53 54 @RunWith(RobolectricTestRunner.class) 55 public class WifiEntryPreferenceTest { 56 57 private Context mContext; 58 59 @Mock 60 private WifiEntry mMockWifiEntry; 61 @Mock 62 private HotspotNetworkEntry mHotspotNetworkEntry; 63 @Mock 64 private WifiUtils.InternetIconInjector mMockIconInjector; 65 66 @Mock 67 private Drawable mMockDrawable0; 68 @Mock 69 private Drawable mMockDrawable1; 70 @Mock 71 private Drawable mMockDrawable2; 72 @Mock 73 private Drawable mMockDrawable3; 74 @Mock 75 private Drawable mMockDrawable4; 76 77 @Mock 78 private Drawable mMockShowXDrawable0; 79 @Mock 80 private Drawable mMockShowXDrawable1; 81 @Mock 82 private Drawable mMockShowXDrawable2; 83 @Mock 84 private Drawable mMockShowXDrawable3; 85 @Mock 86 private Drawable mMockShowXDrawable4; 87 88 private static final String MOCK_TITLE = "title"; 89 private static final String MOCK_SUMMARY = "summary"; 90 private static final String FAKE_URI_STRING = "fakeuri"; 91 92 WifiEntryPreference mPref; 93 94 @Before setUp()95 public void setUp() { 96 mContext = RuntimeEnvironment.application; 97 98 MockitoAnnotations.initMocks(this); 99 100 when(mMockWifiEntry.getTitle()).thenReturn(MOCK_TITLE); 101 when(mMockWifiEntry.getSummary(false /* concise */)).thenReturn(MOCK_SUMMARY); 102 when(mMockWifiEntry.getLevel()).thenReturn(0); 103 when(mMockWifiEntry.shouldShowXLevelIcon()).thenReturn(false); 104 when(mMockWifiEntry.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED); 105 106 when(mMockIconInjector.getIcon(false /* showX */, 0)).thenReturn(mMockDrawable0); 107 when(mMockIconInjector.getIcon(false /* showX */, 1)).thenReturn(mMockDrawable1); 108 when(mMockIconInjector.getIcon(false /* showX */, 2)).thenReturn(mMockDrawable2); 109 when(mMockIconInjector.getIcon(false /* showX */, 3)).thenReturn(mMockDrawable3); 110 when(mMockIconInjector.getIcon(false /* showX */, 4)).thenReturn(mMockDrawable4); 111 112 when(mMockIconInjector.getIcon(true /* showX */, 0)) 113 .thenReturn(mMockShowXDrawable0); 114 when(mMockIconInjector.getIcon(true /* showX */, 1)) 115 .thenReturn(mMockShowXDrawable1); 116 when(mMockIconInjector.getIcon(true /* showX */, 2)) 117 .thenReturn(mMockShowXDrawable2); 118 when(mMockIconInjector.getIcon(true /* showX */, 3)) 119 .thenReturn(mMockShowXDrawable3); 120 when(mMockIconInjector.getIcon(true /* showX */, 4)) 121 .thenReturn(mMockShowXDrawable4); 122 123 mPref = spy(new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector)); 124 } 125 126 @Test constructor_shouldSetWifiEntryTitleAndSummary()127 public void constructor_shouldSetWifiEntryTitleAndSummary() { 128 final WifiEntryPreference pref = 129 new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector); 130 131 assertThat(pref.getTitle()).isEqualTo(MOCK_TITLE); 132 assertThat(pref.getSummary()).isEqualTo(MOCK_SUMMARY); 133 } 134 135 @Test constructor_shouldSetIcon()136 public void constructor_shouldSetIcon() { 137 when(mMockWifiEntry.getLevel()).thenReturn(0); 138 139 final WifiEntryPreference pref = 140 new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector); 141 142 assertThat(pref.getIcon()).isEqualTo(mMockDrawable0); 143 } 144 145 @Test setWifiEntry_connectedStateChanged_setIconAndSummary()146 public void setWifiEntry_connectedStateChanged_setIconAndSummary() { 147 when(mMockWifiEntry.getLevel()).thenReturn(4); 148 when(mMockWifiEntry.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_CONNECTED); 149 150 mPref.setWifiEntry(mMockWifiEntry); 151 152 verify(mPref).setIcon(any()); 153 verify(mPref).setSummary(anyString()); 154 155 // Only the connection state changes. 156 when(mMockWifiEntry.getConnectedState()).thenReturn(WifiEntry.CONNECTED_STATE_DISCONNECTED); 157 reset(mPref); 158 159 mPref.setWifiEntry(mMockWifiEntry); 160 161 // The icon and summary should be set in case. 162 verify(mPref).setIcon(any()); 163 verify(mPref).setSummary(anyString()); 164 } 165 166 @Test titleChanged_refresh_shouldUpdateTitle()167 public void titleChanged_refresh_shouldUpdateTitle() { 168 final String updatedTitle = "updated title"; 169 when(mMockWifiEntry.getTitle()).thenReturn(updatedTitle); 170 171 mPref.refresh(); 172 173 assertThat(mPref.getTitle().toString()).isEqualTo(updatedTitle); 174 } 175 176 @Test summaryChanged_refresh_shouldUpdateSummary()177 public void summaryChanged_refresh_shouldUpdateSummary() { 178 final String updatedSummary = "updated summary"; 179 when(mMockWifiEntry.getSummary(false /* concise */)).thenReturn(updatedSummary); 180 181 mPref.refresh(); 182 183 assertThat(mPref.getSummary().toString()).isEqualTo(updatedSummary); 184 } 185 186 @Test levelChanged_refresh_shouldUpdateLevelIcon()187 public void levelChanged_refresh_shouldUpdateLevelIcon() { 188 final List<Drawable> iconList = new ArrayList<>(); 189 190 when(mMockWifiEntry.getLevel()).thenReturn(0); 191 mPref.refresh(); 192 iconList.add(mPref.getIcon()); 193 when(mMockWifiEntry.getLevel()).thenReturn(1); 194 mPref.refresh(); 195 iconList.add(mPref.getIcon()); 196 when(mMockWifiEntry.getLevel()).thenReturn(2); 197 mPref.refresh(); 198 iconList.add(mPref.getIcon()); 199 when(mMockWifiEntry.getLevel()).thenReturn(3); 200 mPref.refresh(); 201 iconList.add(mPref.getIcon()); 202 when(mMockWifiEntry.getLevel()).thenReturn(4); 203 mPref.refresh(); 204 iconList.add(mPref.getIcon()); 205 when(mMockWifiEntry.getLevel()).thenReturn(-1); 206 mPref.refresh(); 207 iconList.add(mPref.getIcon()); 208 209 assertThat(iconList).containsExactly(mMockDrawable0, mMockDrawable1, 210 mMockDrawable2, mMockDrawable3, mMockDrawable4, null); 211 } 212 213 @Test levelChanged_showXWifiRefresh_shouldUpdateLevelIcon()214 public void levelChanged_showXWifiRefresh_shouldUpdateLevelIcon() { 215 final List<Drawable> iconList = new ArrayList<>(); 216 when(mMockWifiEntry.shouldShowXLevelIcon()).thenReturn(true); 217 218 when(mMockWifiEntry.getLevel()).thenReturn(0); 219 mPref.refresh(); 220 iconList.add(mPref.getIcon()); 221 when(mMockWifiEntry.getLevel()).thenReturn(1); 222 mPref.refresh(); 223 iconList.add(mPref.getIcon()); 224 when(mMockWifiEntry.getLevel()).thenReturn(2); 225 mPref.refresh(); 226 iconList.add(mPref.getIcon()); 227 when(mMockWifiEntry.getLevel()).thenReturn(3); 228 mPref.refresh(); 229 iconList.add(mPref.getIcon()); 230 when(mMockWifiEntry.getLevel()).thenReturn(4); 231 mPref.refresh(); 232 iconList.add(mPref.getIcon()); 233 when(mMockWifiEntry.getLevel()).thenReturn(-1); 234 mPref.refresh(); 235 iconList.add(mPref.getIcon()); 236 237 assertThat(iconList).containsExactly(mMockShowXDrawable0, mMockShowXDrawable1, 238 mMockShowXDrawable2, mMockShowXDrawable3, mMockShowXDrawable4, null); 239 } 240 241 @Test notNull_whenGetHelpUriString_shouldSetImageButtonVisible()242 public void notNull_whenGetHelpUriString_shouldSetImageButtonVisible() { 243 when(mMockWifiEntry.getHelpUriString()).thenReturn(FAKE_URI_STRING); 244 final LayoutInflater inflater = LayoutInflater.from(mContext); 245 final View view = inflater.inflate(mPref.getLayoutResource(), new LinearLayout(mContext), 246 false); 247 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view); 248 249 mPref.onBindViewHolder(holder); 250 251 assertThat(view.findViewById(R.id.icon_button).getVisibility()).isEqualTo(View.VISIBLE); 252 } 253 254 @Test helpButton_whenGetHelpUriStringNotNull_shouldSetCorrectContentDescription()255 public void helpButton_whenGetHelpUriStringNotNull_shouldSetCorrectContentDescription() { 256 when(mMockWifiEntry.getHelpUriString()).thenReturn(FAKE_URI_STRING); 257 final LayoutInflater inflater = LayoutInflater.from(mContext); 258 final View view = inflater.inflate(mPref.getLayoutResource(), new LinearLayout(mContext), 259 false); 260 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view); 261 262 mPref.onBindViewHolder(holder); 263 264 assertThat(view.findViewById(R.id.icon_button).getContentDescription()).isEqualTo( 265 mContext.getString(R.string.help_label)); 266 } 267 268 @Test subscriptionEntry_shouldSetImageButtonGone()269 public void subscriptionEntry_shouldSetImageButtonGone() { 270 when(mMockWifiEntry.isSubscription()).thenReturn(true); 271 final LayoutInflater inflater = LayoutInflater.from(mContext); 272 final View view = inflater.inflate(mPref.getLayoutResource(), new LinearLayout(mContext), 273 false); 274 final PreferenceViewHolder holder = PreferenceViewHolder.createInstanceForTests(view); 275 276 mPref.onBindViewHolder(holder); 277 278 assertThat(view.findViewById(R.id.icon_button).getVisibility()).isEqualTo(View.GONE); 279 } 280 281 @Test updateIcon_shouldSetTintListForDrawable()282 public void updateIcon_shouldSetTintListForDrawable() { 283 reset(mMockDrawable4); 284 285 mPref.updateIcon(false /* showX */, 4 /* level */); 286 287 verify(mMockDrawable4).setTintList(any()); 288 } 289 290 @Test getSecondTargetResId_shouldNotReturnZero()291 public void getSecondTargetResId_shouldNotReturnZero() { 292 assertThat(mPref.getSecondTargetResId()).isNotEqualTo(0); 293 } 294 295 @Test refresh_itsHotspotNetworkEntry_shouldUpdateHotspotIcon()296 public void refresh_itsHotspotNetworkEntry_shouldUpdateHotspotIcon() { 297 int deviceType = NetworkProviderInfo.DEVICE_TYPE_PHONE; 298 when(mHotspotNetworkEntry.getDeviceType()).thenReturn(deviceType); 299 WifiEntryPreference pref = spy( 300 new WifiEntryPreference(mContext, mHotspotNetworkEntry, mMockIconInjector)); 301 302 pref.refresh(); 303 304 verify(pref).updateHotspotIcon(deviceType); 305 } 306 307 @Test refresh_notHotspotNetworkEntry_shouldNotUpdateHotspotIcon()308 public void refresh_notHotspotNetworkEntry_shouldNotUpdateHotspotIcon() { 309 WifiEntryPreference pref = spy( 310 new WifiEntryPreference(mContext, mMockWifiEntry, mMockIconInjector)); 311 312 pref.refresh(); 313 314 verify(pref, never()).updateHotspotIcon(anyInt()); 315 } 316 } 317