Convert more pages to use ActionButtonPreference

- text to speech page
- blue tooth detail page
- installed app page

Change-Id: Ib743a144cc6025055cd496ec077f13fce4db0c69
Bug: 63991885
Test: robotests
This commit is contained in:
Fan Zhang
2017-07-28 17:18:44 -07:00
parent 3eb22656eb
commit 1105a1a465
11 changed files with 248 additions and 296 deletions

View File

@@ -1,48 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2016 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.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="4dp"
android:paddingStart="68dp"
android:paddingEnd="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/tts_play_button"
style="@style/android:Widget.Material.Button.Colored"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingEnd="8dp"
android:text="@string/tts_play"/>
<Button
android:id="@+id/tts_reset_button"
style="@style/android:Widget.Material.Button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="8dp"
android:text="@string/tts_reset"/>
</LinearLayout>

View File

@@ -19,8 +19,8 @@
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="4dp"
android:gravity="center"
android:paddingTop="8dp"
android:paddingStart="68dp"
android:paddingEnd="8dp"
android:orientation="horizontal">
@@ -34,15 +34,13 @@
style="@style/AppActionPrimaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingEnd="8dp" />
android:layout_marginBottom="4dp" />
<Button
android:id="@+id/button1_negative"
style="@style/ActionSecondaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="8dp" />
android:layout_marginBottom="4dp" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
@@ -53,14 +51,12 @@
style="@style/AppActionPrimaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="8dp" />
android:layout_marginBottom="4dp" />
<Button
android:id="@+id/button2_negative"
style="@style/ActionSecondaryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingStart="8dp" />
android:layout_marginBottom="4dp" />
</FrameLayout>
</LinearLayout>

View File

@@ -23,10 +23,8 @@
android:layout="@layout/settings_entity_header"
android:selectable="false"/>
<com.android.settings.applications.LayoutPreference
android:key="action_buttons"
android:layout="@layout/app_action_buttons"
android:selectable="false"/>
<com.android.settings.widget.ActionButtonPreference
android:key="action_buttons" />
<PreferenceCategory
android:key="bluetooth_profiles"/>

View File

@@ -31,10 +31,8 @@
android:selectable="false"
android:order="-9999"/>
<com.android.settings.applications.LayoutPreference
<com.android.settings.widget.ActionButtonPreference
android:key="action_buttons"
android:layout="@layout/app_action_buttons"
android:selectable="false"
android:order="-9998" />
<Preference

View File

