package com.android.settings.widget;
import android.content.Context;
+import android.text.TextUtils;
import android.util.AttributeSet;
+import android.view.View;
+import android.widget.LinearLayout;
+import android.widget.TextView;
import com.android.settings.R;
import androidx.preference.CheckBoxPreference;
+import androidx.preference.PreferenceViewHolder;
/**
* {@link CheckBoxPreference} that used only to display app
super(context);
setLayoutResource(R.layout.preference_app);
}
+
+ @Override
+ public void onBindViewHolder(PreferenceViewHolder holder) {
+ super.onBindViewHolder(holder);
+
+ final TextView appendix = (TextView) holder.findViewById(R.id.appendix);
+ if (appendix != null) {
+ appendix.setVisibility(View.GONE);
+ }
+
+ final LinearLayout layout = (LinearLayout) holder.findViewById(R.id.summary_container);
+ if (layout != null) {
+ // If summary doesn't exist, make it gone
+ layout.setVisibility(TextUtils.isEmpty(getSummary()) ? View.GONE : View.VISIBLE);
+ }
+ }
}
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
+import androidx.preference.PreferenceViewHolder;
+
@RunWith(SettingsRobolectricTestRunner.class)
public class AppCheckBoxPreferenceTest {
+ private static final String SUMMARY = "summary info";
+
private Context mContext;
private AppCheckBoxPreference mPreference;
private AppCheckBoxPreference mAttrPreference;
+ private PreferenceViewHolder mPreferenceViewHolder;
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mPreference = new AppCheckBoxPreference(mContext);
mAttrPreference = new AppCheckBoxPreference(mContext, null /* attrs */);
+ mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(
+ LayoutInflater.from(mContext).inflate(R.layout.preference_app, null));
}
@Test
assertThat(mPreference.getLayoutResource()).isEqualTo(R.layout.preference_app);
assertThat(mAttrPreference.getLayoutResource()).isEqualTo(R.layout.preference_app);
}
+
+ @Test
+ public void onBindViewHolder_noSummary_layoutGone() {
+ mPreference.setSummary("");
+
+ mPreference.onBindViewHolder(mPreferenceViewHolder);
+
+ assertThat(mPreferenceViewHolder.findViewById(R.id.summary_container).getVisibility())
+ .isEqualTo(View.GONE);
+ }
+
+ @Test
+ public void onBindViewHolder_hasSummary_layoutVisible() {
+ mPreference.setSummary(SUMMARY);
+
+ mPreference.onBindViewHolder(mPreferenceViewHolder);
+
+ assertThat(mPreferenceViewHolder.findViewById(R.id.summary_container).getVisibility())
+ .isEqualTo(View.VISIBLE);
+ }
+
+ @Test
+ public void onBindViewHolder_appendixGone() {
+ mPreference.onBindViewHolder(mPreferenceViewHolder);
+
+ assertThat(mPreferenceViewHolder.findViewById(R.id.appendix).getVisibility())
+ .isEqualTo(View.GONE);
+ }
}