1 /* 2 * Copyright (C) 2016 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 package com.android.settings.deviceinfo; 17 18 import android.content.Context; 19 import android.os.Build; 20 import android.os.UserManager; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 import com.android.settings.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 41 import static org.mockito.Matchers.any; 42 import static org.mockito.Mockito.mock; 43 import static org.mockito.Mockito.verify; 44 import static org.mockito.Mockito.when; 45 46 @RunWith(SettingsRobolectricTestRunner.class) 47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 48 public class SystemUpdatePreferenceControllerTest { 49 50 @Mock(answer = RETURNS_DEEP_STUBS) 51 private Context mContext; 52 @Mock 53 private UserManager mUserManager; 54 @Mock(answer = RETURNS_DEEP_STUBS) 55 private PreferenceScreen mScreen; 56 57 private SystemUpdatePreferenceController mController; 58 59 @Before 60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mController = new SystemUpdatePreferenceController(mContext, mUserManager); 63 } 64 65 @Test 66 public void updateNonIndexable_bothAvailable_shouldNotUpdate() { 67 final List<String> keys = new ArrayList<>(); 68 when(mUserManager.isAdminUser()).thenReturn(true); 69 when(mContext.getResources().getBoolean( 70 R.bool.config_additional_system_update_setting_enable)) 71 .thenReturn(true); 72 73 mController.updateNonIndexableKeys(keys); 74 75 assertThat(keys).isEmpty(); 76 } 77 78 @Test 79 public void updateNonIndexable_nothingAvailable_shouldUpdateWith2Prefs() { 80 final List<String> keys = new ArrayList<>(); 81 82 mController.updateNonIndexableKeys(keys); 83 84 assertThat(keys.size()).isEqualTo(1); 85 } 86 87 @Test 88 public void displayPrefs_nothingAvailable_shouldNotDisplay() { 89 final Preference preference = mock(Preference.class); 90 when(mScreen.getPreferenceCount()).thenReturn(1); 91 when(mScreen.getPreference(0)).thenReturn(preference); 92 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 93 94 mController.displayPreference(mScreen); 95 96 verify(mScreen).removePreference(any(Preference.class)); 97 } 98 99 @Test 100 public void updateState_shouldSetToAndroidVersion() { 101 final Preference preference = new Preference(RuntimeEnvironment.application); 102 mController = new SystemUpdatePreferenceController( 103 RuntimeEnvironment.application, mUserManager); 104 mController.updateState(preference); 105 106 assertThat(preference.getSummary()) 107 .isEqualTo(RuntimeEnvironment.application.getString(R.string.about_summary, 108 Build.VERSION.RELEASE)); 109 } 110 111 @Test 112 public void displayPrefs_oneAvailable_shouldDisplayOne() { 113 final Preference preference = mock(Preference.class); 114 when(mScreen.getPreferenceCount()).thenReturn(1); 115 when(mScreen.getPreference(0)).thenReturn(preference); 116 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 117 118 when(mContext.getResources().getBoolean( 119 R.bool.config_additional_system_update_setting_enable)) 120 .thenReturn(true); 121 122 mController.displayPreference(mScreen); 123 124 verify(mScreen).removePreference(any(Preference.class)); 125 } 126 } 127