Merge ab/7061308 into stage.

Bug: 180401296
Merged-In: I35fe00aeeb112d5d9971985619e3ec5095dcdb7b
Change-Id: I07d008e38827280c9e9c6b7fa31393309f9de8a3
This commit is contained in:
Xin Li
2021-02-21 09:25:11 -08:00
237 changed files with 32244 additions and 47288 deletions

View File

@@ -153,6 +153,19 @@ public class UsbDetailsFunctionsControllerTest {
assertThat(prefs.get(0).isChecked()).isTrue();
}
@Test
public void displayRefresh_accessoryEnabled_shouldCheckSwitches() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_ACCESSORY, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
assertThat(prefs.get(0).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
assertThat(prefs.get(0).isChecked()).isTrue();
}
@Test
public void onClickMtp_noneEnabled_shouldEnableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
@@ -250,6 +263,30 @@ public class UsbDetailsFunctionsControllerTest {
UsbManager.FUNCTION_MTP);
}
@Test
public void onRadioButtonClicked_functionMtp_inAccessoryMode_doNothing() {
mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
doReturn(UsbManager.FUNCTION_ACCESSORY).when(mUsbBackend).getCurrentFunctions();
mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_ACCESSORY;
mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
assertThat(mDetailsFunctionsController.mPreviousFunction).isEqualTo(
UsbManager.FUNCTION_ACCESSORY);
}
@Test
public void onRadioButtonClicked_functionMtp_inAccessoryCombinationsMode_doNothing() {
final long function = UsbManager.FUNCTION_ACCESSORY | UsbManager.FUNCTION_AUDIO_SOURCE;
mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
doReturn(UsbManager.FUNCTION_ACCESSORY).when(mUsbBackend).getCurrentFunctions();
mDetailsFunctionsController.mPreviousFunction = function;
mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
assertThat(mDetailsFunctionsController.mPreviousFunction).isEqualTo(function);
}
@Test
public void onRadioButtonClicked_clickSameButton_doNothing() {
mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));

View File

