Update UI for Wifi Direct settings

- Change the launching preference from using an intent to directly a
fragemnt
- Partially convert WifiP2pSettings to a DashboardFragment
- Add a controller to control each pref category, when they have no
child, hide.
- Add a controller to control device's own AP name.

Change-Id: I23685c4d4a85f80ceab5a576005e693e6f8b7cc4
Fix: 36859626
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-04-25 16:19:07 -07:00
parent bd3cbf44ac
commit 022ef86f1a
12 changed files with 448 additions and 79 deletions

View File

@@ -268,7 +268,7 @@
<activity android:name="Settings$ConfigureWifiSettingsActivity" <activity android:name="Settings$ConfigureWifiSettingsActivity"
android:taskAffinity="com.android.settings" android:taskAffinity="com.android.settings"
android:label="@string/wifi_configure_titlebar" android:label="@string/wifi_configure_settings_preference_title"
android:icon="@drawable/ic_settings_wireless" android:icon="@drawable/ic_settings_wireless"
android:configChanges="orientation|keyboardHidden|screenSize" android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName="Settings$WifiSettingsActivity"> android:parentActivityName="Settings$WifiSettingsActivity">

View File

@@ -1878,8 +1878,6 @@
</plurals> </plurals>
<!-- Wi-Fi settings screen, advanced, settings section. This is a header shown above advanced wifi settings. [CHAR LIMIT=30] --> <!-- Wi-Fi settings screen, advanced, settings section. This is a header shown above advanced wifi settings. [CHAR LIMIT=30] -->
<string name="wifi_advanced_titlebar">Advanced Wi\u2011Fi</string> <string name="wifi_advanced_titlebar">Advanced Wi\u2011Fi</string>
<!-- Wi-Fi settings screen, configure, settings section. This is a header shown above configure wifi settings. [CHAR LIMIT=30] -->
<string name="wifi_configure_titlebar">Configure Wi\u2011Fi</string>
<!-- Wi-Fi settings screen, advanced, title of the item to show the Wi-Fi device's MAC address. --> <!-- Wi-Fi settings screen, advanced, title of the item to show the Wi-Fi device's MAC address. -->
<string name="wifi_advanced_mac_address_title">MAC address</string> <string name="wifi_advanced_mac_address_title">MAC address</string>
<!-- Title of the screen to adjust IP settings --> <!-- Title of the screen to adjust IP settings -->

View File

@@ -17,7 +17,7 @@
<PreferenceScreen <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings" xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/wifi_configure_titlebar"> android:title="@string/wifi_configure_settings_preference_title">
<SwitchPreference <SwitchPreference
android:key="enable_wifi_wakeup" android:key="enable_wifi_wakeup"
@@ -70,10 +70,8 @@
<Preference <Preference
android:key="wifi_direct" android:key="wifi_direct"
android:title="@string/wifi_menu_p2p"> android:title="@string/wifi_menu_p2p"
<intent android:targetPackage="com.android.settings" android:fragment="com.android.settings.wifi.p2p.WifiP2pSettings"/>
android:targetClass="com.android.settings.Settings$WifiP2pSettingsActivity"/>
</Preference>
<Preference <Preference
android:key="wps_push_button" android:key="wps_push_button"

View File

