diff --git a/res/xml/about_legal.xml b/res/xml/about_legal.xml index 55faad316d8..c86276a393f 100644 --- a/res/xml/about_legal.xml +++ b/res/xml/about_legal.xml @@ -15,6 +15,7 @@ --> @@ -26,35 +27,32 @@ - - + android:title="@string/copyright_title" + settings:controller="com.android.settings.deviceinfo.legal.CopyrightPreferenceController" /> - - + android:title="@string/license_title" + settings:controller="com.android.settings.deviceinfo.legal.LicensePreferenceController" /> - - + android:title="@string/terms_title" + settings:controller="com.android.settings.deviceinfo.legal.TermsPreferenceController" /> - - + android:title="@string/webview_license_title" + settings:controller="com.android.settings.deviceinfo.legal.WebViewLicensePreferenceController" /> + android:summary="@string/wallpaper_attributions_values" + settings:controller="com.android.settings.deviceinfo.legal.WallpaperAttributionsPreferenceController" /> diff --git a/src/com/android/settings/LegalSettings.java b/src/com/android/settings/LegalSettings.java index 64e4cfc70b4..3538cc9ef8f 100644 --- a/src/com/android/settings/LegalSettings.java +++ b/src/com/android/settings/LegalSettings.java @@ -16,19 +16,11 @@ package com.android.settings; -import android.app.Activity; import android.content.Context; -import android.content.Intent; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; -import android.os.Bundle; import android.provider.SearchIndexableResource; -import android.support.annotation.VisibleForTesting; -import android.support.v7.preference.PreferenceGroup; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; -import com.android.settings.R; +import com.android.settings.dashboard.DashboardFragment; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; import com.android.settingslib.search.SearchIndexable; @@ -37,94 +29,34 @@ import java.util.Arrays; import java.util.List; @SearchIndexable -public class LegalSettings extends SettingsPreferenceFragment implements Indexable { +public class LegalSettings extends DashboardFragment { - private static final String KEY_TERMS = "terms"; - private static final String KEY_LICENSE = "license"; - private static final String KEY_COPYRIGHT = "copyright"; - private static final String KEY_WEBVIEW_LICENSE = "webview_license"; - @VisibleForTesting static final String KEY_WALLPAPER_ATTRIBUTIONS = "wallpaper_attributions"; - - public void onCreate(Bundle icicle) { - super.onCreate(icicle); - addPreferencesFromResource(R.xml.about_legal); - - final Activity act = getActivity(); - // These are contained in the "container" preference group - PreferenceGroup parentPreference = getPreferenceScreen(); - Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_TERMS, - Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); - Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_LICENSE, - Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); - Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_COPYRIGHT, - Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); - Utils.updatePreferenceToSpecificActivityOrRemove(act, parentPreference, KEY_WEBVIEW_LICENSE, - Utils.UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY); - - checkWallpaperAttributionAvailability(act); - } + private static final String TAG = "LegalSettings"; @Override public int getMetricsCategory() { return MetricsEvent.ABOUT_LEGAL_SETTINGS; } - @VisibleForTesting - void checkWallpaperAttributionAvailability(Context context) { - if (!context.getResources().getBoolean( - R.bool.config_show_wallpaper_attribution)) { - removePreference(KEY_WALLPAPER_ATTRIBUTIONS); - } + @Override + protected String getLogTag() { + return TAG; } - public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = - new BaseSearchIndexProvider() { + @Override + protected int getPreferenceScreenResId() { + return R.xml.about_legal; + } - @Override - public List getXmlResourcesToIndex( - Context context, boolean enabled) { - final SearchIndexableResource sir = new SearchIndexableResource(context); - sir.xmlResId = R.xml.about_legal; - return Arrays.asList(sir); - } + public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = + new BaseSearchIndexProvider() { - @Override - public List getNonIndexableKeys(Context context) { - final List keys = super.getNonIndexableKeys(context); - if (!checkIntentAction(context, "android.settings.TERMS")) { - keys.add(KEY_TERMS); + @Override + public List getXmlResourcesToIndex( + Context context, boolean enabled) { + final SearchIndexableResource sir = new SearchIndexableResource(context); + sir.xmlResId = R.xml.about_legal; + return Arrays.asList(sir); } - if (!checkIntentAction(context, "android.settings.LICENSE")) { - keys.add(KEY_LICENSE); - } - if (!checkIntentAction(context, "android.settings.COPYRIGHT")) { - keys.add(KEY_COPYRIGHT); - } - if (!checkIntentAction(context, "android.settings.WEBVIEW_LICENSE")) { - keys.add(KEY_WEBVIEW_LICENSE); - } - keys.add(KEY_WALLPAPER_ATTRIBUTIONS); - return keys; - } - - private boolean checkIntentAction(Context context, String action) { - final Intent intent = new Intent(action); - - // Find the activity that is in the system image - final PackageManager pm = context.getPackageManager(); - final List list = pm.queryIntentActivities(intent, 0); - final int listSize = list.size(); - - for (int i = 0; i < listSize; i++) { - ResolveInfo resolveInfo = list.get(i); - if ((resolveInfo.activityInfo.applicationInfo.flags & - ApplicationInfo.FLAG_SYSTEM) != 0) { - return true; - } - } - - return false; - } - }; - + }; } diff --git a/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java b/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java new file mode 100644 index 00000000000..68e51f40df7 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceController.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; +import android.content.Intent; + +public class CopyrightPreferenceController extends LegalPreferenceController { + + private static final Intent INTENT = new Intent("android.settings.COPYRIGHT"); + + public CopyrightPreferenceController(Context context, String key) { + super(context, key); + } + + @Override + protected Intent getIntent() { + return INTENT; + } +} diff --git a/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java b/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java new file mode 100644 index 00000000000..f64eb4b59fe --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/LegalPreferenceController.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; + +import java.util.List; + + +public abstract class LegalPreferenceController extends BasePreferenceController { + private final PackageManager mPackageManager; + private Preference mPreference; + + public LegalPreferenceController(Context context, String key) { + super(context, key); + mPackageManager = mContext.getPackageManager(); + } + + @Override + public int getAvailabilityStatus() { + if (findMatchingSpecificActivity() != null) { + return AVAILABLE; + } else { + return DISABLED_UNSUPPORTED; + } + } + + @Override + public void displayPreference(PreferenceScreen screen) { + mPreference = screen.findPreference(getPreferenceKey()); + super.displayPreference(screen); + + if (getAvailabilityStatus() == AVAILABLE) { + replacePreferenceIntent(); + } + } + + protected abstract Intent getIntent(); + + private ResolveInfo findMatchingSpecificActivity() { + final Intent intent = getIntent(); + if (intent == null) { + return null; + } + + // Find the activity that is in the system image + final List list = mPackageManager.queryIntentActivities(intent, 0); + if (list == null) { + return null; + } + + for (ResolveInfo resolveInfo : list) { + if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) + != 0) { + return resolveInfo; + } + } + + // Did not find a matching activity + return null; + } + + private void replacePreferenceIntent() { + final ResolveInfo resolveInfo = findMatchingSpecificActivity(); + if (resolveInfo == null) { + return; + } + + // Replace the intent with this specific activity + mPreference.setIntent(new Intent().setClassName( + resolveInfo.activityInfo.packageName, + resolveInfo.activityInfo.name)); + + mPreference.setTitle(resolveInfo.loadLabel(mPackageManager)); + } +} diff --git a/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java b/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java new file mode 100644 index 00000000000..67af15b4fc6 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/LicensePreferenceController.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; +import android.content.Intent; + +public class LicensePreferenceController extends LegalPreferenceController { + + private static final Intent INTENT = new Intent("android.settings.LICENSE"); + + public LicensePreferenceController(Context context, String key) { + super(context, key); + } + + @Override + protected Intent getIntent() { + return INTENT; + } +} diff --git a/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java b/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java new file mode 100644 index 00000000000..bccc44579b9 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/TermsPreferenceController.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; +import android.content.Intent; + +public class TermsPreferenceController extends LegalPreferenceController { + + private static final Intent INTENT = new Intent("android.settings.TERMS"); + + public TermsPreferenceController(Context context, String key) { + super(context, key); + } + + @Override + protected Intent getIntent() { + return INTENT; + } +} diff --git a/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java new file mode 100644 index 00000000000..418b149df97 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceController.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; + +import com.android.settings.R; +import com.android.settings.core.BasePreferenceController; + +public class WallpaperAttributionsPreferenceController extends BasePreferenceController { + + public WallpaperAttributionsPreferenceController(Context context, String key) { + super(context, key); + } + + @Override + public int getAvailabilityStatus() { + return mContext.getResources().getBoolean(R.bool.config_show_wallpaper_attribution) + ? AVAILABLE + : DISABLED_UNSUPPORTED; + } +} diff --git a/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java b/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java new file mode 100644 index 00000000000..9d8b3f95027 --- /dev/null +++ b/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceController.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import android.content.Context; +import android.content.Intent; + +public class WebViewLicensePreferenceController extends LegalPreferenceController { + + private static final Intent INTENT = new Intent("android.settings.WEBVIEW_LICENSE"); + + public WebViewLicensePreferenceController(Context context, String key) { + super(context, key); + } + + @Override + protected Intent getIntent() { + return INTENT; + } +} diff --git a/tests/robotests/src/com/android/settings/LegalSettingsTest.java b/tests/robotests/src/com/android/settings/LegalSettingsTest.java index af0b757a182..28eac07704d 100644 --- a/tests/robotests/src/com/android/settings/LegalSettingsTest.java +++ b/tests/robotests/src/com/android/settings/LegalSettingsTest.java @@ -16,7 +16,6 @@ package com.android.settings; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.spy; import android.content.Context; @@ -28,57 +27,25 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; -import org.robolectric.annotation.Config; import java.util.List; @RunWith(SettingsRobolectricTestRunner.class) public class LegalSettingsTest { - private Context mContext; - private LegalSettings mFragment; - private boolean mWallpaperRemoved; - @Before public void setUp() { MockitoAnnotations.initMocks(this); - mContext = spy(RuntimeEnvironment.application); - mFragment = new LegalSettings() { - @Override - public boolean removePreference(String key) { - if (LegalSettings.KEY_WALLPAPER_ATTRIBUTIONS.equals(key)) { - mWallpaperRemoved = true; - - return true; - } - return false; - } - }; } @Test - public void testNonIndexableKeys_existInXmlLayout() { + public void getNonIndexableKeys_existInXmlLayout() { final Context context = RuntimeEnvironment.application; final List niks = - LegalSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context); + LegalSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context); final List keys = XmlTestUtils.getKeysFromPreferenceXml(context, R.xml.about_legal); assertThat(keys).containsAllIn(niks); } - - @Test - public void testWallpaperAttributions_byDefault_shouldBeShown() { - mFragment.checkWallpaperAttributionAvailability(mContext); - - assertThat(mWallpaperRemoved).isEqualTo(false); - } - - @Test - @Config(qualifiers = "mcc999") - public void testWallpaperAttributions_ifDisabled_shouldNotBeShown() { - mFragment.checkWallpaperAttributionAvailability(mContext); - - assertThat(mWallpaperRemoved).isEqualTo(true); - } } diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java new file mode 100644 index 00000000000..7ca00765fd6 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/CopyrightPreferenceControllerTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +public class CopyrightPreferenceControllerTest { + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private CopyrightPreferenceController mController; + private Preference mPreference; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mPreference = new Preference(mContext); + mPreference.setIntent(new Intent()); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + when(mScreen.findPreference(anyString())).thenReturn(mPreference); + mController = new CopyrightPreferenceController(mContext, "pref_key"); + mController.displayPreference(mScreen); + } + + @Test + public void getIntent_shouldUseRightIntent() { + final Intent intent = mController.getIntent(); + assertThat(intent.getAction()).isEqualTo("android.settings.COPYRIGHT"); + } + + @Test + public void getAvailabilityStatus_systemApp_shouldReturnTrue() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(true /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void getAvailabilityStatus_notSystemApp_shouldReturnFalseAndNoCrash() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(false /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + } + + /** + * Returns a ResolveInfo object for testing + * + * @param isSystemApp If true, the application is a system app. + */ + private ResolveInfo getTestResolveInfo(boolean isSystemApp) { + final ResolveInfo testResolveInfo = new ResolveInfo(); + final ApplicationInfo testAppInfo = new ApplicationInfo(); + if (isSystemApp) { + testAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM; + } + final ActivityInfo testActivityInfo = new ActivityInfo(); + testActivityInfo.name = "TestActivityName"; + testActivityInfo.packageName = "TestPackageName"; + testActivityInfo.applicationInfo = testAppInfo; + testResolveInfo.activityInfo = testActivityInfo; + return testResolveInfo; + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java new file mode 100644 index 00000000000..f9dd8bd49d3 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/LicensePreferenceControllerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +import java.util.ArrayList; +import java.util.List; + + +@RunWith(SettingsRobolectricTestRunner.class) +public class LicensePreferenceControllerTest { + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private LicensePreferenceController mController; + private Preference mPreference; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mPreference = new Preference(mContext); + mPreference.setIntent(new Intent()); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + when(mScreen.findPreference(anyString())).thenReturn(mPreference); + mController = new LicensePreferenceController(mContext, "pref_key"); + mController.displayPreference(mScreen); + } + + @Test + public void getIntent_shouldUseRightIntent() { + final Intent intent = mController.getIntent(); + assertThat(intent.getAction()).isEqualTo("android.settings.LICENSE"); + } + + @Test + public void getAvailabilityStatus_systemApp_shouldReturnTrue() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(true /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void getAvailabilityStatus_notSystemApp_shouldReturnFalseAndNoCrash() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(false /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + } + + /** + * Returns a ResolveInfo object for testing + * + * @param isSystemApp If true, the application is a system app. + */ + private ResolveInfo getTestResolveInfo(boolean isSystemApp) { + final ResolveInfo testResolveInfo = new ResolveInfo(); + final ApplicationInfo testAppInfo = new ApplicationInfo(); + if (isSystemApp) { + testAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM; + } + final ActivityInfo testActivityInfo = new ActivityInfo(); + testActivityInfo.name = "TestActivityName"; + testActivityInfo.packageName = "TestPackageName"; + testActivityInfo.applicationInfo = testAppInfo; + testResolveInfo.activityInfo = testActivityInfo; + return testResolveInfo; + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java new file mode 100644 index 00000000000..3a8eb660b19 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/TermsPreferenceControllerTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +public class TermsPreferenceControllerTest { + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private TermsPreferenceController mController; + private Preference mPreference; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mPreference = new Preference(mContext); + mPreference.setIntent(new Intent()); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + when(mScreen.findPreference(anyString())).thenReturn(mPreference); + mController = new TermsPreferenceController(mContext, "pref_key"); + mController.displayPreference(mScreen); + } + + @Test + public void getIntent_shouldUseRightIntent() { + final Intent intent = mController.getIntent(); + assertThat(intent.getAction()).isEqualTo("android.settings.TERMS"); + } + + @Test + public void getAvailabilityStatus_systemApp_shouldReturnTrue() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(true /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void getAvailabilityStatus_notSystemApp_shouldReturnFalseAndNoCrash() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(false /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + } + + /** + * Returns a ResolveInfo object for testing + * + * @param isSystemApp If true, the application is a system app. + */ + private ResolveInfo getTestResolveInfo(boolean isSystemApp) { + final ResolveInfo testResolveInfo = new ResolveInfo(); + final ApplicationInfo testAppInfo = new ApplicationInfo(); + if (isSystemApp) { + testAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM; + } + final ActivityInfo testActivityInfo = new ActivityInfo(); + testActivityInfo.name = "TestActivityName"; + testActivityInfo.packageName = "TestPackageName"; + testActivityInfo.applicationInfo = testAppInfo; + testResolveInfo.activityInfo = testActivityInfo; + return testResolveInfo; + } +} diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java new file mode 100644 index 00000000000..c4d5577a7f5 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/WallpaperAttributionsPreferenceControllerTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.Context; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +@RunWith(SettingsRobolectricTestRunner.class) +public class WallpaperAttributionsPreferenceControllerTest { + + private Context mContext; + private WallpaperAttributionsPreferenceController mController; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mController = new WallpaperAttributionsPreferenceController(mContext, "pref_key"); + } + + @Test + public void getAvailabilityStatus_byDefault_true() { + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + @Config(qualifiers = "mcc999") + public void getAvailabilityStatus_ifNotVisible_false() { + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + } +} \ No newline at end of file diff --git a/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java new file mode 100644 index 00000000000..982dc91ed20 --- /dev/null +++ b/tests/robotests/src/com/android/settings/deviceinfo/legal/WebViewLicensePreferenceControllerTest.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2018 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.deviceinfo.legal; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.ActivityInfo; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.BasePreferenceController; +import com.android.settings.testutils.SettingsRobolectricTestRunner; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SettingsRobolectricTestRunner.class) +public class WebViewLicensePreferenceControllerTest { + + @Mock + private PackageManager mPackageManager; + @Mock + private PreferenceScreen mScreen; + + private Context mContext; + private WebViewLicensePreferenceController mController; + private Preference mPreference; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + mPreference = new Preference(mContext); + mPreference.setIntent(new Intent()); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + when(mScreen.findPreference(anyString())).thenReturn(mPreference); + mController = new WebViewLicensePreferenceController(mContext, "pref_key"); + mController.displayPreference(mScreen); + } + + @Test + public void getIntent_shouldUseRightIntent() { + final Intent intent = mController.getIntent(); + assertThat(intent.getAction()).isEqualTo("android.settings.WEBVIEW_LICENSE"); + } + + @Test + public void getAvailabilityStatus_systemApp_shouldReturnTrue() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(true /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.AVAILABLE); + } + + @Test + public void getAvailabilityStatus_notSystemApp_shouldReturnFalseAndNoCrash() { + final List testResolveInfos = new ArrayList<>(); + testResolveInfos.add(getTestResolveInfo(false /* isSystemApp */)); + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())) + .thenReturn(testResolveInfos); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + + when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null); + assertThat(mController.getAvailabilityStatus()) + .isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED); + } + + /** + * Returns a ResolveInfo object for testing + * + * @param isSystemApp If true, the application is a system app. + */ + private ResolveInfo getTestResolveInfo(boolean isSystemApp) { + final ResolveInfo testResolveInfo = new ResolveInfo(); + final ApplicationInfo testAppInfo = new ApplicationInfo(); + if (isSystemApp) { + testAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM; + } + final ActivityInfo testActivityInfo = new ActivityInfo(); + testActivityInfo.name = "TestActivityName"; + testActivityInfo.packageName = "TestPackageName"; + testActivityInfo.applicationInfo = testAppInfo; + testResolveInfo.activityInfo = testActivityInfo; + return testResolveInfo; + } +}