Refactor the OpenSupportedLinks

- Use the AppInfoWithHeader to replace the DashboardFragment.
- It provide the AppHeader controller and handle the app update/remove event.

Fixes: 147996227
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.applications.OpenSupportedLinksTest
Change-Id: I1633c39fc926805a379c5c31845897914e5181a6
This commit is contained in:
Sunny Shao
2020-03-04 12:34:15 +08:00
parent 6585f79439
commit 44b74c9cfe
7 changed files with 247 additions and 483 deletions

View File

@@ -18,17 +18,9 @@
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/app_launch_open_domain_urls_title">
<com.android.settingslib.widget.LayoutPreference
android:key="app_header_view"
android:layout="@layout/settings_entity_header"
android:selectable="false"
settings:controller="com.android.settings.applications.AppHeaderPreferenceController"
settings:allowDividerBelow="true"/>
<PreferenceCategory
android:key="app_launch_allow_app_to_open_supported_links"
android:title="@string/app_link_open_always"
settings:controller="com.android.settings.applications.AppOpenSupportedLinksPreferenceController">
android:key="supported_links_radio_group"
android:title="@string/app_link_open_always">
</PreferenceCategory>
<com.android.settingslib.widget.FooterPreference
@@ -37,4 +29,4 @@
android:selectable="false"
settings:searchable="false"/>
</PreferenceScreen>
</PreferenceScreen>

View File