@@ -15,5 +15,13 @@
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="p2p_this_device"
android:selectable="false" />
<PreferenceCategory
android:key="p2p_peer_devices"
android:title="@string/wifi_p2p_peer_devices" />
<PreferenceCategory
android:key="p2p_persistent_group"
android:title="@string/wifi_p2p_remembered_groups" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017 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 android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.PreferenceController;
public abstract class P2pCategoryPreferenceController extends PreferenceController {
protected PreferenceGroup mCategory;
public P2pCategoryPreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mCategory = (PreferenceGroup) screen.findPreference(getPreferenceKey());
}
public void removeAllChildren() {
if (mCategory != null) {
mCategory.removeAll();
mCategory.setVisible(false);
}
}
public void addChild(Preference child) {
if (mCategory != null) {
mCategory.addPreference(child);
mCategory.setVisible(true);
}
}
public void setEnabled(boolean enabled) {
if (mCategory != null) {
mCategory.setEnabled(enabled);
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 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 android.content.Context;
public class P2pPeerCategoryPreferenceController extends P2pCategoryPreferenceController {
public P2pPeerCategoryPreferenceController(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return "p2p_peer_devices";
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2017 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 android.content.Context;
public class P2pPersistentCategoryPreferenceController extends P2pCategoryPreferenceController {
public P2pPersistentCategoryPreferenceController(Context context) {
super(context);
}
@Override
public String getPreferenceKey() {
return "p2p_persistent_group";
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2017 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 android.content.Context;
import android.net.wifi.p2p.WifiP2pDevice;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.settings.core.PreferenceController;
public class P2pThisDevicePreferenceController extends PreferenceController {
private Preference mPreference;
public P2pThisDevicePreferenceController(Context context) {
super(context);
}
@Override
public boolean isAvailable() {
return true;
}
@Override
public String getPreferenceKey() {
return "p2p_this_device";
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
}
public void setEnabled(boolean enabled) {
if (mPreference != null) {
mPreference.setEnabled(enabled);
}
}
public void updateDeviceName(WifiP2pDevice thisDevice) {
if (mPreference != null && thisDevice != null) {
if (TextUtils.isEmpty(thisDevice.deviceName)) {
mPreference.setTitle(thisDevice.deviceAddress);
} else {
mPreference.setTitle(thisDevice.deviceName);
}
}
}
}

View File

@@ -39,8 +39,6 @@ import android.net.wifi.p2p.WifiP2pManager.PersistentGroupInfoListener;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.TextUtils; import android.text.TextUtils;
@@ -53,12 +51,16 @@ import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.core.PreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import java.util.ArrayList;
import java.util.List;
/* /*
* Displays Wi-fi p2p settings UI * Displays Wi-fi p2p settings UI
*/ */
public class WifiP2pSettings extends SettingsPreferenceFragment public class WifiP2pSettings extends DashboardFragment
implements PersistentGroupInfoListener, PeerListListener { implements PersistentGroupInfoListener, PeerListListener {
private static final String TAG = "WifiP2pSettings"; private static final String TAG = "WifiP2pSettings";
@@ -83,9 +85,9 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
private int mConnectedDevices; private int mConnectedDevices;
private boolean mLastGroupFormed = false; private boolean mLastGroupFormed = false;
private PreferenceGroup mPeersGroup; private P2pPeerCategoryPreferenceController mPeerCategoryController;
private PreferenceGroup mPersistentGroup; private P2pPersistentCategoryPreferenceController mPersistentCategoryController;
private Preference mThisDevicePref; private P2pThisDevicePreferenceController mThisDevicePreferenceController;
private static final int DIALOG_DISCONNECT = 1; private static final int DIALOG_DISCONNECT = 1;
private static final int DIALOG_CANCEL_CONNECT = 2; private static final int DIALOG_CANCEL_CONNECT = 2;
@@ -132,7 +134,7 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
mThisDevice = (WifiP2pDevice) intent.getParcelableExtra( mThisDevice = (WifiP2pDevice) intent.getParcelableExtra(
WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); WifiP2pManager.EXTRA_WIFI_P2P_DEVICE);
if (DBG) Log.d(TAG, "Update device info: " + mThisDevice); if (DBG) Log.d(TAG, "Update device info: " + mThisDevice);
updateDevicePref(); mThisDevicePreferenceController.updateDeviceName(mThisDevice);
} else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) { } else if (WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION.equals(action)) {
int discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE, int discoveryState = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE,
WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED); WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED);
@@ -150,21 +152,37 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
} }
}; };
public WifiP2pSettings() { @Override
if (DBG) Log.d(TAG, "Creating WifiP2pSettings ..."); protected String getLogTag() {
return TAG;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.wifi_p2p_settings;
}
@Override
public int getMetricsCategory() {
return MetricsEvent.WIFI_P2P;
}
@Override
protected List<PreferenceController> getPreferenceControllers(Context context) {
final List<PreferenceController> controllers = new ArrayList<>();
mPersistentCategoryController =
new P2pPersistentCategoryPreferenceController(context);
mPeerCategoryController =
new P2pPeerCategoryPreferenceController(context);
mThisDevicePreferenceController = new P2pThisDevicePreferenceController(context);
controllers.add(mPersistentCategoryController);
controllers.add(mPeerCategoryController);
controllers.add(mThisDevicePreferenceController);
return controllers;
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.wifi_p2p_settings);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION);
final Activity activity = getActivity(); final Activity activity = getActivity();
mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); mWifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
if (mWifiP2pManager != null) { if (mWifiP2pManager != null) {
@@ -274,16 +292,17 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
mWifiP2pManager.deletePersistentGroup(mChannel, mWifiP2pManager.deletePersistentGroup(mChannel,
mSelectedGroup.getNetworkId(), mSelectedGroup.getNetworkId(),
new WifiP2pManager.ActionListener() { new WifiP2pManager.ActionListener() {
public void onSuccess() { public void onSuccess() {
if (DBG) Log.d(TAG, " delete group success"); if (DBG) Log.d(TAG, " delete group success");
} }
public void onFailure(int reason) {
if (DBG) Log.d(TAG, " delete group fail " + reason); public void onFailure(int reason) {
} if (DBG) Log.d(TAG, " delete group fail " + reason);
}); }
});
mSelectedGroup = null; mSelectedGroup = null;
} else { } else {
if (DBG) Log.w(TAG, " No selected group to delete!" ); if (DBG) Log.w(TAG, " No selected group to delete!");
} }
} }
} else if (which == DialogInterface.BUTTON_NEGATIVE) { } else if (which == DialogInterface.BUTTON_NEGATIVE) {
@@ -295,31 +314,20 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
} }
}; };
setHasOptionsMenu(true);
final PreferenceScreen preferenceScreen = getPreferenceScreen();
preferenceScreen.removeAll();
preferenceScreen.setOrderingAsAdded(true);
mThisDevicePref = new Preference(getPrefContext());
mThisDevicePref.setPersistent(false);
mThisDevicePref.setSelectable(false);
preferenceScreen.addPreference(mThisDevicePref);
mPeersGroup = new PreferenceCategory(getPrefContext());
mPeersGroup.setTitle(R.string.wifi_p2p_peer_devices);
preferenceScreen.addPreference(mPeersGroup);
mPersistentGroup = new PreferenceCategory(getPrefContext());
mPersistentGroup.setTitle(R.string.wifi_p2p_remembered_groups);
preferenceScreen.addPreference(mPersistentGroup);
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION);
mIntentFilter.addAction(WifiP2pManager.WIFI_P2P_PERSISTENT_GROUPS_CHANGED_ACTION);
final PreferenceScreen preferenceScreen = getPreferenceScreen();
getActivity().registerReceiver(mReceiver, mIntentFilter); getActivity().registerReceiver(mReceiver, mIntentFilter);
if (mWifiP2pManager != null) { if (mWifiP2pManager != null) {
mWifiP2pManager.requestPeers(mChannel, WifiP2pSettings.this); mWifiP2pManager.requestPeers(mChannel, WifiP2pSettings.this);
@@ -490,11 +498,6 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
return null; return null;
} }
@Override
public int getMetricsCategory() {
return MetricsEvent.WIFI_P2P;
}
@Override @Override
public int getDialogMetricsCategory(int dialogId) { public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) { switch (dialogId) {
@@ -524,13 +527,13 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
} }
private void handlePeersChanged() { private void handlePeersChanged() {
mPeersGroup.removeAll(); mPeerCategoryController.removeAllChildren();
mConnectedDevices = 0; mConnectedDevices = 0;
if (DBG) Log.d(TAG, "List of available peers"); if (DBG) Log.d(TAG, "List of available peers");
for (WifiP2pDevice peer: mPeers.getDeviceList()) { for (WifiP2pDevice peer: mPeers.getDeviceList()) {
if (DBG) Log.d(TAG, "-> " + peer); if (DBG) Log.d(TAG, "-> " + peer);
mPeersGroup.addPreference(new WifiP2pPeer(getActivity(), peer)); mPeerCategoryController.addChild(new WifiP2pPeer(getActivity(), peer));
if (peer.status == WifiP2pDevice.CONNECTED) mConnectedDevices++; if (peer.status == WifiP2pDevice.CONNECTED) mConnectedDevices++;
} }
if (DBG) Log.d(TAG, " mConnectedDevices " + mConnectedDevices); if (DBG) Log.d(TAG, " mConnectedDevices " + mConnectedDevices);
@@ -538,12 +541,12 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
@Override @Override
public void onPersistentGroupInfoAvailable(WifiP2pGroupList groups) { public void onPersistentGroupInfoAvailable(WifiP2pGroupList groups) {
mPersistentGroup.removeAll(); mPersistentCategoryController.removeAllChildren();
for (WifiP2pGroup group: groups.getGroupList()) { for (WifiP2pGroup group: groups.getGroupList()) {
if (DBG) Log.d(TAG, " group " + group); if (DBG) Log.d(TAG, " group " + group);
WifiP2pPersistentGroup wppg = new WifiP2pPersistentGroup(getActivity(), group); WifiP2pPersistentGroup wppg = new WifiP2pPersistentGroup(getPrefContext(), group);
mPersistentGroup.addPreference(wppg); mPersistentCategoryController.addChild(wppg);
if (wppg.getGroupName().equals(mSelectedGroupName)) { if (wppg.getGroupName().equals(mSelectedGroupName)) {
if (DBG) Log.d(TAG, "Selecting group " + wppg.getGroupName()); if (DBG) Log.d(TAG, "Selecting group " + wppg.getGroupName());
mSelectedGroup = wppg; mSelectedGroup = wppg;
@@ -568,9 +571,9 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
private void handleP2pStateChanged() { private void handleP2pStateChanged() {
updateSearchMenu(false); updateSearchMenu(false);
mThisDevicePref.setEnabled(mWifiP2pEnabled); mThisDevicePreferenceController.setEnabled(mWifiP2pEnabled);
mPeersGroup.setEnabled(mWifiP2pEnabled); mPersistentCategoryController.setEnabled(mWifiP2pEnabled);
mPersistentGroup.setEnabled(mWifiP2pEnabled); mPeerCategoryController.setEnabled(mWifiP2pEnabled);
} }
private void updateSearchMenu(boolean searching) { private void updateSearchMenu(boolean searching) {
@@ -590,14 +593,4 @@ public class WifiP2pSettings extends SettingsPreferenceFragment
}); });
} }
} }
private void updateDevicePref() {
if (mThisDevice != null) {
if (TextUtils.isEmpty(mThisDevice.deviceName)) {
mThisDevicePref.setTitle(mThisDevice.deviceAddress);
} else {
mThisDevicePref.setTitle(mThisDevice.deviceName);
}
}
}
} }

