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