1/*
2 * Copyright (C) 2016 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
17package android.net.wifi;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.mock;
21import static org.mockito.Mockito.validateMockitoUsage;
22import static org.mockito.Mockito.when;
23
24import android.content.Context;
25import android.os.Handler;
26import android.os.Parcel;
27import android.os.test.TestLooper;
28import android.support.test.filters.SmallTest;
29import android.net.wifi.WifiScanner.ScanSettings;
30
31import com.android.internal.util.test.BidirectionalAsyncChannelServer;
32
33import org.junit.After;
34import org.junit.Before;
35import org.junit.Test;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38
39
40/**
41 * Unit tests for {@link android.net.wifi.WifiScanner}.
42 */
43@SmallTest
44public class WifiScannerTest {
45    @Mock
46    private Context mContext;
47    @Mock
48    private IWifiScanner mService;
49
50    private WifiScanner mWifiScanner;
51    private TestLooper mLooper;
52    private Handler mHandler;
53
54    /**
55     * Setup before tests.
56     */
57    @Before
58    public void setUp() throws Exception {
59        MockitoAnnotations.initMocks(this);
60        mLooper = new TestLooper();
61        mHandler = mock(Handler.class);
62        BidirectionalAsyncChannelServer server = new BidirectionalAsyncChannelServer(
63                mContext, mLooper.getLooper(), mHandler);
64        when(mService.getMessenger()).thenReturn(server.getMessenger());
65        mWifiScanner = new WifiScanner(mContext, mService, mLooper.getLooper());
66        mLooper.dispatchAll();
67    }
68
69    /**
70     * Clean up after tests.
71     */
72    @After
73    public void cleanup() {
74        validateMockitoUsage();
75    }
76
77    /**
78     * Verify parcel read/write for ScanSettings.
79     */
80    @Test
81    public void verifyScanSettingsParcelWithBand() throws Exception {
82        ScanSettings writeSettings = new ScanSettings();
83        writeSettings.type = WifiScanner.TYPE_LOW_POWER;
84        writeSettings.band = WifiScanner.WIFI_BAND_BOTH_WITH_DFS;
85
86        ScanSettings readSettings = parcelWriteRead(writeSettings);
87        assertEquals(readSettings.type, writeSettings.type);
88        assertEquals(readSettings.band, writeSettings.band);
89        assertEquals(0, readSettings.channels.length);
90    }
91
92    /**
93     * Verify parcel read/write for ScanSettings.
94     */
95    @Test
96    public void verifyScanSettingsParcelWithChannels() throws Exception {
97        ScanSettings writeSettings = new ScanSettings();
98        writeSettings.type = WifiScanner.TYPE_HIGH_ACCURACY;
99        writeSettings.band = WifiScanner.WIFI_BAND_UNSPECIFIED;
100        writeSettings.channels = new WifiScanner.ChannelSpec[] {
101                new WifiScanner.ChannelSpec(5),
102                new WifiScanner.ChannelSpec(7)
103        };
104
105        ScanSettings readSettings = parcelWriteRead(writeSettings);
106        assertEquals(writeSettings.type, readSettings.type);
107        assertEquals(writeSettings.band, readSettings.band);
108        assertEquals(2, readSettings.channels.length);
109        assertEquals(5, readSettings.channels[0].frequency);
110        assertEquals(7, readSettings.channels[1].frequency);
111    }
112
113    /**
114     * Write the provided {@link ScanSettings} to a parcel and deserialize it.
115     */
116    private static ScanSettings parcelWriteRead(ScanSettings writeSettings) throws Exception {
117        Parcel parcel = Parcel.obtain();
118        writeSettings.writeToParcel(parcel, 0);
119        parcel.setDataPosition(0);    // Rewind data position back to the beginning for read.
120        return ScanSettings.CREATOR.createFromParcel(parcel);
121    }
122
123}
124