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.ArgumentMatchers.any; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.Activity; 28 import android.content.Context; 29 import android.os.UserManager; 30 31 import androidx.preference.PreferenceScreen; 32 33 import com.android.settings.testutils.FakeFeatureFactory; 34 import com.android.settingslib.applications.DefaultAppInfo; 35 import com.android.settingslib.widget.RadioButtonPreference; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Answers; 41 import org.mockito.InOrder; 42 import org.mockito.Mock; 43 import org.mockito.Mockito; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 48 import java.util.ArrayList; 49 import java.util.List; 50 51 @RunWith(RobolectricTestRunner.class) 52 public class RadioButtonPickerFragmentTest { 53 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private Activity mActivity; 56 @Mock 57 private PreferenceScreen mScreen; 58 @Mock 59 private UserManager mUserManager; 60 61 private TestFragment mFragment; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 FakeFeatureFactory.setupForTest(); 67 mFragment = spy(new TestFragment()); 68 69 when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 70 doReturn(mActivity).when(mFragment).getContext(); 71 doReturn(mScreen).when(mFragment).getPreferenceScreen(); 72 } 73 74 @Test onAttach_userIsInitialized()75 public void onAttach_userIsInitialized() { 76 mFragment.onAttach((Context) mActivity); 77 78 verify(mActivity).getSystemService(Context.USER_SERVICE); 79 } 80 81 @Test displaySingleOption_shouldSelectRadioButton()82 public void displaySingleOption_shouldSelectRadioButton() { 83 final RadioButtonPreference pref = 84 new RadioButtonPreference(RuntimeEnvironment.application); 85 when(mScreen.getPreferenceCount()).thenReturn(1); 86 when(mScreen.getPreference(0)).thenReturn(pref); 87 88 mFragment.mayCheckOnlyRadioButton(); 89 90 assertThat(pref.isChecked()).isTrue(); 91 } 92 93 @Test clickPreference_shouldConfirm()94 public void clickPreference_shouldConfirm() { 95 final RadioButtonPreference pref = 96 new RadioButtonPreference(RuntimeEnvironment.application); 97 pref.setKey("TEST"); 98 99 mFragment.onRadioButtonClicked(pref); 100 101 assertThat(mFragment.setDefaultKeyCalled).isTrue(); 102 } 103 104 @Test staticPreferencesPrepended_addedFirst()105 public void staticPreferencesPrepended_addedFirst() { 106 mFragment.mAppendStaticPreferences = false; 107 mFragment.updateCandidates(); 108 109 InOrder inOrder = Mockito.inOrder(mFragment); 110 inOrder.verify(mFragment).addStaticPreferences(any()); 111 inOrder.verify(mFragment).getRadioButtonPreferenceCustomLayoutResId(); 112 } 113 114 @Test staticPreferencesAppended_addedLast()115 public void staticPreferencesAppended_addedLast() { 116 mFragment.mAppendStaticPreferences = true; 117 mFragment.updateCandidates(); 118 119 InOrder inOrder = Mockito.inOrder(mFragment); 120 inOrder.verify(mFragment).mayCheckOnlyRadioButton(); 121 inOrder.verify(mFragment).addStaticPreferences(any()); 122 } 123 124 @Test shouldHaveNoCustomPreferenceLayout()125 public void shouldHaveNoCustomPreferenceLayout() { 126 assertThat(mFragment.getRadioButtonPreferenceCustomLayoutResId()).isEqualTo(0); 127 } 128 129 public static class TestFragment extends RadioButtonPickerFragment { 130 131 boolean setDefaultKeyCalled; 132 133 @Override getMetricsCategory()134 public int getMetricsCategory() { 135 return 0; 136 } 137 138 @Override getPreferenceScreenResId()139 protected int getPreferenceScreenResId() { 140 return 0; 141 } 142 143 @Override getCandidates()144 protected List<DefaultAppInfo> getCandidates() { 145 return new ArrayList<>(); 146 } 147 148 @Override getDefaultKey()149 protected String getDefaultKey() { 150 return null; 151 } 152 153 @Override setDefaultKey(String key)154 protected boolean setDefaultKey(String key) { 155 setDefaultKeyCalled = true; 156 return true; 157 } 158 159 @Override getContext()160 public Context getContext() { 161 return RuntimeEnvironment.application; 162 } 163 } 164 } 165