Jared Duke | 230d650 | 2024-07-31 21:35:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemfeatures; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.anyInt; |
| 22 | import static org.mockito.Mockito.anyString; |
| 23 | import static org.mockito.Mockito.never; |
| 24 | import static org.mockito.Mockito.verify; |
| 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.content.pm.PackageManager; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Rule; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | import org.junit.runners.JUnit4; |
| 35 | import org.mockito.Mock; |
| 36 | import org.mockito.junit.MockitoJUnit; |
| 37 | import org.mockito.junit.MockitoRule; |
| 38 | |
| 39 | @RunWith(JUnit4.class) |
| 40 | public class SystemFeaturesGeneratorTest { |
| 41 | |
| 42 | @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 43 | |
| 44 | @Mock private Context mContext; |
| 45 | @Mock private PackageManager mPackageManager; |
| 46 | |
| 47 | @Before |
| 48 | public void setUp() { |
| 49 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testReadonlyDisabledNoDefinedFeatures() { |
| 54 | // Always report null for conditional queries if readonly codegen is disabled. |
| 55 | assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isNull(); |
| 56 | assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isNull(); |
| 57 | assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull(); |
| 58 | assertThat(RwNoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull(); |
| 59 | assertThat(RwNoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testReadonlyNoDefinedFeatures() { |
| 64 | // If no features are explicitly declared as readonly available, always report |
| 65 | // null for conditional queries. |
| 66 | assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isNull(); |
| 67 | assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isNull(); |
| 68 | assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull(); |
| 69 | assertThat(RoNoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull(); |
| 70 | assertThat(RoNoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
Jared Duke | 97ffe12 | 2024-08-19 23:30:28 +0000 | [diff] [blame^] | 71 | |
| 72 | // Also ensure we fall back to the PackageManager for feature APIs without an accompanying |
| 73 | // versioned feature definition. |
| 74 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(true); |
| 75 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isTrue(); |
| 76 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(false); |
| 77 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isFalse(); |
Jared Duke | 230d650 | 2024-07-31 21:35:24 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testReadonlyDisabledWithDefinedFeatures() { |
| 82 | // Always fall back to the PackageManager for defined, explicit features queries. |
| 83 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(true); |
| 84 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isTrue(); |
| 85 | |
| 86 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(false); |
| 87 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isFalse(); |
| 88 | |
| 89 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI, 0)).thenReturn(true); |
| 90 | assertThat(RwFeatures.hasFeatureWifi(mContext)).isTrue(); |
| 91 | |
| 92 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_VULKAN, 0)).thenReturn(false); |
| 93 | assertThat(RwFeatures.hasFeatureVulkan(mContext)).isFalse(); |
| 94 | |
| 95 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTO, 0)).thenReturn(false); |
| 96 | assertThat(RwFeatures.hasFeatureAuto(mContext)).isFalse(); |
| 97 | |
| 98 | // For defined and undefined features, conditional queries should report null (unknown). |
| 99 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isNull(); |
| 100 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isNull(); |
| 101 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull(); |
| 102 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull(); |
| 103 | assertThat(RwFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testReadonlyWithDefinedFeatures() { |
| 108 | // Always use the build-time feature version for defined, explicit feature queries, never |
| 109 | // falling back to the runtime query. |
| 110 | assertThat(RoFeatures.hasFeatureWatch(mContext)).isTrue(); |
| 111 | assertThat(RoFeatures.hasFeatureWifi(mContext)).isTrue(); |
| 112 | assertThat(RoFeatures.hasFeatureVulkan(mContext)).isFalse(); |
| 113 | assertThat(RoFeatures.hasFeatureAuto(mContext)).isFalse(); |
| 114 | verify(mPackageManager, never()).hasSystemFeature(anyString(), anyInt()); |
| 115 | |
| 116 | // For defined feature types, conditional queries should reflect the build-time versions. |
| 117 | // VERSION=1 |
| 118 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, -1)).isTrue(); |
| 119 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isTrue(); |
| 120 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 100)).isFalse(); |
| 121 | |
| 122 | // VERSION=0 |
| 123 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, -1)).isTrue(); |
| 124 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isTrue(); |
| 125 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 100)).isFalse(); |
| 126 | |
| 127 | // VERSION=-1 |
| 128 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, -1)).isTrue(); |
| 129 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isFalse(); |
| 130 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 100)).isFalse(); |
| 131 | |
| 132 | // DISABLED |
| 133 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, -1)).isFalse(); |
| 134 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isFalse(); |
| 135 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 100)).isFalse(); |
| 136 | |
Jared Duke | 97ffe12 | 2024-08-19 23:30:28 +0000 | [diff] [blame^] | 137 | // For feature APIs without an associated feature definition, conditional queries should |
| 138 | // report null, and explicit queries should report runtime-defined versions. |
| 139 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_PC, 0)).thenReturn(true); |
| 140 | assertThat(RoFeatures.hasFeaturePc(mContext)).isTrue(); |
| 141 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_PC, 0)).thenReturn(false); |
| 142 | assertThat(RoFeatures.hasFeaturePc(mContext)).isFalse(); |
| 143 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_PC, -1)).isNull(); |
| 144 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_PC, 0)).isNull(); |
| 145 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_PC, 100)).isNull(); |
| 146 | |
Jared Duke | 230d650 | 2024-07-31 21:35:24 +0000 | [diff] [blame] | 147 | // For undefined types, conditional queries should report null (unknown). |
| 148 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", -1)).isNull(); |
| 149 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
| 150 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", 100)).isNull(); |
| 151 | } |
| 152 | } |