• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
41     private static final int FHD_WIDTH = 1080;
42     private static final int QHD_WIDTH = 1440;
43 
44     @Before
45     @UiThreadTest
setup()46     public void setup() {
47         mContext = spy(ApplicationProvider.getApplicationContext());
48         mFragment = spy(new ScreenResolutionFragment());
49     }
50 
51     @Test
52     @UiThreadTest
getDefaultKey_FHD()53     public void getDefaultKey_FHD() {
54         Display.Mode mode = new Display.Mode(0, FHD_WIDTH, 0, 0);
55         doReturn(mode).when(mFragment).getDisplayMode();
56         doReturn(mContext).when(mFragment).getContext();
57 
58         mFragment.onAttach(mContext);
59         assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(FHD_WIDTH));
60     }
61 
62     @Test
63     @UiThreadTest
getDefaultKey_QHD()64     public void getDefaultKey_QHD() {
65         Display.Mode mode = new Display.Mode(0, QHD_WIDTH, 0, 0);
66         doReturn(mode).when(mFragment).getDisplayMode();
67         doReturn(mContext).when(mFragment).getContext();
68 
69         mFragment.onAttach(mContext);
70         assertThat(mFragment.getDefaultKey()).isEqualTo(mFragment.getKeyForResolution(QHD_WIDTH));
71     }
72 
73     @Test
74     @UiThreadTest
setDefaultKey_FHD()75     public void setDefaultKey_FHD() {
76         doReturn(mContext).when(mFragment).getContext();
77         mFragment.onAttach(mContext);
78 
79         mFragment.setDefaultKey(mFragment.getKeyForResolution(FHD_WIDTH));
80 
81         verify(mFragment).setDisplayMode(FHD_WIDTH);
82     }
83 
84     @Test
85     @UiThreadTest
setDefaultKey_QHD()86     public void setDefaultKey_QHD() {
87         doReturn(mContext).when(mFragment).getContext();
88         mFragment.onAttach(mContext);
89 
90         mFragment.setDefaultKey(mFragment.getKeyForResolution(QHD_WIDTH));
91 
92         verify(mFragment).setDisplayMode(QHD_WIDTH);
93     }
94 
95     @Test
96     @UiThreadTest
bindPreferenceExtra_setSummary()97     public void bindPreferenceExtra_setSummary() {
98         doReturn(mContext).when(mFragment).getContext();
99         mFragment.onAttach(mContext);
100         SelectorWithWidgetPreference preference = new SelectorWithWidgetPreference(mContext);
101         ScreenResolutionFragment.ScreenResolutionCandidateInfo candidates =
102                 mock(ScreenResolutionFragment.ScreenResolutionCandidateInfo.class);
103         CharSequence summary = "test summary";
104         doReturn(summary).when(candidates).loadSummary();
105 
106         mFragment.bindPreferenceExtra(preference, "com.example.test", candidates, null, null);
107 
108         assertThat(preference.getSummary().toString().contentEquals(summary)).isTrue();
109     }
110 }
111