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(); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testReadonlyDisabledWithDefinedFeatures() { |
| 75 | // Always fall back to the PackageManager for defined, explicit features queries. |
| 76 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(true); |
| 77 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isTrue(); |
| 78 | |
| 79 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)).thenReturn(false); |
| 80 | assertThat(RwFeatures.hasFeatureWatch(mContext)).isFalse(); |
| 81 | |
| 82 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_WIFI, 0)).thenReturn(true); |
| 83 | assertThat(RwFeatures.hasFeatureWifi(mContext)).isTrue(); |
| 84 | |
| 85 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_VULKAN, 0)).thenReturn(false); |
| 86 | assertThat(RwFeatures.hasFeatureVulkan(mContext)).isFalse(); |
| 87 | |
| 88 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTO, 0)).thenReturn(false); |
| 89 | assertThat(RwFeatures.hasFeatureAuto(mContext)).isFalse(); |
| 90 | |
| 91 | // For defined and undefined features, conditional queries should report null (unknown). |
| 92 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isNull(); |
| 93 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isNull(); |
| 94 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isNull(); |
| 95 | assertThat(RwFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isNull(); |
| 96 | assertThat(RwFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testReadonlyWithDefinedFeatures() { |
| 101 | // Always use the build-time feature version for defined, explicit feature queries, never |
| 102 | // falling back to the runtime query. |
| 103 | assertThat(RoFeatures.hasFeatureWatch(mContext)).isTrue(); |
| 104 | assertThat(RoFeatures.hasFeatureWifi(mContext)).isTrue(); |
| 105 | assertThat(RoFeatures.hasFeatureVulkan(mContext)).isFalse(); |
| 106 | assertThat(RoFeatures.hasFeatureAuto(mContext)).isFalse(); |
| 107 | verify(mPackageManager, never()).hasSystemFeature(anyString(), anyInt()); |
| 108 | |
| 109 | // For defined feature types, conditional queries should reflect the build-time versions. |
| 110 | // VERSION=1 |
| 111 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, -1)).isTrue(); |
| 112 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 0)).isTrue(); |
| 113 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WATCH, 100)).isFalse(); |
| 114 | |
| 115 | // VERSION=0 |
| 116 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, -1)).isTrue(); |
| 117 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 0)).isTrue(); |
| 118 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_WIFI, 100)).isFalse(); |
| 119 | |
| 120 | // VERSION=-1 |
| 121 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, -1)).isTrue(); |
| 122 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 0)).isFalse(); |
| 123 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_VULKAN, 100)).isFalse(); |
| 124 | |
| 125 | // DISABLED |
| 126 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, -1)).isFalse(); |
| 127 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 0)).isFalse(); |
| 128 | assertThat(RoFeatures.maybeHasFeature(PackageManager.FEATURE_AUTO, 100)).isFalse(); |
| 129 | |
| 130 | // For undefined types, conditional queries should report null (unknown). |
| 131 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", -1)).isNull(); |
| 132 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", 0)).isNull(); |
| 133 | assertThat(RoFeatures.maybeHasFeature("com.arbitrary.feature", 100)).isNull(); |
| 134 | } |
| 135 | } |