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