diff --git a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java index 7f82359de93..07bee3bc30a 100644 --- a/src/com/android/settings/wifi/p2p/WifiP2pSettings.java +++ b/src/com/android/settings/wifi/p2p/WifiP2pSettings.java @@ -70,35 +70,35 @@ public class WifiP2pSettings extends DashboardFragment private static final String TAG = "WifiP2pSettings"; private static final boolean DBG = false; - private static final int MENU_ID_SEARCH = Menu.FIRST; - private static final int MENU_ID_RENAME = Menu.FIRST + 1; + @VisibleForTesting static final int MENU_ID_SEARCH = Menu.FIRST; + @VisibleForTesting static final int MENU_ID_RENAME = Menu.FIRST + 1; private final IntentFilter mIntentFilter = new IntentFilter(); - private WifiP2pManager mWifiP2pManager; + @VisibleForTesting WifiP2pManager mWifiP2pManager; private WifiP2pManager.Channel mChannel; - private OnClickListener mRenameListener; - private OnClickListener mDisconnectListener; - private OnClickListener mCancelConnectListener; - private OnClickListener mDeleteGroupListener; + @VisibleForTesting OnClickListener mRenameListener; + @VisibleForTesting OnClickListener mDisconnectListener; + @VisibleForTesting OnClickListener mCancelConnectListener; + @VisibleForTesting OnClickListener mDeleteGroupListener; @VisibleForTesting WifiP2pPeer mSelectedWifiPeer; - private WifiP2pPersistentGroup mSelectedGroup; + @VisibleForTesting WifiP2pPersistentGroup mSelectedGroup; @VisibleForTesting String mSelectedGroupName; private EditText mDeviceNameText; private boolean mWifiP2pEnabled; - private boolean mWifiP2pSearching; - private int mConnectedDevices; - private boolean mLastGroupFormed = false; + @VisibleForTesting boolean mWifiP2pSearching; + @VisibleForTesting int mConnectedDevices; + @VisibleForTesting boolean mLastGroupFormed = false; private boolean mIsIgnoreInitConnectionInfoCallback = false; private P2pPeerCategoryPreferenceController mPeerCategoryController; private P2pPersistentCategoryPreferenceController mPersistentCategoryController; private P2pThisDevicePreferenceController mThisDevicePreferenceController; - private static final int DIALOG_DISCONNECT = 1; - private static final int DIALOG_CANCEL_CONNECT = 2; - private static final int DIALOG_RENAME = 3; - private static final int DIALOG_DELETE_GROUP = 4; + @VisibleForTesting static final int DIALOG_DISCONNECT = 1; + @VisibleForTesting static final int DIALOG_CANCEL_CONNECT = 2; + @VisibleForTesting static final int DIALOG_RENAME = 3; + @VisibleForTesting static final int DIALOG_DELETE_GROUP = 4; private static final String SAVE_DIALOG_PEER = "PEER_STATE"; @VisibleForTesting static final String SAVE_DEVICE_NAME = "DEV_NAME"; @@ -109,7 +109,8 @@ public class WifiP2pSettings extends DashboardFragment @VisibleForTesting String mSavedDeviceName; - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @VisibleForTesting + final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); @@ -198,7 +199,10 @@ public class WifiP2pSettings extends DashboardFragment @Override public void onActivityCreated(Bundle savedInstanceState) { final Activity activity = getActivity(); - mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); + if (mWifiP2pManager == null) { + mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); + } + if (mWifiP2pManager != null) { mChannel = mWifiP2pManager.initialize(activity.getApplicationContext(), getActivity().getMainLooper(), null); diff --git a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java index ba6a0758393..d212ddb6e3c 100644 --- a/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java +++ b/tests/robotests/src/com/android/settings/wifi/p2p/WifiP2pSettingsTest.java @@ -18,13 +18,29 @@ package com.android.settings.wifi.p2p; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import android.app.Dialog; +import android.app.settings.SettingsEnums; import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +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.WifiP2pInfo; +import android.net.wifi.p2p.WifiP2pManager; import android.os.Bundle; +import android.view.MenuItem; import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentTransaction; import com.android.settings.testutils.XmlTestUtils; import com.android.settingslib.core.AbstractPreferenceController; @@ -32,6 +48,7 @@ import com.android.settingslib.core.AbstractPreferenceController; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; @@ -47,12 +64,30 @@ public class WifiP2pSettingsTest { private FragmentActivity mActivity; private WifiP2pSettings mFragment; + @Mock + public WifiP2pManager mWifiP2pManager; + + @Mock + private WifiP2pManager.Channel mChannel; + + @Mock + private WifiP2pPeer mWifiP2pPeer; + + @Mock + private WifiP2pGroup mWifiP2pGroup; + @Before public void setUp() { MockitoAnnotations.initMocks(this); mContext = RuntimeEnvironment.application; mActivity = Robolectric.setupActivity(FragmentActivity.class); - mFragment = spy(new WifiP2pSettings()); + mFragment = new WifiP2pSettings(); + mFragment.mWifiP2pManager = mWifiP2pManager; + doReturn(mChannel).when(mWifiP2pManager).initialize(any(), any(), any()); + FragmentManager fragmentManager = mActivity.getSupportFragmentManager(); + FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); + fragmentTransaction.add(mFragment, null); + fragmentTransaction.commit(); } @Test @@ -71,8 +106,6 @@ public class WifiP2pSettingsTest { @Test public void onActivityCreate_withNullBundle_canNotGetValue() { - when(mFragment.getActivity()).thenReturn(mActivity); - mFragment.onActivityCreated(null); assertThat(mFragment.mSelectedWifiPeer).isNull(); @@ -80,7 +113,6 @@ public class WifiP2pSettingsTest { @Test public void onActivityCreate_withDeviceName_shouldGetDeviceName() { - when(mFragment.getActivity()).thenReturn(mActivity); final String fakeDeviceName = "fakename"; final Bundle bundle = new Bundle(); bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName); @@ -92,7 +124,6 @@ public class WifiP2pSettingsTest { @Test public void onActivityCreate_withGroupName_shouldGetGroupName() { - when(mFragment.getActivity()).thenReturn(mActivity); final String fakeGroupName = "fakegroup"; final Bundle bundle = new Bundle(); bundle.putString(WifiP2pSettings.SAVE_SELECTED_GROUP, fakeGroupName); @@ -102,4 +133,238 @@ public class WifiP2pSettingsTest { assertThat(mFragment.mSelectedGroupName).isEqualTo(fakeGroupName); assertThat(mFragment.mSavedDeviceName).isNull(); } + + @Test + public void networkInfo_afterFragmentAttached_shouldBeRequested() { + verify(mWifiP2pManager, times(1)).requestNetworkInfo(any(), any()); + } + + @Test + public void beSearching_getP2pStateEnabledIntent_shouldBeFalse() { + final Bundle bundle = new Bundle(); + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); + bundle.putInt(WifiP2pManager.EXTRA_WIFI_STATE, WifiP2pManager.WIFI_P2P_STATE_ENABLED); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mWifiP2pSearching).isFalse(); + } + + @Test + public void withEmptyP2pDeviceList_getP2pPeerChangeIntent_connectedDevicesShouldBeZero() { + final WifiP2pDeviceList peers = new WifiP2pDeviceList(); + final Bundle bundle = new Bundle(); + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION); + bundle.putParcelable(WifiP2pManager.EXTRA_P2P_DEVICE_LIST, peers); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mConnectedDevices).isEqualTo(0); + } + + @Test + public void lastGroupForm_whenGroupFormInWifiP2pInfoIsFalse_beSetAsFalse() { + final NetworkInfo networkInfo = mock(NetworkInfo.class); + doReturn(true).when(networkInfo).isConnected(); + final WifiP2pInfo wifiP2pInfo = mock(WifiP2pInfo.class); + wifiP2pInfo.groupFormed = false; + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); + final Bundle bundle = new Bundle(); + bundle.putParcelable(WifiP2pManager.EXTRA_NETWORK_INFO, networkInfo); + bundle.putParcelable(WifiP2pManager.EXTRA_WIFI_P2P_INFO, wifiP2pInfo); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mLastGroupFormed).isFalse(); + } + + @Test + public void lastGroupForm_whenGroupFormInWifiP2pInfoIsTrue_beSetAsTrue() { + final NetworkInfo networkInfo = mock(NetworkInfo.class); + doReturn(true).when(networkInfo).isConnected(); + final WifiP2pInfo wifiP2pInfo = mock(WifiP2pInfo.class); + wifiP2pInfo.groupFormed = true; + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); + final Bundle bundle = new Bundle(); + bundle.putParcelable(WifiP2pManager.EXTRA_NETWORK_INFO, networkInfo); + bundle.putParcelable(WifiP2pManager.EXTRA_WIFI_P2P_INFO, wifiP2pInfo); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mLastGroupFormed).isTrue(); + } + + @Test + public void clickCancel_withInvitedPeerDialog_shouldCallCancelConnection() { + setupOneP2pPeer(WifiP2pDevice.INVITED); + mFragment.mSelectedWifiPeer = mWifiP2pPeer; + final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_CANCEL_CONNECT); + + mFragment.mCancelConnectListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + + verify(mWifiP2pManager, times(1)).cancelConnect(any(), any()); + } + + @Test + public void wifiP2pManager_clickOnFailedPeer_shouldTryToConnect() { + setupOneP2pPeer(WifiP2pDevice.FAILED); + + mFragment.onPreferenceTreeClick(mWifiP2pPeer); + + verify(mWifiP2pManager, times(1)).connect(any(), any(), any()); + } + + @Test + public void withNoStage_discoveryChanged_shouldStopSearching() { + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mWifiP2pSearching).isFalse(); + } + + @Test + public void withStartedStage_discoveryChanged_shouldStartSearching() { + final Bundle bundle = new Bundle(); + bundle.putInt(WifiP2pManager.EXTRA_DISCOVERY_STATE, + WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED); + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + assertThat(mFragment.mWifiP2pSearching).isTrue(); + } + + @Test + public void clickPositiveButton_whenDeleteGroupDialogShow_shouldDeleteGroup() { + 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_POSITIVE); + + assertThat(mFragment.mSelectedGroup).isNull(); + } + + @Test + public void noLastGroupForm_whenP2pDisconnected_shouldStartSearch() { + final NetworkInfo networkInfo = mock(NetworkInfo.class); + doReturn(false).when(networkInfo).isConnected(); + final WifiP2pInfo wifiP2pInfo = mock(WifiP2pInfo.class); + wifiP2pInfo.groupFormed = false; + final Bundle bundle = new Bundle(); + bundle.putParcelable(WifiP2pManager.EXTRA_NETWORK_INFO, networkInfo); + bundle.putParcelable(WifiP2pManager.EXTRA_WIFI_P2P_INFO, wifiP2pInfo); + final Intent intent = new Intent(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION); + intent.putExtras(bundle); + + mFragment.mReceiver.onReceive(mContext, intent); + + verify(mWifiP2pManager, times(1)).discoverPeers(any(), any()); + } + + @Test + public void withValidName_clickRenameDialog_shouldSetName() { + final String fakeDeviceName = "fakeName"; + final Bundle bundle = new Bundle(); + bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName); + mFragment.onActivityCreated(bundle); + final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME); + + mFragment.mRenameListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + + verify(mWifiP2pManager, times(1)).setDeviceName(any(), any(), any()); + } + + @Test + public void withInValidName_whenGetRenameRequest_shouldNotSetName() { + final String fakeDeviceName = "wrongName***"; + final Bundle bundle = new Bundle(); + bundle.putString(WifiP2pSettings.SAVE_DEVICE_NAME, fakeDeviceName); + mFragment.onActivityCreated(bundle); + final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_RENAME); + + mFragment.mRenameListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); + + verify(mWifiP2pManager, times(0)).setDeviceName(any(), any(), any()); + } + + @Test + public void pressDisconnectDialog_clickDisconnectDialog_shouldRemoveGroup() { + setupOneP2pPeer(WifiP2pDevice.CONNECTED); + mFragment.mSelectedWifiPeer = mWifiP2pPeer; + final Dialog dialog = mFragment.onCreateDialog(WifiP2pSettings.DIALOG_DISCONNECT); + + mFragment.mDisconnectListener.onClick(dialog, + DialogInterface.BUTTON_POSITIVE); + + verify(mWifiP2pManager, times(1)).removeGroup(any(), any()); + } + + @Test + public void onCreateDialog_withUnknownId_shouldReturnNull() { + assertThat(mFragment.onCreateDialog(-1 /* id */)).isNull(); + } + + @Test + public void peerDiscovery_whenOnPause_shouldStop() { + mFragment.onPause(); + + verify(mWifiP2pManager, times(1)).stopPeerDiscovery(any(), any()); + } + + @Test + public void whenGetSearchRequest_shouldStartToDiscoverPeer() { + final MenuItem menuItem = mock(MenuItem.class); + doReturn(mFragment.MENU_ID_SEARCH).when(menuItem).getItemId(); + + mFragment.onOptionsItemSelected(menuItem); + + verify(mWifiP2pManager, times(1)).discoverPeers(any(), any()); + } + + @Test + public void getMetrics_withDisconnectId_shouldReturnDisconnectMetricsCategory() { + assertThat(mFragment.getDialogMetricsCategory(WifiP2pSettings.DIALOG_DISCONNECT)).isEqualTo( + SettingsEnums.DIALOG_WIFI_P2P_DISCONNECT); + } + + @Test + public void getMetrics_withCancelConnectId_shouldReturnCancelConnectMetricsCategory() { + assertThat(mFragment.getDialogMetricsCategory( + WifiP2pSettings.DIALOG_CANCEL_CONNECT)).isEqualTo( + SettingsEnums.DIALOG_WIFI_P2P_CANCEL_CONNECT); + } + + @Test + public void getMetrics_withRenameId_shouldReturnRenameMetricsCategory() { + assertThat(mFragment.getDialogMetricsCategory(WifiP2pSettings.DIALOG_RENAME)).isEqualTo( + SettingsEnums.DIALOG_WIFI_P2P_RENAME); + } + + @Test + public void getMetrics_withDeleteGroupId_shouldReturnDeleteGroupMetricsCategory() { + assertThat( + mFragment.getDialogMetricsCategory(WifiP2pSettings.DIALOG_DELETE_GROUP)).isEqualTo( + SettingsEnums.DIALOG_WIFI_P2P_DELETE_GROUP); + } + + @Test + public void getMetrics_withUnknownId_shouldReturnZero() { + assertThat(mFragment.getDialogMetricsCategory(-1 /* dialogId */)).isEqualTo(0); + } + + private void setupOneP2pPeer(int status) { + final WifiP2pDevice wifiP2pDevice = mock(WifiP2pDevice.class); + wifiP2pDevice.status = status; + wifiP2pDevice.deviceAddress = "testAddress"; + wifiP2pDevice.deviceName = "testName"; + mWifiP2pPeer.device = wifiP2pDevice; + } }