diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2020-02-01 02:56:19 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2020-02-01 02:56:19 +0000 |
commit | f7a041ca1920c20336c7026172a436f987bfe4c1 (patch) | |
tree | e338a93c4ee4cfd2f34a9a78dbee5348ba1c9423 | |
parent | a2abdb2ca20907dfc250673a0b009e5cbac4c969 (diff) | |
parent | 85dcba06ddc06edde27c79a655a2c6db440f42b2 (diff) | |
download | TV-f7a041ca1920c20336c7026172a436f987bfe4c1.tar.gz |
Merge "Add test for CustomizationManager"
-rw-r--r-- | common/src/com/android/tv/common/customization/CustomizationManager.java | 35 | ||||
-rw-r--r-- | common/tests/robotests/src/com/android/tv/common/customization/CustomizationManagerTest.java | 85 |
2 files changed, 106 insertions, 14 deletions
diff --git a/common/src/com/android/tv/common/customization/CustomizationManager.java b/common/src/com/android/tv/common/customization/CustomizationManager.java index acdad2c9..6ec60fe4 100644 --- a/common/src/com/android/tv/common/customization/CustomizationManager.java +++ b/common/src/com/android/tv/common/customization/CustomizationManager.java @@ -26,6 +26,7 @@ import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.support.annotation.IntDef; import android.support.annotation.Nullable; +import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import android.util.Log; @@ -159,27 +160,33 @@ public class CustomizationManager { private static String getCustomizationPackageName(Context context) { if (sCustomizationPackage == null) { - sCustomizationPackage = ""; + List<PackageInfo> packageInfos = context.getPackageManager() .getPackagesHoldingPermissions(CUSTOMIZE_PERMISSIONS, 0); - if (!packageInfos.isEmpty()) { - /** Iterate through all packages returning the first vendor customizer */ - for (PackageInfo packageInfo : packageInfos) { - if (packageInfo.packageName.startsWith("com.android") == false) { - sCustomizationPackage = packageInfo.packageName; - break; - } - } + sCustomizationPackage = getCustomizationPackageName(packageInfos); + } + + return sCustomizationPackage; + } - /** If no vendor package found, return first in the list */ - if (TextUtils.isEmpty(sCustomizationPackage)) { - sCustomizationPackage = packageInfos.get(0).packageName; + @VisibleForTesting + static String getCustomizationPackageName(List<PackageInfo> packageInfos) { + String packageName = ""; + if (!packageInfos.isEmpty()) { + /** Iterate through all packages returning the first vendor customizer */ + for (PackageInfo packageInfo : packageInfos) { + if (packageInfo.packageName.startsWith("com.android") == false) { + packageName = packageInfo.packageName; + break; } } + /** If no vendor package found, return first in the list */ + if (TextUtils.isEmpty(packageName)) { + packageName = packageInfos.get(0).packageName; + } } - - return sCustomizationPackage; + return packageName; } /** Initialize TV customization options. Run this API only on the main thread. */ diff --git a/common/tests/robotests/src/com/android/tv/common/customization/CustomizationManagerTest.java b/common/tests/robotests/src/com/android/tv/common/customization/CustomizationManagerTest.java new file mode 100644 index 00000000..bbf9f22a --- /dev/null +++ b/common/tests/robotests/src/com/android/tv/common/customization/CustomizationManagerTest.java @@ -0,0 +1,85 @@ +/* + * 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.tv.common.customization; + +import static com.google.common.truth.Truth.assertThat; + +import android.content.pm.PackageInfo; + +import androidx.test.core.content.pm.PackageInfoBuilder; + +import com.android.tv.testing.constants.ConfigConstants; + +import com.google.common.collect.ImmutableList; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** Tests for {@link CustomizationManager}. */ +@RunWith(RobolectricTestRunner.class) +@Config(sdk = ConfigConstants.SDK) +public class CustomizationManagerTest { + + @Test + public void getCustomizationPackageName_empty() { + assertThat(CustomizationManager.getCustomizationPackageName(ImmutableList.of())).isEmpty(); + } + + @Test + public void getCustomizationPackageName_one() { + assertThat( + CustomizationManager.getCustomizationPackageName( + ImmutableList.of(createPackageInfo("com.example.one")))) + .isEqualTo("com.example.one"); + } + + @Test + public void getCustomizationPackageName_first() { + assertThat( + CustomizationManager.getCustomizationPackageName( + ImmutableList.of( + createPackageInfo("com.example.one"), + createPackageInfo("com.example.two")))) + .isEqualTo("com.example.one"); + } + + @Test + public void getCustomizationPackageName_firstNotAndroid() { + assertThat( + CustomizationManager.getCustomizationPackageName( + ImmutableList.of( + createPackageInfo("com.android.one"), + createPackageInfo("com.example.two"), + createPackageInfo("com.example.three")))) + .isEqualTo("com.example.two"); + } + + @Test + public void getCustomizationPackageName_androidOnly() { + assertThat( + CustomizationManager.getCustomizationPackageName( + ImmutableList.of( + createPackageInfo("com.android.one"), + createPackageInfo("com.android.two")))) + .isEqualTo("com.android.one"); + } + + private static PackageInfo createPackageInfo(String packageName) { + return PackageInfoBuilder.newBuilder().setPackageName(packageName).build(); + } +} |