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 *Visibility_setGoneForPrimaryButton_buttonGroupIsGone 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 package com.android.settings.widget; 17 18 import static android.view.View.GONE; 19 import static android.view.View.VISIBLE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 import static com.google.common.truth.Truth.assertWithMessage; 23 24 import android.content.Context; 25 import android.view.View; 26 import android.widget.Button; 27 28 import androidx.preference.PreferenceViewHolder; 29 import androidx.test.core.app.ApplicationProvider; 30 31 import com.android.settings.R; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RobolectricTestRunner; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class CardPreferenceTest { 40 private CardPreference mCardPreference; 41 private PreferenceViewHolder mHolder; 42 43 @Before setUp()44 public void setUp() { 45 Context context = ApplicationProvider.getApplicationContext(); 46 context.setTheme(R.style.Theme_Settings); 47 mCardPreference = new CardPreference(context); 48 mHolder = 49 PreferenceViewHolder.createInstanceForTests( 50 View.inflate(context, R.layout.card_preference_layout, /* parent= */ null)); 51 } 52 53 @Test newACardPreference_layoutResourceShouldBeCardPreferenceLayout()54 public void newACardPreference_layoutResourceShouldBeCardPreferenceLayout() { 55 Context context = ApplicationProvider.getApplicationContext(); 56 context.setTheme(R.style.SettingsPreferenceTheme); 57 CardPreference cardPreference = new CardPreference(context); 58 assertThat(cardPreference.getLayoutResource()).isEqualTo(R.layout.card_preference_layout); 59 } 60 61 @Test onBindViewHolder_noButtonVisible_buttonsLayoutIsGone()62 public void onBindViewHolder_noButtonVisible_buttonsLayoutIsGone() { 63 mCardPreference.onBindViewHolder(mHolder); 64 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE); 65 } 66 67 @Test onBindViewHolder_setPrimaryButtonVisibility_buttonsLayoutIsVisible()68 public void onBindViewHolder_setPrimaryButtonVisibility_buttonsLayoutIsVisible() { 69 mCardPreference.setPrimaryButtonVisible(true); 70 mCardPreference.onBindViewHolder(mHolder); 71 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(VISIBLE); 72 } 73 74 @Test onBindViewHolder_setPrimaryButtonVisibilityToVisible()75 public void onBindViewHolder_setPrimaryButtonVisibilityToVisible() { 76 mCardPreference.setPrimaryButtonVisible(true); 77 mCardPreference.onBindViewHolder(mHolder); 78 assertThat(getPrimaryButton().getVisibility()).isEqualTo(VISIBLE); 79 } 80 81 @Test onBindViewHolder_setSecondaryButtonVisibility_buttonsLayoutIsVisible()82 public void onBindViewHolder_setSecondaryButtonVisibility_buttonsLayoutIsVisible() { 83 mCardPreference.setSecondaryButtonVisible(true); 84 mCardPreference.onBindViewHolder(mHolder); 85 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(VISIBLE); 86 } 87 88 @Test onBindViewHolder_setSecondaryButtonVisibilityToVisible()89 public void onBindViewHolder_setSecondaryButtonVisibilityToVisible() { 90 mCardPreference.setSecondaryButtonVisible(true); 91 mCardPreference.onBindViewHolder(mHolder); 92 assertThat(getSecondaryButton().getVisibility()).isEqualTo(VISIBLE); 93 } 94 95 @Test onBindViewHolder_setPrimaryButtonTextToExpectedText()96 public void onBindViewHolder_setPrimaryButtonTextToExpectedText() { 97 String expectedText = "primary-button"; 98 mCardPreference.setPrimaryButtonText(expectedText); 99 mCardPreference.onBindViewHolder(mHolder); 100 assertThat(getPrimaryButton().getText().toString()).isEqualTo(expectedText); 101 } 102 103 @Test onBindViewHolder_setSecondaryButtonTextToExpectedText()104 public void onBindViewHolder_setSecondaryButtonTextToExpectedText() { 105 String expectedText = "secondary-button"; 106 mCardPreference.setSecondaryButtonText(expectedText); 107 mCardPreference.onBindViewHolder(mHolder); 108 assertThat(getSecondaryButton().getText().toString()).isEqualTo(expectedText); 109 } 110 111 @Test onBindViewHolder_initialTextForPrimaryButtonShouldBeEmpty()112 public void onBindViewHolder_initialTextForPrimaryButtonShouldBeEmpty() { 113 mCardPreference.onBindViewHolder(mHolder); 114 assertThat(getPrimaryButton().getText().toString()).isEqualTo(""); 115 } 116 117 @Test onBindViewHolder_initialTextForSecondaryButtonShouldBeEmpty()118 public void onBindViewHolder_initialTextForSecondaryButtonShouldBeEmpty() { 119 mCardPreference.onBindViewHolder(mHolder); 120 assertThat(getSecondaryButton().getText().toString()).isEqualTo(""); 121 } 122 123 @Test performClickOnPrimaryButton_callClickListener()124 public void performClickOnPrimaryButton_callClickListener() { 125 final boolean[] hasCalled = {false}; 126 View.OnClickListener clickListener = v -> hasCalled[0] = true; 127 mCardPreference.setPrimaryButtonClickListener(clickListener); 128 mCardPreference.onBindViewHolder(mHolder); 129 getPrimaryButton().performClick(); 130 assertThat(hasCalled[0]).isTrue(); 131 } 132 133 @Test performClickOnSecondaryButton_callClickListener()134 public void performClickOnSecondaryButton_callClickListener() { 135 final boolean[] hasCalled = {false}; 136 View.OnClickListener clickListener = v -> hasCalled[0] = true; 137 mCardPreference.setSecondaryButtonClickListener(clickListener); 138 mCardPreference.onBindViewHolder(mHolder); 139 getSecondaryButton().performClick(); 140 assertThat(hasCalled[0]).isTrue(); 141 } 142 143 @Test onBindViewHolder_primaryButtonDefaultIsGone()144 public void onBindViewHolder_primaryButtonDefaultIsGone() { 145 mCardPreference.onBindViewHolder(mHolder); 146 assertThat(getPrimaryButton().getVisibility()).isEqualTo(GONE); 147 } 148 149 @Test onBindViewHolder_secondaryButtonDefaultIsGone()150 public void onBindViewHolder_secondaryButtonDefaultIsGone() { 151 mCardPreference.onBindViewHolder(mHolder); 152 assertThat(getSecondaryButton().getVisibility()).isEqualTo(GONE); 153 } 154 155 @Test setPrimaryButtonVisibility_setTrueAfterBindViewHolder_isVisible()156 public void setPrimaryButtonVisibility_setTrueAfterBindViewHolder_isVisible() { 157 mCardPreference.setPrimaryButtonVisible(false); 158 mCardPreference.onBindViewHolder(mHolder); 159 mCardPreference.setPrimaryButtonVisible(true); 160 assertThat(getPrimaryButton().getVisibility()).isEqualTo(VISIBLE); 161 } 162 163 @Test setPrimaryButtonText_setAfterBindViewHolder_setOnUi()164 public void setPrimaryButtonText_setAfterBindViewHolder_setOnUi() { 165 String expectedText = "123456"; 166 mCardPreference.onBindViewHolder(mHolder); 167 mCardPreference.setPrimaryButtonText(expectedText); 168 assertThat(getPrimaryButton().getText().toString()).isEqualTo(expectedText); 169 } 170 171 @Test setPrimaryButtonText_setNull_isEmptyText()172 public void setPrimaryButtonText_setNull_isEmptyText() { 173 final String emptyString = ""; 174 mCardPreference.setPrimaryButtonText("1234"); 175 mCardPreference.onBindViewHolder(mHolder); 176 mCardPreference.setPrimaryButtonText(null); 177 assertThat(getPrimaryButton().getText().toString()).isEqualTo(emptyString); 178 } 179 180 @Test setPrimaryButtonClickListener_setAfterOnBindViewHolder()181 public void setPrimaryButtonClickListener_setAfterOnBindViewHolder() { 182 final String[] hasCalled = {""}; 183 String expectedClickedResult = "was called"; 184 View.OnClickListener clickListener = v -> hasCalled[0] = expectedClickedResult; 185 mCardPreference.onBindViewHolder(mHolder); 186 mCardPreference.setPrimaryButtonClickListener(clickListener); 187 getPrimaryButton().performClick(); 188 assertThat(hasCalled[0]).isEqualTo(expectedClickedResult); 189 } 190 191 @Test setPrimaryButtonClickListener_setNull_clearTheOnClickListener()192 public void setPrimaryButtonClickListener_setNull_clearTheOnClickListener() { 193 final String[] hasCalled = {"not called"}; 194 View.OnClickListener clickListener = v -> hasCalled[0] = "called once"; 195 mCardPreference.setPrimaryButtonClickListener(clickListener); 196 mCardPreference.onBindViewHolder(mHolder); 197 mCardPreference.setPrimaryButtonClickListener(null); 198 getPrimaryButton().performClick(); 199 assertThat(hasCalled[0]).isEqualTo("not called"); 200 } 201 202 @Test setSecondaryButtonVisibility_setTrueAfterBindViewHolder_isVisible()203 public void setSecondaryButtonVisibility_setTrueAfterBindViewHolder_isVisible() { 204 mCardPreference.setSecondaryButtonVisible(false); 205 mCardPreference.onBindViewHolder(mHolder); 206 mCardPreference.setSecondaryButtonVisible(true); 207 assertThat(getSecondaryButton().getVisibility()).isEqualTo(VISIBLE); 208 } 209 210 @Test setSecondaryButtonText_setAfterBindViewHolder_setOnUi()211 public void setSecondaryButtonText_setAfterBindViewHolder_setOnUi() { 212 String expectedText = "10101010"; 213 mCardPreference.onBindViewHolder(mHolder); 214 mCardPreference.setSecondaryButtonText(expectedText); 215 assertThat(getSecondaryButton().getText().toString()).isEqualTo(expectedText); 216 } 217 218 @Test setSecondaryButtonText_setNull_isEmptyText()219 public void setSecondaryButtonText_setNull_isEmptyText() { 220 String emptyString = ""; 221 mCardPreference.setSecondaryButtonText("1234"); 222 mCardPreference.onBindViewHolder(mHolder); 223 mCardPreference.setSecondaryButtonText(null); 224 assertThat(getSecondaryButton().getText().toString()).isEqualTo(emptyString); 225 } 226 227 @Test setSecondaryButtonClickListener_setAfterOnBindViewHolder()228 public void setSecondaryButtonClickListener_setAfterOnBindViewHolder() { 229 final String[] hasCalled = {""}; 230 String expectedClickedResult = "2nd was called"; 231 View.OnClickListener clickListener = v -> hasCalled[0] = expectedClickedResult; 232 mCardPreference.onBindViewHolder(mHolder); 233 mCardPreference.setSecondaryButtonClickListener(clickListener); 234 getSecondaryButton().performClick(); 235 assertThat(hasCalled[0]).isEqualTo(expectedClickedResult); 236 } 237 238 @Test setSecondaryButtonClickListener_setNull_clearTheOnClickListener()239 public void setSecondaryButtonClickListener_setNull_clearTheOnClickListener() { 240 final String[] hasCalled = {"not called"}; 241 View.OnClickListener clickListener = v -> hasCalled[0] = "called once"; 242 mCardPreference.setSecondaryButtonClickListener(clickListener); 243 mCardPreference.onBindViewHolder(mHolder); 244 mCardPreference.setSecondaryButtonClickListener(null); 245 getSecondaryButton().performClick(); 246 assertThat(hasCalled[0]).isEqualTo("not called"); 247 } 248 249 @Test setPrimaryButtonVisibility_setGoneForSecondaryButton_buttonGroupIsGone()250 public void setPrimaryButtonVisibility_setGoneForSecondaryButton_buttonGroupIsGone() { 251 mCardPreference.setPrimaryButtonVisible(true); 252 mCardPreference.setSecondaryButtonVisible(false); 253 mCardPreference.onBindViewHolder(mHolder); 254 assertWithMessage("PreCondition: buttonsView should be Visible") 255 .that(getCardPreferenceButtonsView().getVisibility()) 256 .isEqualTo(VISIBLE); 257 mCardPreference.setPrimaryButtonVisible(false); 258 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE); 259 } 260 261 @Test setSecondaryButtonVisibility_setGoneForPrimaryButton_buttonGroupIsGone()262 public void setSecondaryButtonVisibility_setGoneForPrimaryButton_buttonGroupIsGone() { 263 mCardPreference.setPrimaryButtonVisible(false); 264 mCardPreference.setSecondaryButtonVisible(true); 265 mCardPreference.onBindViewHolder(mHolder); 266 assertWithMessage("PreCondition: buttonsView should be Visible") 267 .that(getCardPreferenceButtonsView().getVisibility()) 268 .isEqualTo(VISIBLE); 269 mCardPreference.setSecondaryButtonVisible(false); 270 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE); 271 } 272 273 @Test resetLayoutState_buttonGroupIsGone()274 public void resetLayoutState_buttonGroupIsGone() { 275 mCardPreference.setPrimaryButtonVisible(true); 276 mCardPreference.setSecondaryButtonVisible(true); 277 mCardPreference.onBindViewHolder(mHolder); 278 mCardPreference.resetLayoutState(); 279 assertThat(getCardPreferenceButtonsView().getVisibility()).isEqualTo(GONE); 280 } 281 getCardPreferenceButtonsView()282 private View getCardPreferenceButtonsView() { 283 return mHolder.findViewById(R.id.card_preference_buttons); 284 } 285 getPrimaryButton()286 private Button getPrimaryButton() { 287 return (Button) mHolder.findViewById(android.R.id.button1); 288 } 289 getSecondaryButton()290 private Button getSecondaryButton() { 291 return (Button) mHolder.findViewById(android.R.id.button2); 292 } 293 } 294