1 /* 2 * Copyright (C) 2017 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.server.pm.dex; 18 19 20 import static com.android.server.pm.PackageManagerServiceCompilerMapping.getCompilerFilterForReason; 21 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 32 import com.android.server.pm.PackageManagerService; 33 import com.android.server.pm.PackageManagerServiceCompilerMapping; 34 35 @RunWith(AndroidJUnit4.class) 36 @SmallTest 37 public class DexoptOptionsTests { 38 private final static String mPackageName = "test.android.com"; 39 private final static String mCompilerFilter = 40 PackageManagerServiceCompilerMapping.getDefaultCompilerFilter(); 41 private final static String mSplitName = "split-A.apk"; 42 43 @Test 44 public void testCreateDexoptOptionsEmpty() { 45 DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, /*flags*/ 0); 46 assertEquals(mPackageName, opt.getPackageName()); 47 assertEquals(mCompilerFilter, opt.getCompilerFilter()); 48 assertEquals(null, opt.getSplitName()); 49 assertFalse(opt.isBootComplete()); 50 assertFalse(opt.isCheckForProfileUpdates()); 51 assertFalse(opt.isDexoptOnlySecondaryDex()); 52 assertFalse(opt.isDexoptOnlySharedDex()); 53 assertFalse(opt.isDowngrade()); 54 assertFalse(opt.isForce()); 55 } 56 57 @Test 58 public void testCreateDexoptOptionsFull() { 59 int flags = 60 DexoptOptions.DEXOPT_FORCE | 61 DexoptOptions.DEXOPT_BOOT_COMPLETE | 62 DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | 63 DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | 64 DexoptOptions.DEXOPT_ONLY_SHARED_DEX | 65 DexoptOptions.DEXOPT_DOWNGRADE | 66 DexoptOptions.DEXOPT_AS_SHARED_LIBRARY; 67 68 DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, flags); 69 assertEquals(mPackageName, opt.getPackageName()); 70 assertEquals(mCompilerFilter, opt.getCompilerFilter()); 71 assertEquals(null, opt.getSplitName()); 72 assertTrue(opt.isBootComplete()); 73 assertTrue(opt.isCheckForProfileUpdates()); 74 assertTrue(opt.isDexoptOnlySecondaryDex()); 75 assertTrue(opt.isDexoptOnlySharedDex()); 76 assertTrue(opt.isDowngrade()); 77 assertTrue(opt.isForce()); 78 assertTrue(opt.isDexoptAsSharedLibrary()); 79 } 80 81 @Test 82 public void testCreateDexoptOptionsReason() { 83 int flags = 84 DexoptOptions.DEXOPT_FORCE | 85 DexoptOptions.DEXOPT_BOOT_COMPLETE | 86 DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES; 87 88 int[] reasons = new int[] { 89 PackageManagerService.REASON_FIRST_BOOT, 90 PackageManagerService.REASON_BOOT, 91 PackageManagerService.REASON_INSTALL, 92 PackageManagerService.REASON_BACKGROUND_DEXOPT, 93 PackageManagerService.REASON_AB_OTA, 94 PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE,}; 95 96 for (int reason : reasons) { 97 DexoptOptions opt = new DexoptOptions(mPackageName, reason, flags); 98 assertEquals(mPackageName, opt.getPackageName()); 99 assertEquals(getCompilerFilterForReason(reason), opt.getCompilerFilter()); 100 assertEquals(null, opt.getSplitName()); 101 assertTrue(opt.isBootComplete()); 102 assertTrue(opt.isCheckForProfileUpdates()); 103 assertFalse(opt.isDexoptOnlySecondaryDex()); 104 assertFalse(opt.isDexoptOnlySharedDex()); 105 assertFalse(opt.isDowngrade()); 106 assertTrue(opt.isForce()); 107 assertFalse(opt.isDexoptAsSharedLibrary()); 108 } 109 } 110 111 @Test 112 public void testCreateDexoptOptionsSplit() { 113 int flags = DexoptOptions.DEXOPT_FORCE | DexoptOptions.DEXOPT_BOOT_COMPLETE; 114 115 DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, mSplitName, flags); 116 assertEquals(mPackageName, opt.getPackageName()); 117 assertEquals(mCompilerFilter, opt.getCompilerFilter()); 118 assertEquals(mSplitName, opt.getSplitName()); 119 assertTrue(opt.isBootComplete()); 120 assertFalse(opt.isCheckForProfileUpdates()); 121 assertFalse(opt.isDexoptOnlySecondaryDex()); 122 assertFalse(opt.isDexoptOnlySharedDex()); 123 assertFalse(opt.isDowngrade()); 124 assertTrue(opt.isForce()); 125 assertFalse(opt.isDexoptAsSharedLibrary()); 126 } 127 128 @Test 129 public void testCreateDexoptInvalid() { 130 boolean gotException = false; 131 try { 132 int invalidFlags = 999; 133 new DexoptOptions(mPackageName, mCompilerFilter, invalidFlags); 134 } catch (IllegalArgumentException ignore) { 135 gotException = true; 136 } 137 138 assertTrue(gotException); 139 } 140 }