Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 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 | |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 26 | import androidx.test.filters.SmallTest; |
| 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 29 | import com.android.server.pm.PackageManagerService; |
| 30 | import com.android.server.pm.PackageManagerServiceCompilerMapping; |
| 31 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 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(); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 41 | private final static String mSplitName = "split-A.apk"; |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 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()); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 48 | assertEquals(null, opt.getSplitName()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 49 | assertFalse(opt.isBootComplete()); |
| 50 | assertFalse(opt.isCheckForProfileUpdates()); |
| 51 | assertFalse(opt.isDexoptOnlySecondaryDex()); |
| 52 | assertFalse(opt.isDexoptOnlySharedDex()); |
| 53 | assertFalse(opt.isDowngrade()); |
| 54 | assertFalse(opt.isForce()); |
David Sehr | 2118ec4 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 55 | assertFalse(opt.isDexoptIdleBackgroundJob()); |
Calin Juravle | cc65194 | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 56 | assertFalse(opt.isDexoptInstallWithDexMetadata()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testCreateDexoptOptionsFull() { |
| 61 | int flags = |
| 62 | DexoptOptions.DEXOPT_FORCE | |
| 63 | DexoptOptions.DEXOPT_BOOT_COMPLETE | |
| 64 | DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES | |
| 65 | DexoptOptions.DEXOPT_ONLY_SECONDARY_DEX | |
| 66 | DexoptOptions.DEXOPT_ONLY_SHARED_DEX | |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 67 | DexoptOptions.DEXOPT_DOWNGRADE | |
David Sehr | 2118ec4 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 68 | DexoptOptions.DEXOPT_AS_SHARED_LIBRARY | |
Calin Juravle | cc65194 | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 69 | DexoptOptions.DEXOPT_IDLE_BACKGROUND_JOB | |
| 70 | DexoptOptions.DEXOPT_INSTALL_WITH_DEX_METADATA_FILE; |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 71 | |
| 72 | DexoptOptions opt = new DexoptOptions(mPackageName, mCompilerFilter, flags); |
| 73 | assertEquals(mPackageName, opt.getPackageName()); |
| 74 | assertEquals(mCompilerFilter, opt.getCompilerFilter()); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 75 | assertEquals(null, opt.getSplitName()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 76 | assertTrue(opt.isBootComplete()); |
| 77 | assertTrue(opt.isCheckForProfileUpdates()); |
| 78 | assertTrue(opt.isDexoptOnlySecondaryDex()); |
| 79 | assertTrue(opt.isDexoptOnlySharedDex()); |
| 80 | assertTrue(opt.isDowngrade()); |
| 81 | assertTrue(opt.isForce()); |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 82 | assertTrue(opt.isDexoptAsSharedLibrary()); |
David Sehr | 2118ec4 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 83 | assertTrue(opt.isDexoptIdleBackgroundJob()); |
Calin Juravle | cc65194 | 2018-02-01 17:20:51 +0000 | [diff] [blame] | 84 | assertTrue(opt.isDexoptInstallWithDexMetadata()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testCreateDexoptOptionsReason() { |
| 89 | int flags = |
| 90 | DexoptOptions.DEXOPT_FORCE | |
| 91 | DexoptOptions.DEXOPT_BOOT_COMPLETE | |
| 92 | DexoptOptions.DEXOPT_CHECK_FOR_PROFILES_UPDATES; |
| 93 | |
| 94 | int[] reasons = new int[] { |
| 95 | PackageManagerService.REASON_FIRST_BOOT, |
| 96 | PackageManagerService.REASON_BOOT, |
| 97 | PackageManagerService.REASON_INSTALL, |
| 98 | PackageManagerService.REASON_BACKGROUND_DEXOPT, |
| 99 | PackageManagerService.REASON_AB_OTA, |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 100 | PackageManagerService.REASON_INACTIVE_PACKAGE_DOWNGRADE,}; |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 101 | |
| 102 | for (int reason : reasons) { |
| 103 | DexoptOptions opt = new DexoptOptions(mPackageName, reason, flags); |
| 104 | assertEquals(mPackageName, opt.getPackageName()); |
| 105 | assertEquals(getCompilerFilterForReason(reason), opt.getCompilerFilter()); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 106 | assertEquals(null, opt.getSplitName()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 107 | assertTrue(opt.isBootComplete()); |
| 108 | assertTrue(opt.isCheckForProfileUpdates()); |
| 109 | assertFalse(opt.isDexoptOnlySecondaryDex()); |
| 110 | assertFalse(opt.isDexoptOnlySharedDex()); |
| 111 | assertFalse(opt.isDowngrade()); |
| 112 | assertTrue(opt.isForce()); |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 113 | assertFalse(opt.isDexoptAsSharedLibrary()); |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | @Test |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 118 | public void testCreateDexoptOptionsSplit() { |
| 119 | int flags = DexoptOptions.DEXOPT_FORCE | DexoptOptions.DEXOPT_BOOT_COMPLETE; |
| 120 | |
Calin Juravle | 4bc8f4d | 2018-02-12 12:00:44 -0800 | [diff] [blame] | 121 | DexoptOptions opt = new DexoptOptions(mPackageName, -1, mCompilerFilter, mSplitName, flags); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 122 | assertEquals(mPackageName, opt.getPackageName()); |
| 123 | assertEquals(mCompilerFilter, opt.getCompilerFilter()); |
| 124 | assertEquals(mSplitName, opt.getSplitName()); |
| 125 | assertTrue(opt.isBootComplete()); |
| 126 | assertFalse(opt.isCheckForProfileUpdates()); |
| 127 | assertFalse(opt.isDexoptOnlySecondaryDex()); |
| 128 | assertFalse(opt.isDexoptOnlySharedDex()); |
| 129 | assertFalse(opt.isDowngrade()); |
| 130 | assertTrue(opt.isForce()); |
Calin Juravle | 3b74c41 | 2017-08-03 19:48:37 -0700 | [diff] [blame] | 131 | assertFalse(opt.isDexoptAsSharedLibrary()); |
Calin Juravle | b6f844d | 2017-07-17 15:23:21 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
Calin Juravle | 1d0e83d | 2017-07-17 15:12:01 -0700 | [diff] [blame] | 135 | public void testCreateDexoptInvalid() { |
| 136 | boolean gotException = false; |
| 137 | try { |
| 138 | int invalidFlags = 999; |
| 139 | new DexoptOptions(mPackageName, mCompilerFilter, invalidFlags); |
| 140 | } catch (IllegalArgumentException ignore) { |
| 141 | gotException = true; |
| 142 | } |
| 143 | |
| 144 | assertTrue(gotException); |
| 145 | } |
David Sehr | 2118ec4 | 2017-10-25 14:28:29 -0700 | [diff] [blame] | 146 | } |