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 android.app.Activity;
     20 import android.content.Context;
     21 import android.net.wifi.WifiConfiguration;
     22 import android.net.wifi.WifiManager;
     23 import android.net.wifi.WifiManager.ActionListener;
     24 
     25 import com.android.settings.TestConfig;
     26 import com.android.settingslib.wifi.AccessPoint;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 import org.mockito.Mock;
     31 import org.mockito.MockitoAnnotations;
     32 import org.robolectric.RuntimeEnvironment;
     33 import org.robolectric.annotation.Config;
     34 import org.robolectric.util.ReflectionHelpers;
     35 
     36 import static com.google.common.truth.Truth.assertThat;
     37 
     38 import static org.mockito.ArgumentMatchers.any;
     39 import static org.mockito.ArgumentMatchers.eq;
     40 import static org.mockito.Mockito.doReturn;
     41 import static org.mockito.Mockito.spy;
     42 import static org.mockito.Mockito.verify;
     43 import static org.mockito.Mockito.when;
     44 
     45 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     46 
     47 @RunWith(SettingsRobolectricTestRunner.class)
     48 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     49 public class SavedAccessPointsWifiSettingsTest {
     50 
     51   @Mock private Activity mActivity;
     52   @Mock private WifiManager mockWifiManager;
     53   @Mock private WifiDialog mockWifiDialog;
     54   @Mock private WifiConfigController mockConfigController;
     55   @Mock private WifiConfiguration mockWifiConfiguration;
     56   @Mock private AccessPoint mockAccessPoint;
     57 
     58   private Context mContext;
     59 
     60   private SavedAccessPointsWifiSettings mFragment;
     61 
     62   @Before
     63   public void setUp() {
     64     MockitoAnnotations.initMocks(this);
     65     mContext = RuntimeEnvironment.application;
     66     mFragment = new SavedAccessPointsWifiSettings();
     67     when(mockWifiDialog.getController()).thenReturn(mockConfigController);
     68     when(mockConfigController.getConfig()).thenReturn(mockWifiConfiguration);
     69 
     70     ReflectionHelpers.setField(mFragment, "mWifiManager", mockWifiManager);
     71   }
     72 
     73   @Test
     74   public void onSubmit_shouldInvokeSaveApi() {
     75     mFragment.onSubmit(mockWifiDialog);
     76     verify(mockWifiManager).save(eq(mockWifiConfiguration), any(ActionListener.class));
     77   }
     78 
     79   @Test
     80   public void onForget_shouldInvokeForgetApi() {
     81     ReflectionHelpers.setField(mFragment, "mSelectedAccessPoint", mockAccessPoint);
     82     when(mockAccessPoint.getConfig()).thenReturn(mockWifiConfiguration);
     83 
     84     mFragment.onForget(mockWifiDialog);
     85 
     86     verify(mockWifiManager).forget(eq(mockWifiConfiguration.networkId), any(ActionListener.class));
     87   }
     88 }
     89