1 /*
2  * Copyright (C) 2022 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.display;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 
25 import android.content.Context;
26 import android.view.Display;
27 
28 import androidx.test.annotation.UiThreadTest;
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.settingslib.widget.SelectorWithWidgetPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 
36 public class ScreenResolutionFragmentTest {
37 
38     private Context mContext;
39     private ScreenResolutionFragment mFragment;
40     private ScreenResolutionController mController;
41     private int mHighWidth;
42     private int mFullWidth;
43 
44     @Before
45     @UiThreadTest
setup()46     public void setup() {
47         mContext = spy(ApplicationProvider.getApplicationContext());
48         mFragment = spy(new ScreenResolutionFragment());
49         mController = spy(new ScreenResolutionController(mContext, "test"));
50         mHighWidth = mController.getHighWidth();
51         mFullWidth = mController.getFullWidth();
52     }
53 
54     @Test
55     @UiThreadTest
getDefaultKey_highResolution()56     public void getDefaultKey_highResolution() {
57         Display.Mode mode = new Display.Mode(0, mHighWidth, 0, 0);
58         doReturn(mode).when(mFragment).getDisplayMode();
59         doReturn(mContext).when(mFragment).getContext();
60 
61         mFragment.onAttach(mContext);
62         assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(mHighWidth));
63     }
64 
65     @Test
66     @UiThreadTest
getDefaultKey_fullResolution()67     public void getDefaultKey_fullResolution() {
68         Display.Mode mode = new Display.Mode(0, mFullWidth, 0, 0);
69         doReturn(mode).when(mFragment).getDisplayMode();
70         doReturn(mContext).when(mFragment).getContext();
71 
72         mFragment.onAttach(mContext);
73         assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(mFullWidth));
74     }
75 
76     @Test
77     @UiThreadTest
setDefaultKey_highResolution()78     public void setDefaultKey_highResolution() {
79         doReturn(mContext).when(mFragment).getContext();
80         mFragment.onAttach(mContext);
81 
82         mFragment.setDefaultKey(mFragment.getKeyForResolution(mHighWidth));
83 
84         verify(mFragment).setDisplayMode(mHighWidth);
85     }
86 
87     @Test
88     @UiThreadTest
setDefaultKey_fullResolution()89     public void setDefaultKey_fullResolution() {
90         doReturn(mContext).when(mFragment).getContext();
91         mFragment.onAttach(mContext);
92 
93         mFragment.setDefaultKey(mFragment.getKeyForResolution(mFullWidth));
94 
95         verify(mFragment).setDisplayMode(mFullWidth);
96     }
97 
98     @Test
99     @UiThreadTest
bindPreferenceExtra_setSummary()100     public void bindPreferenceExtra_setSummary() {
101         doReturn(mContext).when(mFragment).getContext();
102         mFragment.onAttach(mContext);
103         SelectorWithWidgetPreference preference = new SelectorWithWidgetPreference(mContext);
104         ScreenResolutionFragment.ScreenResolutionCandidateInfo candidates =
105                 mock(ScreenResolutionFragment.ScreenResolutionCandidateInfo.class);
106         CharSequence summary = "test summary";
107         doReturn(summary).when(candidates).loadSummary();
108 
109         mFragment.bindPreferenceExtra(preference, "com.example.test", candidates, null, null);
110 
111         assertThat(preference.getSummary().toString().contentEquals(summary)).isTrue();
112     }
113 }
114