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