WifiUtilsTest.java revision 04f7f34a713062ec3fd54a4c19481e9f76819899
1/* 2 * Copyright (C) 2017 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 */ 16package com.android.settingslib.wifi; 17 18import static com.google.common.truth.Truth.assertThat; 19 20import static org.mockito.ArgumentMatchers.anyInt; 21import static org.mockito.Mockito.any; 22import static org.mockito.Mockito.when; 23 24import android.content.Context; 25import android.net.NetworkKey; 26import android.net.RssiCurve; 27import android.net.ScoredNetwork; 28import android.net.WifiKey; 29import android.net.wifi.ScanResult; 30import android.net.wifi.WifiNetworkScoreCache; 31import android.os.Bundle; 32import android.os.Parcelable; 33import android.os.SystemClock; 34import android.text.format.DateUtils; 35 36import com.android.settingslib.R; 37import com.android.settingslib.SettingsLibRobolectricTestRunner; 38import com.android.settingslib.TestConfig; 39 40import org.junit.Before; 41import org.junit.Test; 42import org.junit.runner.RunWith; 43import org.mockito.Mock; 44import org.mockito.MockitoAnnotations; 45import org.robolectric.RuntimeEnvironment; 46import org.robolectric.annotation.Config; 47 48import java.util.ArrayList; 49 50@RunWith(SettingsLibRobolectricTestRunner.class) 51@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 52public class WifiUtilsTest { 53 private static final String TEST_SSID = "\"test_ssid\""; 54 private static final String TEST_BSSID = "00:00:00:00:00:00"; 55 private static final long MAX_SCORE_CACHE_AGE_MILLIS = 56 20 * DateUtils.MINUTE_IN_MILLIS; 57 58 private Context mContext; 59 @Mock 60 private RssiCurve mockBadgeCurve; 61 @Mock 62 private WifiNetworkScoreCache mockWifiNetworkScoreCache; 63 64 @Before 65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mContext = RuntimeEnvironment.application; 68 } 69 70 @Test 71 public void testVerboseSummaryString_showsScanResultSpeedLabel() { 72 WifiTracker.sVerboseLogging = true; 73 74 Bundle bundle = new Bundle(); 75 ArrayList<ScanResult> scanResults = buildScanResultCache(); 76 bundle.putParcelableArray(AccessPoint.KEY_SCANRESULTS, 77 scanResults.toArray(new Parcelable[scanResults.size()])); 78 AccessPoint ap = new AccessPoint(mContext, bundle); 79 80 when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class))) 81 .thenReturn(buildScoredNetworkWithGivenBadgeCurve(mockBadgeCurve)); 82 when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST); 83 84 ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */, 85 MAX_SCORE_CACHE_AGE_MILLIS); 86 String summary = WifiUtils.verboseScanResultSummary(ap, scanResults.get(0), null, 0); 87 88 assertThat(summary.contains(mContext.getString(R.string.speed_label_very_fast))).isTrue(); 89 } 90 91 private static ArrayList<ScanResult> buildScanResultCache() { 92 ArrayList<ScanResult> scanResults = new ArrayList<>(); 93 for (int i = 0; i < 5; i++) { 94 ScanResult scanResult = createScanResult(TEST_SSID, "bssid-" + i, i); 95 scanResults.add(scanResult); 96 } 97 return scanResults; 98 } 99 100 private static ScanResult createScanResult(String ssid, String bssid, int rssi) { 101 ScanResult scanResult = new ScanResult(); 102 scanResult.SSID = ssid; 103 scanResult.level = rssi; 104 scanResult.BSSID = bssid; 105 scanResult.timestamp = SystemClock.elapsedRealtime() * 1000; 106 scanResult.capabilities = ""; 107 return scanResult; 108 } 109 110 private ScoredNetwork buildScoredNetworkWithGivenBadgeCurve(RssiCurve badgeCurve) { 111 Bundle attr1 = new Bundle(); 112 attr1.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, badgeCurve); 113 return new ScoredNetwork( 114 new NetworkKey(new WifiKey(TEST_SSID, TEST_BSSID)), 115 badgeCurve, 116 false /* meteredHint */, 117 attr1); 118 } 119} 120