Merge changes Ief9e9690,I1cc1bc2d

* changes:
  Split BluetoothSettings into two pages
  Dupe BluetoothSettings and DeviceListPreferenceFragment
This commit is contained in:
TreeHugger Robot
2017-05-23 01:36:19 +00:00
committed by Android (Google) Code Review
25 changed files with 1777 additions and 253 deletions

View File

@@ -16,14 +16,18 @@
package com.android.settings.bluetooth;
import android.app.Fragment;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.support.v7.preference.Preference.OnPreferenceChangeListener;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.MasterSwitchPreference;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -38,7 +42,10 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -54,17 +61,26 @@ public class BluetoothMasterSwitchPreferenceControllerTest {
private MasterSwitchPreference mPreference;
@Mock
private RestrictionUtils mRestrictionUtils;
@Mock
private Fragment mFragment;
@Mock
private SettingsActivity mActivity;
private Context mContext;
private BluetoothMasterSwitchPreferenceController mController;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application.getApplicationContext();
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mController = new BluetoothMasterSwitchPreferenceController(
mContext, mBluetoothManager, mRestrictionUtils);
mContext, mBluetoothManager, mRestrictionUtils, mFragment, mActivity);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
}
@Test
@@ -84,7 +100,7 @@ public class BluetoothMasterSwitchPreferenceControllerTest {
mController.onPause();
verify(mBluetoothManager.getEventManager()).unregisterCallback(
any(BluetoothCallback.class));
any(BluetoothCallback.class));
}
@Test
@@ -114,4 +130,22 @@ public class BluetoothMasterSwitchPreferenceControllerTest {
verify(mPreference).setSummary("test summary");
}
@Test
public void testHandlePreferenceTreeClick_pairPageEnabled_showNewPage() {
when(mFeatureFactory.bluetoothFeatureProvider.isPairingPageEnabled()).thenReturn(true);
mController.handlePreferenceTreeClick(mPreference);
verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
eq(BluetoothSettings.class.getName()), any(), eq(R.string.bluetooth), any(), any());
}
@Test
public void testHandlePreferenceTreeClick_pairPageDisabled_showOldPage() {
mController.handlePreferenceTreeClick(mPreference);
verify(mActivity).startPreferencePanelAsUser(eq(mFragment),
eq(BluetoothSettingsObsolete.class.getName()), any(), eq(R.string.bluetooth), any(),
any());
}
}

View File

