From f63ad8f5f82075552890ba2a9e098bc10be86ee2 Mon Sep 17 00:00:00 2001 From: Fan Zhang Date: Tue, 10 Oct 2017 14:47:06 -0700 Subject: [PATCH] Refactor status page to DashboardFragment Change-Id: I7ebf1e39de5b5ac852e375873d6445c4ef9c8c0a Fixes: 65492237 Test: robotests --- .../core/PreferenceControllerMixin.java | 13 +- .../BatteryInfoPreferenceController.java | 104 +++++++++++++++ .../ImeiInfoPreferenceController.java | 45 +++++++ .../SimStatusPreferenceController.java | 45 +++++++ .../android/settings/deviceinfo/Status.java | 124 +++++------------- .../BatteryInfoPreferenceControllerTest.java | 105 +++++++++++++++ .../ImeiInfoPreferenceControllerTest.java | 81 ++++++++++++ .../SimStatusPreferenceControllerTest.java | 82 ++++++++++++ 8 files changed, 507 insertions(+), 92 deletions(-) create mode 100644 src/com/android/settings/deviceinfo/BatteryInfoPreferenceController.java create mode 100644 src/com/android/settings/deviceinfo/ImeiInfoPreferenceController.java create mode 100644 src/com/android/settings/deviceinfo/SimStatusPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/BatteryInfoPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/ImeiInfoPreferenceControllerTest.java create mode 100644 tests/robotests/src/com/android/settings/deviceinfo/SimStatusPreferenceControllerTest.java diff --git a/src/com/android/settings/core/PreferenceControllerMixin.java b/src/com/android/settings/core/PreferenceControllerMixin.java index 0f602109fb8..1c54af76884 100644 --- a/src/com/android/settings/core/PreferenceControllerMixin.java +++ b/src/com/android/settings/core/PreferenceControllerMixin.java @@ -15,6 +15,9 @@ */ package com.android.settings.core; +import android.text.TextUtils; +import android.util.Log; + import com.android.settings.search.ResultPayload; import com.android.settings.search.SearchIndexableRaw; import com.android.settingslib.core.AbstractPreferenceController; @@ -26,6 +29,8 @@ import java.util.List; */ public interface PreferenceControllerMixin { + String TAG = "PrefControllerMixin"; + /** * Updates non-indexable keys for search provider. * @@ -34,7 +39,13 @@ public interface PreferenceControllerMixin { default void updateNonIndexableKeys(List keys) { if (this instanceof AbstractPreferenceController) { if (!((AbstractPreferenceController) this).isAvailable()) { - keys.add(((AbstractPreferenceController) this).getPreferenceKey()); + final String key = ((AbstractPreferenceController) this).getPreferenceKey(); + if (TextUtils.isEmpty(key)) { + Log.w(TAG, + "Skipping updateNonIndexableKeys due to empty key " + this.toString()); + return; + } + keys.add(key); } } } diff --git a/src/com/android/settings/deviceinfo/BatteryInfoPreferenceController.java b/src/com/android/settings/deviceinfo/BatteryInfoPreferenceController.java new file mode 100644 index 00000000000..c37ff439b3b --- /dev/null +++ b/src/com/android/settings/deviceinfo/BatteryInfoPreferenceController.java @@ -0,0 +1,104 @@ +/* + * 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.deviceinfo; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.support.annotation.VisibleForTesting; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.Utils; +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; + +public class BatteryInfoPreferenceController extends AbstractPreferenceController + implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop { + + @VisibleForTesting + static final IntentFilter BATTERY_INFO_RECEIVER_INTENT_FILTER = + new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + @VisibleForTesting + static final String KEY_BATTERY_STATUS = "battery_status"; + @VisibleForTesting + static final String KEY_BATTERY_LEVEL = "battery_level"; + + @VisibleForTesting + BroadcastReceiver mBatteryInfoReceiver; + private Preference mBatteryStatus; + private Preference mBatteryLevel; + + + public BatteryInfoPreferenceController(Context context, Lifecycle lifecycle) { + super(context); + mBatteryInfoReceiver = new BatteryInfoReceiver(context); + if (lifecycle != null) { + lifecycle.addObserver(this); + } + } + + @Override + public boolean isAvailable() { + return true; + } + + @Override + public String getPreferenceKey() { + return null; + } + + @Override + public void displayPreference(PreferenceScreen screen) { + super.displayPreference(screen); + mBatteryLevel = screen.findPreference(KEY_BATTERY_LEVEL); + mBatteryStatus = screen.findPreference(KEY_BATTERY_STATUS); + } + + @Override + public void onStart() { + mContext.registerReceiver(mBatteryInfoReceiver, BATTERY_INFO_RECEIVER_INTENT_FILTER); + } + + @Override + public void onStop() { + mContext.unregisterReceiver(mBatteryInfoReceiver); + } + + private class BatteryInfoReceiver extends BroadcastReceiver { + + private final Context mContext; + + public BatteryInfoReceiver(Context context) { + mContext = context; + } + + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { + mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent)); + mBatteryStatus.setSummary(Utils.getBatteryStatus(mContext.getResources(), intent)); + } + } + } +} diff --git a/src/com/android/settings/deviceinfo/ImeiInfoPreferenceController.java b/src/com/android/settings/deviceinfo/ImeiInfoPreferenceController.java new file mode 100644 index 00000000000..8db70c74316 --- /dev/null +++ b/src/com/android/settings/deviceinfo/ImeiInfoPreferenceController.java @@ -0,0 +1,45 @@ +/* + * 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.deviceinfo; + +import android.content.Context; +import android.os.UserManager; + +import com.android.settings.Utils; +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.core.AbstractPreferenceController; + +public class ImeiInfoPreferenceController extends AbstractPreferenceController + implements PreferenceControllerMixin { + + private static final String KEY_IMEI_INFO = "imei_info"; + + public ImeiInfoPreferenceController(Context context) { + super(context); + } + + @Override + public boolean isAvailable() { + return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser() + && !Utils.isWifiOnly(mContext); + } + + @Override + public String getPreferenceKey() { + return KEY_IMEI_INFO; + } +} diff --git a/src/com/android/settings/deviceinfo/SimStatusPreferenceController.java b/src/com/android/settings/deviceinfo/SimStatusPreferenceController.java new file mode 100644 index 00000000000..9dfc95b97bc --- /dev/null +++ b/src/com/android/settings/deviceinfo/SimStatusPreferenceController.java @@ -0,0 +1,45 @@ +/* + * 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.deviceinfo; + +import android.content.Context; +import android.os.UserManager; + +import com.android.settings.Utils; +import com.android.settings.core.PreferenceControllerMixin; +import com.android.settingslib.core.AbstractPreferenceController; + +public class SimStatusPreferenceController extends AbstractPreferenceController + implements PreferenceControllerMixin { + + private static final String KEY_SIM_STATUS = "sim_status"; + + public SimStatusPreferenceController(Context context) { + super(context); + } + + @Override + public boolean isAvailable() { + return ((UserManager) mContext.getSystemService(Context.USER_SERVICE)).isAdminUser() + && !Utils.isWifiOnly(mContext); + } + + @Override + public String getPreferenceKey() { + return KEY_SIM_STATUS; + } +} diff --git a/src/com/android/settings/deviceinfo/Status.java b/src/com/android/settings/deviceinfo/Status.java index 080becc8739..ffdaca9891d 100644 --- a/src/com/android/settings/deviceinfo/Status.java +++ b/src/com/android/settings/deviceinfo/Status.java @@ -16,95 +16,26 @@ package com.android.settings.deviceinfo; -import android.content.BroadcastReceiver; import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.os.Bundle; -import android.os.UserManager; import android.provider.SearchIndexableResource; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceScreen; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.R; -import com.android.settings.SettingsPreferenceFragment; -import com.android.settings.Utils; +import com.android.settings.dashboard.DashboardFragment; import com.android.settings.search.BaseSearchIndexProvider; -import com.android.settings.search.Indexable; +import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.lifecycle.Lifecycle; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Fragment for showing device hardware info, such as MAC addresses and serial numbers */ -public class Status extends SettingsPreferenceFragment implements Indexable { +public class Status extends DashboardFragment { - private static final String KEY_BATTERY_STATUS = "battery_status"; - private static final String KEY_BATTERY_LEVEL = "battery_level"; - private static final String KEY_SIM_STATUS = "sim_status"; - private static final String KEY_IMEI_INFO = "imei_info"; - - private SerialNumberPreferenceController mSerialNumberPreferenceController; - private UptimePreferenceController mUptimePreferenceController; - private Preference mBatteryStatus; - private Preference mBatteryLevel; - private BluetoothAddressPreferenceController mBluetoothAddressPreferenceController; - private IpAddressPreferenceController mIpAddressPreferenceController; - private WifiMacAddressPreferenceController mWifiMacAddressPreferenceController; - private ImsStatusPreferenceController mImsStatusPreferenceController; - - private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { - mBatteryLevel.setSummary(Utils.getBatteryPercentage(intent)); - mBatteryStatus.setSummary(Utils.getBatteryStatus(getResources(), intent)); - } - } - }; - - @Override - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - - final Context context = getContext(); - final Lifecycle lifecycle = getLifecycle(); - mSerialNumberPreferenceController = new SerialNumberPreferenceController(context); - mUptimePreferenceController = new UptimePreferenceController(context, lifecycle); - mBluetoothAddressPreferenceController = - new BluetoothAddressPreferenceController(context, lifecycle); - mIpAddressPreferenceController = new IpAddressPreferenceController(context, lifecycle); - mWifiMacAddressPreferenceController = - new WifiMacAddressPreferenceController(context, lifecycle); - mImsStatusPreferenceController = new ImsStatusPreferenceController(context, lifecycle); - - addPreferencesFromResource(R.xml.device_info_status); - mBatteryLevel = findPreference(KEY_BATTERY_LEVEL); - mBatteryStatus = findPreference(KEY_BATTERY_STATUS); - - final PreferenceScreen screen = getPreferenceScreen(); - - mSerialNumberPreferenceController.displayPreference(screen); - mUptimePreferenceController.displayPreference(screen); - mBluetoothAddressPreferenceController.displayPreference(screen); - mIpAddressPreferenceController.displayPreference(screen); - mWifiMacAddressPreferenceController.displayPreference(screen); - mImsStatusPreferenceController.displayPreference(screen); - - // Remove SimStatus and Imei for Secondary user as it access Phone b/19165700 - // Also remove on Wi-Fi only devices. - //TODO: the bug above will surface in split system user mode. - if (!UserManager.get(getContext()).isAdminUser() - || Utils.isWifiOnly(getContext())) { - removePreferenceFromScreen(KEY_SIM_STATUS); - removePreferenceFromScreen(KEY_IMEI_INFO); - } - } + private static final String TAG = "DeviceStatus"; @Override public int getMetricsCategory() { @@ -112,28 +43,33 @@ public class Status extends SettingsPreferenceFragment implements Indexable { } @Override - public void onResume() { - super.onResume(); - getContext().registerReceiver(mBatteryInfoReceiver, - new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + protected String getLogTag() { + return TAG; } @Override - public void onPause() { - super.onPause(); - - getContext().unregisterReceiver(mBatteryInfoReceiver); + protected int getPreferenceScreenResId() { + return R.xml.device_info_status; } - /** - * Removes the specified preference, if it exists. - * @param key the key for the Preference item - */ - private void removePreferenceFromScreen(String key) { - Preference pref = findPreference(key); - if (pref != null) { - getPreferenceScreen().removePreference(pref); - } + @Override + protected List getPreferenceControllers(Context context) { + return buildPreferenceControllers(context, getLifecycle()); + } + + private static List buildPreferenceControllers(Context context, + Lifecycle lifecycle) { + final List controllers = new ArrayList<>(); + controllers.add(new SerialNumberPreferenceController(context)); + controllers.add(new UptimePreferenceController(context, lifecycle)); + controllers.add(new BluetoothAddressPreferenceController(context, lifecycle)); + controllers.add(new IpAddressPreferenceController(context, lifecycle)); + controllers.add(new WifiMacAddressPreferenceController(context, lifecycle)); + controllers.add(new ImsStatusPreferenceController(context, lifecycle)); + controllers.add(new SimStatusPreferenceController(context)); + controllers.add(new ImeiInfoPreferenceController(context)); + controllers.add(new BatteryInfoPreferenceController(context, lifecycle)); + return controllers; } /** @@ -149,5 +85,11 @@ public class Status extends SettingsPreferenceFragment implements Indexable { sir.xmlResId = R.xml.device_info_status; return Arrays.asList(sir); } + + @Override + public List getPreferenceControllers(Context + context) { + return buildPreferenceControllers(context, null /* lifecycle */); + } }; } diff --git a/tests/robotests/src/com/android/settings/deviceinfo/BatteryInfoPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/BatteryInfoPreferenceControllerTest.java new file mode 100644 index 00000000000..362e8136f7a --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/BatteryInfoPreferenceControllerTest.java @@ -0,0 +1,105 @@ +/* + * 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.deviceinfo; + +import static com.android.settings.deviceinfo.BatteryInfoPreferenceController + .BATTERY_INFO_RECEIVER_INTENT_FILTER; +import static com.android.settings.deviceinfo.BatteryInfoPreferenceController.KEY_BATTERY_LEVEL; +import static com.android.settings.deviceinfo.BatteryInfoPreferenceController.KEY_BATTERY_STATUS; +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.os.BatteryManager; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settingslib.core.lifecycle.Lifecycle; + +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; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class BatteryInfoPreferenceControllerTest { + + private Context mContext; + @Mock + private PreferenceScreen mScreen; + + private Preference mBatteryLevel; + private Preference mBatteryStatus; + private Lifecycle mLifecycle; + private BatteryInfoPreferenceController mController; + + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mLifecycle = new Lifecycle(); + mController = new BatteryInfoPreferenceController(mContext, mLifecycle); + mBatteryLevel = new Preference(mContext); + mBatteryStatus = new Preference(mContext); + when(mScreen.findPreference(KEY_BATTERY_STATUS)).thenReturn(mBatteryStatus); + when(mScreen.findPreference(KEY_BATTERY_LEVEL)).thenReturn(mBatteryLevel); + } + + @Test + public void isAlwaysAvailable() { + assertThat(mController.getPreferenceKey()).isNull(); + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void runThroughLifecycle_shouldRegisterUnregisterBatteryInfoReceiver() { + final Context context = mock(Context.class); + mController = new BatteryInfoPreferenceController(context, mLifecycle); + mLifecycle.onStart(); + mLifecycle.onStop(); + + verify(context).registerReceiver(mController.mBatteryInfoReceiver, + BATTERY_INFO_RECEIVER_INTENT_FILTER); + verify(context).unregisterReceiver(mController.mBatteryInfoReceiver); + } + + @Test + public void onReceiveBatteryInfoBroadcast_shouldUpdatePreferences() { + mController.displayPreference(mScreen); + final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED); + intent.putExtra(BatteryManager.EXTRA_LEVEL, 50); + intent.putExtra(BatteryManager.EXTRA_SCALE, 100); + intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); + + mController.mBatteryInfoReceiver.onReceive(mContext, intent); + + assertThat(mBatteryLevel.getSummary()).isEqualTo("50%"); + assertThat(mBatteryStatus.getSummary()) + .isEqualTo(mContext.getText(R.string.battery_info_status_charging)); + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/ImeiInfoPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/ImeiInfoPreferenceControllerTest.java new file mode 100644 index 00000000000..733ef480000 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/ImeiInfoPreferenceControllerTest.java @@ -0,0 +1,81 @@ +/* + * 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.deviceinfo; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.os.UserManager; + +import com.android.settings.TestConfig; +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.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class ImeiInfoPreferenceControllerTest { + @Mock + private Context mContext; + @Mock + private UserManager mUserManager; + @Mock + private ConnectivityManager mConnectivityManager; + + private ImeiInfoPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); + when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn( + mConnectivityManager); + mController = new ImeiInfoPreferenceController(mContext); + } + + @Test + public void testIsAvailable_isAdminAndHasMobile_shouldReturnTrue() { + when(mUserManager.isAdminUser()).thenReturn(true); + when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) + .thenReturn(true); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void testIsAvailable_isAdminButNoMobile_shouldReturnFalse() { + when(mUserManager.isAdminUser()).thenReturn(true); + when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) + .thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void testIsAvailable_isNotAdmin_shouldReturnFalse() { + when(mUserManager.isAdminUser()).thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/SimStatusPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/SimStatusPreferenceControllerTest.java new file mode 100644 index 00000000000..6a3c05b4871 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/SimStatusPreferenceControllerTest.java @@ -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.deviceinfo; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.os.UserManager; + +import com.android.settings.TestConfig; +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.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class SimStatusPreferenceControllerTest { + + @Mock + private Context mContext; + @Mock + private UserManager mUserManager; + @Mock + private ConnectivityManager mConnectivityManager; + + private SimStatusPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); + when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn( + mConnectivityManager); + mController = new SimStatusPreferenceController(mContext); + } + + @Test + public void testIsAvailable_isAdminAndHasMobile_shouldReturnTrue() { + when(mUserManager.isAdminUser()).thenReturn(true); + when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) + .thenReturn(true); + + assertThat(mController.isAvailable()).isTrue(); + } + + @Test + public void testIsAvailable_isAdminButNoMobile_shouldReturnFalse() { + when(mUserManager.isAdminUser()).thenReturn(true); + when(mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) + .thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } + + @Test + public void testIsAvailable_isNotAdmin_shouldReturnFalse() { + when(mUserManager.isAdminUser()).thenReturn(false); + + assertThat(mController.isAvailable()).isFalse(); + } +}