Merge "Refactor the OpenSupportedLinks" into rvc-dev am: ccb37bb8d1 am: 9625a5ed1f am: 7e00c8d28c am: 7763d89c45

Change-Id: I77207c5d0a6875a33f26217270ea7894b38980f6
This commit is contained in:
TreeHugger Robot
2020-04-15 17:59:57 +00:00
committed by Automerger Merge Worker
7 changed files with 247 additions and 483 deletions

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);
}
}