WificondPnoScannerTest.java revision 1bf983a4211f547593a60523e43112ecdb5c8997
162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius/*
262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * Copyright (C) 2016 The Android Open Source Project
362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius *
462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * Licensed under the Apache License, Version 2.0 (the "License");
562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * you may not use this file except in compliance with the License.
662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * You may obtain a copy of the License at
762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius *
862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius *      http://www.apache.org/licenses/LICENSE-2.0
962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius *
1062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * Unless required by applicable law or agreed to in writing, software
1162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * distributed under the License is distributed on an "AS IS" BASIS,
1262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius * See the License for the specific language governing permissions and
14a8367288377cbaed6371256ca837b7aa22280706Mitchell Wills * limitations under the License.
1562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius */
1662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
17a8367288377cbaed6371256ca837b7aa22280706Mitchell Willspackage com.android.server.wifi.scanner;
1862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
19e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Piusimport static com.android.server.wifi.ScanTestUtil.NativeScanSettingsBuilder;
20e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Piusimport static com.android.server.wifi.ScanTestUtil.assertScanDataEquals;
21e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
22e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Piusimport static org.junit.Assert.*;
23e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Piusimport static org.mockito.Mockito.*;
24e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
2562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.content.Context;
2662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.net.wifi.WifiConfiguration;
2762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.net.wifi.WifiScanner;
28ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Piusimport android.os.SystemClock;
2962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.test.suitebuilder.annotation.SmallTest;
3062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport com.android.internal.R;
32ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Piusimport com.android.server.wifi.Clock;
33a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockAlarmManager;
34a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockLooper;
35a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockResources;
36a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockWifiMonitor;
37a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.ScanResults;
38a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.WifiMonitor;
39a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.WifiNative;
4062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport com.android.server.wifi.scanner.ChannelHelper.ChannelCollection;
4162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
4262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.junit.Before;
4362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.junit.Test;
4462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.InOrder;
4562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.Mock;
4662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.MockitoAnnotations;
4762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
481bf983a4211f547593a60523e43112ecdb5c8997Roshan Piusimport java.util.Arrays;
4962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport java.util.HashSet;
5062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport java.util.Set;
5162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
5262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
5362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius/**
54a8367288377cbaed6371256ca837b7aa22280706Mitchell Wills * Unit tests for {@link com.android.server.wifi.scanner.SupplicantWifiScannerImpl.setPnoList}.
5562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius */
5662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius@SmallTest
5762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piuspublic class SupplicantPnoScannerTest {
5862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
5962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Mock Context mContext;
6062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockAlarmManager mAlarmManager;
6162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockWifiMonitor mWifiMonitor;
6262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockLooper mLooper;
6362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Mock WifiNative mWifiNative;
6462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockResources mResources;
65ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius    @Mock Clock mClock;
6662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    SupplicantWifiScannerImpl mScanner;
6762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
6862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Before
6962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    public void setup() throws Exception {
7062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        MockitoAnnotations.initMocks(this);
7162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
7262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mLooper = new MockLooper();
7362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mAlarmManager = new MockAlarmManager();
7462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor = new MockWifiMonitor();
7562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources = new MockResources();
7662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
7762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getInterfaceName()).thenReturn("a_test_interface_name");
7862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mContext.getSystemService(Context.ALARM_SERVICE))
7962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .thenReturn(mAlarmManager.getAlarmManager());
8062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mContext.getResources()).thenReturn(mResources);
81ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius        when(mClock.elapsedRealtime()).thenReturn(SystemClock.elapsedRealtime());
8262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
8362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
8462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
856c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the HW disconnected PNO scan triggers a supplicant PNO scan and invokes the
866c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * OnPnoNetworkFound callback when the scan results are received.
8762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
8862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
89dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startHwDisconnectedPnoScan() {
9062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithHwPnoScanSupport();
9162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
9262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
93dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
9462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        ScanResults scanResults = createDummyScanResults();
9562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
9662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
9762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
9862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
99dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
10062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
10162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
10262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
10362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
1046c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that we pause & resume HW PNO scan when a single scan is scheduled and invokes the
1056c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * OnPnoNetworkFound callback when the scan results are received.
10662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
10762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
108dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void pauseResumeHwDisconnectedPnoScanForSingleScan() {
10962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithHwPnoScanSupport();
11062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
11162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
112dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
11362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanEventHandler eventHandler = mock(WifiNative.ScanEventHandler.class);
11462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings settings = createDummyScanSettings();
11562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        ScanResults scanResults = createDummyScanResults();
11662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
11762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        InOrder order = inOrder(eventHandler, mWifiNative);
11862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
11962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
12062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start single scan
12162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertTrue(mScanner.startSingleScan(settings, eventHandler));
12262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify that the PNO scan was paused and single scan runs successfully
12362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        expectSuccessfulSingleScanWithHwPnoEnabled(order, eventHandler,
12462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                expectedBandScanFreqs(WifiScanner.WIFI_BAND_24_GHZ), new HashSet<Integer>(),
12562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                scanResults);
12662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(eventHandler);
12762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
12862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order = inOrder(pnoEventHandler, mWifiNative);
129e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Resume PNO scan after the single scan results are received and PNO monitor debounce
130e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // alarm fires.
131e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
132e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
133e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                        SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
134e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
13562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Now verify that PNO scan is resumed successfully
136dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
13762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
13862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
13962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
14062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
1416c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the SW disconnected PNO scan triggers a background scan and invokes the
1426c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
14362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
14462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
145dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startSwDisconnectedPnoScan() {
14662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithSwPnoScanSupport();
1476c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(false);
148dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    }
149dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
150dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    /**
1516c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the HW connected PNO scan triggers a background scan and invokes the
1526c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
153dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     */
154dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    @Test
155dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startHwConnectedPnoScan() {
156dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        createScannerWithHwPnoScanSupport();
1576c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(true);
158dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    }
159dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
160dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    /**
1616c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the SW connected PNO scan triggers a background scan and invokes the
1626c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
163dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     */
164dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    @Test
165dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startSwConnectedPnoScan() {
166dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        createScannerWithSwPnoScanSupport();
1676c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(true);
1686c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    }
169dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
170e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    /**
171e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * Verify that the HW PNO delayed failure cleans up the scan settings cleanly.
172e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 1. Start Hw PNO.
173e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 2. Start Single Scan which should pause PNO scan.
174e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 3. Fail the PNO scan resume and verify that the OnPnoScanFailed callback is invoked.
175e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 4. Now restart a new PNO scan to ensure that the failure was cleanly handled.
176e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     */
177e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    @Test
178e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    public void delayedHwDisconnectedPnoScanFailure() {
179e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        createScannerWithHwPnoScanSupport();
180e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
181e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
182e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
183e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.ScanEventHandler eventHandler = mock(WifiNative.ScanEventHandler.class);
184e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.ScanSettings settings = createDummyScanSettings();
185e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        ScanResults scanResults = createDummyScanResults();
186e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
187e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        InOrder order = inOrder(eventHandler, mWifiNative);
188e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Start PNO scan
189e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
190e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Start single scan
191e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue(mScanner.startSingleScan(settings, eventHandler));
192e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Verify that the PNO scan was paused and single scan runs successfully
193e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        expectSuccessfulSingleScanWithHwPnoEnabled(order, eventHandler,
194e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                expectedBandScanFreqs(WifiScanner.WIFI_BAND_24_GHZ), new HashSet<Integer>(),
195e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                scanResults);
196e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(eventHandler);
197e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
198e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Fail the PNO resume and check that the OnPnoScanFailed callback is invoked.
199e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order = inOrder(pnoEventHandler, mWifiNative);
200e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        when(mWifiNative.setPnoScan(true)).thenReturn(false);
201e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
202e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
203e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                        SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
204e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
205e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order.verify(pnoEventHandler).onPnoScanFailed();
206e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
207e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
208e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Add a new PNO scan request
209e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
210e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
211e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
212e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                        SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
213e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
214e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
215e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
216e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    }
217e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
218e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    /**
219e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * Verify that the HW PNO scan stop failure still resets the PNO scan state.
220e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 1. Start Hw PNO.
221e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 2. Stop Hw PNO scan which raises a stop command to WifiNative which is failed.
222e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 3. Now restart a new PNO scan to ensure that the failure was cleanly handled.
223e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     */
224e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    @Test
225e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    public void ignoreHwDisconnectedPnoScanStopFailure() {
226e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        createScannerWithHwPnoScanSupport();
227e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
228e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
229e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
230e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
231e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Start PNO scan
232e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
233e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
234e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Fail the PNO stop.
235e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        when(mWifiNative.setPnoScan(false)).thenReturn(false);
236e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue(mScanner.resetHwPnoList());
237e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue("dispatch pno monitor alarm",
238e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                mAlarmManager.dispatch(
239e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                        SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
240e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        mLooper.dispatchAll();
241e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        verify(mWifiNative).setPnoScan(false);
242e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
243e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Add a new PNO scan request and ensure it runs successfully.
244e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
245e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue("dispatch pno monitor alarm",
246e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                mAlarmManager.dispatch(
247e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                        SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
248e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        mLooper.dispatchAll();
249e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
250e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        ScanResults scanResults = createDummyScanResults();
251e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
252e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        verifyNoMoreInteractions(pnoEventHandler);
253e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    }
254e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
2551bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
2561bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * Verify that the HW PNO scan is forcefully stopped (bypass debounce logic) and restarted when
2571bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * settings change.
2581bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Start Hw PNO.
2591bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Stop Hw PNO .
2601bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 3. Now restart a new PNO scan with different settings.
2611bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 4. Ensure that the stop was issued before we start again.
2621bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
2631bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    @Test
2641bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    public void forceRestartHwDisconnectedPnoScanWhenSettingsChange() {
2651bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        createScannerWithHwPnoScanSupport();
2661bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2671bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
2681bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
2691bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
2701bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2711bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Start PNO scan
2721bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
2731bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
2741bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2751bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Stop PNO now. This should trigger the debounce timer and not stop PNO.
2761bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mScanner.resetHwPnoList());
2771bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mAlarmManager.isPending(
2781bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius                SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
2791bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        order.verify(mWifiNative, never()).setPnoScan(false);
2801bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2811bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Now restart PNO scan with an extra network in settings.
2821bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList =
2831bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius                Arrays.copyOf(pnoSettings.networkList, pnoSettings.networkList.length + 1);
2841bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList[pnoSettings.networkList.length - 1] =
2851bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius                createDummyPnoNetwork("ssid_pno_new", 6, 6);
2861bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
2871bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2881bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // This should bypass the debounce timer and stop PNO scan immediately and then start
2891bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // a new debounce timer for the start.
2901bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        order.verify(mWifiNative).setPnoScan(false);
2911bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2921bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Trigger the debounce timer and ensure we start PNO scan again.
2931bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        mAlarmManager.dispatch(SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG);
2941bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        mLooper.dispatchAll();
2951bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        order.verify(mWifiNative).setPnoScan(true);
2961bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
2971bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2981bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
2991bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * Verify that the HW PNO scan is not forcefully stopped (bypass debounce logic) when
3001bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * settings don't change.
3011bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Start Hw PNO.
3021bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Stop Hw PNO .
3031bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 3. Now restart a new PNO scan with same settings.
3041bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 4. Ensure that the stop was never issued.
3051bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
3061bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    @Test
3071bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    public void noForceRestartHwDisconnectedPnoScanWhenNoSettingsChange() {
3081bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        createScannerWithHwPnoScanSupport();
3091bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3101bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
3111bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
3121bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
3131bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3141bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Start PNO scan
3151bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
3161bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
3171bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3181bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Stop PNO now. This should trigger the debounce timer and not stop PNO.
3191bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mScanner.resetHwPnoList());
3201bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mAlarmManager.isPending(
3211bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius                SupplicantWifiScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
3221bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        order.verify(mWifiNative, never()).setPnoScan(false);
3231bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3241bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Now restart PNO scan with the same settings.
3251bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
3261bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3271bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Trigger the debounce timer and ensure that we neither stop/start.
3281bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        mLooper.dispatchAll();
3291bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        order.verify(mWifiNative, never()).setPnoScan(anyBoolean());
3301bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
3311bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3326c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    private void doSuccessfulSwPnoScanTest(boolean isConnectedPno) {
333dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
3346c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(isConnectedPno);
33562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanEventHandler scanEventHandler = mock(WifiNative.ScanEventHandler.class);
33662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings scanSettings = createDummyScanSettings();
33762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        ScanResults scanResults = createDummyScanResults();
33862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3396c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        InOrder order = inOrder(scanEventHandler, mWifiNative);
34062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
34162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
34262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(scanSettings, pnoSettings, scanEventHandler, pnoEventHandler);
34362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3446c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        expectSuccessfulSwPnoScan(order, scanEventHandler, scanResults);
34562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
34662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
34762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
34862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
34962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void createScannerWithHwPnoScanSupport() {
35062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources.setBoolean(R.bool.config_wifi_background_scan_support, true);
351ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius        mScanner =
352ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius                new SupplicantWifiScannerImpl(mContext, mWifiNative, mLooper.getLooper(), mClock);
35362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
35462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
35562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void createScannerWithSwPnoScanSupport() {
35662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources.setBoolean(R.bool.config_wifi_background_scan_support, false);
357ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius        mScanner =
358ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius                new SupplicantWifiScannerImpl(mContext, mWifiNative, mLooper.getLooper(), mClock);
35962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
36062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3611bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    private WifiNative.PnoNetwork createDummyPnoNetwork(String ssid, int networkId, int priority) {
3621bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoNetwork pnoNetwork = new WifiNative.PnoNetwork();
3631bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoNetwork.ssid = ssid;
3641bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoNetwork.networkId = networkId;
3651bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoNetwork.priority = priority;
3661bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        return pnoNetwork;
3671bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
3681bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
369dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    private WifiNative.PnoSettings createDummyPnoSettings(boolean isConnected) {
37062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoSettings pnoSettings = new WifiNative.PnoSettings();
371dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        pnoSettings.isConnected = isConnected;
37262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        pnoSettings.networkList = new WifiNative.PnoNetwork[2];
3731bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList[0] = createDummyPnoNetwork("ssid_pno_1", 1, 1);
3741bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList[1] = createDummyPnoNetwork("ssid_pno_2", 2, 2);
37562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return pnoSettings;
37662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
37762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
37862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private WifiNative.ScanSettings createDummyScanSettings() {
37962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings settings = new NativeScanSettingsBuilder()
38062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .withBasePeriod(10000)
38162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .withMaxApPerScan(10)
38262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .addBucketWithBand(10000, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN,
38362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                        WifiScanner.WIFI_BAND_24_GHZ)
38462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .build();
38562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return settings;
38662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
38762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
38862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private ScanResults createDummyScanResults() {
38962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return ScanResults.create(0, 2400, 2450, 2450, 2400, 2450, 2450, 2400, 2450, 2450);
39062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
39162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
39262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void startSuccessfulPnoScan(WifiNative.ScanSettings scanSettings,
39362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.PnoSettings pnoSettings, WifiNative.ScanEventHandler scanEventHandler,
39462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.PnoEventHandler pnoEventHandler) {
395e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        reset(mWifiNative);
39662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.setNetworkVariable(anyInt(), anyString(), anyString())).thenReturn(true);
39762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.enableNetworkWithoutConnect(anyInt())).thenReturn(true);
39862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Scans succeed
39962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.scan(any(Set.class), any(Set.class))).thenReturn(true);
400e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        when(mWifiNative.setPnoScan(anyBoolean())).thenReturn(true);
4016c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius
402e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        if (mScanner.isHwPnoSupported(pnoSettings.isConnected)) {
403e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            // This should happen only for HW PNO scan
404e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            assertTrue(mScanner.setHwPnoList(pnoSettings, pnoEventHandler));
405e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        } else {
406e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            // This should happen only for SW PNO scan
40762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            assertTrue(mScanner.startBatchedScan(scanSettings, scanEventHandler));
408e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
40962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        }
41062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
41162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
41262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private Set<Integer> expectedBandScanFreqs(int band) {
41362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        ChannelCollection collection = mScanner.getChannelHelper().createChannelCollection();
41462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        collection.addBand(band);
41562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return collection.getSupplicantScanFreqs();
41662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
41762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
41862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
41962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * Verify that the PNO scan was successfully started.
42062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
4211bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    private void expectHwDisconnectedPnoScanStart(InOrder order,
4221bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            WifiNative.PnoSettings pnoSettings) {
42362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        for (int i = 0; i < pnoSettings.networkList.length; i++) {
42462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.PnoNetwork network = pnoSettings.networkList[i];
42562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            order.verify(mWifiNative).setNetworkVariable(network.networkId,
42662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                    WifiConfiguration.priorityVarName, Integer.toString(network.priority));
42762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            order.verify(mWifiNative).enableNetworkWithoutConnect(network.networkId);
42862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        }
42962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify  HW PNO scan started
430e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order.verify(mWifiNative).setPnoScan(true);
4311bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
4321bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
4331bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
4341bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     *
4351bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Verify that the PNO scan was successfully started.
4361bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Send scan results and ensure that the |onPnoNetworkFound| callback was called.
4371bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
4381bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    private void expectSuccessfulHwDisconnectedPnoScan(InOrder order,
4391bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            WifiNative.PnoSettings pnoSettings, WifiNative.PnoEventHandler eventHandler,
4401bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            ScanResults scanResults) {
4411bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
44262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
44362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Setup scan results
44462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
44562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
44662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
44762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
44862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
44962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
45062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(eventHandler).onPnoNetworkFound(scanResults.getRawScanResults());
45162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
45262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
45362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
45462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * Verify that the single scan results were delivered and that the PNO scan was paused and
45562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * resumed either side of it.
45662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
45762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void expectSuccessfulSingleScanWithHwPnoEnabled(InOrder order,
45862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.ScanEventHandler eventHandler, Set<Integer> expectedScanFreqs,
45962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            Set<Integer> expectedHiddenNetIds, ScanResults scanResults) {
46062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Pause PNO scan first
461e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order.verify(mWifiNative).setPnoScan(false);
46262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
46362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(mWifiNative).scan(eq(expectedScanFreqs), eq(expectedHiddenNetIds));
46462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
46562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
46662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
46762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
46862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
46962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
47062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
47162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(eventHandler).onScanStatus(WifiNative.WIFI_SCAN_RESULTS_AVAILABLE);
47262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertScanDataEquals(scanResults.getScanData(), mScanner.getLatestSingleScanResults());
47362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
47462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
47562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
476dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     * Verify that the SW PNO scan was successfully started. This could either be disconnected
477dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     * or connected PNO.
4786c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * This is basically ensuring that the background scan runs successfully and returns the
4796c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * expected result.
48062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
48162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void expectSuccessfulSwPnoScan(InOrder order,
4826c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius            WifiNative.ScanEventHandler eventHandler, ScanResults scanResults) {
48362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
48462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify scan started
48562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(mWifiNative).scan(any(Set.class), any(Set.class));
48662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
48762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Make sure that HW PNO scan was not started
4888ba794562167643688ee38352f98345403fa22c8Roshan Pius        verify(mWifiNative, never()).setPnoScan(anyBoolean());
48962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
49062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Setup scan results
49162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
49262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
49362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
49462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
49562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
49662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
4976c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        // Verify background scan results delivered
4986c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        order.verify(eventHandler).onScanStatus(WifiNative.WIFI_SCAN_RESULTS_AVAILABLE);
4996c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        WifiScanner.ScanData[] scanData = mScanner.getLatestBatchedScanResults(true);
5006c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        WifiScanner.ScanData lastScanData = scanData[scanData.length -1];
5016c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        assertScanDataEquals(scanResults.getScanData(), lastScanData);
50262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
50362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius}
504