• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.system;
17 
18 import static android.os.SystemUpdateManager.KEY_STATUS;
19 import static android.os.SystemUpdateManager.KEY_TITLE;
20 import static android.os.SystemUpdateManager.STATUS_IDLE;
21 import static android.os.SystemUpdateManager.STATUS_UNKNOWN;
22 import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.os.Build;
30 import android.os.Bundle;
31 import android.os.SystemUpdateManager;
32 
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceScreen;
35 
36 import com.android.settings.R;
37 import com.android.settings.testutils.shadow.ShadowUserManager;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.shadows.ShadowApplication;
50 
51 import java.util.ArrayList;
52 import java.util.List;
53 
54 @RunWith(RobolectricTestRunner.class)
55 @Config(shadows = ShadowUserManager.class)
56 public class SystemUpdatePreferenceControllerTest {
57 
58     @Mock
59     private PreferenceScreen mScreen;
60     @Mock
61     private SystemUpdateManager mSystemUpdateManager;
62 
63     private Context mContext;
64     private ShadowUserManager mShadowUserManager;
65     private SystemUpdatePreferenceController mController;
66     private Preference mPreference;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mContext = RuntimeEnvironment.application;
72         mShadowUserManager = ShadowUserManager.getShadow();
73 
74         ShadowApplication.getInstance().setSystemService(Context.SYSTEM_UPDATE_SERVICE,
75                 mSystemUpdateManager);
76         mController = new SystemUpdatePreferenceController(mContext);
77         mPreference = new Preference(RuntimeEnvironment.application);
78         mPreference.setKey(mController.getPreferenceKey());
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
80     }
81 
82     @After
cleanUp()83     public void cleanUp() {
84         mShadowUserManager.setIsAdminUser(false);
85     }
86 
87     @Test
updateNonIndexable_ifAvailable_shouldNotUpdate()88     public void updateNonIndexable_ifAvailable_shouldNotUpdate() {
89         final List<String> keys = new ArrayList<>();
90         mShadowUserManager.setIsAdminUser(true);
91 
92         mController.updateNonIndexableKeys(keys);
93 
94         assertThat(keys).isEmpty();
95     }
96 
97     @Test
updateNonIndexable_ifNotAvailable_shouldUpdate()98     public void updateNonIndexable_ifNotAvailable_shouldUpdate() {
99         mShadowUserManager.setIsAdminUser(false);
100         final List<String> keys = new ArrayList<>();
101 
102         mController.updateNonIndexableKeys(keys);
103 
104         assertThat(keys).hasSize(1);
105     }
106 
107     @Test
displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay()108     public void displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
109         mShadowUserManager.setIsAdminUser(false);
110         mController.displayPreference(mScreen);
111 
112         assertThat(mPreference.isVisible()).isFalse();
113     }
114 
115     @Ignore
116     @Test
117     @Config(qualifiers = "mcc999")
displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay()118     public void displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
119         mShadowUserManager.setIsAdminUser(true);
120         mController.displayPreference(mScreen);
121 
122         assertThat(mPreference.isVisible()).isFalse();
123     }
124 
125     @Test
displayPrefs_ifAvailable_shouldDisplay()126     public void displayPrefs_ifAvailable_shouldDisplay() {
127         mShadowUserManager.setIsAdminUser(true);
128 
129         mController.displayPreference(mScreen);
130 
131         assertThat(mPreference.isVisible()).isTrue();
132     }
133 
134     @Test
updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion()135     public void updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
136         final Bundle bundle = new Bundle();
137         bundle.putInt(KEY_STATUS, STATUS_UNKNOWN);
138         when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
139 
140         mController.updateState(mPreference);
141 
142         assertThat(mPreference.getSummary())
143                 .isEqualTo(mContext.getString(R.string.android_version_summary,
144                         Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY));
145     }
146 
147     @Test
updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion()148     public void updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
149         final String testReleaseName = "ANDROID TEST VERSION";
150 
151         final Bundle bundle = new Bundle();
152         bundle.putInt(KEY_STATUS, STATUS_IDLE);
153         bundle.putString(KEY_TITLE, testReleaseName);
154         when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
155 
156         mController.updateState(mPreference);
157 
158         assertThat(mPreference.getSummary())
159                 .isEqualTo(mContext.getString(R.string.android_version_summary, testReleaseName));
160     }
161 
162     @Test
updateState_systemUpdateInProgress_shouldSetToUpdatePending()163     public void updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
164         final Bundle bundle = new Bundle();
165         bundle.putInt(KEY_STATUS, STATUS_WAITING_DOWNLOAD);
166         when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
167 
168         mController.updateState(mPreference);
169 
170         assertThat(mPreference.getSummary())
171                 .isEqualTo(mContext.getString(R.string.android_version_pending_update_summary));
172     }
173 }
174