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 
17 package com.android.settings.deviceinfo;
18 
19 import android.content.Context;
20 import android.support.v7.preference.Preference;
21 import android.support.v7.preference.PreferenceScreen;
22 
23 import com.android.settings.SettingsRobolectricTestRunner;
24 import com.android.settings.TestConfig;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.robolectric.annotation.Config;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Matchers.anyString;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41 
42 @RunWith(SettingsRobolectricTestRunner.class)
43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
44 public class SerialNumberPreferenceControllerTest {
45 
46     @Mock(answer = RETURNS_DEEP_STUBS)
47     private Context mContext;
48     @Mock(answer = RETURNS_DEEP_STUBS)
49     private PreferenceScreen mScreen;
50 
51     private SerialNumberPreferenceController mController;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56     }
57 
58     @Test
testIsAvaiable_noSerial_shouldReturnFalse()59     public void testIsAvaiable_noSerial_shouldReturnFalse() {
60         mController = new SerialNumberPreferenceController(mContext, null);
61 
62         assertThat(mController.isAvailable()).isFalse();
63     }
64 
65     @Test
testIsAvaiable_hasSerial_shouldReturnTrue()66     public void testIsAvaiable_hasSerial_shouldReturnTrue() {
67         mController = new SerialNumberPreferenceController(mContext, "123");
68 
69         assertThat(mController.isAvailable()).isTrue();
70     }
71 
72     @Test
testDisplay_noSerial_shouldHidePreference()73     public void testDisplay_noSerial_shouldHidePreference() {
74         final Preference preference = mock(Preference.class);
75         when(mScreen.getPreferenceCount()).thenReturn(1);
76         when(mScreen.getPreference(0)).thenReturn(preference);
77         mController = new SerialNumberPreferenceController(mContext, null);
78         when(preference.getKey()).thenReturn(mController.getPreferenceKey());
79 
80         mController.displayPreference(mScreen);
81 
82         verify(mScreen).removePreference(any(Preference.class));
83     }
84 
85     @Test
testDisplay_hasSerial_shouldSummary()86     public void testDisplay_hasSerial_shouldSummary() {
87         final String serial = "123";
88         final Preference preference = mock(Preference.class);
89         when(mScreen.findPreference(anyString())).thenReturn(preference);
90 
91         mController = new SerialNumberPreferenceController(mContext, serial);
92         mController.displayPreference(mScreen);
93 
94         verify(mScreen, never()).removePreference(any(Preference.class));
95         verify(preference).setSummary(serial);
96     }
97 }
98