1 /* 2 * Copyright (C) 2017 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.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 24 import android.content.Context; 25 import android.view.LayoutInflater; 26 import android.widget.LinearLayout; 27 import android.widget.Switch; 28 29 import androidx.preference.Preference.OnPreferenceChangeListener; 30 import androidx.preference.PreferenceViewHolder; 31 32 import com.android.settings.R; 33 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class MasterSwitchPreferenceTest { 43 44 private Context mContext; 45 private MasterSwitchPreference mPreference; 46 private PreferenceViewHolder mHolder; 47 private LinearLayout mWidgetView; 48 49 @Before setUp()50 public void setUp() { 51 mContext = RuntimeEnvironment.application; 52 mPreference = new MasterSwitchPreference(mContext); 53 LayoutInflater inflater = LayoutInflater.from(mContext); 54 mHolder = PreferenceViewHolder.createInstanceForTests(inflater.inflate( 55 com.android.settingslib.R.layout.preference_two_target, null)); 56 mWidgetView = mHolder.itemView.findViewById(android.R.id.widget_frame); 57 inflater.inflate(R.layout.restricted_preference_widget_master_switch, mWidgetView, true); 58 } 59 60 @Test createNewPreference_shouldSetLayout()61 public void createNewPreference_shouldSetLayout() { 62 assertThat(mPreference.getWidgetLayoutResource()) 63 .isEqualTo(R.layout.restricted_preference_widget_master_switch); 64 } 65 66 @Test setChecked_shouldUpdateButtonCheckedState()67 public void setChecked_shouldUpdateButtonCheckedState() { 68 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 69 mPreference.onBindViewHolder(mHolder); 70 71 mPreference.setChecked(true); 72 assertThat(toggle.isChecked()).isTrue(); 73 74 mPreference.setChecked(false); 75 assertThat(toggle.isChecked()).isFalse(); 76 } 77 78 @Test setSwitchEnabled_shouldUpdateButtonEnabledState()79 public void setSwitchEnabled_shouldUpdateButtonEnabledState() { 80 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 81 mPreference.onBindViewHolder(mHolder); 82 83 mPreference.setSwitchEnabled(true); 84 assertThat(toggle.isEnabled()).isTrue(); 85 86 mPreference.setSwitchEnabled(false); 87 assertThat(toggle.isEnabled()).isFalse(); 88 } 89 90 @Test setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound()91 public void setSwitchEnabled_shouldUpdateButtonEnabledState_beforeViewBound() { 92 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 93 94 mPreference.setSwitchEnabled(false); 95 mPreference.onBindViewHolder(mHolder); 96 assertThat(toggle.isEnabled()).isFalse(); 97 } 98 99 @Test clickWidgetView_shouldToggleButton()100 public void clickWidgetView_shouldToggleButton() { 101 assertThat(mWidgetView).isNotNull(); 102 103 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 104 mPreference.onBindViewHolder(mHolder); 105 106 toggle.performClick(); 107 assertThat(toggle.isChecked()).isTrue(); 108 109 toggle.performClick(); 110 assertThat(toggle.isChecked()).isFalse(); 111 } 112 113 @Test clickWidgetView_shouldNotToggleButtonIfDisabled()114 public void clickWidgetView_shouldNotToggleButtonIfDisabled() { 115 assertThat(mWidgetView).isNotNull(); 116 117 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 118 mPreference.onBindViewHolder(mHolder); 119 toggle.setEnabled(false); 120 121 mWidgetView.performClick(); 122 assertThat(toggle.isChecked()).isFalse(); 123 } 124 125 @Test clickWidgetView_shouldNotifyPreferenceChanged()126 public void clickWidgetView_shouldNotifyPreferenceChanged() { 127 128 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 129 130 final OnPreferenceChangeListener listener = mock(OnPreferenceChangeListener.class); 131 mPreference.setOnPreferenceChangeListener(listener); 132 mPreference.onBindViewHolder(mHolder); 133 134 mPreference.setChecked(false); 135 toggle.performClick(); 136 verify(listener).onPreferenceChange(mPreference, true); 137 138 mPreference.setChecked(true); 139 toggle.performClick(); 140 verify(listener).onPreferenceChange(mPreference, false); 141 } 142 143 @Test setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton()144 public void setDisabledByAdmin_hasEnforcedAdmin_shouldDisableButton() { 145 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 146 toggle.setEnabled(true); 147 mPreference.onBindViewHolder(mHolder); 148 149 mPreference.setDisabledByAdmin(mock(EnforcedAdmin.class)); 150 assertThat(toggle.isEnabled()).isFalse(); 151 } 152 153 @Test setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton()154 public void setDisabledByAdmin_noEnforcedAdmin_shouldEnableButton() { 155 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 156 toggle.setEnabled(false); 157 mPreference.onBindViewHolder(mHolder); 158 159 mPreference.setDisabledByAdmin(null); 160 assertThat(toggle.isEnabled()).isTrue(); 161 } 162 163 @Test onBindViewHolder_toggleButtonShouldHaveContentDescription()164 public void onBindViewHolder_toggleButtonShouldHaveContentDescription() { 165 final Switch toggle = (Switch) mHolder.findViewById(R.id.switchWidget); 166 final String label = "TestButton"; 167 mPreference.setTitle(label); 168 169 mPreference.onBindViewHolder(mHolder); 170 171 assertThat(toggle.getContentDescription()).isEqualTo(label); 172 } 173 } 174