1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.settingslib.wifi;
17
18import android.net.wifi.ScanResult;
19import android.net.wifi.WifiConfiguration;
20import android.net.wifi.WifiInfo;
21
22import com.android.settingslib.BaseTest;
23import com.android.settingslib.wifi.AccessPoint.AccessPointListener;
24
25import org.mockito.ArgumentCaptor;
26import org.mockito.Mockito;
27
28// TODO: Add some coverage
29public class AccessPointTest extends BaseTest {
30
31    private static final String TEST_SSID = "TestSsid";
32    private static final int NETWORK_ID = 0;
33
34    private AccessPointListener mAccessPointListener;
35    private AccessPoint mAccessPoint;
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mAccessPointListener = Mockito.mock(AccessPointListener.class);
41
42        WifiConfiguration wifiConfig = new WifiConfiguration();
43        wifiConfig.networkId = NETWORK_ID;
44        wifiConfig.SSID = TEST_SSID;
45
46        mAccessPoint = new AccessPoint(mContext, wifiConfig);
47        mAccessPoint.setListener(mAccessPointListener);
48    }
49
50    public void testOnLevelChanged() {
51        ScanResult result = new ScanResult();
52        result.capabilities = "";
53        result.SSID = TEST_SSID;
54
55        // Give it a level.
56        result.level = WifiTrackerTest.levelToRssi(1);
57        mAccessPoint.update(result);
58        verifyOnLevelChangedCallback(1);
59
60        // Give it a better level.
61        result.level = WifiTrackerTest.levelToRssi(2);
62        mAccessPoint.update(result);
63        verifyOnLevelChangedCallback(1);
64    }
65
66    public void testOnAccessPointChangedCallback() {
67        WifiInfo wifiInfo = Mockito.mock(WifiInfo.class);
68        Mockito.when(wifiInfo.getNetworkId()).thenReturn(NETWORK_ID);
69
70        mAccessPoint.update(wifiInfo, null);
71        verifyOnAccessPointsCallback(1);
72
73        mAccessPoint.update(null, null);
74        verifyOnAccessPointsCallback(2);
75
76        ScanResult result = new ScanResult();
77        result.capabilities = "";
78        result.SSID = TEST_SSID;
79        mAccessPoint.update(result);
80        verifyOnAccessPointsCallback(3);
81    }
82
83    private void verifyOnLevelChangedCallback(int num) {
84        ArgumentCaptor<AccessPoint> accessPoint = ArgumentCaptor.forClass(AccessPoint.class);
85        Mockito.verify(mAccessPointListener, Mockito.atLeast(num))
86                .onLevelChanged(accessPoint.capture());
87        assertEquals(mAccessPoint, accessPoint.getValue());
88    }
89
90    private void verifyOnAccessPointsCallback(int num) {
91        ArgumentCaptor<AccessPoint> accessPoint = ArgumentCaptor.forClass(AccessPoint.class);
92        Mockito.verify(mAccessPointListener, Mockito.atLeast(num))
93                .onAccessPointChanged(accessPoint.capture());
94        assertEquals(mAccessPoint, accessPoint.getValue());
95    }
96
97}
98