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.development;
18 
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.spy;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.pm.PackageManager;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.webview.WebViewUpdateServiceWrapper;
31 import com.android.settingslib.applications.DefaultAppInfo;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.util.ReflectionHelpers;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class WebViewAppPreferenceControllerTest {
44 
45     @Mock
46     private PreferenceScreen mPreferenceScreen;
47     @Mock
48     private PackageManager mPackageManager;
49     @Mock
50     private WebViewUpdateServiceWrapper mWebViewUpdateServiceWrapper;
51     @Mock
52     private Preference mPreference;
53     @Mock
54     private DefaultAppInfo mAppInfo;
55 
56     private WebViewAppPreferenceController mController;
57 
58     @Before
setup()59     public void setup() {
60         MockitoAnnotations.initMocks(this);
61         mController = spy(new WebViewAppPreferenceController(RuntimeEnvironment.application));
62         ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
63         ReflectionHelpers
64             .setField(mController, "mWebViewUpdateServiceWrapper", mWebViewUpdateServiceWrapper);
65         doReturn(mAppInfo).when(mController).getDefaultAppInfo();
66         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
67             .thenReturn(mPreference);
68         mController.displayPreference(mPreferenceScreen);
69     }
70 
71     @Test
updateState_hasAppLabel_shouldSetAppLabelAndIcon()72     public void updateState_hasAppLabel_shouldSetAppLabelAndIcon() {
73         when(mAppInfo.loadLabel()).thenReturn("SomeRandomAppLabel!!!");
74 
75         mController.updateState(mPreference);
76 
77         verify(mPreference).setSummary("SomeRandomAppLabel!!!");
78     }
79 
80     @Test
updateState_noAppLabel_shouldSetAppDefaultLabelAndNullIcon()81     public void updateState_noAppLabel_shouldSetAppDefaultLabelAndNullIcon() {
82         when(mAppInfo.loadLabel()).thenReturn(null);
83 
84         mController.updateState(mPreference);
85 
86         verify(mPreference).setSummary(R.string.app_list_preference_none);
87     }
88 }
89