Move SystemUpdateManager.retrieveSystemUpdateInfo

Into Kotlin Coroutine for true async.

Bug: 300851543
Test: manual - on system page
Test: unit test
Change-Id: Ibec5c9d0934d71ed1a5808cadf3b3542eb3d5fa0
This commit is contained in:
Chaohui Wang
2023-09-19 12:28:54 +08:00
parent 47a587ae36
commit cda836fff7
8 changed files with 401 additions and 333 deletions

View File

@@ -53,7 +53,6 @@
<bool name="config_show_pointer_speed">false</bool>
<bool name="config_show_vibrate_input_devices">false</bool>
<bool name="config_show_reset_dashboard">false</bool>
<bool name="config_show_system_update_settings">false</bool>
<bool name="config_show_device_model">false</bool>
<bool name="config_show_top_level_accessibility">false</bool>
<bool name="config_show_top_level_battery">false</bool>

View File

@@ -1,171 +0,0 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system;
import static android.os.SystemUpdateManager.KEY_STATUS;
import static android.os.SystemUpdateManager.KEY_TITLE;
import static android.os.SystemUpdateManager.STATUS_IDLE;
import static android.os.SystemUpdateManager.STATUS_UNKNOWN;
import static android.os.SystemUpdateManager.STATUS_WAITING_DOWNLOAD;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemUpdateManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUserManager.class)
public class SystemUpdatePreferenceControllerTest {
@Mock
private PreferenceScreen mScreen;
@Mock
private SystemUpdateManager mSystemUpdateManager;
private Context mContext;
private ShadowUserManager mShadowUserManager;
private SystemUpdatePreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mShadowUserManager = ShadowUserManager.getShadow();
ShadowApplication.getInstance().setSystemService(Context.SYSTEM_UPDATE_SERVICE,
mSystemUpdateManager);
mController = new SystemUpdatePreferenceController(mContext);
mPreference = new Preference(RuntimeEnvironment.application);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@After
public void cleanUp() {
mShadowUserManager.setIsAdminUser(false);
}
@Test
public void updateNonIndexable_ifAvailable_shouldNotUpdate() {
final List<String> keys = new ArrayList<>();
mShadowUserManager.setIsAdminUser(true);
mController.updateNonIndexableKeys(keys);
assertThat(keys).isEmpty();
}
@Test
public void updateNonIndexable_ifNotAvailable_shouldUpdate() {
mShadowUserManager.setIsAdminUser(false);
final List<String> keys = new ArrayList<>();
mController.updateNonIndexableKeys(keys);
assertThat(keys).hasSize(1);
}
@Test
public void displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
mShadowUserManager.setIsAdminUser(false);
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isFalse();
}
@Test
@Config(qualifiers = "mcc999")
public void displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
mShadowUserManager.setIsAdminUser(true);
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isFalse();
}
@Test
public void displayPrefs_ifAvailable_shouldDisplay() {
mShadowUserManager.setIsAdminUser(true);
mController.displayPreference(mScreen);
assertThat(mPreference.isVisible()).isTrue();
}
@Test
public void updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
final Bundle bundle = new Bundle();
bundle.putInt(KEY_STATUS, STATUS_UNKNOWN);
when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.android_version_summary,
Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY));
}
@Test
public void updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
final String testReleaseName = "ANDROID TEST VERSION";
final Bundle bundle = new Bundle();
bundle.putInt(KEY_STATUS, STATUS_IDLE);
bundle.putString(KEY_TITLE, testReleaseName);
when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.android_version_summary, testReleaseName));
}
@Test
public void updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
final Bundle bundle = new Bundle();
bundle.putInt(KEY_STATUS, STATUS_WAITING_DOWNLOAD);
when(mSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle);
mController.updateState(mPreference);
assertThat(mPreference.getSummary())
.isEqualTo(mContext.getString(R.string.android_version_pending_update_summary));
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.os.Bundle
import android.os.SystemUpdateManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class SystemUpdateManagerExtKtTest {
private val mockSystemUpdateManager = mock<SystemUpdateManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { getSystemService(SystemUpdateManager::class.java) } doReturn mockSystemUpdateManager
}
@Test
fun getSystemUpdateInfo() = runTest {
val bundle = Bundle()
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
val info = context.getSystemUpdateInfo()
assertThat(info).isSameInstanceAs(bundle)
}
}

View File

@@ -0,0 +1,168 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.system
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.SystemClock
import android.os.SystemUpdateManager
import android.os.UserManager
import androidx.lifecycle.testing.TestLifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settingslib.spaprivileged.framework.common.userManager
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class SystemUpdatePreferenceControllerTest {
private val mockUserManager = mock<UserManager>()
private val mockSystemUpdateManager = mock<SystemUpdateManager>()
private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
on { userManager } doReturn mockUserManager
on { getSystemService(SystemUpdateManager::class.java) } doReturn mockSystemUpdateManager
}
private val resources = spy(context.resources) {
on { getBoolean(R.bool.config_show_system_update_settings) } doReturn true
}
private val preference = Preference(context).apply { key = KEY }
private val preferenceScreen = mock<PreferenceScreen> {
onGeneric { findPreference(KEY) } doReturn preference
}
private val controller = SystemUpdatePreferenceController(context, KEY)
@Before
fun setUp() {
whenever(context.resources).thenReturn(resources)
}
@Test
fun updateNonIndexable_ifAvailable_shouldNotUpdate() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
val keys = mutableListOf<String>()
controller.updateNonIndexableKeys(keys)
assertThat(keys).isEmpty()
}
@Test
fun updateNonIndexable_ifNotAvailable_shouldUpdate() {
whenever(mockUserManager.isAdminUser).thenReturn(false)
val keys = mutableListOf<String>()
controller.updateNonIndexableKeys(keys)
assertThat(keys).containsExactly(KEY)
}
@Test
fun displayPrefs_ifVisible_butNotAdminUser_shouldNotDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(false)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isFalse()
}
@Test
fun displayPrefs_ifAdminUser_butNotVisible_shouldNotDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
whenever(resources.getBoolean(R.bool.config_show_system_update_settings)).thenReturn(false)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isFalse()
}
@Test
fun displayPrefs_ifAvailable_shouldDisplay() {
whenever(mockUserManager.isAdminUser).thenReturn(true)
controller.displayPreference(preferenceScreen)
assertThat(preference.isVisible).isTrue()
}
@Test
fun updateState_systemUpdateStatusUnknown_shouldSetToAndroidVersion() {
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_UNKNOWN)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary).isEqualTo(
context.getString(
R.string.android_version_summary,
Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY,
)
)
}
@Test
fun updateState_systemUpdateStatusIdle_shouldSetToAndroidVersion() {
val testReleaseName = "ANDROID TEST VERSION"
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_IDLE)
putString(SystemUpdateManager.KEY_TITLE, testReleaseName)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary)
.isEqualTo(context.getString(R.string.android_version_summary, testReleaseName))
}
@Test
fun updateState_systemUpdateInProgress_shouldSetToUpdatePending() {
val bundle = Bundle().apply {
putInt(SystemUpdateManager.KEY_STATUS, SystemUpdateManager.STATUS_WAITING_DOWNLOAD)
}
whenever(mockSystemUpdateManager.retrieveSystemUpdateInfo()).thenReturn(bundle)
controller.displayPreference(preferenceScreen)
controller.onViewCreated(TestLifecycleOwner())
SystemClock.sleep(100)
assertThat(preference.summary)
.isEqualTo(context.getString(R.string.android_version_pending_update_summary))
}
private companion object {
const val KEY = "test_key"
}
}