[Wi-Fi] Enhance Wifi Settings unit test cases.

Add new unit test cases in
1. WifiP2pSettingsTest.java
2. WifiConfigController2Test.java
3. WifiP2PPreferenceControllerTest.java
4. WifiP2pPeerTest.java

the coverage rate of
1. com.android.settings.wifi will raise from 44% to 45%
2. com.android.settings.wifi.P2p will raise from 69% to 85%

Bug: 151696220
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2pSettingsTest
      make RunSettingsRoboTests ROBOTEST_FILTER=WifiConfigController2Test
      make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2pPeerTest
      make RunSettingsRoboTests ROBOTEST_FILTER=WifiP2PPreferenceControllerTest
Change-Id: I365494e1654376b23b08f18b1a799ffc59323a4c
This commit is contained in:
govenliu
2020-05-06 15:32:54 +08:00
parent 02e712ce42
commit 1e0d40f2bd
7 changed files with 504 additions and 19 deletions

View File

@@ -39,7 +39,6 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
@@ -132,4 +131,17 @@ public class WifiP2PPreferenceControllerTest {
mController.displayPreference(mScreen);
verify(mWifiDirectPreference, times(2)).setEnabled(false);
}
@Test
public void updateState_withLocationDisabled_preferenceShouldBeDisable() {
when(mWifiManager.isWifiEnabled()).thenReturn(true);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
Intent dummyIntent = new Intent();
mController.displayPreference(mScreen);
verify(mWifiDirectPreference).setEnabled(true);
when(mLocationManager.isLocationEnabled()).thenReturn(false);
mController.mLocationReceiver.onReceive(mContext, dummyIntent);
verify(mWifiDirectPreference).setEnabled(false);
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.p2p;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.WifiP2pDevice;
import androidx.preference.Preference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class WifiP2pPeerTest {
private static final String DEVICE_NAME = "fakeName";
private static final String OTHER_NAME = "otherName";
private static final String MAC_ADDRESS = "00:11:22:33:44:55";
private Context mContext;
private WifiP2pPeer mPreference;
@Mock
private WifiP2pDevice mWifiP2pDevice;
@Mock
private WifiP2pPeer mOtherWifiP2pPeer;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
}
@Test
public void compareTo_withSameDeviceName_shouldBeZero() {
setupOneOtherP2pPeer(DEVICE_NAME, null /* address */);
mWifiP2pDevice.deviceName = DEVICE_NAME;
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
assertThat(mPreference.compareTo(mOtherWifiP2pPeer)).isEqualTo(0);
}
@Test
public void compareTo_withDifferentDeviceName_shouldNotZero() {
setupOneOtherP2pPeer(DEVICE_NAME, null /* address */);
mWifiP2pDevice.deviceName = OTHER_NAME;
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
assertThat(mPreference.compareTo(mOtherWifiP2pPeer)).isNotEqualTo(0);
}
@Test
public void compareTo_withSameDeviceAddress_shouldBeZero() {
setupOneOtherP2pPeer(null /* name */, MAC_ADDRESS);
mWifiP2pDevice.deviceAddress = MAC_ADDRESS;
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
assertThat(mPreference.compareTo(mOtherWifiP2pPeer)).isEqualTo(0);
}
@Test
public void compareTo_withLowerDeviceStatus_shouldBeOne() {
setupOneOtherP2pPeer(DEVICE_NAME, null /* address */);
mWifiP2pDevice.status = WifiP2pDevice.FAILED;
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
assertThat(mPreference.compareTo(mOtherWifiP2pPeer)).isEqualTo(1);
}
@Test
public void compareTo_withNotPeerParameter_shouldBeOne() {
final Preference fakePreference = mock(Preference.class);
setupOneOtherP2pPeer(DEVICE_NAME, null /* address */);
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
assertThat(mPreference.compareTo(fakePreference)).isEqualTo(1);
}
@Test
public void signalLevel_afterNewPreference_shouldBeExpected() {
mPreference = new WifiP2pPeer(mContext, mWifiP2pDevice);
final int expectSignalLevel = WifiManager.calculateSignalLevel(mPreference.mRssi,
WifiP2pPeer.SIGNAL_LEVELS);
assertThat(mPreference.getLevel()).isEqualTo(expectSignalLevel);
}
private void setupOneOtherP2pPeer(String name, String address) {
final WifiP2pDevice wifiP2pDevice = mock(WifiP2pDevice.class);
wifiP2pDevice.status = WifiP2pDevice.CONNECTED;
wifiP2pDevice.deviceAddress = address;
wifiP2pDevice.deviceName = name;
mOtherWifiP2pPeer.device = wifiP2pDevice;
}
}

