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.settingslib.deviceinfo; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import com.android.settingslib.SettingsLibRobolectricTestRunner; 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 35 @RunWith(SettingsLibRobolectricTestRunner.class) 36 public class SerialNumberPreferenceControllerTest { 37 38 @Mock 39 private PreferenceScreen mScreen; 40 41 private Context mContext; 42 private Preference mPreference; 43 private AbstractSerialNumberPreferenceController mController; 44 45 @Before 46 public void setUp() { 47 MockitoAnnotations.initMocks(this); 48 mContext = RuntimeEnvironment.application; 49 mPreference = new Preference(mContext); 50 mPreference.setKey(AbstractSerialNumberPreferenceController.KEY_SERIAL_NUMBER); 51 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 52 } 53 54 @Test 55 public void testIsAvaiable_noSerial_shouldReturnFalse() { 56 mController = new TestPreferenceController(mContext, null); 57 58 assertThat(mController.isAvailable()).isFalse(); 59 } 60 61 @Test 62 public void testIsAvailable_hasSerial_shouldReturnTrue() { 63 mController = new TestPreferenceController(mContext, "123"); 64 65 assertThat(mController.isAvailable()).isTrue(); 66 } 67 68 @Test 69 public void testDisplay_noSerial_shouldHidePreference() { 70 mController = new TestPreferenceController(mContext, null); 71 72 mController.displayPreference(mScreen); 73 74 assertThat(mPreference.isVisible()).isFalse(); 75 } 76 77 @Test 78 public void testDisplay_hasSerial_shouldSetSummary() { 79 final String serial = "123"; 80 81 mController = new TestPreferenceController(mContext, serial); 82 mController.displayPreference(mScreen); 83 84 assertThat(mPreference.isVisible()).isTrue(); 85 assertThat(mPreference.getSummary()).isEqualTo(serial); 86 } 87 88 private static class TestPreferenceController 89 extends AbstractSerialNumberPreferenceController { 90 91 TestPreferenceController(Context context, String serialNumber) { 92 super(context, serialNumber); 93 } 94 } 95 } 96