1 /* 2 * Copyright (C) 2018 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.settingslib.CustomDialogPreference.CustomPreferenceDialogFragment; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.never; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.times; 28 import static org.mockito.Mockito.verify; 29 import static org.mockito.Mockito.when; 30 31 import android.app.AlertDialog; 32 import android.content.Context; 33 import android.net.wifi.WifiConfiguration; 34 import android.os.Bundle; 35 import android.os.Parcelable; 36 import android.view.LayoutInflater; 37 import android.view.View; 38 import android.widget.Button; 39 import android.widget.CheckBox; 40 import android.widget.LinearLayout; 41 42 import com.android.settings.R; 43 import com.android.settings.testutils.SettingsRobolectricTestRunner; 44 45 import org.junit.Assert; 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.util.ReflectionHelpers; 51 52 import java.util.List; 53 54 @RunWith(SettingsRobolectricTestRunner.class) 55 public class HotspotApBandSelectionPreferenceTest { 56 private HotspotApBandSelectionPreference mPreference; 57 private Context mContext; 58 private Button mSaveButton; 59 private View mLayout; 60 61 @Before 62 public void setUp() { 63 mContext = RuntimeEnvironment.application; 64 mSaveButton = spy(new Button(mContext)); 65 66 final CustomPreferenceDialogFragment fragment = mock(CustomPreferenceDialogFragment.class); 67 final AlertDialog dialog = mock(AlertDialog.class); 68 when(fragment.getDialog()).thenReturn(dialog); 69 when(dialog.getButton(anyInt())).thenReturn(mSaveButton); 70 71 mPreference = new HotspotApBandSelectionPreference(mContext); 72 ReflectionHelpers.setField(mPreference, "mFragment", fragment); 73 74 final LayoutInflater inflater = LayoutInflater.from(mContext); 75 mLayout = inflater.inflate(R.layout.hotspot_ap_band_selection_dialog, 76 new LinearLayout(mContext), false); 77 } 78 79 @Test 80 public void getWifiBand_updatesBandPresetConfigProvided() { 81 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY); 82 mPreference.onBindDialogView(mLayout); 83 84 // check that the boxes are set correctly when a pre-existing config is set 85 assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_ANY); 86 } 87 88 @Test 89 public void getWifiBand_updatesBandWhenBoxesToggled() { 90 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY); 91 mPreference.onBindDialogView(mLayout); 92 93 assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_ANY); 94 95 // make sure we have the expected box then toggle it 96 mPreference.mBox2G.setChecked(false); 97 98 // check that band is updated 99 assertThat(mPreference.getWifiBand()).isEqualTo(WifiConfiguration.AP_BAND_5GHZ); 100 } 101 102 @Test 103 public void onSaveInstanceState_skipWhenDialogGone() { 104 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ); 105 mPreference.onBindDialogView(mLayout); 106 // remove the fragment to make the dialog unavailable 107 ReflectionHelpers.setField(mPreference, "mFragment", null); 108 109 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY); 110 mPreference.onBindDialogView(mLayout); 111 112 // state should only be saved when the dialog is available 113 Parcelable parcelable = mPreference.onSaveInstanceState(); 114 mPreference.onRestoreInstanceState(parcelable); 115 assertThat(mPreference.mShouldRestore).isFalse(); 116 } 117 118 @Test 119 public void onSaveInstanceState_doesNotCrashWhenViewGone() { 120 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ); 121 mPreference.onBindDialogView(mLayout); 122 // When the device dozes the view and dialog can become null 123 mPreference.mBox5G = null; 124 mPreference.mBox2G = null; 125 ReflectionHelpers.setField(mPreference, "mFragment", null); 126 127 // make sure it does not crash and state is not restored 128 Parcelable parcelable = mPreference.onSaveInstanceState(); 129 mPreference.onRestoreInstanceState(parcelable); 130 assertThat(mPreference.mShouldRestore).isFalse(); 131 } 132 133 @Test 134 public void onSaveInstanceState_presentWhenDialogPresent() { 135 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_2GHZ); 136 mPreference.onBindDialogView(mLayout); 137 138 Parcelable parcelable = mPreference.onSaveInstanceState(); 139 mPreference.onRestoreInstanceState(parcelable); 140 assertThat(mPreference.mShouldRestore).isTrue(); 141 } 142 143 @Test 144 public void positiveButton_updatedCorrectly() { 145 mPreference.setExistingConfigValue(WifiConfiguration.AP_BAND_ANY); 146 mPreference.onBindDialogView(mLayout); 147 148 // button is enabled whole time so far since we have a pre-existing selection 149 verify(mSaveButton, never()).setEnabled(false); 150 151 // clear all boxes and make sure it stays enabled until empty 152 mPreference.mBox2G.setChecked(false); 153 mPreference.mBox5G.setChecked(false); 154 155 // button should be disabled now 156 verify(mSaveButton, times(1)).setEnabled(false); 157 } 158 } 159