WificondPnoScannerTest.java revision 05d51a2d8eb67ea2eaf4cae43280d1cf9fe92645
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
25f4267b6840dbc7f430638c35c5448187b6e83846Christopher Wileyimport android.app.test.TestAlarmManager;
2662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.content.Context;
2762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.net.wifi.WifiScanner;
28ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Piusimport android.os.SystemClock;
296259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Piusimport android.os.test.TestLooper;
3062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport android.test.suitebuilder.annotation.SmallTest;
3162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport com.android.internal.R;
33ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Piusimport com.android.server.wifi.Clock;
34a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockResources;
35a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.MockWifiMonitor;
36a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.ScanResults;
37a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.WifiMonitor;
38a8367288377cbaed6371256ca837b7aa22280706Mitchell Willsimport com.android.server.wifi.WifiNative;
3962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport com.android.server.wifi.scanner.ChannelHelper.ChannelCollection;
4062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
4162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.junit.Before;
4262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.junit.Test;
4362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.InOrder;
4462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.Mock;
4562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport org.mockito.MockitoAnnotations;
4662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
471bf983a4211f547593a60523e43112ecdb5c8997Roshan Piusimport java.util.Arrays;
4862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Piusimport java.util.Set;
4962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
5062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius/**
5105d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang * Unit tests for {@link com.android.server.wifi.scanner.WificondScannerImpl.setPnoList}.
5262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius */
5362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius@SmallTest
5405d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wangpublic class WificondPnoScannerTest {
5562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
5662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Mock Context mContext;
57f4267b6840dbc7f430638c35c5448187b6e83846Christopher Wiley    TestAlarmManager mAlarmManager;
5862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockWifiMonitor mWifiMonitor;
59f4267b6840dbc7f430638c35c5448187b6e83846Christopher Wiley    TestLooper mLooper;
6062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Mock WifiNative mWifiNative;
6162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    MockResources mResources;
62ee0ab818341d44614ffe56ae73ecc08b974c2cbbRoshan Pius    @Mock Clock mClock;
6305d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang    WificondScannerImpl mScanner;
6462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
6562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Before
6662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    public void setup() throws Exception {
6762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        MockitoAnnotations.initMocks(this);
6862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
69f4267b6840dbc7f430638c35c5448187b6e83846Christopher Wiley        mLooper = new TestLooper();
70f4267b6840dbc7f430638c35c5448187b6e83846Christopher Wiley        mAlarmManager = new TestAlarmManager();
7162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor = new MockWifiMonitor();
7262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources = new MockResources();
7362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
7462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getInterfaceName()).thenReturn("a_test_interface_name");
7562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mContext.getSystemService(Context.ALARM_SERVICE))
7662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .thenReturn(mAlarmManager.getAlarmManager());
7762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mContext.getResources()).thenReturn(mResources);
785de29ec72c54cb73fdc1b59072f66a2627b8764cNingyuan Wang        when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime());
7962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
8062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
8162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
8205d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang     * Verify that the HW disconnected PNO scan triggers a wificond PNO scan and invokes the
836c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * OnPnoNetworkFound callback when the scan results are received.
8462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
8562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
86dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startHwDisconnectedPnoScan() {
8762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithHwPnoScanSupport();
8862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
8962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
90dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
913219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        ScanResults scanResults = createDummyScanResults(false);
9262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
9362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
9462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
9562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
96dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
9762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
9862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
9962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
10062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
1016c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that we pause & resume HW PNO scan when a single scan is scheduled and invokes the
1026c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * OnPnoNetworkFound callback when the scan results are received.
10362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
10462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
105dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void pauseResumeHwDisconnectedPnoScanForSingleScan() {
10662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithHwPnoScanSupport();
10762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
10862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
109dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
11062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanEventHandler eventHandler = mock(WifiNative.ScanEventHandler.class);
11162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings settings = createDummyScanSettings();
1123219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        ScanResults scanResults = createDummyScanResults(true);
11362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
11462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        InOrder order = inOrder(eventHandler, mWifiNative);
11562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
11662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
11762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start single scan
11862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertTrue(mScanner.startSingleScan(settings, eventHandler));
11962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify that the PNO scan was paused and single scan runs successfully
12062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        expectSuccessfulSingleScanWithHwPnoEnabled(order, eventHandler,
1216259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius                expectedBandScanFreqs(WifiScanner.WIFI_BAND_24_GHZ), scanResults);
12262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(eventHandler);
12362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
12462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order = inOrder(pnoEventHandler, mWifiNative);
125e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Resume PNO scan after the single scan results are received and PNO monitor debounce
126e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // alarm fires.
127e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
128e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
12905d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                        WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
130e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
13162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Now verify that PNO scan is resumed successfully
132dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
13362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
13462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
13562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
13662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
1376c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the SW disconnected PNO scan triggers a background scan and invokes the
1386c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
13962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
14062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    @Test
141dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startSwDisconnectedPnoScan() {
14262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        createScannerWithSwPnoScanSupport();
1436c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(false);
144dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    }
145dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
146dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    /**
1476c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the HW connected PNO scan triggers a background scan and invokes the
1486c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
149dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     */
150dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    @Test
151dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startHwConnectedPnoScan() {
152dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        createScannerWithHwPnoScanSupport();
1536c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(true);
154dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    }
155dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
156dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    /**
1576c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * Verify that the SW connected PNO scan triggers a background scan and invokes the
1586c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * background scan callbacks when scan results are received.
159dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     */
160dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    @Test
161dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    public void startSwConnectedPnoScan() {
162dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        createScannerWithSwPnoScanSupport();
1636c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        doSuccessfulSwPnoScanTest(true);
1646c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    }
165dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius
166e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    /**
167e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * Verify that the HW PNO delayed failure cleans up the scan settings cleanly.
168e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 1. Start Hw PNO.
169e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 2. Start Single Scan which should pause PNO scan.
170e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 3. Fail the PNO scan resume and verify that the OnPnoScanFailed callback is invoked.
171e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     * 4. Now restart a new PNO scan to ensure that the failure was cleanly handled.
172e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius     */
173e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    @Test
174e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    public void delayedHwDisconnectedPnoScanFailure() {
175e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        createScannerWithHwPnoScanSupport();
176e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
177e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
178e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
179e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.ScanEventHandler eventHandler = mock(WifiNative.ScanEventHandler.class);
180e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        WifiNative.ScanSettings settings = createDummyScanSettings();
1813219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        ScanResults scanResults = createDummyScanResults(true);
182e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
183e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        InOrder order = inOrder(eventHandler, mWifiNative);
184e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Start PNO scan
185e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
186e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Start single scan
187e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue(mScanner.startSingleScan(settings, eventHandler));
188e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Verify that the PNO scan was paused and single scan runs successfully
189e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        expectSuccessfulSingleScanWithHwPnoEnabled(order, eventHandler,
1906259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius                expectedBandScanFreqs(WifiScanner.WIFI_BAND_24_GHZ), scanResults);
191e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(eventHandler);
192e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
193e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Fail the PNO resume and check that the OnPnoScanFailed callback is invoked.
194e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order = inOrder(pnoEventHandler, mWifiNative);
19504c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        when(mWifiNative.startPnoScan(any(WifiNative.PnoSettings.class))).thenReturn(false);
196e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
197e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
19805d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                        WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
199e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
200e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        order.verify(pnoEventHandler).onPnoScanFailed();
201e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
202e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
203e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        // Add a new PNO scan request
204e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
205e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertTrue("dispatch pno monitor alarm",
206e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius                mAlarmManager.dispatch(
20705d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                        WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
208e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        assertEquals("dispatch message after alarm", 1, mLooper.dispatchAll());
209e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
210e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
211e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius    }
212e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
213e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    /**
214e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * Verify that the HW PNO scan stop failure still resets the PNO scan state.
215e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 1. Start Hw PNO.
216e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 2. Stop Hw PNO scan which raises a stop command to WifiNative which is failed.
217e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     * 3. Now restart a new PNO scan to ensure that the failure was cleanly handled.
218e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius     */
219e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    @Test
220e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    public void ignoreHwDisconnectedPnoScanStopFailure() {
221e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        createScannerWithHwPnoScanSupport();
222e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
223e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
224e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
225e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
226e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Start PNO scan
227e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
228e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
229e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Fail the PNO stop.
23004c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        when(mWifiNative.stopPnoScan()).thenReturn(false);
231e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue(mScanner.resetHwPnoList());
232e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue("dispatch pno monitor alarm",
233e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                mAlarmManager.dispatch(
23405d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                        WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
235e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        mLooper.dispatchAll();
23604c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        verify(mWifiNative).stopPnoScan();
237e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
238e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        // Add a new PNO scan request and ensure it runs successfully.
239e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
240e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        assertTrue("dispatch pno monitor alarm",
241e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius                mAlarmManager.dispatch(
24205d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                        WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
243e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        mLooper.dispatchAll();
244e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
2453219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        ScanResults scanResults = createDummyScanResults(false);
246e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        expectSuccessfulHwDisconnectedPnoScan(order, pnoSettings, pnoEventHandler, scanResults);
247e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        verifyNoMoreInteractions(pnoEventHandler);
248e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius    }
249e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius
2501bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
2511bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * Verify that the HW PNO scan is forcefully stopped (bypass debounce logic) and restarted when
2521bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * settings change.
2531bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Start Hw PNO.
2541bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Stop Hw PNO .
2551bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 3. Now restart a new PNO scan with different settings.
2561bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 4. Ensure that the stop was issued before we start again.
2571bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
2581bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    @Test
2591bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    public void forceRestartHwDisconnectedPnoScanWhenSettingsChange() {
2601bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        createScannerWithHwPnoScanSupport();
2611bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2621bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
2631bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
2641bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
2651bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2661bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Start PNO scan
2671bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
2681bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
2691bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2701bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Stop PNO now. This should trigger the debounce timer and not stop PNO.
2711bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mScanner.resetHwPnoList());
2721bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mAlarmManager.isPending(
27305d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
27404c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative, never()).stopPnoScan();
2751bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2761bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Now restart PNO scan with an extra network in settings.
2771bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList =
2781bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius                Arrays.copyOf(pnoSettings.networkList, pnoSettings.networkList.length + 1);
2791bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoSettings.networkList[pnoSettings.networkList.length - 1] =
2806259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius                createDummyPnoNetwork("ssid_pno_new");
2811bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
2821bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2831bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // This should bypass the debounce timer and stop PNO scan immediately and then start
2841bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // a new debounce timer for the start.
28504c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative).stopPnoScan();
2861bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2871bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Trigger the debounce timer and ensure we start PNO scan again.
28805d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang        mAlarmManager.dispatch(WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG);
2891bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        mLooper.dispatchAll();
29004c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative).startPnoScan(pnoSettings);
2911bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
2921bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
2931bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
2941bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * Verify that the HW PNO scan is not forcefully stopped (bypass debounce logic) when
2951bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * settings don't change.
2961bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Start Hw PNO.
2971bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Stop Hw PNO .
2981bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 3. Now restart a new PNO scan with same settings.
2991bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 4. Ensure that the stop was never issued.
3001bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
3011bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    @Test
3021bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    public void noForceRestartHwDisconnectedPnoScanWhenNoSettingsChange() {
3031bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        createScannerWithHwPnoScanSupport();
3041bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3051bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
3061bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(false);
3071bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        InOrder order = inOrder(pnoEventHandler, mWifiNative);
3081bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3091bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Start PNO scan
3101bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
3111bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
3121bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3131bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Stop PNO now. This should trigger the debounce timer and not stop PNO.
3141bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mScanner.resetHwPnoList());
3151bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        assertTrue(mAlarmManager.isPending(
31605d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                WificondScannerImpl.HwPnoDebouncer.PNO_DEBOUNCER_ALARM_TAG));
31704c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative, never()).stopPnoScan();
3181bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3191bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Now restart PNO scan with the same settings.
3201bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        startSuccessfulPnoScan(null, pnoSettings, null, pnoEventHandler);
3211bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3221bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        // Trigger the debounce timer and ensure that we neither stop/start.
3231bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        mLooper.dispatchAll();
32404c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative, never()).startPnoScan(any(WifiNative.PnoSettings.class));
32504c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative, never()).stopPnoScan();
3261bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
3271bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
3286c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius    private void doSuccessfulSwPnoScanTest(boolean isConnectedPno) {
329dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        WifiNative.PnoEventHandler pnoEventHandler = mock(WifiNative.PnoEventHandler.class);
3306c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        WifiNative.PnoSettings pnoSettings = createDummyPnoSettings(isConnectedPno);
33162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanEventHandler scanEventHandler = mock(WifiNative.ScanEventHandler.class);
33262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings scanSettings = createDummyScanSettings();
3333219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        ScanResults scanResults = createDummyScanResults(false);
33462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3356c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        InOrder order = inOrder(scanEventHandler, mWifiNative);
33662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
33762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Start PNO scan
33862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        startSuccessfulPnoScan(scanSettings, pnoSettings, scanEventHandler, pnoEventHandler);
33962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3406c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        expectSuccessfulSwPnoScan(order, scanEventHandler, scanResults);
34162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
34262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        verifyNoMoreInteractions(pnoEventHandler);
34362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
34462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
34562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void createScannerWithHwPnoScanSupport() {
34662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources.setBoolean(R.bool.config_wifi_background_scan_support, true);
34705d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang        mScanner = new WificondScannerImpl(mContext, mWifiNative, mWifiMonitor,
34805d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                mLooper.getLooper(), mClock);
34962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
35062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
35162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void createScannerWithSwPnoScanSupport() {
35262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mResources.setBoolean(R.bool.config_wifi_background_scan_support, false);
35305d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang        mScanner = new WificondScannerImpl(mContext, mWifiNative, mWifiMonitor,
35405d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang                mLooper.getLooper(), mClock);
35562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
35662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3576259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius    private WifiNative.PnoNetwork createDummyPnoNetwork(String ssid) {
3581bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        WifiNative.PnoNetwork pnoNetwork = new WifiNative.PnoNetwork();
3591bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        pnoNetwork.ssid = ssid;
3601bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        return pnoNetwork;
3611bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
3621bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
363dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius    private WifiNative.PnoSettings createDummyPnoSettings(boolean isConnected) {
36462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.PnoSettings pnoSettings = new WifiNative.PnoSettings();
365dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius        pnoSettings.isConnected = isConnected;
36662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        pnoSettings.networkList = new WifiNative.PnoNetwork[2];
3676259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius        pnoSettings.networkList[0] = createDummyPnoNetwork("ssid_pno_1");
3686259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius        pnoSettings.networkList[1] = createDummyPnoNetwork("ssid_pno_2");
36962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return pnoSettings;
37062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
37162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
37262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private WifiNative.ScanSettings createDummyScanSettings() {
37362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        WifiNative.ScanSettings settings = new NativeScanSettingsBuilder()
37462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .withBasePeriod(10000)
37562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .withMaxApPerScan(10)
37662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .addBucketWithBand(10000, WifiScanner.REPORT_EVENT_AFTER_EACH_SCAN,
37762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                        WifiScanner.WIFI_BAND_24_GHZ)
37862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius                .build();
37962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return settings;
38062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
38162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
3823219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills    private ScanResults createDummyScanResults(boolean allChannelsScanned) {
3833219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills        return ScanResults.create(0, allChannelsScanned, 2400, 2450, 2450, 2400, 2450, 2450, 2400,
3843219e97566a093ed10b2a9d6fe77ed9089084d63Mitchell Wills                2450, 2450);
38562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
38662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
38762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void startSuccessfulPnoScan(WifiNative.ScanSettings scanSettings,
38862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.PnoSettings pnoSettings, WifiNative.ScanEventHandler scanEventHandler,
38962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.PnoEventHandler pnoEventHandler) {
390e483fd4a3151e59b3d0a596572b4d9f035fa8424Roshan Pius        reset(mWifiNative);
39162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Scans succeed
39262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.scan(any(Set.class), any(Set.class))).thenReturn(true);
39304c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        when(mWifiNative.startPnoScan(any(WifiNative.PnoSettings.class))).thenReturn(true);
39404c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        when(mWifiNative.stopPnoScan()).thenReturn(true);
3956c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius
396e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        if (mScanner.isHwPnoSupported(pnoSettings.isConnected)) {
397e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            // This should happen only for HW PNO scan
398e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            assertTrue(mScanner.setHwPnoList(pnoSettings, pnoEventHandler));
399e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius        } else {
400e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius            // This should happen only for SW PNO scan
40162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            assertTrue(mScanner.startBatchedScan(scanSettings, scanEventHandler));
402e7ba2963bedf426a1d8ba09ab535260ef364512bRoshan Pius
40362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        }
40462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
40562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
40662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private Set<Integer> expectedBandScanFreqs(int band) {
40762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        ChannelCollection collection = mScanner.getChannelHelper().createChannelCollection();
40862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        collection.addBand(band);
40962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        return collection.getSupplicantScanFreqs();
41062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
41162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
41262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
41362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * Verify that the PNO scan was successfully started.
41462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
4151bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    private void expectHwDisconnectedPnoScanStart(InOrder order,
4161bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            WifiNative.PnoSettings pnoSettings) {
41762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify  HW PNO scan started
41804c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative).startPnoScan(any(WifiNative.PnoSettings.class));
4191bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    }
4201bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius
4211bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    /**
4221bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     *
4231bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 1. Verify that the PNO scan was successfully started.
4241bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     * 2. Send scan results and ensure that the |onPnoNetworkFound| callback was called.
4251bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius     */
4261bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius    private void expectSuccessfulHwDisconnectedPnoScan(InOrder order,
4271bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            WifiNative.PnoSettings pnoSettings, WifiNative.PnoEventHandler eventHandler,
4281bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius            ScanResults scanResults) {
4291bf983a4211f547593a60523e43112ecdb5c8997Roshan Pius        expectHwDisconnectedPnoScanStart(order, pnoSettings);
43062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
43162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Setup scan results
43262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
43362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
43462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
43562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
43662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
43762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
43862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(eventHandler).onPnoNetworkFound(scanResults.getRawScanResults());
43962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
44062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
44162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
44262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * Verify that the single scan results were delivered and that the PNO scan was paused and
44362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     * resumed either side of it.
44462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
44562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void expectSuccessfulSingleScanWithHwPnoEnabled(InOrder order,
44662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius            WifiNative.ScanEventHandler eventHandler, Set<Integer> expectedScanFreqs,
4476259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius            ScanResults scanResults) {
44862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Pause PNO scan first
44904c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        order.verify(mWifiNative).stopPnoScan();
45062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
4516259b630ddb59b642729a2d2113d81ed8e33a0e3Roshan Pius        order.verify(mWifiNative).scan(eq(expectedScanFreqs), any(Set.class));
45262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
45362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
45462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
45562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
45662bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
45762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
45862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
45962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(eventHandler).onScanStatus(WifiNative.WIFI_SCAN_RESULTS_AVAILABLE);
46062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertScanDataEquals(scanResults.getScanData(), mScanner.getLatestSingleScanResults());
46162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
46262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
46362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    /**
464dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     * Verify that the SW PNO scan was successfully started. This could either be disconnected
465dcd877d6c143db557884993ea437e2a432cb0ba3Roshan Pius     * or connected PNO.
4666c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * This is basically ensuring that the background scan runs successfully and returns the
4676c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius     * expected result.
46862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius     */
46962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    private void expectSuccessfulSwPnoScan(InOrder order,
4706c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius            WifiNative.ScanEventHandler eventHandler, ScanResults scanResults) {
47162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
47262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Verify scan started
47362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        order.verify(mWifiNative).scan(any(Set.class), any(Set.class));
47462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
47562bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Make sure that HW PNO scan was not started
47604c453c2e07efc30b99528926f205740226f1c7bNingyuan Wang        verify(mWifiNative, never()).startPnoScan(any(WifiNative.PnoSettings.class));
47762bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
47862bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Setup scan results
47962bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        when(mWifiNative.getScanResults()).thenReturn(scanResults.getScanDetailArrayList());
48062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
48162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        // Notify scan has finished
48262bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        mWifiMonitor.sendMessage(mWifiNative.getInterfaceName(), WifiMonitor.SCAN_RESULTS_EVENT);
48362bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius        assertEquals("dispatch message after results event", 1, mLooper.dispatchAll());
48462bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius
4856c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        // Verify background scan results delivered
4866c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        order.verify(eventHandler).onScanStatus(WifiNative.WIFI_SCAN_RESULTS_AVAILABLE);
4876c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        WifiScanner.ScanData[] scanData = mScanner.getLatestBatchedScanResults(true);
48805d51a2d8eb67ea2eaf4cae43280d1cf9fe92645Ningyuan Wang        WifiScanner.ScanData lastScanData = scanData[scanData.length - 1];
4896c5018cef1eb7acbcfa7fc6c9b7c018bab7ba7baRoshan Pius        assertScanDataEquals(scanResults.getScanData(), lastScanData);
49062bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius    }
49162bc101940ae1f5e60c4d8861a149b900dbf5e5cRoshan Pius}
492