Migrate LegalSettings to DashboardFragment

- CopyrightPreferenceController
- LegalPreferenceController
- LicensePreferenceController
- TermsPreferenceController
- WallpaperAttributionsPreferenceController
- WebViewLicensePreferenceController

Test: manual
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.deviceinfo
      make RunSettingsRoboTests -j ROBOTEST_FILTER=CodeInspectionTest
      atest UniquePreferenceTest

Change-Id: I28108eb926f9efec63390c2354cb0ebc0a785ec2
This commit is contained in:
Yanting_Yang
2018-04-11 21:21:17 +08:00
committed by Fan Zhang
parent 9d03322d2c
commit 42513afac0
14 changed files with 825 additions and 135 deletions

View File

@@ -15,6 +15,7 @@
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/legal_information"
android:key="legal_screen">
@@ -26,35 +27,32 @@
<!-- Copyright information -->
<Preference
android:key="copyright"
android:title="@string/copyright_title">
<intent android:action="android.settings.COPYRIGHT"/>
</Preference>
android:title="@string/copyright_title"
settings:controller="com.android.settings.deviceinfo.legal.CopyrightPreferenceController" />
<!-- License information -->
<Preference
android:key="license"
android:title="@string/license_title">
<intent android:action="android.settings.LICENSE"/>
</Preference>
android:title="@string/license_title"
settings:controller="com.android.settings.deviceinfo.legal.LicensePreferenceController" />
<!-- Terms and conditions -->
<Preference
android:key="terms"
android:title="@string/terms_title">
<intent android:action="android.settings.TERMS"/>
</Preference>
android:title="@string/terms_title"
settings:controller="com.android.settings.deviceinfo.legal.TermsPreferenceController" />
<!-- System WebView License information -->
<Preference
android:key="webview_license"
android:title="@string/webview_license_title">
<intent android:action="android.settings.WEBVIEW_LICENSE"/>
</Preference>
android:title="@string/webview_license_title"
settings:controller="com.android.settings.deviceinfo.legal.WebViewLicensePreferenceController" />
<Preference
android:key="wallpaper_attributions"
android:title="@string/wallpaper_attributions"
android:summary="@string/wallpaper_attributions_values"/>
android:summary="@string/wallpaper_attributions_values"
settings:controller="com.android.settings.deviceinfo.legal.WallpaperAttributionsPreferenceController" />
</PreferenceScreen>

View File

@@ -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,47 +29,26 @@ 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 =
@Override
protected int getPreferenceScreenResId() {
return R.xml.about_legal;
}
public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {
@Override
@@ -87,44 +58,5 @@ public class LegalSettings extends SettingsPreferenceFragment implements Indexab
sir.xmlResId = R.xml.about_legal;
return Arrays.asList(sir);
}
@Override
public List<String> getNonIndexableKeys(Context context) {
final List<String> keys = super.getNonIndexableKeys(context);
if (!checkIntentAction(context, "android.settings.TERMS")) {
keys.add(KEY_TERMS);
}
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<ResolveInfo> 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;
}
};
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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,36 +27,19 @@ 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<String> niks =
LegalSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
@@ -66,19 +48,4 @@ public class LegalSettingsTest {
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);
}
}

View File

@@ -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<ResolveInfo> 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<ResolveInfo> 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;
}
}

View File

@@ -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<ResolveInfo> 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<ResolveInfo> 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;
}
}

View File

@@ -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<ResolveInfo> 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<ResolveInfo> 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;
}
}

View File

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

View File

@@ -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<ResolveInfo> 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<ResolveInfo> 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;
}
}