Move ActionButtonPreference into SettingsLib
Since this widget will be used by other app, we move it into SettingsLib. Test: robotest Bug: 120005054 Change-Id: Ib54b33d676ca6f742f18fae398a36b217340373c
This commit is contained in:
13
packages/SettingsLib/ActionButtonsPreference/Android.bp
Normal file
13
packages/SettingsLib/ActionButtonsPreference/Android.bp
Normal file
@@ -0,0 +1,13 @@
|
||||
android_library {
|
||||
name: "ActionButtonsPreference",
|
||||
|
||||
srcs: ["src/**/*.java"],
|
||||
resource_dirs: ["res"],
|
||||
|
||||
static_libs: [
|
||||
"androidx.preference_preference",
|
||||
],
|
||||
|
||||
sdk_version: "system_current",
|
||||
min_sdk_version: "21",
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.android.settingslib.widget">
|
||||
|
||||
<uses-sdk android:minSdkVersion="21" />
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<LinearLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingStart="8dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<Button
|
||||
android:id="@+id/button1"
|
||||
style="@style/SettingsActionButton"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button2"
|
||||
style="@style/SettingsActionButton"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button3"
|
||||
style="@style/SettingsActionButton"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/button4"
|
||||
style="@style/SettingsActionButton"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 2018 The Android Open Source Project
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<style name="SettingsActionButton" parent="android:Widget.DeviceDefault.Button.Borderless.Colored">
|
||||
<item name="android:drawablePadding">4dp</item>
|
||||
<item name="android:drawableTint">@*android:color/btn_colored_borderless_text_material</item>
|
||||
<item name="android:layout_marginEnd">8dp</item>
|
||||
<item name="android:paddingTop">20dp</item>
|
||||
<item name="android:paddingBottom">20dp</item>
|
||||
</style>
|
||||
</resources>
|
||||
@@ -0,0 +1,393 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
/**
|
||||
* This preference provides a four buttons layout with Settings style.
|
||||
* It looks like below
|
||||
*
|
||||
* --------------------------------------------------
|
||||
* button1 | button2 | button3 | button4 |
|
||||
* --------------------------------------------------
|
||||
*
|
||||
* User can set title / icon / click listener for each button.
|
||||
*
|
||||
* By default, four buttons are visible.
|
||||
* However, there are two cases which button should be invisible(View.GONE).
|
||||
*
|
||||
* 1. User sets invisible for button. ex: ActionButtonPreference.setButton1Visible(false)
|
||||
* 2. User doesn't set any title or icon for button.
|
||||
*/
|
||||
public class ActionButtonsPreference extends Preference {
|
||||
|
||||
private static final String TAG = "ActionButtonPreference";
|
||||
private final ButtonInfo mButton1Info = new ButtonInfo();
|
||||
private final ButtonInfo mButton2Info = new ButtonInfo();
|
||||
private final ButtonInfo mButton3Info = new ButtonInfo();
|
||||
private final ButtonInfo mButton4Info = new ButtonInfo();
|
||||
|
||||
public ActionButtonsPreference(Context context, AttributeSet attrs,
|
||||
int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonsPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonsPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init();
|
||||
}
|
||||
|
||||
public ActionButtonsPreference(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
setLayoutResource(R.layout.settings_action_buttons);
|
||||
setSelectable(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
holder.setDividerAllowedAbove(true);
|
||||
holder.setDividerAllowedBelow(true);
|
||||
|
||||
mButton1Info.mButton = (Button) holder.findViewById(R.id.button1);
|
||||
mButton2Info.mButton = (Button) holder.findViewById(R.id.button2);
|
||||
mButton3Info.mButton = (Button) holder.findViewById(R.id.button3);
|
||||
mButton4Info.mButton = (Button) holder.findViewById(R.id.button4);
|
||||
|
||||
mButton1Info.setUpButton();
|
||||
mButton2Info.setUpButton();
|
||||
mButton3Info.setUpButton();
|
||||
mButton4Info.setUpButton();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility state of button1.
|
||||
*/
|
||||
public ActionButtonsPreference setButton1Visible(boolean isVisible) {
|
||||
if (isVisible != mButton1Info.mIsVisible) {
|
||||
mButton1Info.mIsVisible = isVisible;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed in button1.
|
||||
*/
|
||||
public ActionButtonsPreference setButton1Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton1Info.mText)) {
|
||||
mButton1Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the drawable to be displayed above of text in button1.
|
||||
*/
|
||||
public ActionButtonsPreference setButton1Icon(@DrawableRes int iconResId) {
|
||||
if (iconResId == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final Drawable icon;
|
||||
try {
|
||||
icon = getContext().getDrawable(iconResId);
|
||||
mButton1Info.mIcon = icon;
|
||||
notifyChanged();
|
||||
} catch (Resources.NotFoundException exception) {
|
||||
Log.e(TAG, "Resource does not exist: " + iconResId);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the enabled state of button1.
|
||||
*/
|
||||
public ActionButtonsPreference setButton1Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton1Info.mIsEnabled) {
|
||||
mButton1Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback to be invoked when button1 is clicked.
|
||||
*/
|
||||
public ActionButtonsPreference setButton1OnClickListener(
|
||||
View.OnClickListener listener) {
|
||||
if (listener != mButton1Info.mListener) {
|
||||
mButton1Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility state of button2.
|
||||
*/
|
||||
public ActionButtonsPreference setButton2Visible(boolean isVisible) {
|
||||
if (isVisible != mButton2Info.mIsVisible) {
|
||||
mButton2Info.mIsVisible = isVisible;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed in button2.
|
||||
*/
|
||||
public ActionButtonsPreference setButton2Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton2Info.mText)) {
|
||||
mButton2Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the drawable to be displayed above of text in button2.
|
||||
*/
|
||||
public ActionButtonsPreference setButton2Icon(@DrawableRes int iconResId) {
|
||||
if (iconResId == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final Drawable icon;
|
||||
try {
|
||||
icon = getContext().getDrawable(iconResId);
|
||||
mButton2Info.mIcon = icon;
|
||||
notifyChanged();
|
||||
} catch (Resources.NotFoundException exception) {
|
||||
Log.e(TAG, "Resource does not exist: " + iconResId);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the enabled state of button2.
|
||||
*/
|
||||
public ActionButtonsPreference setButton2Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton2Info.mIsEnabled) {
|
||||
mButton2Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback to be invoked when button2 is clicked.
|
||||
*/
|
||||
public ActionButtonsPreference setButton2OnClickListener(
|
||||
View.OnClickListener listener) {
|
||||
if (listener != mButton2Info.mListener) {
|
||||
mButton2Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility state of button3.
|
||||
*/
|
||||
public ActionButtonsPreference setButton3Visible(boolean isVisible) {
|
||||
if (isVisible != mButton3Info.mIsVisible) {
|
||||
mButton3Info.mIsVisible = isVisible;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed in button3.
|
||||
*/
|
||||
public ActionButtonsPreference setButton3Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton3Info.mText)) {
|
||||
mButton3Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the drawable to be displayed above of text in button3.
|
||||
*/
|
||||
public ActionButtonsPreference setButton3Icon(@DrawableRes int iconResId) {
|
||||
if (iconResId == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final Drawable icon;
|
||||
try {
|
||||
icon = getContext().getDrawable(iconResId);
|
||||
mButton3Info.mIcon = icon;
|
||||
notifyChanged();
|
||||
} catch (Resources.NotFoundException exception) {
|
||||
Log.e(TAG, "Resource does not exist: " + iconResId);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the enabled state of button3.
|
||||
*/
|
||||
public ActionButtonsPreference setButton3Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton3Info.mIsEnabled) {
|
||||
mButton3Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback to be invoked when button3 is clicked.
|
||||
*/
|
||||
public ActionButtonsPreference setButton3OnClickListener(
|
||||
View.OnClickListener listener) {
|
||||
if (listener != mButton3Info.mListener) {
|
||||
mButton3Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility state of button4.
|
||||
*/
|
||||
public ActionButtonsPreference setButton4Visible(boolean isVisible) {
|
||||
if (isVisible != mButton4Info.mIsVisible) {
|
||||
mButton4Info.mIsVisible = isVisible;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the text to be displayed in button4.
|
||||
*/
|
||||
public ActionButtonsPreference setButton4Text(@StringRes int textResId) {
|
||||
final String newText = getContext().getString(textResId);
|
||||
if (!TextUtils.equals(newText, mButton4Info.mText)) {
|
||||
mButton4Info.mText = newText;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the drawable to be displayed above of text in button4.
|
||||
*/
|
||||
public ActionButtonsPreference setButton4Icon(@DrawableRes int iconResId) {
|
||||
if (iconResId == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
final Drawable icon;
|
||||
try {
|
||||
icon = getContext().getDrawable(iconResId);
|
||||
mButton4Info.mIcon = icon;
|
||||
notifyChanged();
|
||||
} catch (Resources.NotFoundException exception) {
|
||||
Log.e(TAG, "Resource does not exist: " + iconResId);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the enabled state of button4.
|
||||
*/
|
||||
public ActionButtonsPreference setButton4Enabled(boolean isEnabled) {
|
||||
if (isEnabled != mButton4Info.mIsEnabled) {
|
||||
mButton4Info.mIsEnabled = isEnabled;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a callback to be invoked when button4 is clicked.
|
||||
*/
|
||||
public ActionButtonsPreference setButton4OnClickListener(
|
||||
View.OnClickListener listener) {
|
||||
if (listener != mButton4Info.mListener) {
|
||||
mButton4Info.mListener = listener;
|
||||
notifyChanged();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
static class ButtonInfo {
|
||||
private Button mButton;
|
||||
private CharSequence mText;
|
||||
private Drawable mIcon;
|
||||
private View.OnClickListener mListener;
|
||||
private boolean mIsEnabled = true;
|
||||
private boolean mIsVisible = true;
|
||||
|
||||
void setUpButton() {
|
||||
mButton.setText(mText);
|
||||
mButton.setOnClickListener(mListener);
|
||||
mButton.setEnabled(mIsEnabled);
|
||||
mButton.setCompoundDrawablesWithIntrinsicBounds(
|
||||
null /* left */, mIcon /* top */, null /* right */, null /* bottom */);
|
||||
|
||||
if (shouldBeVisible()) {
|
||||
mButton.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
mButton.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* By default, four buttons are visible.
|
||||
* However, there are two cases which button should be invisible.
|
||||
*
|
||||
* 1. User set invisible for this button. ex: mIsVisible = false.
|
||||
* 2. User didn't set any title or icon.
|
||||
*/
|
||||
private boolean shouldBeVisible() {
|
||||
return mIsVisible && (!TextUtils.isEmpty(mText) || mIcon != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ android_library {
|
||||
"SettingsLibSearchWidget",
|
||||
"SettingsLibSettingsSpinner",
|
||||
"SettingsLayoutPreference",
|
||||
"ActionButtonsPreference",
|
||||
],
|
||||
|
||||
// ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_SHARED_JAVA_LIBRARIES
|
||||
|
||||
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settingslib.widget;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ActionButtonsPreferenceTest {
|
||||
|
||||
private Context mContext;
|
||||
private View mRootView;
|
||||
private ActionButtonsPreference mPref;
|
||||
private PreferenceViewHolder mHolder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mRootView = View.inflate(mContext, R.layout.settings_action_buttons, null /* parent */);
|
||||
mHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
|
||||
mPref = new ActionButtonsPreference(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setTitle_shouldShowButtonByDefault() {
|
||||
mPref.setButton1Text(R.string.install_other_apps);
|
||||
mPref.setButton2Text(R.string.install_other_apps);
|
||||
mPref.setButton3Text(R.string.install_other_apps);
|
||||
mPref.setButton4Text(R.string.install_other_apps);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setIcon_shouldShowButtonByDefault() {
|
||||
mPref.setButton1Icon(R.drawable.ic_plus);
|
||||
mPref.setButton2Icon(R.drawable.ic_plus);
|
||||
mPref.setButton3Icon(R.drawable.ic_plus);
|
||||
mPref.setButton4Icon(R.drawable.ic_plus);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_notSetTitleOrIcon_shouldNotShowButtonByDefault() {
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setVisibleIsGoneAndSetTitle_shouldNotShowButton() {
|
||||
mPref.setButton1Text(R.string.install_other_apps).setButton1Visible(false);
|
||||
mPref.setButton2Text(R.string.install_other_apps).setButton2Visible(false);
|
||||
mPref.setButton3Text(R.string.install_other_apps).setButton3Visible(false);
|
||||
mPref.setButton4Text(R.string.install_other_apps).setButton4Visible(false);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setVisibleIsGoneAndSetIcon_shouldNotShowButton() {
|
||||
mPref.setButton1Icon(R.drawable.ic_plus).setButton1Visible(false);
|
||||
mPref.setButton2Icon(R.drawable.ic_plus).setButton2Visible(false);
|
||||
mPref.setButton3Icon(R.drawable.ic_plus).setButton3Visible(false);
|
||||
mPref.setButton4Icon(R.drawable.ic_plus).setButton4Visible(false);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setVisibility_shouldUpdateButtonVisibility() {
|
||||
mPref.setButton1Text(R.string.install_other_apps).setButton1Visible(false);
|
||||
mPref.setButton2Text(R.string.install_other_apps).setButton2Visible(false);
|
||||
mPref.setButton3Text(R.string.install_other_apps).setButton3Visible(false);
|
||||
mPref.setButton4Text(R.string.install_other_apps).setButton4Visible(false);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.GONE);
|
||||
|
||||
mPref.setButton1Visible(true);
|
||||
mPref.setButton2Visible(true);
|
||||
mPref.setButton3Visible(true);
|
||||
mPref.setButton4Visible(true);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button2).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button3).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
assertThat(mRootView.findViewById(R.id.button4).getVisibility())
|
||||
.isEqualTo(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setEnabled_shouldEnableButton() {
|
||||
mPref.setButton1Enabled(true);
|
||||
mPref.setButton2Enabled(false);
|
||||
mPref.setButton3Enabled(true);
|
||||
mPref.setButton4Enabled(false);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(mRootView.findViewById(R.id.button1).isEnabled()).isTrue();
|
||||
assertThat(mRootView.findViewById(R.id.button2).isEnabled()).isFalse();
|
||||
assertThat(mRootView.findViewById(R.id.button3).isEnabled()).isTrue();
|
||||
assertThat(mRootView.findViewById(R.id.button4).isEnabled()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setText_shouldShowSameText() {
|
||||
mPref.setButton1Text(R.string.install_other_apps);
|
||||
mPref.setButton2Text(R.string.install_other_apps);
|
||||
mPref.setButton3Text(R.string.install_other_apps);
|
||||
mPref.setButton4Text(R.string.install_other_apps);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
|
||||
assertThat(((Button) mRootView.findViewById(R.id.button1)).getText())
|
||||
.isEqualTo(mContext.getText(R.string.install_other_apps));
|
||||
assertThat(((Button) mRootView.findViewById(R.id.button2)).getText())
|
||||
.isEqualTo(mContext.getText(R.string.install_other_apps));
|
||||
assertThat(((Button) mRootView.findViewById(R.id.button3)).getText())
|
||||
.isEqualTo(mContext.getText(R.string.install_other_apps));
|
||||
assertThat(((Button) mRootView.findViewById(R.id.button4)).getText())
|
||||
.isEqualTo(mContext.getText(R.string.install_other_apps));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_setButtonIcon_iconMustDisplayAboveText() {
|
||||
mPref.setButton1Text(R.string.install_other_apps);
|
||||
mPref.setButton1Icon(R.drawable.ic_plus);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
final Drawable[] drawablesAroundText =
|
||||
((Button) mRootView.findViewById(R.id.button1))
|
||||
.getCompoundDrawables();
|
||||
|
||||
assertThat(drawablesAroundText[1 /* top */]).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setButtonIcon_iconResourceIdIsZero_shouldNotDisplayIcon() {
|
||||
mPref.setButton1Text(R.string.install_other_apps);
|
||||
mPref.setButton1Icon(0);
|
||||
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
final Drawable[] drawablesAroundText =
|
||||
((Button) mRootView.findViewById(R.id.button1))
|
||||
.getCompoundDrawables();
|
||||
|
||||
assertThat(drawablesAroundText[1 /* top */]).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setButtonIcon_iconResourceIdNotExisting_shouldNotDisplayIconAndCrash() {
|
||||
mPref.setButton1Text(R.string.install_other_apps);
|
||||
mPref.setButton1Icon(999999999 /* not existing id */);
|
||||
// Should not crash here
|
||||
mPref.onBindViewHolder(mHolder);
|
||||
final Drawable[] drawablesAroundText =
|
||||
((Button) mRootView.findViewById(R.id.button1))
|
||||
.getCompoundDrawables();
|
||||
|
||||
assertThat(drawablesAroundText[1 /* top */]).isNull();
|
||||
}
|
||||
|
||||
public static ActionButtonsPreference createMock() {
|
||||
final ActionButtonsPreference pref = mock(ActionButtonsPreference.class);
|
||||
when(pref.setButton1Text(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton1Icon(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton1Enabled(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton1Visible(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton1OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
|
||||
|
||||
when(pref.setButton2Text(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton2Icon(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton2Enabled(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton2Visible(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton2OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
|
||||
|
||||
when(pref.setButton3Text(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton3Icon(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton3Enabled(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton3Visible(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton3OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
|
||||
|
||||
when(pref.setButton4Text(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton4Icon(anyInt())).thenReturn(pref);
|
||||
when(pref.setButton4Enabled(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton4Visible(anyBoolean())).thenReturn(pref);
|
||||
when(pref.setButton4OnClickListener(any(View.OnClickListener.class))).thenReturn(pref);
|
||||
return pref;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user