View File

@@ -21,6 +21,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -33,6 +34,7 @@ import android.net.NetworkInfo;
import android.net.wifi.p2p.WifiP2pDevice;
import android.net.wifi.p2p.WifiP2pDeviceList;
import android.net.wifi.p2p.WifiP2pGroup;
import android.net.wifi.p2p.WifiP2pGroupList;
import android.net.wifi.p2p.WifiP2pInfo;
import android.net.wifi.p2p.WifiP2pManager;
import android.os.Bundle;
@@ -360,6 +362,161 @@ public class WifiP2pSettingsTest {
assertThat(mFragment.getDialogMetricsCategory(-1 /* dialogId */)).isEqualTo(0);
}
@Test
public void onSaveInstanceState_withWiFiPeer_shouldGetP2pDeviceType() {
setupOneP2pPeer(WifiP2pDevice.CONNECTED);
mFragment.onPreferenceTreeClick(mWifiP2pPeer);
final Bundle outBundle = new Bundle();
mFragment.onSaveInstanceState(outBundle);
final Object object = outBundle.getParcelable(WifiP2pSettings.SAVE_DIALOG_PEER);
assertThat(object instanceof WifiP2pDevice).isTrue();
}
@Test
public void onSaveInstanceState_withDeviceNameText_shouldSaveName() {
final String fakeDeviceName = "fakeName";
final Bundle createBundle = new Bundle();
createBundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName);
mFragment.onActivityCreated(createBundle);
final Bundle outBundle = new Bundle();
final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME);
mFragment.onSaveInstanceState(outBundle);
final String string = outBundle.getString(WifiP2pSettings.SAVE_DEVICE_NAME);
assertThat(string).isEqualTo(fakeDeviceName);
}
@Test
public void onSaveInstanceState_withSelectedGroup_shouldSaveGroupName() {
final String fakeGroupName = "fakeGroupName";
final WifiP2pPersistentGroup wifiP2pPersistentGroup = spy(
new WifiP2pPersistentGroup(mContext,
mWifiP2pGroup));
doReturn(fakeGroupName).when(wifiP2pPersistentGroup).getGroupName();
mFragment.mSelectedGroup = wifiP2pPersistentGroup;
final Bundle outBundle = new Bundle();
mFragment.onSaveInstanceState(outBundle);
assertThat(outBundle.getString(WifiP2pSettings.SAVE_SELECTED_GROUP)).isEqualTo(
fakeGroupName);
}
@Test
public void persistentController_withOneGroup_shouldBeAvailable() {
final String fakeGroupName = new String("fakeGroupName");
doReturn(fakeGroupName).when(mWifiP2pGroup).getNetworkName();
final List<WifiP2pGroup> groupList = new ArrayList<>();
groupList.add(mWifiP2pGroup);
final WifiP2pGroupList wifiP2pGroupList = mock(WifiP2pGroupList.class);
doReturn(groupList).when(wifiP2pGroupList).getGroupList();
final Bundle bundle = new Bundle();
bundle.putString(WifiP2pSettings.SAVE_SELECTED_GROUP, fakeGroupName);
mFragment.onActivityCreated(bundle);
mFragment.onPersistentGroupInfoAvailable(wifiP2pGroupList);
assertThat(mFragment.mPersistentCategoryController.isAvailable()).isTrue();
}
@Test
public void persistentController_withNoGroup_shouldBeUnavailable() {
final WifiP2pGroupList wifiP2pGroupList = mock(WifiP2pGroupList.class);
final List<WifiP2pGroup> groupList = new ArrayList<>();
doReturn(groupList).when(wifiP2pGroupList).getGroupList();
mFragment.onPersistentGroupInfoAvailable(wifiP2pGroupList);
assertThat(mFragment.mPersistentCategoryController.isAvailable()).isFalse();
}
@Test
public void peersCategoryController_withOnePeerDevice_shouldBeAvailable() {
final WifiP2pDevice wifiP2pDevice = mock(WifiP2pDevice.class);
final ArrayList<WifiP2pDevice> deviceList = new ArrayList<>();
deviceList.add(wifiP2pDevice);
final WifiP2pDeviceList peers = mock(WifiP2pDeviceList.class);
doReturn(deviceList).when(peers).getDeviceList();
mFragment.onPeersAvailable(peers);
assertThat(mFragment.mPeerCategoryController.isAvailable()).isTrue();
}
@Test
public void peersCategoryController_withNoPeerDevice_shouldBeUnavailable() {
final ArrayList<WifiP2pDevice> deviceList = new ArrayList<>();
final WifiP2pDeviceList peers = mock(WifiP2pDeviceList.class);
doReturn(deviceList).when(peers).getDeviceList();
mFragment.onPeersAvailable(peers);
assertThat(mFragment.mPeerCategoryController.isAvailable()).isFalse();
}
@Test
public void thisDeviceController_onDeviceInfoAvailable_shouldUpdateDeviceName() {
final WifiP2pDevice wifiP2pDevice = mock(WifiP2pDevice.class);
final P2pThisDevicePreferenceController thisDevicePreferenceController = mock(
P2pThisDevicePreferenceController.class);
mFragment.mThisDevicePreferenceController = thisDevicePreferenceController;
mFragment.onDeviceInfoAvailable(wifiP2pDevice);
verify(thisDevicePreferenceController, times(1)).updateDeviceName(any());
}
@Test
public void p2pThisDeviceChange_shouldRequestDeviceInfoAgain() {
final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mFragment.mReceiver.onReceive(mContext, intent);
verify(mWifiP2pManager, times(2)).requestDeviceInfo(any(), any());
}
@Test
public void p2pPersistentGroupChange_shouldRequestGroupInfo() {
final Intent intent = new Intent(WifiP2pManager.ACTION_WIFI_P2P_PERSISTENT_GROUPS_CHANGED);
mFragment.mReceiver.onReceive(mContext, intent);
verify(mWifiP2pManager, times(1)).requestPersistentGroupInfo(any(), any());
}
@Test
public void onActivityCreate_withNullP2pManager_shouldGetP2pManagerAgain() {
mFragment.mWifiP2pManager = null;
mFragment.onActivityCreated(new Bundle());
assertThat(mFragment.mWifiP2pManager).isNotNull();
}
@Test
public void onActivityCreate_withNullChannel_shouldSetP2pManagerNull() {
doReturn(null).when(mWifiP2pManager).initialize(any(), any(), any());
mFragment.onActivityCreated(new Bundle());
assertThat(mFragment.mWifiP2pManager).isNull();
}
@Test
public void clickNegativeButton_whenDeleteGroupDialogShow_shouldSetGroupNull() {
final WifiP2pPersistentGroup wifiP2pPersistentGroup = new WifiP2pPersistentGroup(mContext,
mWifiP2pGroup);
mFragment.mSelectedGroup = wifiP2pPersistentGroup;
final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_DELETE_GROUP);
mFragment.mDeleteGroupListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
assertThat(mFragment.mSelectedGroup).isNull();
}
private void setupOneP2pPeer(int status) {
final WifiP2pDevice wifiP2pDevice = mock(WifiP2pDevice.class);
wifiP2pDevice.status = status;