OSDN Git Service

Merge "Zen Condition text and primary click changes"
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / system / SystemUpdatePreferenceControllerTest.java
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 import static com.google.common.truth.Truth.assertThat;
24 import static org.mockito.Mockito.when;
25
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;
32
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;
37
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;
46
47 import java.util.ArrayList;
48 import java.util.List;
49
50 @RunWith(SettingsRobolectricTestRunner.class)
51 @Config(
52         manifest = TestConfig.MANIFEST_PATH,
53         sdk = TestConfig.SDK_VERSION,
54         shadows = {
55                 ShadowUserManager.class
56         })
57 public class SystemUpdatePreferenceControllerTest {
58
59     @Mock
60     private PreferenceScreen mScreen;
61     @Mock
62     private SystemUpdateManager mSystemUpdateManager;
63
64     private Context mContext;
65     private SystemUpdatePreferenceController mController;
66     private Preference mPreference;
67
68     @Before
69     public void setUp() {
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);
78     }
79
80     @Test
81     public void updateNonIndexable_ifAvailable_shouldNotUpdate() {
82         final List<String> keys = new ArrayList<>();
83         ShadowUserManager.getShadow().setIsAdminUser(true);
84
85         mController.updateNonIndexableKeys(keys);
86
87         assertThat(keys).isEmpty();
88     }
89
90     @Test
91     public void updateNonIndexable_ifNotAvailable_shouldUpdate() {
92         ShadowUserManager.getShadow().setIsAdminUser(false);
93         final List<String> keys = new ArrayList<>();
94
95         mController.updateNonIndexableKeys(keys);
96
97         assertThat(keys).hasSize(1);
98     }
99
100     @Test
101     public void displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
102         ShadowUserManager.getShadow().setIsAdminUser(false);
103         mController.displayPreference(mScreen);
104
105         assertThat(mPreference.isVisible()).isFalse();
106     }
107
108     @Test
109     @Config(qualifiers = "mcc999")
110     public void displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
111         ShadowUserManager.getShadow().setIsAdminUser(true);
112         mController.displayPreference(mScreen);
113
114         assertThat(mPreference.isVisible()).isFalse();
115     }
116
117     @Test
118     public void displayPrefs_ifAvailable_shouldDisplay() {
119         ShadowUserManager.getShadow().setIsAdminUser(true);
120
121         mController.displayPreference(mScreen);
122
123         assertThat(mPreference.isVisible()).isTrue();
124     }
125
126     @Test
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);
131
132         mController.updateState(mPreference);
133
134         assertThat(mPreference.getSummary()).isEqualTo(
135                 mContext.getString(R.string.android_version_summary, Build.VERSION.RELEASE));
136     }
137
138     @Test
139     public void updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
140         final String testReleaseName = "ANDROID TEST VERSION";
141
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);
146
147         mController.updateState(mPreference);
148
149         assertThat(mPreference.getSummary()).isEqualTo(
150                 mContext.getString(R.string.android_version_summary, testReleaseName));
151     }
152
153     @Test
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);
158
159         mController.updateState(mPreference);
160
161         assertThat(mPreference.getSummary()).isEqualTo(
162                 mContext.getString(R.string.android_version_pending_update_summary));
163     }
164 }