1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 */ 16 17 package com.android.settings.widget; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.view.View; 26 import android.widget.Button; 27 28 import com.android.settings.R; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.util.ReflectionHelpers; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class TwoStateButtonPreferenceTest { 40 41 private TwoStateButtonPreference mPreference; 42 private Context mContext; 43 private Button mButtonOn; 44 private Button mButtonOff; 45 46 @Before setUp()47 public void setUp() { 48 MockitoAnnotations.initMocks(this); 49 mContext = spy(RuntimeEnvironment.application); 50 mPreference = spy(new TwoStateButtonPreference(mContext, null /* AttributeSet */)); 51 mButtonOn = new Button(mContext); 52 mButtonOn.setId(R.id.state_on_button); 53 mButtonOff = new Button(mContext); 54 mButtonOff.setId(R.id.state_off_button); 55 ReflectionHelpers.setField(mPreference, "mButtonOn", mButtonOn); 56 ReflectionHelpers.setField(mPreference, "mButtonOff", mButtonOff); 57 } 58 59 @Test testSetButtonVisibility_stateOn_onlyShowButtonOn()60 public void testSetButtonVisibility_stateOn_onlyShowButtonOn() { 61 mPreference.setChecked(true /* stateOn */); 62 63 assertThat(mButtonOn.getVisibility()).isEqualTo(View.GONE); 64 assertThat(mButtonOff.getVisibility()).isEqualTo(View.VISIBLE); 65 } 66 67 @Test testSetButtonVisibility_stateOff_onlyShowButtonOff()68 public void testSetButtonVisibility_stateOff_onlyShowButtonOff() { 69 mPreference.setChecked(false /* stateOn */); 70 71 assertThat(mButtonOn.getVisibility()).isEqualTo(View.VISIBLE); 72 assertThat(mButtonOff.getVisibility()).isEqualTo(View.GONE); 73 } 74 75 @Test testSetButtonEnabled_enabled_buttonEnabled()76 public void testSetButtonEnabled_enabled_buttonEnabled() { 77 mPreference.setButtonEnabled(true /* enabled */); 78 79 assertThat(mButtonOn.isEnabled()).isTrue(); 80 assertThat(mButtonOff.isEnabled()).isTrue(); 81 } 82 83 @Test testSetButtonEnabled_disabled_buttonDisabled()84 public void testSetButtonEnabled_disabled_buttonDisabled() { 85 mPreference.setButtonEnabled(false /* enabled */); 86 87 assertThat(mButtonOn.isEnabled()).isFalse(); 88 assertThat(mButtonOff.isEnabled()).isFalse(); 89 } 90 91 @Test onClick_shouldPropagateChangeToListener()92 public void onClick_shouldPropagateChangeToListener() { 93 mPreference.onClick(mButtonOn); 94 verify(mPreference).callChangeListener(true); 95 96 mPreference.onClick(mButtonOff); 97 verify(mPreference).callChangeListener(false); 98 } 99 } 100