@@ -1,106 +0,0 @@
/*
* Copyright (C) 2020 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;
import static com.android.settings.widget.EntityHeaderController.ActionType;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.widget.LayoutPreference;
/**
* The header controller displays on the top of the page.
*/
public class AppHeaderPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnResume {
private DashboardFragment mParent;
private PackageInfo mPackageInfo;
private Lifecycle mLifecycle;
private LayoutPreference mHeaderPreference;
public AppHeaderPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
/**
* @param fragment set the parent fragment.
* @return return controller-self.
*/
public AppHeaderPreferenceController setParentFragment(DashboardFragment fragment) {
mParent = fragment;
return this;
}
/**
* @param packageInfo set the {@link PackageInfo}.
* @return return controller-self.
*/
public AppHeaderPreferenceController setPackageInfo(PackageInfo packageInfo) {
mPackageInfo = packageInfo;
return this;
}
/**
* @param lifeCycle set the {@link Lifecycle}.
* @return return controller-self.
*/
public AppHeaderPreferenceController setLifeCycle(Lifecycle lifeCycle) {
mLifecycle = lifeCycle;
return this;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mHeaderPreference = screen.findPreference(getPreferenceKey());
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void onResume() {
final Activity activity = mParent.getActivity();
final PackageManager packageManager = activity.getPackageManager();
EntityHeaderController
.newInstance(activity, mParent, mHeaderPreference.findViewById(R.id.entity_header))
.setRecyclerView(mParent.getListView(), mLifecycle)
.setIcon(Utils.getBadgedIcon(mParent.getContext(), mPackageInfo.applicationInfo))
.setLabel(mPackageInfo.applicationInfo.loadLabel(packageManager))
.setSummary(mPackageInfo)
.setIsInstantApp(AppUtils.isInstant(mPackageInfo.applicationInfo))
.setPackageName(mPackageInfo.packageName)
.setUid(mPackageInfo.applicationInfo.uid)
.setButtonActions(ActionType.ACTION_NONE, ActionType.ACTION_NONE)
.done(mParent.getActivity(), true /* rebindActions */);
}
}

View File

@@ -28,7 +28,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
@@ -50,8 +49,6 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
private static final String FRAGMENT_OPEN_SUPPORTED_LINKS =
"com.android.settings.applications.OpenSupportedLinks";
public static final String KEY_PACKAGE_INFO = "pkg_info";
private static final Intent sBrowserIntent;
static {
@@ -79,7 +76,8 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
mAppLinkState = findPreference(KEY_APP_LINK_STATE);
mAppLinkState.setOnPreferenceClickListener(preference -> {
final Bundle args = new Bundle();
args.putParcelable(KEY_PACKAGE_INFO, this.mPackageInfo);
args.putString(ARG_PACKAGE_NAME, mPackageName);
args.putInt(ARG_PACKAGE_UID, mUserId);
new SubSettingLauncher(this.getContext())
.setDestination(FRAGMENT_OPEN_SUPPORTED_LINKS)
@@ -145,7 +143,6 @@ public class AppLaunchSettings extends AppInfoWithHeader implements OnClickListe
private void setAppLinkStateSummary() {
final int state = mPm.getIntentVerificationStatusAsUser(mPackageName,
UserHandle.myUserId());
Log.d("[sunny]", "setAppLinkStateSummary+ state=" + state);
mAppLinkState.setSummary(linkStateToResourceId(state));
}

View File

@@ -1,184 +0,0 @@
/*
* Copyright (C) 2020 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;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.RadioButtonPreference;
/**
* The radio group controller supports users to choose what kind supported links they need.
*/
public class AppOpenSupportedLinksPreferenceController extends BasePreferenceController
implements RadioButtonPreference.OnClickListener {
private static final String TAG = "OpenLinksPrefCtrl";
private static final String KEY_LINK_OPEN_ALWAYS = "app_link_open_always";
private static final String KEY_LINK_OPEN_ASK = "app_link_open_ask";
private static final String KEY_LINK_OPEN_NEVER = "app_link_open_never";
private Context mContext;
private PackageManager mPackageManager;
private String mPackageName;
private int mCurrentIndex;
private PreferenceCategory mPreferenceCategory;
private String[] mRadioKeys = {KEY_LINK_OPEN_ALWAYS, KEY_LINK_OPEN_ASK, KEY_LINK_OPEN_NEVER};
@VisibleForTesting
RadioButtonPreference mAllowOpening;
@VisibleForTesting
RadioButtonPreference mAskEveryTime;
@VisibleForTesting
RadioButtonPreference mNotAllowed;
public AppOpenSupportedLinksPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mContext = context;
mPackageManager = context.getPackageManager();
}
/**
* @param pkg selected package name.
* @return return controller-self.
*/
public AppOpenSupportedLinksPreferenceController setInit(String pkg) {
mPackageName = pkg;
return this;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceCategory = screen.findPreference(getPreferenceKey());
mAllowOpening = makeRadioPreference(KEY_LINK_OPEN_ALWAYS, R.string.app_link_open_always);
final int entriesNo = getEntriesNo();
//This to avoid the summary line wrap
mAllowOpening.setAppendixVisibility(View.GONE);
mAllowOpening.setSummary(
mContext.getResources().getQuantityString(R.plurals.app_link_open_always_summary,
entriesNo, entriesNo));
mAskEveryTime = makeRadioPreference(KEY_LINK_OPEN_ASK, R.string.app_link_open_ask);
mNotAllowed = makeRadioPreference(KEY_LINK_OPEN_NEVER, R.string.app_link_open_never);
final int state = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
UserHandle.myUserId());
mCurrentIndex = linkStateToIndex(state);
setRadioStatus(mCurrentIndex);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void onRadioButtonClicked(RadioButtonPreference preference) {
final int clickedIndex = preferenceKeyToIndex(preference.getKey());
if (mCurrentIndex != clickedIndex) {
mCurrentIndex = clickedIndex;
setRadioStatus(mCurrentIndex);
updateAppLinkState(indexToLinkState(mCurrentIndex));
}
}
private RadioButtonPreference makeRadioPreference(String key, int resourceId) {
RadioButtonPreference pref = new RadioButtonPreference(mPreferenceCategory.getContext());
pref.setKey(key);
pref.setTitle(resourceId);
pref.setOnClickListener(this);
mPreferenceCategory.addPreference(pref);
return pref;
}
private int linkStateToIndex(int state) {
switch (state) {
case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS:
return 0; // Always
case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER:
return 2; // Never
default:
return 1; // Ask
}
}
private int indexToLinkState(int index) {
switch (index) {
case 0:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
case 2:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
default:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
}
}
private int preferenceKeyToIndex(String key) {
for (int i = 0; i < mRadioKeys.length; i++) {
if (TextUtils.equals(key, mRadioKeys[i])) {
return i;
}
}
return 1; // Ask
}
private void setRadioStatus(int index) {
mAllowOpening.setChecked(index == 0 ? true : false);
mAskEveryTime.setChecked(index == 1 ? true : false);
mNotAllowed.setChecked(index == 2 ? true : false);
}
private boolean updateAppLinkState(final int newState) {
final int userId = UserHandle.myUserId();
final int priorState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
userId);
if (priorState == newState) {
return false;
}
boolean success = mPackageManager.updateIntentVerificationStatusAsUser(mPackageName,
newState, userId);
if (success) {
// Read back the state to see if the change worked
final int updatedState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
userId);
success = (newState == updatedState);
} else {
Log.e(TAG, "Couldn't update intent verification status!");
}
return success;
}
@VisibleForTesting
int getEntriesNo() {
return Utils.getHandledDomains(mPackageManager, mPackageName).size();
}
}

View File

@@ -15,48 +15,176 @@
*/
package com.android.settings.applications;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;
import android.view.View;
import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.PreferenceCategory;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.widget.RadioButtonPreference;
/**
* Display the Open Supported Links page. Allow users choose what kind supported links they need.
*/
public class OpenSupportedLinks extends DashboardFragment {
public class OpenSupportedLinks extends AppInfoWithHeader implements
RadioButtonPreference.OnClickListener {
private static final String TAG = "OpenSupportedLinks";
private static final String RADIO_GROUP_KEY = "supported_links_radio_group";
private static final String FOOTER_KEY = "supported_links_footer";
private static final String KEY_LINK_OPEN_ALWAYS = "app_link_open_always";
private static final String KEY_LINK_OPEN_ASK = "app_link_open_ask";
private static final String KEY_LINK_OPEN_NEVER = "app_link_open_never";
private static final int ALLOW_ALWAYS_OPENING = 0;
private static final int ASK_EVERY_TIME = 1;
private static final int NOT_ALLOWED_OPENING = 2;
private int mCurrentIndex;
private String[] mRadioKeys = {KEY_LINK_OPEN_ALWAYS, KEY_LINK_OPEN_ASK, KEY_LINK_OPEN_NEVER};
@VisibleForTesting
PackageInfo mPackageInfo;
PackageManager mPackageManager;
@VisibleForTesting
PreferenceCategory mPreferenceCategory;
@VisibleForTesting
RadioButtonPreference mAllowOpening;
@VisibleForTesting
RadioButtonPreference mAskEveryTime;
@VisibleForTesting
RadioButtonPreference mNotAllowed;
@Override
public void onAttach(Context context) {
super.onAttach(context);
final Bundle args = getArguments();
mPackageInfo = (args != null) ? args.getParcelable(AppLaunchSettings.KEY_PACKAGE_INFO)
: null;
if (mPackageInfo == null) {
Log.w(TAG, "Missing PackageInfo; maybe reinstalling?");
return;
}
use(AppHeaderPreferenceController.class).setParentFragment(this).setPackageInfo(
mPackageInfo).setLifeCycle(getSettingsLifecycle());
use(AppOpenSupportedLinksPreferenceController.class).setInit(mPackageInfo.packageName);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPackageManager = getPackageManager();
addPreferencesFromResource(R.xml.open_supported_links);
initRadioPreferencesGroup();
updateFooterPreference();
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
public int getMetricsCategory() {
return SettingsEnums.OPEN_SUPPORTED_LINKS;
}
/**
* Here to handle radio group and generate the radios.
*/
@VisibleForTesting
void initRadioPreferencesGroup() {
mPreferenceCategory = findPreference(RADIO_GROUP_KEY);
mAllowOpening = makeRadioPreference(KEY_LINK_OPEN_ALWAYS, R.string.app_link_open_always);
final int entriesNo = getEntriesNo();
//This to avoid the summary line wrap
mAllowOpening.setAppendixVisibility(View.GONE);
mAllowOpening.setSummary(getResources().getQuantityString(
R.plurals.app_link_open_always_summary, entriesNo, entriesNo));
mAskEveryTime = makeRadioPreference(KEY_LINK_OPEN_ASK, R.string.app_link_open_ask);
mNotAllowed = makeRadioPreference(KEY_LINK_OPEN_NEVER, R.string.app_link_open_never);
final int state = mPackageManager.getIntentVerificationStatusAsUser(mPackageName, mUserId);
mCurrentIndex = linkStateToIndex(state);
setRadioStatus(mCurrentIndex);
}
@Override
public void onRadioButtonClicked(RadioButtonPreference preference) {
final int clickedIndex = preferenceKeyToIndex(preference.getKey());
if (mCurrentIndex != clickedIndex) {
mCurrentIndex = clickedIndex;
setRadioStatus(mCurrentIndex);
updateAppLinkState(indexToLinkState(mCurrentIndex));
}
}
private RadioButtonPreference makeRadioPreference(String key, int stringId) {
final RadioButtonPreference pref = new RadioButtonPreference(
mPreferenceCategory.getContext());
pref.setKey(key);
pref.setTitle(stringId);
pref.setOnClickListener(this);
mPreferenceCategory.addPreference(pref);
return pref;
}
@VisibleForTesting
int getEntriesNo() {
return Utils.getHandledDomains(mPackageManager, mPackageName).size();
}
private int linkStateToIndex(int state) {
switch (state) {
case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS:
return ALLOW_ALWAYS_OPENING;
case INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER:
return NOT_ALLOWED_OPENING;
default:
return ASK_EVERY_TIME;
}
}
private int indexToLinkState(int index) {
switch (index) {
case 0:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
case 2:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
default:
return INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
}
}
private void setRadioStatus(int index) {
mAllowOpening.setChecked(index == ALLOW_ALWAYS_OPENING);
mAskEveryTime.setChecked(index == ASK_EVERY_TIME);
mNotAllowed.setChecked(index == NOT_ALLOWED_OPENING);
}
private int preferenceKeyToIndex(String key) {
for (int i = 0; i < mRadioKeys.length; i++) {
if (TextUtils.equals(key, mRadioKeys[i])) {
return i;
}
}
return ASK_EVERY_TIME;
}
private void updateAppLinkState(final int newState) {
final int priorState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
mUserId);
if (priorState == newState) {
return;
}
final boolean success = mPackageManager.updateIntentVerificationStatusAsUser(mPackageName,
newState, mUserId);
if (success) {
// Read back the state to see if the change worked
final int updatedState = mPackageManager.getIntentVerificationStatusAsUser(mPackageName,
mUserId);
} else {
Log.e(TAG, "Couldn't update intent verification status!");
}
}
/**
* Here is handle the Footer.
*/
private void updateFooterPreference() {
final FooterPreference footer = findPreference(FOOTER_KEY);
if (footer == null) {
Log.w(TAG, "Can't find the footer preference.");
@@ -65,25 +193,9 @@ public class OpenSupportedLinks extends DashboardFragment {
addLinksToFooter(footer);
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.open_supported_links;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.OPEN_SUPPORTED_LINKS;
}
@VisibleForTesting
void addLinksToFooter(FooterPreference footer) {
final ArraySet<String> result = Utils.getHandledDomains(getPackageManager(),
mPackageInfo.packageName);
final ArraySet<String> result = Utils.getHandledDomains(mPackageManager, mPackageName);
if (result.isEmpty()) {
Log.w(TAG, "Can't find any app links.");
return;
@@ -94,4 +206,14 @@ public class OpenSupportedLinks extends DashboardFragment {
}
footer.setTitle(title);
}
@Override
protected boolean refreshUi() {
return true;
}
@Override
protected AlertDialog createDialog(int id, int errorCode) {
return null;
}
}

View File

@@ -1,131 +0,0 @@
/*
* Copyright (C) 2020 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;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.ArraySet;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.shadow.ShadowUtils;
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;
@RunWith(RobolectricTestRunner.class)
public class AppOpenSupportedLinksPreferenceControllerTest {
private static final String TEST_KEY = "test_key";
private static final String TEST_DOMAIN_LINK = "aaa.bbb.ccc";
private static final String TEST_PACKAGE = "ssl.test.package.com";
@Mock
private PackageManager mPackageManager;
private Context mContext;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
private PreferenceCategory mCategory;
private AppOpenSupportedLinksPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mPackageManager).when(mContext).getPackageManager();
mPreferenceManager = new PreferenceManager(mContext);
mScreen = spy(mPreferenceManager.createPreferenceScreen(mContext));
mCategory = spy(new PreferenceCategory(mContext));
mController = spy(new AppOpenSupportedLinksPreferenceController(mContext, TEST_KEY));
mController.setInit(TEST_PACKAGE);
}
@Test
public void displayPreference_statusAlways_allowOpenChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS);
mController.displayPreference(mScreen);
assertThat(mController.mAllowOpening.isChecked()).isTrue();
assertThat(mController.mAskEveryTime.isChecked()).isFalse();
assertThat(mController.mNotAllowed.isChecked()).isFalse();
}
@Test
public void displayPreference_statusAsk_askEveryTimeChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK);
mController.displayPreference(mScreen);
assertThat(mController.mAllowOpening.isChecked()).isFalse();
assertThat(mController.mAskEveryTime.isChecked()).isTrue();
assertThat(mController.mNotAllowed.isChecked()).isFalse();
}
@Test
public void displayPreference_statusNever_notAllowedChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER);
mController.displayPreference(mScreen);
assertThat(mController.mAllowOpening.isChecked()).isFalse();
assertThat(mController.mAskEveryTime.isChecked()).isFalse();
assertThat(mController.mNotAllowed.isChecked()).isTrue();
}
@Test
@Config(shadows = ShadowUtils.class)
public void getEntriesNo_oneHandledDomains_returnOne() {
initHandledDomains();
assertThat(mController.getEntriesNo()).isEqualTo(1);
}
private void init(int status) {
doReturn(mCategory).when(mScreen).findPreference(any(CharSequence.class));
doReturn(true).when(mCategory).addPreference(any(Preference.class));
when(mPackageManager.getIntentVerificationStatusAsUser(anyString(), anyInt())).thenReturn(
status);
}
private void initHandledDomains() {
final ArraySet<String> domainLinks = new ArraySet<>();
domainLinks.add(TEST_DOMAIN_LINK);
ShadowUtils.setHandledDomains(domainLinks);
}
}

View File

@@ -15,15 +15,27 @@
*/
package com.android.settings.applications;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.util.ArraySet;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.widget.FooterPreference;
@@ -31,6 +43,8 @@ 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;
@@ -40,15 +54,25 @@ import org.robolectric.annotation.Config;
public class OpenSupportedLinksTest {
private static final String TEST_FOOTER_TITLE = "FooterTitle";
private static final String TEST_DOMAIN_LINK = "aaa.bbb.ccc";
private static final String TEST_SUMMARY = "TestSummary";
private static final String TEST_PACKAGE = "ssl.test.package.com";
@Mock
private PackageManager mPackageManager;
@Mock
private Resources mResources;
private Context mContext;
private TestFragment mSettings;
private FooterPreference mFooter;
private PreferenceCategory mCategory;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mSettings = spy(new TestFragment(mContext));
mSettings = spy(new TestFragment(mContext, mPackageManager));
mCategory = spy(new PreferenceCategory(mContext));
mFooter = new FooterPreference.Builder(mContext).setTitle(TEST_FOOTER_TITLE).build();
}
@@ -75,18 +99,68 @@ public class OpenSupportedLinksTest {
assertThat(mFooter.getTitle().toString()).contains(TEST_DOMAIN_LINK);
}
@Test
public void initRadioPreferencesGroup_statusAlways_allowOpenChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS);
mSettings.initRadioPreferencesGroup();
assertThat(mSettings.mAllowOpening.isChecked()).isTrue();
assertThat(mSettings.mAskEveryTime.isChecked()).isFalse();
assertThat(mSettings.mNotAllowed.isChecked()).isFalse();
}
@Test
public void initRadioPreferencesGroup_statusAsk_askEveryTimeChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK);
mSettings.initRadioPreferencesGroup();
assertThat(mSettings.mAllowOpening.isChecked()).isFalse();
assertThat(mSettings.mAskEveryTime.isChecked()).isTrue();
assertThat(mSettings.mNotAllowed.isChecked()).isFalse();
}
@Test
public void initRadioPreferencesGroup_statusNever_notAllowedChecked() {
init(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER);
mSettings.initRadioPreferencesGroup();
assertThat(mSettings.mAllowOpening.isChecked()).isFalse();
assertThat(mSettings.mAskEveryTime.isChecked()).isFalse();
assertThat(mSettings.mNotAllowed.isChecked()).isTrue();
}
@Test
public void getEntriesNo_oneHandledDomains_returnOne() {
initHandledDomains();
assertThat(mSettings.getEntriesNo()).isEqualTo(1);
}
private void init(int status) {
doReturn(status).when(mPackageManager).getIntentVerificationStatusAsUser(anyString(),
anyInt());
doReturn(mCategory).when(mSettings).findPreference(any(CharSequence.class));
doReturn(mResources).when(mSettings).getResources();
when(mResources.getQuantityString(anyInt(), anyInt(), anyInt())).thenReturn(TEST_SUMMARY);
doReturn(true).when(mCategory).addPreference(any(Preference.class));
}
public static class TestFragment extends OpenSupportedLinks {
private final Context mContext;
public TestFragment(Context context) {
public TestFragment(Context context, PackageManager packageManager) {
mContext = context;
mPackageInfo = new PackageInfo();
mPackageInfo.packageName = "ssl.test.package.com";
}
@Override
protected PackageManager getPackageManager() {
return mContext.getPackageManager();
mPackageManager = packageManager;
mPackageName = TEST_PACKAGE;
}
}
private void initHandledDomains() {
final ArraySet<String> domainLinks = new ArraySet<>();
domainLinks.add(TEST_DOMAIN_LINK);
ShadowUtils.setHandledDomains(domainLinks);
}
}