View File

@@ -7,8 +7,9 @@ com.android.settings.fuelgauge.PowerUsageDetail
com.android.settings.fuelgauge.AdvancedPowerUsageDetail com.android.settings.fuelgauge.AdvancedPowerUsageDetail
com.android.settings.deviceinfo.StorageProfileFragment com.android.settings.deviceinfo.StorageProfileFragment
com.android.settings.wifi.details.WifiNetworkDetailsFragment com.android.settings.wifi.details.WifiNetworkDetailsFragment
com.android.settings.wifi.p2p.WifiP2pSettings
com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionCamera com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionCamera
com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionLocation com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionLocation
com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionMicrophone com.android.settings.enterprise.ApplicationListFragment$AdminGrantedPermissionMicrophone
com.android.settings.enterprise.ApplicationListFragment$EnterpriseInstalledPackages com.android.settings.enterprise.ApplicationListFragment$EnterpriseInstalledPackages
com.android.settings.enterprise.EnterpriseSetDefaultAppsListFragment com.android.settings.enterprise.EnterpriseSetDefaultAppsListFragment

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 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 android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class P2pCategoryPreferenceControllerTest {
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private PreferenceCategory mCategory;
private P2pCategoryPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mPreferenceScreen.findPreference(anyString())).thenReturn(mCategory);
mController = new P2pCategoryPreferenceController(RuntimeEnvironment.application) {
@Override
public String getPreferenceKey() {
return "test_key";
}
};
mController.displayPreference(mPreferenceScreen);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void removeAllChildren_shouldRemove() {
mController.removeAllChildren();
verify(mCategory).removeAll();
verify(mCategory).setVisible(false);
}
@Test
public void addChild_shouldAdd() {
final Preference pref = new Preference(RuntimeEnvironment.application);
mController.addChild(pref);
verify(mCategory).addPreference(pref);
verify(mCategory).setVisible(true);
}
@Test
public void shouldToggleEnable() {
mController.setEnabled(false);
verify(mCategory).setEnabled(false);
mController.setEnabled(true);
verify(mCategory).setEnabled(true);
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2017 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 android.net.wifi.p2p.WifiP2pDevice;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class P2pThisDevicePreferenceControllerTest {
@Mock
private PreferenceScreen mPreferenceScreen;
private Preference mPreference;
private P2pThisDevicePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mPreference = new Preference(RuntimeEnvironment.application);
when(mPreferenceScreen.findPreference(anyString())).thenReturn(mPreference);
mController = new P2pThisDevicePreferenceController(RuntimeEnvironment.application);
}
@Test
public void isAlwaysAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void updateDeviceName_emptyName_shouldUseIpAddress() {
WifiP2pDevice device = new WifiP2pDevice();
device.deviceAddress = "address";
mController.displayPreference(mPreferenceScreen);
mController.updateDeviceName(device);
assertThat(mPreference.getTitle()).isEqualTo(device.deviceAddress);
}
@Test
public void updateDeviceName_hasName_shouldUseName() {
WifiP2pDevice device = new WifiP2pDevice();
device.deviceAddress = "address";
device.deviceName = "name";
mController.displayPreference(mPreferenceScreen);
mController.updateDeviceName(device);
assertThat(mPreference.getTitle()).isEqualTo(device.deviceName);
}
}