Merge "Migrate LegalSettings to DashboardFragment"
This commit is contained in:
committed by
Android (Google) Code Review
commit
f9a768d6c3
@@ -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<String> niks =
|
||||
LegalSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
|
||||
LegalSettings.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
|
||||
|
||||
final List<String> 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);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user