@@ -0,0 +1,133 @@
/*
* 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.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.res.Resources;
import android.os.UserManager;
import android.support.v7.preference.PreferenceGroup;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.widget.FooterPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothPairingDetailTest {
@Mock
private UserManager mUserManager;
@Mock
private Resources mResource;
@Mock
private LocalBluetoothAdapter mLocalAdapter;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocalBluetoothManager mLocalManager;
@Mock
private PreferenceGroup mPreferenceGroup;
private BluetoothPairingDetail mFragment;
private Context mContext;
private BluetoothProgressCategory mAvailableDevicesCategory;
private FooterPreference mFooterPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mFragment = spy(new BluetoothPairingDetail());
doReturn(mContext).when(mFragment).getContext();
doReturn(mResource).when(mFragment).getResources();
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
mFooterPreference = new FooterPreference(mContext);
mFragment.mLocalAdapter = mLocalAdapter;
mFragment.mLocalManager = mLocalManager;
mFragment.mDeviceListGroup = mPreferenceGroup;
}
@Test
public void testInitPreferencesFromPreferenceScreen_findPreferences() {
doReturn(mAvailableDevicesCategory).when(mFragment).findPreference(
BluetoothPairingDetail.KEY_AVAIL_DEVICES);
doReturn(mFooterPreference).when(mFragment).findPreference(
BluetoothPairingDetail.KEY_FOOTER_PREF);
mFragment.initPreferencesFromPreferenceScreen();
assertThat(mFragment.mAvailableDevicesCategory).isEqualTo(mAvailableDevicesCategory);
assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
}
@Test
public void testStartScanning_startScanAndRemoveDevices() {
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
mFragment.mDeviceListGroup = mAvailableDevicesCategory;
mFragment.startScanning();
verify(mLocalAdapter).startScanning(true);
verify(mAvailableDevicesCategory).removeAll();
}
@Test
public void testUpdateContent_stateOn_addDevices() {
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
mFragment.mFooterPreference = mFooterPreference;
doNothing().when(mFragment).addDeviceCategory(any(), anyInt(), any(), anyBoolean());
mFragment.updateContent(BluetoothAdapter.STATE_ON);
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
R.string.bluetooth_preference_found_devices,
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
verify(mLocalAdapter).setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}
@Test
public void testUpdateContent_stateOff_finish() {
mFragment.updateContent(BluetoothAdapter.STATE_OFF);
verify(mFragment).finish();
}
}

View File

@@ -18,6 +18,11 @@ package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.FragmentManager;
@@ -27,6 +32,7 @@ import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.R;
@@ -45,7 +51,7 @@ import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothPairingPreferenceControllerTest {
private static final int ORDER = 1;
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceFragment mFragment;
@@ -59,6 +65,8 @@ public class BluetoothPairingPreferenceControllerTest {
private FragmentTransaction mFragmentTransaction;
@Mock
private PreferenceScreen mPreferenceScreen;
@Mock
private SettingsActivity mSettingsActivity;
private Preference mPreference;
private BluetoothPairingPreferenceController mController;
@@ -70,16 +78,29 @@ public class BluetoothPairingPreferenceControllerTest {
mContext = RuntimeEnvironment.application;
when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext);
mController = new BluetoothPairingPreferenceController(mContext, mFragment);
mPreference = new Preference(mContext);
mPreference.setKey(BluetoothPairingPreferenceController.KEY_PAIRING);
mController = new BluetoothPairingPreferenceController(mContext, mFragment,
mSettingsActivity);
}
@Test
public void testCreateBluetoothPairingPreference() {
Preference pref = mController.createBluetoothPairingPreference();
Preference pref = mController.createBluetoothPairingPreference(ORDER);
assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add));
assertThat(pref.getOrder()).isEqualTo(ORDER);
assertThat(pref.getTitle()).isEqualTo(
mContext.getString(R.string.bluetooth_pairing_pref_title));
}
@Test
public void testHandlePreferenceTreeClick_startFragment() {
mController.handlePreferenceTreeClick(mPreference);
verify(mSettingsActivity).startPreferencePanelAsUser(eq(mFragment), anyString(), any(),
anyInt(), any(), any());
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothSettingsObsoleteTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private BluetoothSettingsObsolete mFragment;
private FakeFeatureFactory mFeatureFactory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mFragment = spy(new BluetoothSettingsObsolete());
doReturn(mContext).when(mFragment).getContext();
}
@Test
public void testSearchIndexProvider_pairPageEnabled_keyAdded() {
doReturn(true).when(mFeatureFactory.bluetoothFeatureProvider).isPairingPageEnabled();
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mContext);
assertThat(keys).contains(BluetoothSettingsObsolete.DATA_KEY_REFERENCE);
}
@Test
public void testSearchIndexProvider_pairPageDisabled_keyNotAdded() {
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mContext);
assertThat(keys).doesNotContain(BluetoothSettingsObsolete.DATA_KEY_REFERENCE);
}
}

View File

@@ -23,24 +23,33 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.view.View;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.widget.FooterPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class BluetoothSettingsTest {
@@ -51,23 +60,42 @@ public class BluetoothSettingsTest {
@Mock
private Resources mResource;
@Mock
private Context mContext;
@Mock
private LocalBluetoothAdapter mLocalAdapter;
@Mock
private Activity mActivity;
@Mock
private PreferenceGroup mPairedDevicesCategory;
@Mock
private BluetoothPairingPreferenceController mPairingPreferenceController;
private Context mContext;
private BluetoothSettings mFragment;
private Preference mMyDevicePreference;
private FakeFeatureFactory mFeatureFactory;
private Preference mFooterPreference;
private TextView mEmptyMessage;
private View mContainer;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mFragment = spy(new BluetoothSettings());
doReturn(mContext).when(mFragment).getContext();
doReturn(mResource).when(mFragment).getResources();
doReturn(mActivity).when(mFragment).getActivity();
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
mContainer = new View(mContext);
mEmptyMessage = new TextView(mContext);
doReturn(mContainer).when(mActivity).findViewById(android.R.id.list_container);
doReturn(mEmptyMessage).when(mActivity).findViewById(android.R.id.empty);
mFooterPreference = new FooterPreference(RuntimeEnvironment.application);
mFragment.setLocalBluetoothAdapter(mLocalAdapter);
mFragment.mPairingPrefController = mPairingPreferenceController;
}
@Test
@@ -81,9 +109,56 @@ public class BluetoothSettingsTest {
doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
eq(R.string.bluetooth_footer_mac_message), any());
mFragment.updateMyDevicePreference(mMyDevicePreference);
mFragment.updateFooterPreference(mFooterPreference);
assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
assertThat(mFooterPreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
}
@Test
public void testDisplayEmptyMessage_showEmptyMessage() {
mFragment.displayEmptyMessage(true);
assertThat(mContainer.getVisibility()).isEqualTo(View.INVISIBLE);
assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.VISIBLE);
}
@Test
public void testDisplayEmptyMessage_hideEmptyMessage() {
mFragment.displayEmptyMessage(false);
assertThat(mContainer.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.GONE);
}
@Test
public void testInitPreferencesFromPreferenceScreen() {
doReturn(mPairedDevicesCategory).when(mFragment).findPreference(
BluetoothSettings.KEY_PAIRED_DEVICES);
doReturn(mFooterPreference).when(mFragment).findPreference(
BluetoothSettings.KEY_FOOTER_PREF);
mFragment.initPreferencesFromPreferenceScreen();
assertThat(mFragment.mPairedDevicesCategory).isEqualTo(mPairedDevicesCategory);
assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
}
@Test
public void testSearchIndexProvider_pairPageEnabled_keyNotAdded() {
doReturn(true).when(mFeatureFactory.bluetoothFeatureProvider).isPairingPageEnabled();
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mContext);
assertThat(keys).doesNotContain(BluetoothSettings.DATA_KEY_REFERENCE);
}
@Test
public void testSearchIndexProvider_pairPageDisabled_keyAdded() {
final List<String> keys = mFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
mContext);
assertThat(keys).contains(BluetoothSettings.DATA_KEY_REFERENCE);
}
}

View File

@@ -0,0 +1,131 @@
/*
* 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.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.res.Resources;
import android.os.UserManager;
import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.PreferenceController;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
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 java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class DeviceListPreferenceFragmentTest {
private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
@Mock
private UserManager mUserManager;
@Mock
private Resources mResource;
@Mock
private Context mContext;
@Mock
private LocalBluetoothAdapter mLocalAdapter;
private TestFragment mFragment;
private Preference mMyDevicePreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = spy(new TestFragment());
doReturn(mContext).when(mFragment).getContext();
doReturn(mResource).when(mFragment).getResources();
mFragment.mLocalAdapter = mLocalAdapter;
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
}
@Test
public void setUpdateMyDevicePreference_setTitleCorrectly() {
doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
eq(R.string.bluetooth_footer_mac_message), any());
mFragment.updateFooterPreference(mMyDevicePreference);
assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
}
/**
* Fragment to test since {@code DeviceListPreferenceFragment} is abstract
*/
public static class TestFragment extends DeviceListPreferenceFragment {
public TestFragment() {
super("");
}
@Override
public int getMetricsCategory() {
return 0;
}
@Override
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
}
@Override
void initPreferencesFromPreferenceScreen() {
}
@Override
public String getDeviceListKey() {
return null;
}
@Override
protected String getLogTag() {
return null;
}
@Override
protected int getPreferenceScreenResId() {
return 0;
}
@Override
protected List<PreferenceController> getPreferenceControllers(Context context) {
return null;
}
}
}

View File

@@ -22,10 +22,10 @@ import android.nfc.NfcManager;
import android.provider.SearchIndexableResource;
import com.android.settings.R;
import com.android.settings.nfc.NfcPreferenceController;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.dashboard.SummaryLoader;
import com.android.settings.nfc.NfcPreferenceController;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settingslib.drawer.CategoryKey;