Create a new Open supported links page

- Add a new OpenSupportedLinks fragment
- Add a new AppHeaderPreferenceController
- Add a new AppOpenSupportedLinksPreferenceController

Fixes: 143185713
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.applications
Change-Id: I6568950b7b918378baaa80e2ca0d596913620928
This commit is contained in:
Sunny Shao
2019-10-28 20:40:53 +08:00
parent 162e2b92b8
commit 6b032f4e2d
10 changed files with 727 additions and 99 deletions

View File

@@ -0,0 +1,131 @@
/*
* 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

@@ -0,0 +1,92 @@
/*
* 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.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.ArraySet;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.widget.FooterPreference;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUtils.class)
public class OpenSupportedLinksTest {
private static final String TEST_FOOTER_TITLE = "FooterTitle";
private static final String TEST_DOMAIN_LINK = "aaa.bbb.ccc";
private Context mContext;
private TestFragment mSettings;
private FooterPreference mFooter;
@Before
public void setUp() {
mContext = spy(RuntimeEnvironment.application);
mSettings = spy(new TestFragment(mContext));
mFooter = new FooterPreference.Builder(mContext).setTitle(TEST_FOOTER_TITLE).build();
}
@After
public void tearDown() {
ShadowUtils.reset();
}
@Test
public void addLinksToFooter_noHandledDomains_returnDefaultFooterTitle() {
mSettings.addLinksToFooter(mFooter);
assertThat(mFooter.getTitle()).isEqualTo(TEST_FOOTER_TITLE);
}
@Test
public void addLinksToFooter_oneHandledDomains_returnDomainsFooterTitle() {
final ArraySet<String> domainLinks = new ArraySet<>();
domainLinks.add(TEST_DOMAIN_LINK);
ShadowUtils.setHandledDomains(domainLinks);
mSettings.addLinksToFooter(mFooter);
assertThat(mFooter.getTitle().toString()).contains(TEST_DOMAIN_LINK);
}
public static class TestFragment extends OpenSupportedLinks {
private final Context mContext;
public TestFragment(Context context) {
mContext = context;
mPackageInfo = new PackageInfo();
mPackageInfo.packageName = "ssl.test.package.com";
}
@Override
protected PackageManager getPackageManager() {
return mContext.getPackageManager();
}
}
}