Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 android.content.pm.cts;
     18 
     19 import static android.content.pm.ApplicationInfo.CATEGORY_MAPS;
     20 import static android.content.pm.ApplicationInfo.CATEGORY_PRODUCTIVITY;
     21 import static android.content.pm.ApplicationInfo.CATEGORY_UNDEFINED;
     22 import static android.content.pm.ApplicationInfo.FLAG_MULTIARCH;
     23 import static android.content.pm.ApplicationInfo.FLAG_SUPPORTS_RTL;
     24 
     25 import static org.junit.Assert.assertArrayEquals;
     26 import static org.junit.Assert.assertEquals;
     27 import static org.junit.Assert.assertFalse;
     28 import static org.junit.Assert.assertNotNull;
     29 import static org.junit.Assert.assertNull;
     30 import static org.junit.Assert.assertTrue;
     31 
     32 import android.content.Context;
     33 import android.content.cts.R;
     34 import android.content.pm.ApplicationInfo;
     35 import android.content.pm.PackageManager.NameNotFoundException;
     36 import android.os.Parcel;
     37 import android.os.Process;
     38 import android.os.UserHandle;
     39 import android.platform.test.annotations.AppModeFull;
     40 import android.util.StringBuilderPrinter;
     41 
     42 import androidx.test.InstrumentationRegistry;
     43 import androidx.test.runner.AndroidJUnit4;
     44 
     45 import org.junit.Before;
     46 import org.junit.Test;
     47 import org.junit.runner.RunWith;
     48 
     49 /**
     50  * Test {@link ApplicationInfo}.
     51  */
     52 @RunWith(AndroidJUnit4.class)
     53 @AppModeFull // TODO(Instant) Figure out which APIs should work.
     54 public class ApplicationInfoTest {
     55     private static final String SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME = "com.android.cts.stub";
     56     private static final String DIRECT_BOOT_UNAWARE_PACKAGE_NAME =
     57             "android.content.cts.directbootunaware";
     58     private static final String PARTIALLY_DIRECT_BOOT_AWARE_PACKAGE_NAME =
     59             "android.content.cts.partiallydirectbootaware";
     60 
     61     private ApplicationInfo mApplicationInfo;
     62     private String mPackageName;
     63 
     64     @Before
     65     public void setUp() throws Exception {
     66         mPackageName = getContext().getPackageName();
     67     }
     68 
     69     private Context getContext() {
     70         return InstrumentationRegistry.getInstrumentation().getTargetContext();
     71     }
     72 
     73     @Test
     74     public void testConstructor() {
     75         ApplicationInfo info = new ApplicationInfo();
     76         // simple test to ensure packageName is copied by copy constructor
     77         // TODO: consider expanding to check all member variables
     78         info.packageName = mPackageName;
     79         ApplicationInfo copy = new ApplicationInfo(info);
     80         assertEquals(info.packageName, copy.packageName);
     81     }
     82 
     83     @Test
     84     public void testWriteToParcel() throws NameNotFoundException {
     85         mApplicationInfo = getContext().getPackageManager().getApplicationInfo(mPackageName, 0);
     86 
     87         Parcel p = Parcel.obtain();
     88         mApplicationInfo.writeToParcel(p, 0);
     89 
     90         p.setDataPosition(0);
     91         ApplicationInfo info = ApplicationInfo.CREATOR.createFromParcel(p);
     92         assertEquals(mApplicationInfo.taskAffinity, info.taskAffinity);
     93         assertEquals(mApplicationInfo.permission, info.permission);
     94         assertEquals(mApplicationInfo.processName, info.processName);
     95         assertEquals(mApplicationInfo.className, info.className);
     96         assertEquals(mApplicationInfo.theme, info.theme);
     97         assertEquals(mApplicationInfo.flags, info.flags);
     98         assertEquals(mApplicationInfo.sourceDir, info.sourceDir);
     99         assertEquals(mApplicationInfo.publicSourceDir, info.publicSourceDir);
    100         assertEquals(mApplicationInfo.sharedLibraryFiles, info.sharedLibraryFiles);
    101         assertEquals(mApplicationInfo.dataDir, info.dataDir);
    102         assertEquals(mApplicationInfo.uid, info.uid);
    103         assertEquals(mApplicationInfo.enabled, info.enabled);
    104         assertEquals(mApplicationInfo.manageSpaceActivityName, info.manageSpaceActivityName);
    105         assertEquals(mApplicationInfo.descriptionRes, info.descriptionRes);
    106     }
    107 
    108     @Test
    109     public void testToString() {
    110         mApplicationInfo = new ApplicationInfo();
    111         assertNotNull(mApplicationInfo.toString());
    112     }
    113 
    114     @Test
    115     public void testDescribeContents() throws NameNotFoundException {
    116        mApplicationInfo = getContext().getPackageManager().getApplicationInfo(mPackageName, 0);
    117 
    118         assertEquals(0, mApplicationInfo.describeContents());
    119     }
    120 
    121     @Test
    122     public void testDump() {
    123         mApplicationInfo = new ApplicationInfo();
    124 
    125         StringBuilder sb = new StringBuilder();
    126         assertEquals(0, sb.length());
    127         StringBuilderPrinter p = new StringBuilderPrinter(sb);
    128 
    129         String prefix = "";
    130         mApplicationInfo.dump(p, prefix);
    131         assertNotNull(sb.toString());
    132         assertTrue(sb.length() > 0);
    133     }
    134 
    135     @Test
    136     public void testLoadDescription() throws NameNotFoundException {
    137         mApplicationInfo = getContext().getPackageManager().getApplicationInfo(mPackageName, 0);
    138 
    139         assertNull(mApplicationInfo.loadDescription(getContext().getPackageManager()));
    140 
    141         mApplicationInfo.descriptionRes = R.string.hello_world;
    142         assertEquals(getContext().getResources().getString(R.string.hello_world),
    143                 mApplicationInfo.loadDescription(getContext().getPackageManager()));
    144     }
    145 
    146     @Test
    147     public void verifyOwnInfo() throws NameNotFoundException {
    148         mApplicationInfo = getContext().getPackageManager().getApplicationInfo(mPackageName, 0);
    149 
    150         assertEquals("Android TestCase", mApplicationInfo.nonLocalizedLabel);
    151         assertEquals(R.drawable.size_48x48, mApplicationInfo.icon);
    152         assertEquals("android.content.cts.MockApplication", mApplicationInfo.name);
    153         int flags = FLAG_MULTIARCH | FLAG_SUPPORTS_RTL;
    154         assertEquals(flags, mApplicationInfo.flags & flags);
    155         assertEquals(CATEGORY_PRODUCTIVITY, mApplicationInfo.category);
    156     }
    157 
    158     @Test
    159     public void verifyDefaultValues() throws NameNotFoundException {
    160         // The application "com.android.cts.stub" does not have any attributes set
    161         mApplicationInfo = getContext().getPackageManager().getApplicationInfo(
    162                 SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME, 0);
    163         int currentUserId = Process.myUserHandle().getIdentifier();
    164 
    165         assertNull(mApplicationInfo.className);
    166         assertNull(mApplicationInfo.permission);
    167         assertEquals(SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME, mApplicationInfo.packageName);
    168         assertEquals(SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME, mApplicationInfo.processName);
    169         assertEquals(SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME, mApplicationInfo.taskAffinity);
    170         assertTrue(UserHandle.isApp(mApplicationInfo.uid));
    171         assertEquals(0, mApplicationInfo.theme);
    172         assertEquals(0, mApplicationInfo.requiresSmallestWidthDp);
    173         assertEquals(0, mApplicationInfo.compatibleWidthLimitDp);
    174         assertEquals(0, mApplicationInfo.largestWidthLimitDp);
    175         assertNotNull(mApplicationInfo.sourceDir);
    176         assertEquals(mApplicationInfo.sourceDir, mApplicationInfo.publicSourceDir);
    177         assertNull(mApplicationInfo.splitSourceDirs);
    178         assertArrayEquals(mApplicationInfo.splitSourceDirs, mApplicationInfo.splitPublicSourceDirs);
    179         assertEquals("/data/user/" + currentUserId + "/" + SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME,
    180                 mApplicationInfo.dataDir);
    181         assertEquals("/data/user_de/" + currentUserId + "/" + SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME,
    182                 mApplicationInfo.deviceProtectedDataDir);
    183         assertEquals("/data/user/" + currentUserId + "/" + SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME,
    184                 mApplicationInfo.credentialProtectedDataDir);
    185         assertNull(mApplicationInfo.sharedLibraryFiles);
    186         assertTrue(mApplicationInfo.enabled);
    187         assertNull(mApplicationInfo.manageSpaceActivityName);
    188         assertEquals(0, mApplicationInfo.descriptionRes);
    189         assertEquals(0, mApplicationInfo.uiOptions);
    190         assertEquals(CATEGORY_UNDEFINED, mApplicationInfo.category);
    191     }
    192 
    193     @Test(expected=IllegalArgumentException.class)
    194     public void setOwnAppCategory() throws Exception {
    195         getContext().getPackageManager().setApplicationCategoryHint(getContext().getPackageName(),
    196                 CATEGORY_MAPS);
    197     }
    198 
    199     @Test(expected=IllegalArgumentException.class)
    200     public void setAppCategoryByNotInstaller() throws Exception {
    201         getContext().getPackageManager().setApplicationCategoryHint(
    202                 SYNC_ACCOUNT_ACCESS_STUB_PACKAGE_NAME, CATEGORY_MAPS);
    203     }
    204 
    205     @Test
    206     public void testDirectBootUnawareAppIsNotEncryptionAware() throws Exception {
    207         ApplicationInfo applicationInfo = getContext().getPackageManager().getApplicationInfo(
    208                 DIRECT_BOOT_UNAWARE_PACKAGE_NAME, 0);
    209         assertFalse(applicationInfo.isEncryptionAware());
    210     }
    211 
    212     @Test
    213     public void testPartiallyDirectBootAwareAppIsEncryptionAware() throws Exception {
    214         ApplicationInfo applicationInfo = getContext().getPackageManager().getApplicationInfo(
    215                 PARTIALLY_DIRECT_BOOT_AWARE_PACKAGE_NAME, 0);
    216         assertTrue(applicationInfo.isEncryptionAware());
    217     }
    218 }
    219