@@ -18,18 +18,25 @@ package com.android.settings.display;
import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.testutils.shadow.ShadowUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUtils.class)
public class BatteryPercentagePreferenceControllerTest {
private static final String PREF_KEY = "battery_percentage";
@@ -43,6 +50,11 @@ public class BatteryPercentagePreferenceControllerTest {
mController = new BatteryPercentagePreferenceController(mContext, PREF_KEY);
}
@After
public void tearDown() {
ShadowUtils.reset();
}
private int getPercentageSetting() {
return Settings.System.getInt(mContext.getContentResolver(), SHOW_BATTERY_PERCENT, 0);
}
@@ -60,4 +72,11 @@ public class BatteryPercentagePreferenceControllerTest {
final int isOn = getPercentageSetting();
assertThat(isOn).isEqualTo(0);
}
@Test
public void getAvailabilityStatus_batteryNotPresent_shouldReturnConditionallyUnavailable() {
ShadowUtils.setIsBatteryPresent(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
}
}

View File

@@ -66,6 +66,7 @@ public class BatteryBroadcastReceiverTest {
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
mBatteryBroadcastReceiver.mBatteryLevel = BATTERY_INIT_LEVEL;
mBatteryBroadcastReceiver.mBatteryStatus = BATTERY_INIT_STATUS;
mBatteryBroadcastReceiver.mBatteryHealth = BatteryManager.BATTERY_HEALTH_UNKNOWN;
mBatteryBroadcastReceiver.setBatteryChangedListener(mBatteryListener);
mChargingIntent = new Intent(Intent.ACTION_BATTERY_CHANGED);
@@ -90,6 +91,34 @@ public class BatteryBroadcastReceiverTest {
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
}
@Test
@Config(shadows = {
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
BatteryFixSliceTest.ShadowBatteryTipLoader.class
})
public void testOnReceive_batteryHealthChanged_dataUpdated() {
mChargingIntent
.putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_OVERHEAT);
mBatteryBroadcastReceiver.onReceive(mContext, mChargingIntent);
assertThat(mBatteryBroadcastReceiver.mBatteryHealth)
.isEqualTo(BatteryManager.BATTERY_HEALTH_OVERHEAT);
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_HEALTH);
}
@Test
@Config(shadows = {
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
BatteryFixSliceTest.ShadowBatteryTipLoader.class
})
public void onReceive_batteryNotPresent_shouldShowHelpMessage() {
mChargingIntent.putExtra(BatteryManager.EXTRA_PRESENT, false);
mBatteryBroadcastReceiver.onReceive(mContext, mChargingIntent);
verify(mBatteryListener).onBatteryChanged(BatteryUpdateType.BATTERY_NOT_PRESENT);
}
@Test
@Config(shadows = {
BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
@@ -118,6 +147,8 @@ public class BatteryBroadcastReceiverTest {
assertThat(mBatteryBroadcastReceiver.mBatteryLevel).isEqualTo(batteryLevel);
assertThat(mBatteryBroadcastReceiver.mBatteryStatus).isEqualTo(batteryStatus);
assertThat(mBatteryBroadcastReceiver.mBatteryHealth)
.isEqualTo(BatteryManager.BATTERY_HEALTH_UNKNOWN);
verify(mBatteryListener, never()).onBatteryChanged(anyInt());
}
@@ -136,6 +167,8 @@ public class BatteryBroadcastReceiverTest {
.isEqualTo(Utils.getBatteryPercentage(mChargingIntent));
assertThat(mBatteryBroadcastReceiver.mBatteryStatus)
.isEqualTo(Utils.getBatteryStatus(mContext, mChargingIntent));
assertThat(mBatteryBroadcastReceiver.mBatteryHealth)
.isEqualTo(BatteryManager.BATTERY_HEALTH_UNKNOWN);
// 2 times because register will force update the battery
verify(mBatteryListener, times(2)).onBatteryChanged(BatteryUpdateType.MANUAL);
}

View File

@@ -44,6 +44,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.widget.LayoutPreference;
@@ -61,7 +62,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowPowerManager;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowEntityHeaderController.class)
@Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
public class BatteryHeaderPreferenceControllerTest {
private static final String PREF_KEY = "battery_header";
@@ -116,7 +117,7 @@ public class BatteryHeaderPreferenceControllerTest {
mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
mController = new BatteryHeaderPreferenceController(mContext, PREF_KEY);
mController = spy(new BatteryHeaderPreferenceController(mContext, PREF_KEY));
mLifecycle.addObserver(mController);
mController.setActivity(mActivity);
mController.setFragment(mPreferenceFragment);
@@ -129,6 +130,7 @@ public class BatteryHeaderPreferenceControllerTest {
@After
public void tearDown() {
ShadowEntityHeaderController.reset();
ShadowUtils.reset();
}
@Test
@@ -173,6 +175,15 @@ public class BatteryHeaderPreferenceControllerTest {
assertThat(mSummary.getText()).isEqualTo(BATTERY_STATUS);
}
@Test
public void updatePreference_isOverheat_showEmptyText() {
mBatteryInfo.isOverheated = true;
mController.updateHeaderPreference(mBatteryInfo);
assertThat(mSummary.getText().toString().isEmpty()).isTrue();
}
@Test
public void onStart_shouldStyleActionBar() {
when(mEntityHeaderController.setRecyclerView(nullable(RecyclerView.class), eq(mLifecycle)))
@@ -214,4 +225,13 @@ public class BatteryHeaderPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE);
}
@Test
public void displayPreference_batteryNotPresent_shouldShowHelpMessage() {
ShadowUtils.setIsBatteryPresent(false);
mController.displayPreference(mPreferenceScreen);
verify(mController).showHelpMessage();
}
}

View File

@@ -246,6 +246,22 @@ public class BatteryInfoTest {
assertThat(info.chargeLabel).isEqualTo("100%");
}
@Test
public void testGetBatteryInfo_chargingWithOverheated_updateChargeLabel() {
doReturn(TEST_CHARGE_TIME_REMAINING)
.when(mBatteryStats)
.computeChargeTimeRemaining(anyLong());
mChargingBatteryBroadcast
.putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_OVERHEAT);
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
mBatteryStats, DUMMY_ESTIMATE, SystemClock.elapsedRealtime() * 1000,
false /* shortString */);
assertThat(info.isOverheated).isTrue();
assertThat(info.chargeLabel).isEqualTo("50% - Battery limited temporarily");
}
// Make our battery stats return a sequence of battery events.
private void mockBatteryStatsHistory() {
// Mock out new data every time start...Locked is called.

View File

@@ -144,6 +144,8 @@ public class BatteryUtilsTest {
@Mock
private BatterySipper mIdleBatterySipper;
@Mock
private BatteryInfo mBatteryInfo;
@Mock
private Bundle mBundle;
@Mock
private UserManager mUserManager;
@@ -754,4 +756,36 @@ public class BatteryUtilsTest {
assertThat(estimate.isBasedOnUsage()).isTrue();
assertThat(estimate.getAverageDischargeTime()).isEqualTo(1000);
}
@Test
public void testIsBatteryDefenderOn_isOverheatedAndIsCharging_returnTrue() {
mBatteryInfo.isOverheated = true;
mBatteryInfo.discharging = false;
assertThat(mBatteryUtils.isBatteryDefenderOn(mBatteryInfo)).isTrue();
}
@Test
public void testIsBatteryDefenderOn_isOverheatedAndDischarging_returnFalse() {
mBatteryInfo.isOverheated = true;
mBatteryInfo.discharging = true;
assertThat(mBatteryUtils.isBatteryDefenderOn(mBatteryInfo)).isFalse();
}
@Test
public void testIsBatteryDefenderOn_notOverheatedAndDischarging_returnFalse() {
mBatteryInfo.isOverheated = false;
mBatteryInfo.discharging = true;
assertThat(mBatteryUtils.isBatteryDefenderOn(mBatteryInfo)).isFalse();
}
@Test
public void testIsBatteryDefenderOn_notOverheatedAndIsCharging_returnFalse() {
mBatteryInfo.isOverheated = false;
mBatteryInfo.discharging = false;
assertThat(mBatteryUtils.isBatteryDefenderOn(mBatteryInfo)).isFalse();
}
}

View File

@@ -45,7 +45,6 @@ import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.VisibleForTesting;
import androidx.loader.app.LoaderManager;
import androidx.preference.PreferenceScreen;
@@ -56,6 +55,7 @@ import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.XmlTestUtils;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.instrumentation.VisibilityLoggerMixin;
import com.android.settingslib.widget.LayoutPreference;
@@ -72,6 +72,7 @@ import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
@@ -224,6 +225,7 @@ public class PowerUsageSummaryTest {
}
@Test
@Config(shadows = ShadowUtils.class)
public void nonIndexableKeys_MatchPreferenceKeys() {
final Context context = RuntimeEnvironment.application;
final List<String> niks =

View File

@@ -24,6 +24,8 @@ import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -66,4 +68,12 @@ public class TopLevelBatteryPreferenceControllerTest {
info.chargeLabel = "5% - charging";
assertThat(getDashboardLabel(mContext, info)).isEqualTo("5% - charging");
}
@Test
public void getSummary_batteryNotPresent_shouldShowWarningMessage() {
mController.mIsBatteryPresent = false;
assertThat(mController.getSummary())
.isEqualTo(mContext.getString(R.string.battery_missing_message));
}
}

View File

@@ -50,6 +50,7 @@ import java.util.List;
public class BatteryTipLoaderTest {
private static final int[] TIP_ORDER = {
BatteryTip.TipType.BATTERY_DEFENDER,
BatteryTip.TipType.BATTERY_SAVER,
BatteryTip.TipType.HIGH_DEVICE_USAGE,
BatteryTip.TipType.LOW_BATTERY,

View File

@@ -23,10 +23,12 @@ import static org.mockito.Mockito.when;
import com.android.settings.SettingsActivity;
import com.android.settings.core.InstrumentedPreferenceFragment;
import com.android.settings.fuelgauge.batterytip.actions.BatteryDefenderAction;
import com.android.settings.fuelgauge.batterytip.actions.BatterySaverAction;
import com.android.settings.fuelgauge.batterytip.actions.OpenBatterySaverAction;
import com.android.settings.fuelgauge.batterytip.actions.OpenRestrictAppFragmentAction;
import com.android.settings.fuelgauge.batterytip.actions.RestrictAppAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryDefenderTip;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.EarlyWarningTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
@@ -53,6 +55,7 @@ public class BatteryTipUtilsTest {
private RestrictAppTip mRestrictAppTip;
private EarlyWarningTip mEarlyWarningTip;
private LowBatteryTip mLowBatteryTip;
private BatteryDefenderTip mBatteryDefenderTip;
@Before
public void setUp() {
@@ -67,6 +70,7 @@ public class BatteryTipUtilsTest {
mLowBatteryTip = spy(
new LowBatteryTip(BatteryTip.StateType.NEW, false /* powerSaveModeOn */,
"" /* summary */));
mBatteryDefenderTip = spy(new BatteryDefenderTip(BatteryTip.StateType.NEW));
}
@Test
@@ -116,4 +120,13 @@ public class BatteryTipUtilsTest {
assertThat(BatteryTipUtils.getActionForBatteryTip(mLowBatteryTip, mSettingsActivity,
mFragment)).isInstanceOf(OpenBatterySaverAction.class);
}
@Test
public void
testGetActionForBatteryTip_typeBatteryDefenderStateNew_returnActionBatteryDefender() {
when(mBatteryDefenderTip.getState()).thenReturn(BatteryTip.StateType.NEW);
assertThat(BatteryTipUtils.getActionForBatteryTip(mBatteryDefenderTip, mSettingsActivity,
mFragment)).isInstanceOf(BatteryDefenderAction.class);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2020 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.fuelgauge.batterytip.detectors;
import static com.google.common.truth.Truth.assertThat;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class BatteryDefenderDetectorTest {
@Mock
private BatteryInfo mBatteryInfo;
private BatteryDefenderDetector mBatteryDefenderDetector;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mBatteryInfo.discharging = false;
mBatteryDefenderDetector = new BatteryDefenderDetector(mBatteryInfo);
}
@Test
public void testDetect_notOverheated_tipInvisible() {
mBatteryInfo.isOverheated = false;
assertThat(mBatteryDefenderDetector.detect().isVisible()).isFalse();
}
@Test
public void testDetect_isOverheated_tipNew() {
mBatteryInfo.isOverheated = true;
assertThat(mBatteryDefenderDetector.detect().getState())
.isEqualTo(BatteryTip.StateType.NEW);
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2020 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.fuelgauge.batterytip.tips;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class BatteryDefenderTipTest {
private Context mContext;
private BatteryDefenderTip mBatteryDefenderTip;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mBatteryDefenderTip = new BatteryDefenderTip(BatteryTip.StateType.NEW);
}
@Test
public void getTitle_showTitle() {
assertThat(mBatteryDefenderTip.getTitle(mContext))
.isEqualTo(mContext.getString(R.string.battery_tip_limited_temporarily_title));
}
@Test
public void getSummary_showSummary() {
assertThat(mBatteryDefenderTip.getSummary(mContext))
.isEqualTo(mContext.getString(R.string.battery_tip_limited_temporarily_summary));
}
@Test
public void getIcon_showIcon() {
assertThat(mBatteryDefenderTip.getIconId())
.isEqualTo(R.drawable.ic_battery_status_good_24dp);
}
}

View File

@@ -23,6 +23,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
@@ -54,6 +56,7 @@ import com.android.settingslib.media.MediaOutputSliceConstants;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
@@ -203,31 +206,45 @@ public class MediaOutputIndicatorSliceTest {
}
@Test
public void getMediaOutputSliceIntent_withActiveLocalMedia_verifyIntentExtra() {
public void onNotifyChange_withActiveLocalMedia_verifyIntentExtra() {
when(mMediaController.getSessionToken()).thenReturn(mToken);
when(mMediaController.getPackageName()).thenReturn(TEST_PACKAGE_NAME);
doReturn(mMediaController).when(sMediaOutputIndicatorWorker)
.getActiveLocalMediaController();
final Intent intent = mMediaOutputIndicatorSlice.getMediaOutputSliceIntent();
ArgumentCaptor<Intent> argument = ArgumentCaptor.forClass(Intent.class);
mMediaOutputIndicatorSlice.onNotifyChange(null);
verify(mContext, times(2)).sendBroadcast(argument.capture());
List<Intent> intentList = argument.getAllValues();
Intent intent = intentList.get(0);
assertThat(TextUtils.equals(TEST_PACKAGE_NAME, intent.getStringExtra(
MediaOutputSliceConstants.EXTRA_PACKAGE_NAME))).isTrue();
assertThat(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT).isEqualTo(intent.getAction());
assertThat(TextUtils.equals(Utils.SETTINGS_PACKAGE_NAME, intent.getPackage())).isTrue();
assertThat(MediaOutputSliceConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG).isEqualTo(
intent.getAction());
assertThat(TextUtils.equals(MediaOutputSliceConstants.SYSTEMUI_PACKAGE_NAME,
intent.getPackage())).isTrue();
assertThat(mToken == intent.getExtras().getParcelable(
MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN)).isTrue();
}
@Test
public void getMediaOutputSliceIntent_withoutActiveLocalMedia_verifyIntentExtra() {
public void onNotifyChange_withoutActiveLocalMedia_verifyIntentExtra() {
doReturn(mMediaController).when(sMediaOutputIndicatorWorker)
.getActiveLocalMediaController();
final Intent intent = mMediaOutputIndicatorSlice.getMediaOutputSliceIntent();
ArgumentCaptor<Intent> argument = ArgumentCaptor.forClass(Intent.class);
mMediaOutputIndicatorSlice.onNotifyChange(null);
verify(mContext, times(2)).sendBroadcast(argument.capture());
List<Intent> intentList = argument.getAllValues();
Intent intent = intentList.get(0);
assertThat(TextUtils.isEmpty(intent.getStringExtra(
MediaOutputSliceConstants.EXTRA_PACKAGE_NAME))).isTrue();
assertThat(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT).isEqualTo(intent.getAction());
assertThat(TextUtils.equals(Utils.SETTINGS_PACKAGE_NAME, intent.getPackage())).isTrue();
assertThat(MediaOutputSliceConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG).isEqualTo(
intent.getAction());
assertThat(TextUtils.equals(MediaOutputSliceConstants.SYSTEMUI_PACKAGE_NAME,
intent.getPackage())).isTrue();
assertThat(intent.getExtras().getParcelable(
MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN) == null).isTrue();
}

View File

@@ -261,4 +261,29 @@ public class MediaOutputIndicatorWorkerTest {
assertThat(mMediaOutputIndicatorWorker.getActiveLocalMediaController()).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
final PlaybackState playbackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
when(remoteMediaController.getPlaybackInfo()).thenReturn(playbackInfo);
when(remoteMediaController.getPlaybackState()).thenReturn(playbackState);
assertThat(mMediaOutputIndicatorWorker.getActiveLocalMediaController()).isNull();
}
}

View File

@@ -0,0 +1,180 @@
/*
* Copyright (C) 2020 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.media;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.VolumeProvider;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class MediaOutputUtilsTest {
@Mock
private MediaSessionManager mMediaSessionManager;
@Mock
private MediaController mMediaController;
private Context mContext;
private List<MediaController> mMediaControllers = new ArrayList<>();
private PlaybackState mPlaybackState;
private MediaController.PlaybackInfo mPlaybackInfo;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mMediaSessionManager).when(mContext).getSystemService(MediaSessionManager.class);
mMediaControllers.add(mMediaController);
doReturn(mMediaControllers).when(mMediaSessionManager).getActiveSessions(any());
}
@Test
public void getActiveLocalMediaController_localMediaPlaying_returnController() {
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isEqualTo(
mMediaController);
}
@Test
public void getActiveLocalMediaController_remoteMediaPlaying_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_localMediaStopped_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_STOPPED, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
mMediaControllers.clear();
final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
final PlaybackState playbackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
mMediaControllers.add(mMediaController);
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
when(remoteMediaController.getPlaybackInfo()).thenReturn(playbackInfo);
when(remoteMediaController.getPlaybackState()).thenReturn(playbackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveLocalMediaAndRemoteMedia_returnNull() {
final MediaController.PlaybackInfo playbackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
final PlaybackState playbackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
initPlayback();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
when(remoteMediaController.getPlaybackInfo()).thenReturn(playbackInfo);
when(remoteMediaController.getPlaybackState()).thenReturn(playbackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
private void initPlayback() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.build();
}
}

View File

@@ -37,11 +37,9 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
@@ -51,9 +49,13 @@ import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.graph.SignalDrawable;
import org.junit.After;
import org.junit.Before;
@@ -62,7 +64,6 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@@ -72,11 +73,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowSubscriptionManager.class)
public class SubscriptionsPreferenceControllerTest {
@@ -126,6 +122,7 @@ public class SubscriptionsPreferenceControllerTest {
mController = spy(
new SubscriptionsPreferenceController(mContext, mLifecycle, mUpdateListener,
KEY, 5));
doReturn(true).when(mController).canSubscriptionBeDisplayed(any(), anyInt());
doReturn(mSignalStrengthIcon).when(mController).getIcon(anyInt(), anyInt(), anyBoolean());
}
@@ -459,6 +456,17 @@ public class SubscriptionsPreferenceControllerTest {
eq(true));
}
@Test
public void displayPreference_subscriptionsWithSameGroupUUID_onlyOneWillBeSeen() {
doReturn(false).when(mController).canSubscriptionBeDisplayed(any(), eq(3));
final List<SubscriptionInfo> subs = setupMockSubscriptions(3);
SubscriptionUtil.setActiveSubscriptionsForTesting(subs.subList(0, 3));
mController.onResume();
mController.displayPreference(mScreen);
verify(mPreferenceCategory, times(2)).addPreference(any(Preference.class));
}
@Test
public void onMobileDataEnabledChange_mobileDataTurnedOff_bothSubsHaveCutOut() {

View File

@@ -19,7 +19,6 @@ package com.android.settings.panel;
import static com.android.settings.panel.SettingsPanelActivity.KEY_MEDIA_PACKAGE_NAME;
import static com.android.settings.panel.SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT;
import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
import static com.google.common.truth.Truth.assertThat;
@@ -67,13 +66,4 @@ public class PanelFeatureProviderImplTest {
assertThat(panel).isInstanceOf(VolumePanel.class);
}
@Test
public void getPanel_mediaOutputKey_returnsCorrectPanel() {
mBundle.putString(KEY_PANEL_TYPE_ARGUMENT, ACTION_MEDIA_OUTPUT);
final PanelContent panel = mProvider.getPanel(mContext, mBundle);
assertThat(panel).isInstanceOf(MediaOutputPanel.class);
}
}

View File

@@ -299,9 +299,9 @@ public class MediaOutputPreferenceControllerTest {
mPreference.setKey(TEST_KEY);
mController.handlePreferenceTreeClick(mPreference);
verify(mContext).startActivity(intentCaptor.capture());
verify(mContext).sendBroadcast(intentCaptor.capture());
assertThat(intentCaptor.getValue().getAction())
.isEqualTo(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT);
.isEqualTo(MediaOutputSliceConstants.ACTION_LAUNCH_MEDIA_OUTPUT_DIALOG);
}
/**

View File

@@ -46,6 +46,7 @@ public class ShadowUtils {
private static boolean sIsSystemAlertWindowEnabled;
private static boolean sIsVoiceCapable;
private static ArraySet<String> sResultLinks = new ArraySet<>();
private static boolean sIsBatteryPresent;
@Implementation
protected static int enforceSameOwner(Context context, int userId) {
@@ -67,6 +68,7 @@ public class ShadowUtils {
sIsDemoUser = false;
sIsVoiceCapable = false;
sResultLinks = new ArraySet<>();
sIsBatteryPresent = true;
}
public static void setIsDemoUser(boolean isDemoUser) {
@@ -155,4 +157,13 @@ public class ShadowUtils {
public static void setHandledDomains(ArraySet<String> links) {
sResultLinks = links;
}
@Implementation
protected static boolean isBatteryPresent(Context context) {
return sIsBatteryPresent;
}
public static void setIsBatteryPresent(boolean isBatteryPresent) {
sIsBatteryPresent = isBatteryPresent;
}
}

View File

@@ -18,7 +18,9 @@ package com.android.settings.wifi;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
@@ -32,6 +34,7 @@ import android.net.wifi.WifiEnterpriseConfig.Eap;
import android.net.wifi.WifiEnterpriseConfig.Phase2;
import android.net.wifi.WifiManager;
import android.os.ServiceSpecificException;
import android.security.Credentials;
import android.security.KeyStore;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -78,6 +81,10 @@ public class WifiConfigController2Test {
private KeyStore mKeyStore;
private View mView;
private Spinner mHiddenSettingsSpinner;
private Spinner mEapCaCertSpinner;
private Spinner mEapUserCertSpinner;
private String mUseSystemCertsString;
private String mDoNotProvideEapUserCertString;
private ShadowSubscriptionManager mShadowSubscriptionManager;
public WifiConfigController2 mController;
@@ -98,6 +105,9 @@ public class WifiConfigController2Test {
private static final String NUMBER_AND_CHARACTER_KEY = "123456abcd";
private static final String PARTIAL_NUMBER_AND_CHARACTER_KEY = "123456abc?";
private static final int DHCP = 0;
// Saved certificates
private static final String SAVED_CA_CERT = "saved CA cert";
private static final String SAVED_USER_CERT = "saved user cert";
@Before
public void setUp() {
@@ -108,6 +118,11 @@ public class WifiConfigController2Test {
mView = LayoutInflater.from(mContext).inflate(R.layout.wifi_dialog, null);
final Spinner ipSettingsSpinner = mView.findViewById(R.id.ip_settings);
mHiddenSettingsSpinner = mView.findViewById(R.id.hidden_settings);
mEapCaCertSpinner = mView.findViewById(R.id.ca_cert);
mEapUserCertSpinner = mView.findViewById(R.id.user_cert);
mUseSystemCertsString = mContext.getString(R.string.wifi_use_system_certs);
mDoNotProvideEapUserCertString =
mContext.getString(R.string.wifi_do_not_provide_eap_user_cert);
ipSettingsSpinner.setSelection(DHCP);
mShadowSubscriptionManager = shadowOf(mContext.getSystemService(SubscriptionManager.class));
@@ -835,4 +850,75 @@ public class WifiConfigController2Test {
final WifiConfiguration wifiConfiguration = mController.getConfig();
assertThat(wifiConfiguration.carrierId).isEqualTo(carrierId);
}
@Test
public void loadCaCertificateValue_shouldPersistentAsDefault() {
setUpModifyingSavedCertificateConfigController(null, null);
assertThat(mEapCaCertSpinner.getSelectedItem()).isEqualTo(mUseSystemCertsString);
}
@Test
public void loadSavedCaCertificateValue_shouldBeCorrectValue() {
setUpModifyingSavedCertificateConfigController(SAVED_CA_CERT, null);
assertThat(mEapCaCertSpinner.getSelectedItem()).isEqualTo(SAVED_CA_CERT);
}
@Test
public void loadUserCertificateValue_shouldPersistentAsDefault() {
setUpModifyingSavedCertificateConfigController(null, null);
assertThat(mEapUserCertSpinner.getSelectedItem()).isEqualTo(mDoNotProvideEapUserCertString);
}
@Test
public void loadSavedUserCertificateValue_shouldBeCorrectValue() {
setUpModifyingSavedCertificateConfigController(null, SAVED_USER_CERT);
assertThat(mEapUserCertSpinner.getSelectedItem()).isEqualTo(SAVED_USER_CERT);
}
private void setUpModifyingSavedCertificateConfigController(String savedCaCertificate,
String savedUserCertificate) {
final WifiConfiguration mockWifiConfig = mock(WifiConfiguration.class);
final WifiEnterpriseConfig mockWifiEnterpriseConfig = mock(WifiEnterpriseConfig.class);
mockWifiConfig.enterpriseConfig = mockWifiEnterpriseConfig;
when(mWifiEntry.isSaved()).thenReturn(true);
when(mWifiEntry.getSecurity()).thenReturn(WifiEntry.SECURITY_EAP);
when(mWifiEntry.getWifiConfiguration()).thenReturn(mockWifiConfig);
when(mockWifiConfig.getIpConfiguration()).thenReturn(mock(IpConfiguration.class));
when(mockWifiEnterpriseConfig.getEapMethod()).thenReturn(Eap.TLS);
if (savedCaCertificate != null) {
String[] savedCaCertificates = new String[]{savedCaCertificate};
when(mockWifiEnterpriseConfig.getCaCertificateAliases())
.thenReturn(savedCaCertificates);
when(mKeyStore.list(eq(Credentials.CA_CERTIFICATE), anyInt()))
.thenReturn(savedCaCertificates);
}
if (savedUserCertificate != null) {
String[] savedUserCertificates = new String[]{savedUserCertificate};
when(mockWifiEnterpriseConfig.getClientCertificateAlias())
.thenReturn(savedUserCertificate);
when(mKeyStore.list(eq(Credentials.USER_PRIVATE_KEY), anyInt()))
.thenReturn(savedUserCertificates);
}
mController = new TestWifiConfigController2(mConfigUiBase, mView, mWifiEntry,
WifiConfigUiBase2.MODE_MODIFY);
// Because Robolectric has a different behavior from normal flow.
//
// Normal flow:
// showSecurityFields start -> mEapMethodSpinner.setSelection
// -> showSecurityFields end -> mController.onItemSelected
//
// Robolectric flow:
// showSecurityFields start -> mEapMethodSpinner.setSelection
// -> mController.onItemSelected -> showSecurityFields end
//
// We need to add a redundant mEapMethodSpinner.setSelection here to verify whether the
// certificates are covered by mController.onItemSelected after showSecurityFields end.
mController.mEapMethodSpinner.setSelection(Eap.TLS);
}
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (C) 2020 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.details2;
import static com.android.settings.wifi.WifiSettings.WIFI_DIALOG_ID;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.settings.SettingsEnums;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.AbstractPreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
@RunWith(RobolectricTestRunner.class)
public class WifiNetworkDetailsFragment2Test {
final String TEST_PREFERENCE_KEY = "TEST_PREFERENCE_KEY";
private WifiNetworkDetailsFragment2 mFragment;
@Before
public void setUp() {
mFragment = new WifiNetworkDetailsFragment2();
}
@Test
public void getMetricsCategory_shouldReturnWifiNetworkDetails() {
assertThat(mFragment.getMetricsCategory()).isEqualTo(SettingsEnums.WIFI_NETWORK_DETAILS);
}
@Test
public void getDialogMetricsCategory_withCorrectId_shouldReturnDialogWifiApEdit() {
assertThat(mFragment.getDialogMetricsCategory(WIFI_DIALOG_ID)).isEqualTo(
SettingsEnums.DIALOG_WIFI_AP_EDIT);
}
@Test
public void getDialogMetricsCategory_withWrongId_shouldReturnZero() {
assertThat(mFragment.getDialogMetricsCategory(-1 /* dialogId */)).isEqualTo(0);
}
@Test
public void onCreateOptionsMenu_shouldSetCorrectIcon() {
final Menu menu = mock(Menu.class);
final MenuItem menuItem = mock(MenuItem.class);
doReturn(menuItem).when(menu).add(anyInt(), eq(Menu.FIRST), anyInt(), anyInt());
mFragment.onCreateOptionsMenu(menu, mock(MenuInflater.class));
verify(menuItem).setIcon(com.android.internal.R.drawable.ic_mode_edit);
}
@Test
public void refreshPreferences_controllerShouldUpdateStateAndDisplayPreference() {
final FakeFragment fragment = spy(new FakeFragment());
final PreferenceScreen screen = mock(PreferenceScreen.class);
final Preference preference = mock(Preference.class);
final TestController controller = mock(TestController.class);
doReturn(screen).when(fragment).getPreferenceScreen();
doReturn(preference).when(screen).findPreference(TEST_PREFERENCE_KEY);
doReturn(TEST_PREFERENCE_KEY).when(controller).getPreferenceKey();
fragment.mControllers = new ArrayList<>();
fragment.mControllers.add(controller);
fragment.addPreferenceController(controller);
fragment.refreshPreferences();
verify(controller).updateState(preference);
verify(controller).displayPreference(screen);
}
// Fake WifiNetworkDetailsFragment2 to override the protected method as public.
public class FakeFragment extends WifiNetworkDetailsFragment2 {
@Override
public void addPreferenceController(AbstractPreferenceController controller) {
super.addPreferenceController(controller);
}
}
public class TestController extends BasePreferenceController {
public TestController() {
super(RuntimeEnvironment.application, TEST_PREFERENCE_KEY);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}
}

View File

@@ -89,7 +89,15 @@ public class WifiPrivacyPreferenceController2Test {
}
@Test
public void testUpdateState_canSetPrivacy_shouldBeSelectable() {
public void testUpdateState_canSetPrivacyInNextUpdate_shouldBeSelectable() {
// Return false in WifiEntry#canSetPrivacy to make preference un-selectable first.
when(mMockWifiEntry.canSetPrivacy()).thenReturn(false);
mPreferenceController.updateState(mDropDownPreference);
assertThat(mDropDownPreference.isSelectable()).isFalse();
// Return true in WifiEntry#canSetPrivacy to verify preference back to selectable.
when(mMockWifiEntry.canSetPrivacy()).thenReturn(true);
mPreferenceController.updateState(mDropDownPreference);
@@ -98,7 +106,15 @@ public class WifiPrivacyPreferenceController2Test {
}
@Test
public void testUpdateState_canNotSetPrivacy_shouldNotSelectable() {
public void testUpdateState_canNotSetPrivacyInNextUpdate_shouldNotBeSelectable() {
// Return true in WifiEntry#canSetPrivacy to make preference selectable first.
when(mMockWifiEntry.canSetPrivacy()).thenReturn(true);
mPreferenceController.updateState(mDropDownPreference);
assertThat(mDropDownPreference.isSelectable()).isTrue();
// Return false in WifiEntry#canSetPrivacy to verify preference back to un-selectable.
when(mMockWifiEntry.canSetPrivacy()).thenReturn(false);
mPreferenceController.updateState(mDropDownPreference);