@@ -51,10 +51,8 @@
android:defaultValue="100"
android:max="400"/>
<com.android.settings.applications.LayoutPreference
android:key="action_buttons"
android:layout="@layout/tts_action_buttons"
android:selectable="false"/>
<com.android.settings.widget.ActionButtonPreference
android:key="action_buttons" />
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -67,7 +67,6 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.IWebViewUpdateService;
import android.widget.Button;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.os.BatterySipper;
@@ -94,6 +93,7 @@ import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.notification.AppNotificationSettings;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.NotificationBackend.AppRow;
import com.android.settings.widget.ActionButtonPreference;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.AppItem;
import com.android.settingslib.RestrictedLockUtils;
@@ -123,8 +123,7 @@ import java.util.Set;
* uninstall the application.
*/
public class InstalledAppDetails extends AppInfoBase
implements View.OnClickListener, OnPreferenceClickListener,
LoaderManager.LoaderCallbacks<AppStorageStats> {
implements OnPreferenceClickListener, LoaderManager.LoaderCallbacks<AppStorageStats> {
private static final String LOG_TAG = "InstalledAppDetails";
@@ -166,10 +165,7 @@ public class InstalledAppDetails extends AppInfoBase
private boolean mInitialized;
private boolean mShowUninstalled;
private LayoutPreference mHeader;
private LayoutPreference mActionButtons;
private Button mUninstallButton;
private boolean mUpdatedSysApp = false;
private Button mForceStopButton;
private Preference mNotificationPreference;
private Preference mStoragePreference;
private Preference mPermissionsPreference;
@@ -187,6 +183,8 @@ public class InstalledAppDetails extends AppInfoBase
private ChartData mChartData;
private INetworkStatsSession mStatsSession;
@VisibleForTesting
ActionButtonPreference mActionButtons;
@VisibleForTesting
Preference mBatteryPreference;
@VisibleForTesting
@@ -231,7 +229,7 @@ public class InstalledAppDetails extends AppInfoBase
};
@VisibleForTesting
boolean handleDisableable(Button button) {
boolean handleDisableable() {
boolean disableable = false;
// Try to prevent the user from bricking their phone
// by not allowing disabling of apps signed with the
@@ -239,13 +237,19 @@ public class InstalledAppDetails extends AppInfoBase
if (mHomePackages.contains(mAppEntry.info.packageName)
|| Utils.isSystemPackage(getContext().getResources(), mPm, mPackageInfo)) {
// Disable button for core system applications.
button.setText(R.string.disable_text);
mActionButtons
.setButton1Text(R.string.disable_text)
.setButton1Positive(false);
} else if (mAppEntry.info.enabled && !isDisabledUntilUsed()) {
button.setText(R.string.disable_text);
mActionButtons
.setButton1Text(R.string.disable_text)
.setButton1Positive(false);
disableable = !mApplicationFeatureProvider.getKeepEnabledPackages()
.contains(mAppEntry.info.packageName);
} else {
button.setText(R.string.enable_text);
mActionButtons
.setButton1Text(R.string.enable_text)
.setButton1Positive(true);
disableable = true;
}
@@ -259,11 +263,11 @@ public class InstalledAppDetails extends AppInfoBase
private void initUninstallButtons() {
final boolean isBundled = (mAppEntry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
boolean enabled = true;
boolean enabled;
if (isBundled) {
enabled = handleDisableable(mUninstallButton);
enabled = handleDisableable();
} else {
enabled = initUnintsallButtonForUserApp();
enabled = initUninstallButtonForUserApp();
}
// If this is a device admin, it can't be uninstalled or disabled.
// We do this here so the text of the button is still set correctly.
@@ -326,15 +330,15 @@ public class InstalledAppDetails extends AppInfoBase
throw new RuntimeException(e);
}
mUninstallButton.setEnabled(enabled);
mActionButtons.setButton1Enabled(enabled);
if (enabled) {
// Register listener
mUninstallButton.setOnClickListener(this);
mActionButtons.setButton1OnClickListener(v -> handleUninstallButtonClick());
}
}
@VisibleForTesting
boolean initUnintsallButtonForUserApp() {
boolean initUninstallButtonForUserApp() {
boolean enabled = true;
if ((mPackageInfo.applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED) == 0
&& mUserManager.getUsers().size() >= 2) {
@@ -343,9 +347,9 @@ public class InstalledAppDetails extends AppInfoBase
enabled = false;
} else if (AppUtils.isInstant(mPackageInfo.applicationInfo)) {
enabled = false;
mUninstallButton.setVisibility(View.GONE);
mActionButtons.setButton1Visible(false);
}
mUninstallButton.setText(R.string.uninstall_text);
mActionButtons.setButton1Text(R.string.uninstall_text);
return enabled;
}
@@ -447,7 +451,10 @@ public class InstalledAppDetails extends AppInfoBase
}
final Activity activity = getActivity();
mHeader = (LayoutPreference) findPreference(KEY_HEADER);
mActionButtons = (LayoutPreference) findPreference(KEY_ACTION_BUTTONS);
mActionButtons = ((ActionButtonPreference) findPreference(KEY_ACTION_BUTTONS))
.setButton2Text(R.string.force_stop)
.setButton2Positive(false)
.setButton2Enabled(false);
EntityHeaderController.newInstance(activity, this, mHeader.findViewById(R.id.entity_header))
.setRecyclerView(getListView(), getLifecycle())
.setPackageName(mPackageName)
@@ -456,7 +463,6 @@ public class InstalledAppDetails extends AppInfoBase
EntityHeaderController.ActionType.ACTION_NONE)
.styleActionBar(activity)
.bindHeaderButtons();
prepareUninstallAndStop();
mNotificationPreference = findPreference(KEY_NOTIFICATION);
mNotificationPreference.setOnPreferenceClickListener(this);
@@ -516,13 +522,6 @@ public class InstalledAppDetails extends AppInfoBase
return true;
}
private void prepareUninstallAndStop() {
mForceStopButton = (Button) mActionButtons.findViewById(R.id.right_button);
mForceStopButton.setText(R.string.force_stop);
mUninstallButton = (Button) mActionButtons.findViewById(R.id.left_button);
mForceStopButton.setEnabled(false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, UNINSTALL_UPDATES, 0, R.string.app_factory_reset)
@@ -905,12 +904,10 @@ public class InstalledAppDetails extends AppInfoBase
}
private void updateForceStopButton(boolean enabled) {
if (mAppsControlDisallowedBySystem) {
mForceStopButton.setEnabled(false);
} else {
mForceStopButton.setEnabled(enabled);
mForceStopButton.setOnClickListener(this);
}
mActionButtons
.setButton2Enabled(mAppsControlDisallowedBySystem ? false : enabled)
.setButton2OnClickListener(mAppsControlDisallowedBySystem
? null : v -> handleForceStopButtonClick());
}
@VisibleForTesting
@@ -921,7 +918,7 @@ public class InstalledAppDetails extends AppInfoBase
updateForceStopButton(false);
} else if (AppUtils.isInstant(mPackageInfo.applicationInfo)) {
updateForceStopButton(false);
mForceStopButton.setVisibility(View.GONE);
mActionButtons.setButton2Visible(false);
} else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_STOPPED) == 0) {
// If the app isn't explicitly stopped, then always show the
// force stop button.
@@ -968,17 +965,12 @@ public class InstalledAppDetails extends AppInfoBase
SUB_INFO_FRAGMENT);
}
/*
* Method implementing functionality of buttons clicked
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v) {
private void handleUninstallButtonClick() {
if (mAppEntry == null) {
setIntentAndFinish(true, true);
return;
}
String packageName = mAppEntry.info.packageName;
if (v == mUninstallButton) {
final String packageName = mAppEntry.info.packageName;
if (mDpm.packageHasActiveAdmins(mPackageInfo.packageName)) {
stopListeningToPackageRemove();
Activity activity = getActivity();
@@ -1021,7 +1013,13 @@ public class InstalledAppDetails extends AppInfoBase
} else {
uninstallPkg(packageName, false, false);
}
} else if (v == mForceStopButton) {
}
private void handleForceStopButtonClick() {
if (mAppEntry == null) {
setIntentAndFinish(true, true);
return;
}
if (mAppsControlDisallowedAdmin != null && !mAppsControlDisallowedBySystem) {
RestrictedLockUtils.sendShowAdminSupportDetailsIntent(
getActivity(), mAppsControlDisallowedAdmin);
@@ -1030,7 +1028,6 @@ public class InstalledAppDetails extends AppInfoBase
//forceStopPackage(mAppInfo.packageName);
}
}
}
/** Returns whether there is only one user on this device, not including the system-only user */
private boolean isSingleUser() {

View File

@@ -19,11 +19,9 @@ package com.android.settings.bluetooth;
import android.content.Context;
import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.widget.ActionButtonPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -35,7 +33,8 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
private static final String KEY_ACTION_BUTTONS = "action_buttons";
private boolean mIsConnected;
private LayoutPreference mActionButtons;
private boolean mButton1Initialized;
private ActionButtonPreference mActionButtons;
public BluetoothDetailsButtonsController(Context context, PreferenceFragment fragment,
CachedBluetoothDevice device, Lifecycle lifecycle) {
@@ -51,35 +50,35 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
@Override
protected void init(PreferenceScreen screen) {
mActionButtons = (LayoutPreference) screen.findPreference(getPreferenceKey());
Button rightButton = (Button) mActionButtons.findViewById(R.id.right_button);
rightButton.setText(R.string.forget);
rightButton.setOnClickListener((view) -> {
onForgetButtonPressed();
});
mActionButtons = ((ActionButtonPreference) screen.findPreference(getPreferenceKey()))
.setButton2Text(R.string.forget)
.setButton2OnClickListener((view) -> onForgetButtonPressed())
.setButton2Positive(false)
.setButton2Enabled(true);
}
@Override
protected void refresh() {
Button leftButton = (Button) mActionButtons.findViewById(R.id.left_button);
leftButton.setEnabled(!mCachedDevice.isBusy());
boolean notInitialized = TextUtils.isEmpty(leftButton.getText());
mActionButtons.setButton1Enabled(!mCachedDevice.isBusy());
boolean previouslyConnected = mIsConnected;
mIsConnected = mCachedDevice.isConnected();
if (mIsConnected) {
if (notInitialized || !previouslyConnected) {
leftButton.setText(R.string.bluetooth_device_context_disconnect);
leftButton.setOnClickListener((view) -> {
mCachedDevice.disconnect();
});
if (!mButton1Initialized || !previouslyConnected) {
mActionButtons
.setButton1Text(R.string.bluetooth_device_context_disconnect)
.setButton1OnClickListener(view -> mCachedDevice.disconnect())
.setButton1Positive(false);
mButton1Initialized = true;
}
} else {
if (notInitialized || previouslyConnected) {
leftButton.setText(R.string.bluetooth_device_context_connect);
leftButton.setOnClickListener((view) -> {
mCachedDevice.connect(true);
});
if (!mButton1Initialized || previouslyConnected) {
mActionButtons
.setButton1Text(R.string.bluetooth_device_context_connect)
.setButton1OnClickListener(
view -> mCachedDevice.connect(true /* connectAllProfiles */))
.setButton1Positive(true);
mButton1Initialized = true;
}
}
}
@@ -88,4 +87,5 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
public String getPreferenceKey() {
return KEY_ACTION_BUTTONS;
}
}

View File

@@ -16,7 +16,10 @@
package com.android.settings.tts;
import android.support.v7.preference.ListPreference;
import static android.provider.Settings.Secure.TTS_DEFAULT_PITCH;
import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
@@ -26,24 +29,23 @@ import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.EngineInfo;
import android.speech.tts.TtsEngines;
import android.speech.tts.UtteranceProgressListener;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import java.util.Comparator;
import java.util.Collections;
import com.android.settings.applications.LayoutPreference;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.widget.SeekBarPreference;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.ActionButtonPreference;
import com.android.settings.widget.GearPreference;
import android.widget.Button;
import com.android.settings.widget.SeekBarPreference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -51,13 +53,8 @@ import java.util.MissingResourceException;
import java.util.Objects;
import java.util.Set;
import static android.provider.Settings.Secure.TTS_DEFAULT_PITCH;
import static android.provider.Settings.Secure.TTS_DEFAULT_RATE;
import static android.provider.Settings.Secure.TTS_DEFAULT_SYNTH;
public class TextToSpeechSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener,
View.OnClickListener,
GearPreference.OnGearClickListener {
private static final String STATE_KEY_LOCALE_ENTRIES = "locale_entries";
@@ -108,9 +105,7 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
private SeekBarPreference mDefaultPitchPref;
private SeekBarPreference mDefaultRatePref;
private Button mResetButton;
private Button mPlayButton;
private LayoutPreference mActionButtons;
private ActionButtonPreference mActionButtons;
private int mDefaultPitch = TextToSpeech.Engine.DEFAULT_PITCH;
private int mDefaultRate = TextToSpeech.Engine.DEFAULT_RATE;
@@ -126,7 +121,6 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
private String mSampleText = null;
private ListPreference mLocalePreference;
private Preference mInstallVoicesPreference;
/**
* Default locale used by selected TTS engine, null if not connected to any engine.
@@ -172,12 +166,15 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
mDefaultPitchPref = (SeekBarPreference) findPreference(KEY_DEFAULT_PITCH);
mDefaultRatePref = (SeekBarPreference) findPreference(KEY_DEFAULT_RATE);
mActionButtons = (LayoutPreference) findPreference(KEY_ACTION_BUTTONS);
mPlayButton = (Button) mActionButtons.findViewById(R.id.tts_play_button);
mPlayButton.setOnClickListener(this);
mPlayButton.setEnabled(false);
mResetButton = (Button) mActionButtons.findViewById(R.id.tts_reset_button);
mResetButton.setOnClickListener(this);
mActionButtons = ((ActionButtonPreference) findPreference(KEY_ACTION_BUTTONS))
.setButton1Text(R.string.tts_play)
.setButton1Positive(true)
.setButton1OnClickListener(v -> speakSampleText())
.setButton1Enabled(false)
.setButton2Text(R.string.tts_reset)
.setButton2Positive(false)
.setButton2OnClickListener(v -> resetTts())
.setButton1Enabled(true);
if (savedInstanceState == null) {
mLocalePreference.setEnabled(false);
@@ -656,17 +653,7 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
mTts.setLanguage((locale != null) ? locale : Locale.getDefault());
}
/** Called when mPlayButton, mResetButton is clicked. */
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tts_play_button:
// Play button.
// Get the sample text from the TTS engine; onActivityResult will do
// the actual speaking
speakSampleText();
break;
case R.id.tts_reset_button:
private void resetTts() {
// Reset button.
int speechRateSeekbarProgress =
getSeekBarProgressFromValue(
@@ -678,10 +665,6 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
KEY_DEFAULT_PITCH, TextToSpeech.Engine.DEFAULT_PITCH);
mDefaultPitchPref.setProgress(pitchSeekbarProgress);
updateSpeechPitchValue(pitchSeekbarProgress);
break;
default:
break;
}
}
private void updateSpeechRate(int speechRateSeekBarProgress) {
@@ -715,7 +698,7 @@ public class TextToSpeechSettings extends SettingsPreferenceFragment
}
private void updateWidgetState(boolean enable) {
mPlayButton.setEnabled(enable);
mActionButtons.setButton1Enabled(enable);
mDefaultRatePref.setEnabled(enable);
mDefaultPitchPref.setEnabled(enable);
}

View File

@@ -17,6 +17,18 @@
package com.android.settings.applications;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
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.app.AlertDialog;
import android.app.AppOpsManager;
import android.app.Fragment;
@@ -35,18 +47,18 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import android.view.View;
import android.widget.Button;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.instantapps.InstantAppButtonsController;
import com.android.settings.applications.instantapps.InstantAppButtonsController.ShowDialogDelegate;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.ActionButtonPreferenceTest;
import com.android.settingslib.Utils;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.applications.ApplicationsState.AppEntry;
@@ -69,18 +81,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
@@ -133,6 +133,7 @@ public final class InstalledAppDetailsTest {
mBatteryPreference = new Preference(mShadowContext);
mAppDetail.mBatteryPreference = mBatteryPreference;
mAppDetail.mActionButtons = ActionButtonPreferenceTest.createMock();
mBatterySipper.drainType = BatterySipper.DrainType.IDLE;
mBatterySipper.uidObj = mUid;
@@ -281,15 +282,13 @@ public final class InstalledAppDetailsTest {
appEntry.info = info;
final PackageInfo packageInfo = mock(PackageInfo.class);
packageInfo.applicationInfo = info;
final Button uninstallButton = mock(Button.class);
ReflectionHelpers.setField(mAppDetail, "mUserManager", mUserManager);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(mAppDetail, "mUninstallButton", uninstallButton);
mAppDetail.initUnintsallButtonForUserApp();
verify(uninstallButton).setVisibility(View.GONE);
mAppDetail.initUninstallButtonForUserApp();
verify(mAppDetail.mActionButtons).setButton1Visible(false);
}
// Tests that we don't show the force stop button for instant apps (they aren't allowed to run
@@ -303,15 +302,13 @@ public final class InstalledAppDetailsTest {
final AppEntry appEntry = mock(AppEntry.class);
final ApplicationInfo info = new ApplicationInfo();
appEntry.info = info;
final Button forceStopButton = mock(Button.class);
ReflectionHelpers.setField(mAppDetail, "mDpm", mDevicePolicyManager);
ReflectionHelpers.setField(mAppDetail, "mPackageInfo", packageInfo);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
ReflectionHelpers.setField(mAppDetail, "mForceStopButton", forceStopButton);
mAppDetail.checkForceStop();
verify(forceStopButton).setVisibility(View.GONE);
verify(mAppDetail.mActionButtons).setButton2Visible(false);
}
@Test
@@ -484,10 +481,9 @@ public final class InstalledAppDetailsTest {
ReflectionHelpers.setField(mAppDetail, "mHomePackages", homePackages);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
final Button button = mock(Button.class);
assertThat(mAppDetail.handleDisableable(button)).isFalse();
verify(button).setText(R.string.disable_text);
assertThat(mAppDetail.handleDisableable()).isFalse();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test
@@ -506,10 +502,31 @@ public final class InstalledAppDetailsTest {
ReflectionHelpers.setField(mAppDetail, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
final Button button = mock(Button.class);
assertThat(mAppDetail.handleDisableable(button)).isTrue();
verify(button).setText(R.string.disable_text);
assertThat(mAppDetail.handleDisableable()).isTrue();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test
@Config(shadows = ShadowUtils.class)
public void handleDisableable_appIsDisabled_buttonShouldShowEnable() {
final ApplicationInfo info = new ApplicationInfo();
info.packageName = "pkg";
info.enabled = false;
info.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
final AppEntry appEntry = mock(AppEntry.class);
appEntry.info = info;
when(mFeatureFactory.applicationFeatureProvider.getKeepEnabledPackages()).thenReturn(
new HashSet<>());
ReflectionHelpers.setField(mAppDetail, "mApplicationFeatureProvider",
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
assertThat(mAppDetail.handleDisableable()).isTrue();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.enable_text);
verify(mAppDetail.mActionButtons).setButton1Positive(true);
}
@Test
@@ -532,10 +549,8 @@ public final class InstalledAppDetailsTest {
mFeatureFactory.applicationFeatureProvider);
ReflectionHelpers.setField(mAppDetail, "mAppEntry", appEntry);
final Button button = mock(Button.class);
assertThat(mAppDetail.handleDisableable(button)).isFalse();
verify(button).setText(R.string.disable_text);
assertThat(mAppDetail.handleDisableable()).isFalse();
verify(mAppDetail.mActionButtons).setButton1Text(R.string.disable_text);
}
@Test

View File

@@ -48,7 +48,7 @@ public class LayoutPreferenceTest {
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mPreference = new LayoutPreference(mContext, R.layout.app_action_buttons);
mPreference = new LayoutPreference(mContext, R.layout.two_action_buttons);
mRootView = mPreference.mRootView;
mHolder = PreferenceViewHolder.createInstanceForTests(LayoutInflater.from(mContext)
.inflate(R.layout.layout_preference_frame, null, false));
@@ -79,12 +79,12 @@ public class LayoutPreferenceTest {
@Test
public void disableSomeView_shouldMaintainStateAfterBind() {
mPreference.findViewById(R.id.left_button).setEnabled(false);
mPreference.findViewById(R.id.right_button).setEnabled(true);
mPreference.findViewById(R.id.button1_positive).setEnabled(false);
mPreference.findViewById(R.id.button2_positive).setEnabled(true);
mPreference.onBindViewHolder(mHolder);
assertThat(mPreference.findViewById(R.id.left_button).isEnabled()).isFalse();
assertThat(mPreference.findViewById(R.id.right_button).isEnabled()).isTrue();
assertThat(mPreference.findViewById(R.id.button1_positive).isEnabled()).isFalse();
assertThat(mPreference.findViewById(R.id.button2_positive).isEnabled()).isTrue();
}
}

View File

@@ -17,7 +17,7 @@
package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@@ -26,17 +26,20 @@ import static org.mockito.Mockito.when;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice;
import com.android.settings.widget.ActionButtonPreference;
import com.android.settings.widget.ActionButtonPreferenceTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@@ -44,22 +47,34 @@ import org.robolectric.annotation.Config;
shadows = SettingsShadowBluetoothDevice.class)
public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase {
private BluetoothDetailsButtonsController mController;
private LayoutPreference mLayoutPreference;
private ActionButtonPreference mButtonsPref;
private Button mLeftButton;
private Button mRightButton;
@Override
public void setUp() {
super.setUp();
final View buttons = View.inflate(
RuntimeEnvironment.application, R.layout.two_action_buttons, null /* parent */);
mLeftButton = buttons.findViewById(R.id.button1_positive);
mRightButton = buttons.findViewById(R.id.button2_positive);
mController = new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice,
mLifecycle);
mLeftButton = new Button(mContext);
mRightButton = new Button(mContext);
mLayoutPreference = new LayoutPreference(mContext, R.layout.app_action_buttons);
mLayoutPreference.setKey(mController.getPreferenceKey());
mScreen.addPreference(mLayoutPreference);
mLeftButton = (Button) mLayoutPreference.findViewById(R.id.left_button);
mRightButton = (Button) mLayoutPreference.findViewById(R.id.right_button);
mButtonsPref = ActionButtonPreferenceTest.createMock();
when(mButtonsPref.getKey()).thenReturn(mController.getPreferenceKey());
when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
mLeftButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref;
});
when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
mRightButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref;
});
mScreen.addPreference(mButtonsPref);
setupDevice(mDeviceConfig);
when(mCachedDevice.isBusy()).thenReturn(false);
}
@@ -67,15 +82,16 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
@Test
public void connected() {
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
assertThat(mRightButton.getText()).isEqualTo(mContext.getString(R.string.forget));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton2Text(R.string.forget);
}
@Test
public void clickOnDisconnect() {
showScreen(mController);
mLeftButton.callOnClick();
verify(mCachedDevice).disconnect();
}
@@ -84,8 +100,7 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
mLeftButton.callOnClick();
verify(mCachedDevice).connect(eq(true));
@@ -95,14 +110,12 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void becomeDisconnected() {
showScreen(mController);
// By default we start out with the device connected.
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
// Now make the device appear to have changed to disconnected.
when(mCachedDevice.isConnected()).thenReturn(false);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
// Click the button and make sure that connect (not disconnect) gets called.
mLeftButton.callOnClick();
@@ -115,14 +128,13 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
// Now make the device appear to have changed to connected.
when(mCachedDevice.isConnected()).thenReturn(true);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
// Click the button and make sure that disconnnect (not connect) gets called.
mLeftButton.callOnClick();
@@ -150,24 +162,27 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void startsOutBusy() {
when(mCachedDevice.isBusy()).thenReturn(true);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
assertThat(mRightButton.getText()).isEqualTo(mContext.getString(R.string.forget));
assertThat(mLeftButton.isEnabled()).isFalse();
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton1Enabled(false);
verify(mButtonsPref).setButton2Text(R.string.forget);
// Now pretend the device became non-busy.
when(mCachedDevice.isBusy()).thenReturn(false);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.isEnabled()).isTrue();
verify(mButtonsPref).setButton1Enabled(true);
}
@Test
public void becomesBusy() {
showScreen(mController);
assertThat(mLeftButton.isEnabled()).isTrue();
verify(mButtonsPref).setButton1Enabled(true);
// Now pretend the device became busy.
when(mCachedDevice.isBusy()).thenReturn(true);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.isEnabled()).isFalse();
verify(mButtonsPref).setButton1Enabled(false);
}
}