1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.widget; 18 19 import static com.android.settings.widget.RadioButtonPreferenceWithExtraWidget.EXTRA_WIDGET_VISIBILITY_GONE; 20 import static com.android.settings.widget.RadioButtonPreferenceWithExtraWidget.EXTRA_WIDGET_VISIBILITY_INFO; 21 import static com.android.settings.widget.RadioButtonPreferenceWithExtraWidget.EXTRA_WIDGET_VISIBILITY_SETTING; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.junit.Assert.assertEquals; 26 27 import android.app.Application; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.widget.ImageView; 31 import android.widget.TextView; 32 33 import androidx.preference.PreferenceViewHolder; 34 35 import com.android.settings.R; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 43 @RunWith(RobolectricTestRunner.class) 44 public class RadioButtonPreferenceWithExtraWidgetTest { 45 46 private Application mContext; 47 private RadioButtonPreferenceWithExtraWidget mPreference; 48 49 private TextView mSummary; 50 private ImageView mExtraWidget; 51 private View mExtraWidgetDivider; 52 53 private boolean mIsClickListenerCalled = false; 54 private View.OnClickListener mClickListener = new View.OnClickListener() { 55 @Override 56 public void onClick(View v) { 57 mIsClickListenerCalled = true; 58 } 59 }; 60 61 @Before setUp()62 public void setUp() { 63 mContext = RuntimeEnvironment.application; 64 mPreference = new RadioButtonPreferenceWithExtraWidget(mContext); 65 mPreference.setSummary("test summary"); 66 67 View view = LayoutInflater.from(mContext) 68 .inflate(R.layout.preference_radio_with_extra_widget, null); 69 PreferenceViewHolder preferenceViewHolder = 70 PreferenceViewHolder.createInstanceForTests(view); 71 mPreference.onBindViewHolder(preferenceViewHolder); 72 73 mSummary = view.findViewById(android.R.id.summary); 74 mExtraWidget = view.findViewById(R.id.radio_extra_widget); 75 mExtraWidgetDivider = view.findViewById(R.id.radio_extra_widget_divider); 76 } 77 78 @Test shouldHaveRadioPreferenceWithExtraWidgetLayout()79 public void shouldHaveRadioPreferenceWithExtraWidgetLayout() { 80 assertThat(mPreference.getLayoutResource()) 81 .isEqualTo(R.layout.preference_radio_with_extra_widget); 82 } 83 84 @Test iconSpaceReservedShouldBeFalse()85 public void iconSpaceReservedShouldBeFalse() { 86 assertThat(mPreference.isIconSpaceReserved()).isFalse(); 87 } 88 89 @Test summaryShouldBeVisible()90 public void summaryShouldBeVisible() { 91 assertEquals(View.VISIBLE, mSummary.getVisibility()); 92 } 93 94 @Test testSetExtraWidgetVisibility_gone()95 public void testSetExtraWidgetVisibility_gone() { 96 mPreference.setExtraWidgetVisibility(EXTRA_WIDGET_VISIBILITY_GONE); 97 assertEquals(View.GONE, mExtraWidget.getVisibility()); 98 assertEquals(View.GONE, mExtraWidgetDivider.getVisibility()); 99 assertThat(mExtraWidget.isClickable()).isFalse(); 100 } 101 102 @Test testSetExtraWidgetVisibility_info()103 public void testSetExtraWidgetVisibility_info() { 104 mPreference.setExtraWidgetVisibility(EXTRA_WIDGET_VISIBILITY_INFO); 105 assertEquals(View.VISIBLE, mExtraWidget.getVisibility()); 106 assertEquals(View.VISIBLE, mExtraWidgetDivider.getVisibility()); 107 assertThat(mExtraWidget.isClickable()).isTrue(); 108 assertEquals(mContext.getResources().getText(R.string.information_label), 109 mExtraWidget.getContentDescription()); 110 } 111 112 @Test testSetExtraWidgetVisibility_setting()113 public void testSetExtraWidgetVisibility_setting() { 114 mPreference.setExtraWidgetVisibility(EXTRA_WIDGET_VISIBILITY_SETTING); 115 assertEquals(View.VISIBLE, mExtraWidget.getVisibility()); 116 assertEquals(View.VISIBLE, mExtraWidgetDivider.getVisibility()); 117 assertThat(mExtraWidget.isClickable()).isTrue(); 118 assertEquals(mContext.getResources().getText(R.string.settings_label), 119 mExtraWidget.getContentDescription()); 120 } 121 122 @Test testSetExtraWidgetOnClickListener()123 public void testSetExtraWidgetOnClickListener() { 124 mPreference.setExtraWidgetOnClickListener(mClickListener); 125 126 assertThat(mIsClickListenerCalled).isFalse(); 127 mExtraWidget.callOnClick(); 128 assertThat(mIsClickListenerCalled).isTrue(); 129 } 130 131 @Test extraWidgetStaysEnabledWhenPreferenceIsDisabled()132 public void extraWidgetStaysEnabledWhenPreferenceIsDisabled() { 133 mPreference.setEnabled(false); 134 mExtraWidget.setEnabled(false); 135 136 assertThat(mExtraWidget.isEnabled()).isFalse(); 137 mPreference.setExtraWidgetOnClickListener(mClickListener); 138 assertThat(mExtraWidget.isEnabled()).isTrue(); 139 } 140 } 141