Add UI for mainline modules licenses.
Added a module licenses option that lives in Legal information settings. Clicking that option opens module licenses page, which displays every module by name, filtered to exclude modules without license files. Clicking a module in the list opens HTMLViewer. Created ModuleLicensesProvider, a new ContentProvider that serves as a redirect for the Uris sent to HTMLViewer so that they open asset files. In order to provide the redirect, the provider will write the license file to a file in Settings' cache directory when the license does not exist in the cache or is outdated. The provider then opens that cached file. Fixes: 135183006 Test: robotests Change-Id: I7d69da34780c8c4efb150d0c0411078c12bc80d8
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ModuleInfo;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ModuleLicensePreferenceTest {
|
||||
public static final String PACKAGE_NAME = "com.android.test_package";
|
||||
public static final String NAME = "Test Package";
|
||||
private Context mContext;
|
||||
private ModuleInfo mModuleInfo;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = Robolectric.setupActivity(Activity.class);
|
||||
mModuleInfo = new ModuleInfo();
|
||||
mModuleInfo.setPackageName(PACKAGE_NAME);
|
||||
mModuleInfo.setName(NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ctor_properKeyAndTitle() {
|
||||
ModuleLicensePreference pref = new ModuleLicensePreference(mContext, mModuleInfo);
|
||||
|
||||
assertThat(pref.getKey()).isEqualTo(PACKAGE_NAME);
|
||||
assertThat(pref.getTitle()).isEqualTo(NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onClick_sendsCorrectIntent() {
|
||||
ModuleLicensePreference pref = new ModuleLicensePreference(mContext, mModuleInfo);
|
||||
|
||||
pref.onClick();
|
||||
|
||||
Intent intent = ShadowApplication.getInstance().getNextStartedActivity();
|
||||
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_VIEW);
|
||||
assertThat(intent.getData())
|
||||
.isEqualTo(ModuleLicenseProvider.getUriForPackage(PACKAGE_NAME));
|
||||
assertThat(intent.getType()).isEqualTo(ModuleLicenseProvider.LICENSE_FILE_MIME_TYPE);
|
||||
assertThat(intent.getCharSequenceExtra(Intent.EXTRA_TITLE)).isEqualTo(NAME);
|
||||
assertThat(intent.getFlags()).isEqualTo(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
assertThat(intent.getCategories()).contains(Intent.CATEGORY_DEFAULT);
|
||||
assertThat(intent.getPackage()).isEqualTo("com.android.htmlviewer");
|
||||
}
|
||||
}
|
@@ -0,0 +1,393 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.android.settings.deviceinfo.legal.ModuleLicenseProvider.LICENSE_FILE_NAME;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ModuleInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.res.Resources;
|
||||
import android.net.Uri;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ModuleLicenseProviderTest {
|
||||
public static final String PACKAGE_NAME = "com.android.test_package";
|
||||
@Test
|
||||
public void onCreate_returnsTrue() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
assertThat(provider.onCreate()).isTrue();
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void query_throwsUnsupportedOperationException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.query(null, null, null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void insert_throwsUnsupportedOperationException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.insert(null, null);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void delete_throwsUnsupportedOperationException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.delete(null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void update_throwsUnsupportedOperationException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.update(null, null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_notContentScheme_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme("badscheme")
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_invalidAuthority_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority("notmyauthority")
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_emptyPath_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_missingPackageName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_missingFileName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_incorrectFileName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath("badname.txt")
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void getType_packageNotAModule_throwsIllegalArgumentException()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
ModuleLicenseProvider provider = spy(new ModuleLicenseProvider());
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(provider.getContext()).thenReturn(context);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
when(packageManager.getModuleInfo(PACKAGE_NAME, 0))
|
||||
.thenThrow(new PackageManager.NameNotFoundException());
|
||||
|
||||
provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getType_validUri_returnsHtmlMimeType()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
ModuleLicenseProvider provider = spy(new ModuleLicenseProvider());
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(provider.getContext()).thenReturn(context);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
when(packageManager.getModuleInfo(PACKAGE_NAME, 0))
|
||||
.thenReturn(new ModuleInfo());
|
||||
|
||||
assertThat(provider.getType(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build())).isEqualTo(ModuleLicenseProvider.LICENSE_FILE_MIME_TYPE);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_notContentScheme_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme("badscheme")
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_invalidAuthority_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority("notmyauthority")
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_emptyPath_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_missingPackageName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_missingFileName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_incorrectFileName_throwsIllegalArgumentException() {
|
||||
ModuleLicenseProvider provider = new ModuleLicenseProvider();
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath("badname.txt")
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_packageNotAModule_throwsIllegalArgumentException()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
ModuleLicenseProvider provider = spy(new ModuleLicenseProvider());
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(provider.getContext()).thenReturn(context);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
when(packageManager.getModuleInfo(PACKAGE_NAME, 0))
|
||||
.thenThrow(new PackageManager.NameNotFoundException());
|
||||
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build(), "r");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void openFile_validUri_notReadMode_throwsIllegalArgumentException()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
ModuleLicenseProvider provider = spy(new ModuleLicenseProvider());
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(provider.getContext()).thenReturn(context);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
when(packageManager.getModuleInfo(PACKAGE_NAME, 0))
|
||||
.thenReturn(new ModuleInfo());
|
||||
|
||||
provider.openFile(new Uri.Builder()
|
||||
.scheme(ContentResolver.SCHEME_CONTENT)
|
||||
.authority(ModuleLicenseProvider.AUTHORITY)
|
||||
.appendPath(PACKAGE_NAME)
|
||||
.appendPath(LICENSE_FILE_NAME)
|
||||
.build(), "badmode");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCachedHtmlFileOutdated_packageNotInSharedPrefs_returnTrue()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
Context context = RuntimeEnvironment.application;
|
||||
context.getSharedPreferences(ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit().clear().commit();
|
||||
|
||||
assertThat(ModuleLicenseProvider.isCachedHtmlFileOutdated(context, PACKAGE_NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCachedHtmlFileOutdated_versionCodeDiffersFromSharedPref_returnTrue()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
Context context = spy(RuntimeEnvironment.application);
|
||||
SharedPreferences.Editor editor = context.getSharedPreferences(
|
||||
ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit();
|
||||
editor.clear().commit();
|
||||
editor.putLong(PACKAGE_NAME, 900L).commit();
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
doReturn(packageManager).when(context).getPackageManager();
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.setLongVersionCode(1000L);
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
|
||||
assertThat(ModuleLicenseProvider.isCachedHtmlFileOutdated(context, PACKAGE_NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCachedHtmlFileOutdated_fileDoesNotExist_returnTrue()
|
||||
throws PackageManager.NameNotFoundException {
|
||||
Context context = spy(RuntimeEnvironment.application);
|
||||
context.getSharedPreferences(ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit().clear().commit();
|
||||
SharedPreferences.Editor editor = context.getSharedPreferences(
|
||||
ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit();
|
||||
editor.clear().commit();
|
||||
editor.putLong(PACKAGE_NAME, 1000L).commit();
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
doReturn(packageManager).when(context).getPackageManager();
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.setLongVersionCode(1000L);
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
new File(context.getCacheDir() + "/" + PACKAGE_NAME, LICENSE_FILE_NAME).delete();
|
||||
|
||||
assertThat(ModuleLicenseProvider.isCachedHtmlFileOutdated(context, PACKAGE_NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCachedHtmlFileOutdated_fileIsEmpty_returnTrue()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = spy(RuntimeEnvironment.application);
|
||||
context.getSharedPreferences(ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit().clear().commit();
|
||||
SharedPreferences.Editor editor = context.getSharedPreferences(
|
||||
ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit();
|
||||
editor.clear().commit();
|
||||
editor.putLong(PACKAGE_NAME, 1000L).commit();
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
doReturn(packageManager).when(context).getPackageManager();
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.setLongVersionCode(1000L);
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
new File(context.getCacheDir(), PACKAGE_NAME).mkdir();
|
||||
File file = new File(context.getCacheDir() + "/" + PACKAGE_NAME, LICENSE_FILE_NAME);
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
|
||||
assertThat(ModuleLicenseProvider.isCachedHtmlFileOutdated(context, PACKAGE_NAME)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isCachedHtmlFileOutdated_notOutdated_returnFalse()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = spy(RuntimeEnvironment.application);
|
||||
context.getSharedPreferences(ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit().clear().commit();
|
||||
SharedPreferences.Editor editor = context.getSharedPreferences(
|
||||
ModuleLicenseProvider.PREFS_NAME, Context.MODE_PRIVATE)
|
||||
.edit();
|
||||
editor.clear().commit();
|
||||
editor.putLong(PACKAGE_NAME, 1000L).commit();
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
doReturn(packageManager).when(context).getPackageManager();
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.setLongVersionCode(1000L);
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
new File(context.getCacheDir(), PACKAGE_NAME).mkdir();
|
||||
File file = new File(context.getCacheDir() + "/" + PACKAGE_NAME, LICENSE_FILE_NAME);
|
||||
file.delete();
|
||||
file.createNewFile();
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
|
||||
writer.write("test");
|
||||
}
|
||||
|
||||
assertThat(ModuleLicenseProvider.isCachedHtmlFileOutdated(context, PACKAGE_NAME)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUriForPackage_returnsProperlyFormattedUri() {
|
||||
assertThat(ModuleLicenseProvider.getUriForPackage(PACKAGE_NAME))
|
||||
.isEqualTo(Uri.parse("content://com.android.settings.module_licenses/com.android.test_package/NOTICE.html"));
|
||||
}
|
||||
}
|
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.android.settings.deviceinfo.legal.ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ModuleInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ModuleLicensesListPreferenceControllerTest {
|
||||
private static final String PREFERENCE_KEY = "key";
|
||||
private static final String PACKAGE_NAME = "com.android.test_package";
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_validLicenses_returnsAvailable()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
ModuleInfo moduleInfo = new ModuleInfo();
|
||||
moduleInfo.setPackageName(PACKAGE_NAME);
|
||||
when(packageManager.getInstalledModules(0))
|
||||
.thenReturn(Collections.singletonList(moduleInfo));
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX)).thenReturn(
|
||||
packageInfo);
|
||||
Resources resources = mock(Resources.class);
|
||||
when(packageManager.getResourcesForApplication(applicationInfo)).thenReturn(resources);
|
||||
AssetManager manager = mock(AssetManager.class);
|
||||
when(resources.getAssets()).thenReturn(manager);
|
||||
when(manager.list("")).thenReturn(new String[]{GZIPPED_LICENSE_FILE_NAME});
|
||||
|
||||
ModuleLicensesListPreferenceController controller =
|
||||
new ModuleLicensesListPreferenceController(context, PREFERENCE_KEY);
|
||||
assertThat(controller.getAvailabilityStatus())
|
||||
.isEqualTo(BasePreferenceController.AVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noModules_returnsConditionallyUnavailable() {
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
when(packageManager.getInstalledModules(0))
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
ModuleLicensesListPreferenceController controller =
|
||||
new ModuleLicensesListPreferenceController(context, PREFERENCE_KEY);
|
||||
assertThat(controller.getAvailabilityStatus())
|
||||
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAvailabilityStatus_noLicenses_returnsConditionallyUnavailable()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = mock(Context.class);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
ModuleInfo moduleInfo = new ModuleInfo();
|
||||
moduleInfo.setPackageName(PACKAGE_NAME);
|
||||
when(packageManager.getInstalledModules(0))
|
||||
.thenReturn(Collections.singletonList(moduleInfo));
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
when(packageManager.getPackageInfo(PACKAGE_NAME, PackageManager.MATCH_APEX)).thenReturn(
|
||||
packageInfo);
|
||||
Resources resources = mock(Resources.class);
|
||||
when(packageManager.getResourcesForApplication(applicationInfo)).thenReturn(resources);
|
||||
AssetManager manager = mock(AssetManager.class);
|
||||
when(resources.getAssets()).thenReturn(manager);
|
||||
when(manager.list("")).thenReturn(new String[]{});
|
||||
|
||||
ModuleLicensesListPreferenceController controller =
|
||||
new ModuleLicensesListPreferenceController(context, PREFERENCE_KEY);
|
||||
assertThat(controller.getAvailabilityStatus())
|
||||
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
|
||||
}
|
||||
}
|
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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.android.settings.deviceinfo.legal.ModuleLicenseProvider.GZIPPED_LICENSE_FILE_NAME;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.ModuleInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.content.res.Resources;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class ModuleLicensesPreferenceControllerTest {
|
||||
|
||||
private static final String PREFERENCE_KEY = "key";
|
||||
private static final String MODULE_1_NAME = "Module 1";
|
||||
private static final String MODULE_1_PACKAGE_NAME = "com.android.module_one";
|
||||
private static final String MODULE_2_NAME = "Module 2";
|
||||
private static final String MODULE_2_PACKAGE_NAME = "com.android.module_two";
|
||||
private ModuleInfo mModuleOne;
|
||||
private ModuleInfo mModuleTwo;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mModuleOne = new ModuleInfo();
|
||||
mModuleOne.setName(MODULE_1_NAME);
|
||||
mModuleOne.setPackageName(MODULE_1_PACKAGE_NAME);
|
||||
mModuleTwo = new ModuleInfo();
|
||||
mModuleTwo.setName(MODULE_2_NAME);
|
||||
mModuleTwo.setPackageName(MODULE_2_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_alphabeticalOrder()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = mock(Context.class);
|
||||
ModuleLicensesPreferenceController controller =
|
||||
new ModuleLicensesPreferenceController(context, PREFERENCE_KEY);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
PreferenceScreen screen = mock(PreferenceScreen.class);
|
||||
PreferenceGroup group = spy(new MockPreferenceGroup(RuntimeEnvironment.application, null));
|
||||
when(screen.findPreference(PREFERENCE_KEY)).thenReturn(group);
|
||||
when(group.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
|
||||
when(packageManager.getInstalledModules(0))
|
||||
.thenReturn(Arrays.asList(mModuleTwo, mModuleOne));
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
when(packageManager.getPackageInfo(MODULE_1_PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
when(packageManager.getPackageInfo(MODULE_2_PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
Resources resources = mock(Resources.class);
|
||||
when(packageManager.getResourcesForApplication(applicationInfo)).thenReturn(resources);
|
||||
AssetManager manager = mock(AssetManager.class);
|
||||
when(resources.getAssets()).thenReturn(manager);
|
||||
when(manager.list("")).thenReturn(new String[]{GZIPPED_LICENSE_FILE_NAME});
|
||||
|
||||
controller.displayPreference(screen);
|
||||
|
||||
assertThat(group.getPreferenceCount()).isEqualTo(2);
|
||||
assertThat(group.getPreference(0).getTitle()).isEqualTo(MODULE_1_NAME);
|
||||
assertThat(group.getPreference(1).getTitle()).isEqualTo(MODULE_2_NAME);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_includeOnlyModulesWithLicenseFile()
|
||||
throws PackageManager.NameNotFoundException, IOException {
|
||||
Context context = mock(Context.class);
|
||||
ModuleLicensesPreferenceController controller =
|
||||
new ModuleLicensesPreferenceController(context, PREFERENCE_KEY);
|
||||
PackageManager packageManager = mock(PackageManager.class);
|
||||
when(context.getPackageManager()).thenReturn(packageManager);
|
||||
PreferenceScreen screen = mock(PreferenceScreen.class);
|
||||
PreferenceGroup group = spy(new MockPreferenceGroup(RuntimeEnvironment.application, null));
|
||||
when(screen.findPreference(PREFERENCE_KEY)).thenReturn(group);
|
||||
when(group.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
|
||||
when(packageManager.getInstalledModules(0))
|
||||
.thenReturn(Arrays.asList(mModuleTwo, mModuleOne));
|
||||
PackageInfo packageInfo = new PackageInfo();
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
when(packageManager.getPackageInfo(MODULE_1_PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo);
|
||||
Resources resources = mock(Resources.class);
|
||||
when(packageManager.getResourcesForApplication(applicationInfo)).thenReturn(resources);
|
||||
AssetManager manager = mock(AssetManager.class);
|
||||
when(resources.getAssets()).thenReturn(manager);
|
||||
when(manager.list("")).thenReturn(new String[]{GZIPPED_LICENSE_FILE_NAME});
|
||||
PackageInfo packageInfo2 = new PackageInfo();
|
||||
ApplicationInfo applicationInfo2 = new ApplicationInfo();
|
||||
packageInfo2.applicationInfo = applicationInfo2;
|
||||
when(packageManager.getPackageInfo(MODULE_2_PACKAGE_NAME, PackageManager.MATCH_APEX))
|
||||
.thenReturn(packageInfo2);
|
||||
Resources resources2 = mock(Resources.class);
|
||||
when(packageManager.getResourcesForApplication(applicationInfo2)).thenReturn(resources2);
|
||||
AssetManager manager2 = mock(AssetManager.class);
|
||||
when(resources2.getAssets()).thenReturn(manager2);
|
||||
when(manager2.list("")).thenReturn(new String[]{});
|
||||
|
||||
controller.displayPreference(screen);
|
||||
|
||||
assertThat(group.getPreferenceCount()).isEqualTo(1);
|
||||
assertThat(group.getPreference(0).getTitle()).isEqualTo(MODULE_1_NAME);
|
||||
}
|
||||
|
||||
private static class MockPreferenceGroup extends PreferenceGroup {
|
||||
List<Preference> mList = new ArrayList<>();
|
||||
|
||||
public MockPreferenceGroup(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addPreference(Preference preference) {
|
||||
mList.add(preference);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPreferenceCount() {
|
||||
return mList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Preference getPreference(int index) {
|
||||
return mList.get(index);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user