2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.system;
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 import static com.google.common.truth.Truth.assertThat;
24 import static org.mockito.Mockito.when;
26 import android.content.Context;
27 import android.os.Build;
28 import android.os.Bundle;
29 import android.os.SystemUpdateManager;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.PreferenceScreen;
33 import com.android.settings.R;
34 import com.android.settings.TestConfig;
35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
36 import com.android.settings.testutils.shadow.ShadowUserManager;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 import org.robolectric.shadows.ShadowApplication;
47 import java.util.ArrayList;
48 import java.util.List;
50 @RunWith(SettingsRobolectricTestRunner.class)
52 manifest = TestConfig.MANIFEST_PATH,
53 sdk = TestConfig.SDK_VERSION,
55 ShadowUserManager.class
57 public class SystemUpdatePreferenceControllerTest {
60 private PreferenceScreen mScreen;
62 private SystemUpdateManager mSystemUpdateManager;
64 private Context mContext;
65 private SystemUpdatePreferenceController mController;
66 private Preference mPreference;
70 MockitoAnnotations.initMocks(this);
71 mContext = RuntimeEnvironment.application;
72 ShadowApplication.getInstance().setSystemService(Context.SYSTEM_UPDATE_SERVICE,
73 mSystemUpdateManager);
74 mController = new SystemUpdatePreferenceController(mContext);
75 mPreference = new Preference(RuntimeEnvironment.application);
76 mPreference.setKey(mController.getPreferenceKey());
77 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
81 public void updateNonIndexable_ifAvailable_shouldNotUpdate() {
82 final List<String> keys = new ArrayList<>();
83 ShadowUserManager.getShadow().setIsAdminUser(true);
85 mController.updateNonIndexableKeys(keys);
87 assertThat(keys).isEmpty();
91 public void updateNonIndexable_ifNotAvailable_shouldUpdate() {
92 ShadowUserManager.getShadow().setIsAdminUser(false);
93 final List<String> keys = new ArrayList<>();
95 mController.updateNonIndexableKeys(keys);
97 assertThat(keys).hasSize(1);
101 public void displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
102 ShadowUserManager.getShadow().setIsAdminUser(false);
103 mController.displayPreference(mScreen);
105 assertThat(mPreference.isVisible()).isFalse();
109 @Config(qualifiers = "mcc999")
110 public void displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
111 ShadowUserManager.getShadow().setIsAdminUser(true);
112 mController.displayPreference(mScreen);
114 assertThat(mPreference.isVisible()).isFalse();
118 public void displayPrefs_ifAvailable_shouldDisplay() {
119 ShadowUserManager.getShadow().setIsAdminUser(true);
121 mController.displayPreference(mScreen);
123 assertThat(mPreference.isVisible()).isTrue();
127 public void updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
128 final Bundle bundle = new Bundle();
129 bundle.putInt(KEY_STATUS, STATUS_UNKNOWN);
130 when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
132 mController.updateState(mPreference);
134 assertThat(mPreference.getSummary()).isEqualTo(
135 mContext.getString(R.string.android_version_summary, Build.VERSION.RELEASE));
139 public void updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
140 final String testReleaseName = "ANDROID TEST VERSION";
142 final Bundle bundle = new Bundle();
143 bundle.putInt(KEY_STATUS, STATUS_IDLE);
144 bundle.putString(KEY_TITLE, testReleaseName);
145 when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
147 mController.updateState(mPreference);
149 assertThat(mPreference.getSummary()).isEqualTo(
150 mContext.getString(R.string.android_version_summary, testReleaseName));
154 public void updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
155 final Bundle bundle = new Bundle();
156 bundle.putInt(KEY_STATUS, STATUS_WAITING_DOWNLOAD);
157 when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
159 mController.updateState(mPreference);
161 assertThat(mPreference.getSummary()).isEqualTo(
162 mContext.getString(R.string.android_version_pending_update_summary));