Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2018 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 android.net.wifi;
     18 
     19 import static org.junit.Assert.assertArrayEquals;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertNull;
     22 import static org.mockito.Mockito.validateMockitoUsage;
     23 
     24 import android.os.Parcel;
     25 import android.support.test.filters.SmallTest;
     26 import android.net.wifi.WifiScanner.ScanSettings;
     27 
     28 import org.junit.After;
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.mockito.MockitoAnnotations;
     32 
     33 
     34 /**
     35  * Unit tests for {@link android.net.wifi.WifiScanner}.
     36  */
     37 @SmallTest
     38 public class ScanResultTest {
     39     public static final String TEST_SSID = "\"test_ssid\"";
     40     public static final String TEST_BSSID = "04:ac:fe:45:34:10";
     41     public static final String TEST_CAPS = "CCMP";
     42     public static final int TEST_LEVEL = -56;
     43     public static final int TEST_FREQUENCY = 2412;
     44     public static final long TEST_TSF = 04660l;
     45 
     46     /**
     47      * Setup before tests.
     48      */
     49     @Before
     50     public void setUp() throws Exception {
     51         MockitoAnnotations.initMocks(this);
     52     }
     53 
     54     /**
     55      * Clean up after tests.
     56      */
     57     @After
     58     public void cleanup() {
     59         validateMockitoUsage();
     60     }
     61 
     62     /**
     63      * Verify parcel read/write for ScanResult.
     64      */
     65     @Test
     66     public void verifyScanResultParcelWithoutRadioChainInfo() throws Exception {
     67         ScanResult writeScanResult = createScanResult();
     68         ScanResult readScanResult = parcelReadWrite(writeScanResult);
     69         assertScanResultEquals(writeScanResult, readScanResult);
     70     }
     71 
     72     /**
     73      * Verify parcel read/write for ScanResult.
     74      */
     75     @Test
     76     public void verifyScanResultParcelWithZeroRadioChainInfo() throws Exception {
     77         ScanResult writeScanResult = createScanResult();
     78         writeScanResult.radioChainInfos = new ScanResult.RadioChainInfo[0];
     79         ScanResult readScanResult = parcelReadWrite(writeScanResult);
     80         assertNull(readScanResult.radioChainInfos);
     81     }
     82 
     83     /**
     84      * Verify parcel read/write for ScanResult.
     85      */
     86     @Test
     87     public void verifyScanResultParcelWithRadioChainInfo() throws Exception {
     88         ScanResult writeScanResult = createScanResult();
     89         writeScanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
     90         writeScanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
     91         writeScanResult.radioChainInfos[0].id = 0;
     92         writeScanResult.radioChainInfos[0].level = -45;
     93         writeScanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
     94         writeScanResult.radioChainInfos[1].id = 1;
     95         writeScanResult.radioChainInfos[1].level = -54;
     96         ScanResult readScanResult = parcelReadWrite(writeScanResult);
     97         assertScanResultEquals(writeScanResult, readScanResult);
     98     }
     99 
    100     /**
    101      * Verify copy constructor for ScanResult.
    102      */
    103     @Test
    104     public void verifyScanResultCopyWithoutRadioChainInfo() throws Exception {
    105         ScanResult scanResult = createScanResult();
    106         ScanResult copyScanResult = new ScanResult(scanResult);
    107         assertScanResultEquals(scanResult, copyScanResult);
    108     }
    109 
    110     /**
    111      * Verify copy constructor for ScanResult.
    112      */
    113     @Test
    114     public void verifyScanResultCopyWithRadioChainInfo() throws Exception {
    115         ScanResult scanResult = createScanResult();
    116         scanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
    117         scanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
    118         scanResult.radioChainInfos[0].id = 0;
    119         scanResult.radioChainInfos[0].level = -45;
    120         scanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
    121         scanResult.radioChainInfos[1].id = 1;
    122         scanResult.radioChainInfos[1].level = -54;
    123         ScanResult copyScanResult = new ScanResult(scanResult);
    124         assertScanResultEquals(scanResult, copyScanResult);
    125     }
    126 
    127     /**
    128      * Verify toString for ScanResult.
    129      */
    130     @Test
    131     public void verifyScanResultToStringWithoutRadioChainInfo() throws Exception {
    132         ScanResult scanResult = createScanResult();
    133         assertEquals("SSID: \"test_ssid\", BSSID: 04:ac:fe:45:34:10, capabilities: CCMP, " +
    134                 "level: -56, frequency: 2412, timestamp: 2480, distance: 0(cm), distanceSd: 0(cm), " +
    135                 "passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, " +
    136                 "80211mcResponder: is not supported, Carrier AP: no, " +
    137                 "Carrier AP EAP Type: 0, Carrier name: null, " +
    138                 "Radio Chain Infos: null", scanResult.toString());
    139     }
    140 
    141     /**
    142      * Verify toString for ScanResult.
    143      */
    144     @Test
    145     public void verifyScanResultToStringWithRadioChainInfo() throws Exception {
    146         ScanResult scanResult = createScanResult();
    147         scanResult.radioChainInfos = new ScanResult.RadioChainInfo[2];
    148         scanResult.radioChainInfos[0] = new ScanResult.RadioChainInfo();
    149         scanResult.radioChainInfos[0].id = 0;
    150         scanResult.radioChainInfos[0].level = -45;
    151         scanResult.radioChainInfos[1] = new ScanResult.RadioChainInfo();
    152         scanResult.radioChainInfos[1].id = 1;
    153         scanResult.radioChainInfos[1].level = -54;
    154         assertEquals("SSID: \"test_ssid\", BSSID: 04:ac:fe:45:34:10, capabilities: CCMP, " +
    155                 "level: -56, frequency: 2412, timestamp: 2480, distance: 0(cm), distanceSd: 0(cm), " +
    156                 "passpoint: no, ChannelBandwidth: 0, centerFreq0: 0, centerFreq1: 0, " +
    157                 "80211mcResponder: is not supported, Carrier AP: no, " +
    158                 "Carrier AP EAP Type: 0, Carrier name: null, " +
    159                 "Radio Chain Infos: [RadioChainInfo: id=0, level=-45, " +
    160                 "RadioChainInfo: id=1, level=-54]", scanResult.toString());
    161     }
    162 
    163     /**
    164      * Write the provided {@link ScanResult} to a parcel and deserialize it.
    165      */
    166     private static ScanResult parcelReadWrite(ScanResult writeResult) throws Exception {
    167         Parcel parcel = Parcel.obtain();
    168         writeResult.writeToParcel(parcel, 0);
    169         parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
    170         return ScanResult.CREATOR.createFromParcel(parcel);
    171     }
    172 
    173     private static ScanResult createScanResult() {
    174         ScanResult result = new ScanResult();
    175         result.wifiSsid = WifiSsid.createFromAsciiEncoded(TEST_SSID);
    176         result.BSSID = TEST_BSSID;
    177         result.capabilities = TEST_CAPS;
    178         result.level = TEST_LEVEL;
    179         result.frequency = TEST_FREQUENCY;
    180         result.timestamp = TEST_TSF;
    181         return result;
    182     }
    183 
    184     private static void assertScanResultEquals(ScanResult expected, ScanResult actual) {
    185         assertEquals(expected.SSID, actual.SSID);
    186         assertEquals(expected.BSSID, actual.BSSID);
    187         assertEquals(expected.capabilities, actual.capabilities);
    188         assertEquals(expected.level, actual.level);
    189         assertEquals(expected.frequency, actual.frequency);
    190         assertEquals(expected.timestamp, actual.timestamp);
    191         assertArrayEquals(expected.radioChainInfos, actual.radioChainInfos);
    192     }
    193 }
    194