Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.wifi;
     18 
     19 import static org.mockito.ArgumentMatchers.any;
     20 import static org.mockito.ArgumentMatchers.eq;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.verify;
     23 import static org.mockito.Mockito.when;
     24 
     25 import android.net.wifi.WifiConfiguration;
     26 import android.net.wifi.WifiManager;
     27 import android.net.wifi.WifiManager.ActionListener;
     28 import android.os.Handler;
     29 
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settingslib.wifi.AccessPoint;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Mock;
     37 import org.mockito.MockitoAnnotations;
     38 import org.robolectric.util.ReflectionHelpers;
     39 
     40 @RunWith(SettingsRobolectricTestRunner.class)
     41 public class SavedAccessPointsWifiSettingsTest {
     42 
     43     @Mock
     44     private WifiManager mockWifiManager;
     45     @Mock
     46     private WifiDialog mockWifiDialog;
     47     @Mock
     48     private WifiConfigController mockConfigController;
     49     @Mock
     50     private WifiConfiguration mockWifiConfiguration;
     51     @Mock
     52     private AccessPoint mockAccessPoint;
     53     @Mock
     54     private Handler mHandler;
     55 
     56     private SavedAccessPointsWifiSettings mSettings;
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         mSettings = new SavedAccessPointsWifiSettings();
     62         ReflectionHelpers.setField(mSettings, "mHandler", mHandler);
     63         ReflectionHelpers.setField(mSettings, "mWifiManager", mockWifiManager);
     64 
     65         when(mockWifiDialog.getController()).thenReturn(mockConfigController);
     66         when(mockConfigController.getConfig()).thenReturn(mockWifiConfiguration);
     67     }
     68 
     69     @Test
     70     public void onForget_isPasspointConfig_shouldSendMessageToHandler() {
     71         final AccessPoint accessPoint = mock(AccessPoint.class);
     72         when(accessPoint.isPasspointConfig()).thenReturn(true);
     73         ReflectionHelpers.setField(mSettings, "mSelectedAccessPoint", accessPoint);
     74 
     75         mSettings.onForget(null);
     76 
     77         verify(mHandler).sendEmptyMessage(SavedAccessPointsWifiSettings.MSG_UPDATE_PREFERENCES);
     78     }
     79 
     80     @Test
     81     public void onForget_onSuccess_shouldSendMessageToHandler() {
     82         mSettings.mForgetListener.onSuccess();
     83 
     84         verify(mHandler).sendEmptyMessage(SavedAccessPointsWifiSettings.MSG_UPDATE_PREFERENCES);
     85     }
     86 
     87     @Test
     88     public void onForget_onFailure_shouldSendMessageToHandler() {
     89         mSettings.mForgetListener.onFailure(0);
     90 
     91         verify(mHandler).sendEmptyMessage(SavedAccessPointsWifiSettings.MSG_UPDATE_PREFERENCES);
     92     }
     93 
     94     @Test
     95     public void onSubmit_shouldInvokeSaveApi() {
     96         mSettings.onSubmit(mockWifiDialog);
     97         verify(mockWifiManager).save(eq(mockWifiConfiguration), any(ActionListener.class));
     98     }
     99 
    100     @Test
    101     public void onForget_shouldInvokeForgetApi() {
    102         ReflectionHelpers.setField(mSettings, "mSelectedAccessPoint", mockAccessPoint);
    103         when(mockAccessPoint.getConfig()).thenReturn(mockWifiConfiguration);
    104         mSettings.onForget(mockWifiDialog);
    105         verify(mockWifiManager)
    106                 .forget(eq(mockWifiConfiguration.networkId), any(ActionListener.class));
    107     }
    108 }
    109