• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 package android.net.wifi.nl80211;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.os.Parcel;
22 
23 import androidx.test.filters.SmallTest;
24 
25 import org.junit.Before;
26 import org.junit.Test;
27 
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 
32 /**
33  * Unit tests for {@link android.net.wifi.nl80211.SingleScanSettingsResult}.
34  */
35 @SmallTest
36 public class SingleScanSettingsTest {
37 
38     private static final byte[] TEST_SSID_1 =
39             new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
40     private static final byte[] TEST_SSID_2 =
41             new byte[] {'A', 'n', 'd', 'r', 'o', 'i', 'd', 'T', 'e', 's', 't'};
42     private static final int TEST_FREQUENCY_1 = 2456;
43     private static final int TEST_FREQUENCY_2 = 5215;
44     private static final int TEST_VALUE = 42;
45 
46     private ChannelSettings mChannelSettings1;
47     private ChannelSettings mChannelSettings2;
48     private HiddenNetwork mHiddenNetwork1;
49     private HiddenNetwork mHiddenNetwork2;
50 
51     @Before
setUp()52     public void setUp() {
53         mChannelSettings1 = new ChannelSettings();
54         mChannelSettings1.frequency = TEST_FREQUENCY_1;
55         mChannelSettings2 = new ChannelSettings();
56         mChannelSettings2.frequency = TEST_FREQUENCY_2;
57 
58         mHiddenNetwork1 = new HiddenNetwork();
59         mHiddenNetwork1.ssid = TEST_SSID_1;
60         mHiddenNetwork2 = new HiddenNetwork();
61         mHiddenNetwork2.ssid = TEST_SSID_2;
62     }
63 
64     /**
65      *  SingleScanSettings object can be serialized and deserialized, while keeping the
66      *  values unchanged.
67      */
68     @Test
canSerializeAndDeserialize()69     public void canSerializeAndDeserialize() {
70         SingleScanSettings scanSettings = new SingleScanSettings();
71         scanSettings.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
72 
73         scanSettings.channelSettings =
74                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
75         scanSettings.hiddenNetworks =
76                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
77         scanSettings.enable6GhzRnr = true;
78 
79         Parcel parcel = Parcel.obtain();
80         scanSettings.writeToParcel(parcel, 0);
81         // Rewind the pointer to the head of the parcel.
82         parcel.setDataPosition(0);
83         SingleScanSettings scanSettingsDeserialized =
84                 SingleScanSettings.CREATOR.createFromParcel(parcel);
85 
86         assertEquals(scanSettings, scanSettingsDeserialized);
87         assertEquals(scanSettings.hashCode(), scanSettingsDeserialized.hashCode());
88     }
89 
90     /**
91      * Tests usage of {@link SingleScanSettings} as a HashMap key type.
92      */
93     @Test
testAsHashMapKey()94     public void testAsHashMapKey() {
95         SingleScanSettings scanSettings1 = new SingleScanSettings();
96         scanSettings1.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
97         scanSettings1.channelSettings =
98                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
99         scanSettings1.hiddenNetworks =
100                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
101 
102         SingleScanSettings scanSettings2 = new SingleScanSettings();
103         scanSettings2.scanType = IWifiScannerImpl.SCAN_TYPE_HIGH_ACCURACY;
104         scanSettings2.channelSettings =
105                 new ArrayList<>(Arrays.asList(mChannelSettings1, mChannelSettings2));
106         scanSettings2.hiddenNetworks =
107                 new ArrayList<>(Arrays.asList(mHiddenNetwork1, mHiddenNetwork2));
108 
109         assertEquals(scanSettings1, scanSettings2);
110         assertEquals(scanSettings1.hashCode(), scanSettings2.hashCode());
111 
112         HashMap<SingleScanSettings, Integer> map = new HashMap<>();
113         map.put(scanSettings1, TEST_VALUE);
114 
115         assertEquals(TEST_VALUE, map.get(scanSettings2).intValue());
116     }
117 }
118