Fix crashes related to ExternalSourcesDetails.

- ExternalSourcesSettingsTest crashes due to the package moved of
ExternalSourcesDetails. Corresponding xml has been updated in previous
CLs. So, reverting the suppress tag for the test class.
- ExternalSourcesDetails can be launched directly with
MANAGE_UNKNOWN_APP_SOURCES intent, and client can pass in invalid
package while launching ExternalSourcesDetails. Need to check whether
the app info is invalid before we try to launch the fragment.
- also fix test failure for not founding the app list as list object
type and id had been changed.

Change-Id: Id7787ca889f770e10d7a8e9fbf8dc79c9d6e884d
Fixes: 70383636
Test: make RunSettingsRoboTests, make SettingsUnitTests
This commit is contained in:
Doris Ling
2017-12-11 17:32:03 -08:00
parent 30ba48c540
commit e29587e334
3 changed files with 103 additions and 4 deletions

View File

@@ -107,6 +107,9 @@ public class ExternalSourcesDetails extends AppInfoWithHeader
@Override
protected boolean refreshUi() {
if (mPackageInfo == null || mPackageInfo.applicationInfo == null) {
return false;
}
if (mUserManager.hasBaseUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES,
UserHandle.of(UserHandle.myUserId()))) {
mSwitchPref.setChecked(false);

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2017 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.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.UserManager;
import com.android.settings.TestConfig;
import com.android.settings.applications.AppStateInstallAppsBridge;
import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.RestrictedSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ExternalSourcesDetailsTest {
@Mock
private UserManager mUserManager;
@Mock
private RestrictedSwitchPreference mSwitchPref;
@Mock
private PackageInfo mPackageInfo;
private ExternalSourcesDetails mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = new ExternalSourcesDetails();
ReflectionHelpers.setField(mFragment, "mUserManager", mUserManager);
ReflectionHelpers.setField(mFragment, "mSwitchPref", mSwitchPref);
}
@Test
public void refreshUi_noPackageInfo_shouldReturnFalseAndNoCrash() {
mFragment.refreshUi();
assertThat(mFragment.refreshUi()).isFalse();
// should not crash
}
@Test
public void refreshUi_noApplicationInfo_shouldReturnFalseAndNoCrash() {
ReflectionHelpers.setField(mFragment, "mPackageInfo", mPackageInfo);
mFragment.refreshUi();
assertThat(mFragment.refreshUi()).isFalse();
// should not crash
}
@Test
public void refreshUi_hasApplicationInfo_shouldReturnTrue() {
ReflectionHelpers.setField(mFragment, "mPackageInfo", mPackageInfo);
mPackageInfo.applicationInfo = new ApplicationInfo();
final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class);
ReflectionHelpers.setField(mFragment, "mAppBridge", appBridge);
when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt()))
.thenReturn(mock(InstallAppsState.class));
mFragment.refreshUi();
assertThat(mFragment.refreshUi()).isTrue();
}
}

View File

@@ -37,7 +37,6 @@ import android.os.UserManager;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.filters.Suppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
@@ -46,7 +45,7 @@ import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.widget.ListView;
import android.support.v7.widget.RecyclerView;
import android.widget.Switch;
import android.widget.TextView;
@@ -57,7 +56,6 @@ import org.junit.runner.RunWith;
import java.util.List;
@Suppress
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExternalSourcesSettingsTest {
@@ -124,7 +122,8 @@ public class ExternalSourcesSettingsTest {
final String testAppLabel = getApplicationLabel(mPackageName);
mContext.startActivity(createManageExternalSourcesListIntent());
final BySelector preferenceListSelector = By.clazz(ListView.class).res("android:id/list");
final BySelector preferenceListSelector =
By.clazz(RecyclerView.class).res("com.android.settings:id/apps_list");
final UiObject2 preferenceList = mUiDevice.wait(Until.findObject(preferenceListSelector),
START_ACTIVITY_TIMEOUT);
assertNotNull("App list not shown", preferenceList);