Merge "Move BT, NFC out of Conntection pref" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-03-28 17:38:34 +00:00
committed by Android (Google) Code Review
14 changed files with 718 additions and 262 deletions

View File

@@ -16,13 +16,10 @@
package com.android.settings.connecteddevice;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
;
import android.provider.SearchIndexableResource;
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowBluetoothPan;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.testutils.shadow.ShadowUserManager;
@@ -59,8 +56,8 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
@Test
public void testSearchIndexProvider_shouldIndexResource() {
final List<SearchIndexableResource> indexRes =
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getXmlResourcesToIndex(RuntimeEnvironment.application, true /* enabled */);
assertThat(indexRes).isNotNull();
assertThat(indexRes.get(0).xmlResId).isEqualTo(mFragment.getPreferenceScreenResId());
@@ -70,14 +67,4 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
public void testGetCategoryKey_returnCategoryDevice() {
assertThat(mFragment.getCategoryKey()).isEqualTo(CategoryKey.CATEGORY_DEVICE);
}
@Test
public void testNonIndexableKeys_existInXmlLayout() {
final Context context = RuntimeEnvironment.application;
final List<String> niks =
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(context);
assertThat(niks).contains(BluetoothMasterSwitchPreferenceController.KEY_TOGGLE_BLUETOOTH);
}
}
}

View File

@@ -30,6 +30,7 @@ import android.content.pm.PackageManager;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.nfc.NfcPreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowBluetoothPan;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
@@ -77,6 +78,7 @@ public class ConnectedDeviceDashboardFragmentTest {
final List<String> niks = ConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);
assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_SAVED_DEVICES);
assertThat(niks).containsExactly(KEY_CONNECTED_DEVICES, KEY_SAVED_DEVICES,
NfcPreferenceController.KEY_TOGGLE_NFC);
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (C) 2018 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.nfc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedPreference;
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.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
public class AndroidBeamPreferenceControllerTest {
Context mContext;
@Mock
private NfcAdapter mNfcAdapter;
@Mock
NfcManager mManager;
@Mock
private UserManager mUserManager;
@Mock
private PreferenceScreen mScreen;
private RestrictedPreference mAndroidBeamPreference;
private AndroidBeamPreferenceController mAndroidBeamController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(false);
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
mAndroidBeamController = new AndroidBeamPreferenceController(mContext);
mAndroidBeamPreference = new RestrictedPreference(RuntimeEnvironment.application);
when(mScreen.findPreference(mAndroidBeamController.getPreferenceKey())).thenReturn(
mAndroidBeamPreference);
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
Settings.Global.RADIO_NFC);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,
0);
mAndroidBeamController.displayPreference(mScreen);
}
@Test
public void isAvailable_hasNfc_shouldReturnTrue() {
when(mNfcAdapter.isEnabled()).thenReturn(true);
assertThat(mAndroidBeamController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noNfcAdapter_shouldReturnFalse() {
ReflectionHelpers.setField(mAndroidBeamController, "mNfcAdapter", null);
assertThat(mAndroidBeamController.isAvailable()).isFalse();
}
@Test
public void isBeamEnable_disAllowBeam_shouldReturnFalse() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
when(RestrictedLockUtils.hasBaseUserRestriction(mContext,
UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId())).thenReturn(true);
mAndroidBeamController.displayPreference(mScreen);
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
}
@Test
public void isBeamEnable_nfcStateOn_shouldReturnTrue() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
try {
mAndroidBeamController.onResume();
} catch (NullPointerException e) {
// skip because it's just test
// it will meet NullPointerException in checkRestrictionAndSetDisabled
}
assertThat(mAndroidBeamPreference.isEnabled()).isTrue();
}
@Test
public void isBeamEnable_nfcStateNotOn_shouldReturnFalse() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
mAndroidBeamController.onResume();
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
mAndroidBeamController.onResume();
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
mAndroidBeamController.onResume();
assertThat(mAndroidBeamPreference.isEnabled()).isFalse();
}
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright (C) 2018 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.nfc;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
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.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
public class NfcPreferenceControllerTest {
Context mContext;
@Mock
private NfcAdapter mNfcAdapter;
@Mock
NfcManager mManager;
@Mock
private UserManager mUserManager;
@Mock
private PreferenceScreen mScreen;
private SwitchPreference mNfcPreference;
private NfcPreferenceController mNfcController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mManager);
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
mNfcController = new NfcPreferenceController(mContext);
mNfcPreference = new SwitchPreference(RuntimeEnvironment.application);
when(mScreen.findPreference(mNfcController.getPreferenceKey())).thenReturn(mNfcPreference);
Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
Settings.Global.RADIO_NFC);
Settings.Global.putInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,
0);
mNfcController.displayPreference(mScreen);
}
@Test
public void isAvailable_hasNfc_shouldReturnTrue() {
when(mNfcAdapter.isEnabled()).thenReturn(true);
assertThat(mNfcController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noNfcAdapter_shouldReturnFalse() {
ReflectionHelpers.setField(mNfcController, "mNfcAdapter", null);
assertThat(mNfcController.isAvailable()).isFalse();
}
@Test
public void isNfcEnable_nfcStateNotTurning_shouldReturnTrue() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
mNfcController.onResume();
assertThat(mNfcPreference.isEnabled()).isTrue();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
mNfcController.onResume();
assertThat(mNfcPreference.isEnabled()).isTrue();
}
@Test
public void isNfcEnable_nfcStateTurning_shouldReturnFalse() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
mNfcController.onResume();
assertThat(mNfcPreference.isEnabled()).isFalse();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
mNfcController.onResume();
assertThat(mNfcPreference.isEnabled()).isFalse();
}
@Test
public void isNfcChecked_nfcStateOn_shouldReturnTrue() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_ON);
mNfcController.onResume();
assertThat(mNfcPreference.isChecked()).isTrue();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_ON);
mNfcController.onResume();
assertThat(mNfcPreference.isChecked()).isTrue();
}
@Test
public void isNfcChecked_nfcStateOff_shouldReturnFalse() {
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_OFF);
mNfcController.onResume();
assertThat(mNfcPreference.isChecked()).isFalse();
when(mNfcAdapter.getAdapterState()).thenReturn(NfcAdapter.STATE_TURNING_OFF);
mNfcController.onResume();
assertThat(mNfcPreference.isChecked()).isFalse();
}
}