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.display;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.support.v7.preference.ListPreference;
24 import com.android.settings.R;
25 import com.android.settings.SettingsRobolectricTestRunner;
26 import com.android.settings.TestConfig;
27 import com.android.settings.display.ThemePreferenceController.OverlayManager;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.annotation.Config;
35 
36 import static org.mockito.Matchers.any;
37 import static org.mockito.Matchers.anyInt;
38 import static org.mockito.Mockito.doReturn;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.spy;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
43 
44 @RunWith(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public class ThemePreferenceControllerTest {
47 
48     @Mock
49     private ListPreference mPreference;
50     @Mock
51     private Context mContext;
52     @Mock
53     private PackageManager mPackageManager;
54     @Mock
55     private ApplicationInfo mApplicationInfo;
56 
57     private ThemePreferenceController mController;
58 
59     @Before
setUp()60     public void setUp() throws NameNotFoundException {
61         MockitoAnnotations.initMocks(this);
62         when(mPackageManager.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo);
63         when(mContext.getPackageManager()).thenReturn(mPackageManager);
64         mController = spy(new ThemePreferenceController(mContext, mock(OverlayManager.class)));
65     }
66 
67     @Test
updateState_themeSet_shouldSetPreferenceValue()68     public void updateState_themeSet_shouldSetPreferenceValue() {
69         final String[] themes = {"Theme1", "Theme2"};
70         doReturn("Theme1").when(mController).getCurrentTheme();
71         doReturn(themes).when(mController).getAvailableThemes();
72 
73         mController.updateState(mPreference);
74 
75         verify(mPreference).setValue("Theme1");
76     }
77 }
78