blob: feabd7004168210ca8ecbf058ea59f40d8b28145 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package dexpreopt
16
17import (
Colin Crossfeec25b2019-01-30 17:32:39 -080018 "android/soong/android"
Anton Hansson4ec91dd2019-10-02 17:42:44 +010019 "fmt"
Colin Cross43f08db2018-11-12 10:13:39 -080020 "testing"
21)
22
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000023func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010024 return testModuleConfig(ctx, name, "system")
25}
26
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000027func testSystemProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010028 return testModuleConfig(ctx, name, "system/product")
29}
30
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000031func testProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010032 return testModuleConfig(ctx, name, "product")
33}
34
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000035func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleConfig {
36 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010037 Name: name,
38 DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name),
39 BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)),
40 DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)),
Colin Cross69f59a32019-02-15 10:39:37 -080041 UncompressedDex: false,
42 HasApkLibraries: false,
43 PreoptFlags: nil,
44 ProfileClassListing: android.OptionalPath{},
45 ProfileIsTextListing: false,
46 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000047 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080048 Archs: []android.ArchType{android.Arm},
49 DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000050 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
51 DexPreoptImageLocations: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080052 PreoptBootClassPathDexFiles: nil,
53 PreoptBootClassPathDexLocations: nil,
54 PreoptExtractedApk: false,
55 NoCreateAppImage: false,
56 ForceCreateAppImage: false,
57 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080058 }
Colin Cross43f08db2018-11-12 10:13:39 -080059}
60
61func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000062 config := android.TestConfig("out", nil, "", nil)
63 ctx := android.PathContextForTesting(config)
64 globalSoong := GlobalSoongConfigForTests(config)
65 global := GlobalConfigForTests(ctx)
66 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080067
Martin Stjernholm75a48d82020-01-10 20:32:59 +000068 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080069 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080070 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080071 }
72
Colin Crossdeabb942019-02-11 14:11:09 -080073 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080074 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
75 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080076 }
77
Colin Cross2cdd5df2019-02-25 10:25:24 -080078 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080079 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
80 }
81}
82
83func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000084 config := android.TestConfig("out", nil, "", nil)
85 ctx := android.PathContextForTesting(config)
86 globalSoong := GlobalSoongConfigForTests(config)
Anton Hansson4ec91dd2019-10-02 17:42:44 +010087 global := GlobalConfigForTests(ctx)
88 systemModule := testSystemModuleConfig(ctx, "Stest")
89 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
90 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -080091
92 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -080093
Anton Hansson4ec91dd2019-10-02 17:42:44 +010094 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000095 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +010096 expectedPartition string
97 }
98 tests := []struct {
99 patterns []string
100 moduleTests []moduleTest
101 }{
102 {
103 patterns: []string{"app/%"},
104 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100105 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100106 {module: systemProductModule, expectedPartition: "system/product"},
107 {module: productModule, expectedPartition: "product"},
108 },
109 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000110 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100111 {
112 patterns: []string{"app/%", "product/app/%"},
113 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100114 {module: systemModule, expectedPartition: "system_other/system"},
115 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000116 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100117 },
118 },
Colin Cross43f08db2018-11-12 10:13:39 -0800119 }
120
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100121 for _, test := range tests {
122 global.PatternsOnSystemOther = test.patterns
123 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000124 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100125 if err != nil {
126 t.Fatal(err)
127 }
128
129 name := mt.module.Name
130 wantInstalls := android.RuleBuilderInstalls{
131 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
132 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
133 }
134
135 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000136 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100137 }
138 }
Colin Cross43f08db2018-11-12 10:13:39 -0800139 }
140
Colin Cross43f08db2018-11-12 10:13:39 -0800141}
142
143func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000144 config := android.TestConfig("out", nil, "", nil)
145 ctx := android.PathContextForTesting(config)
146 globalSoong := GlobalSoongConfigForTests(config)
147 global := GlobalConfigForTests(ctx)
148 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800149
Colin Cross69f59a32019-02-15 10:39:37 -0800150 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800151
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000152 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800153 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800154 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800155 }
156
Colin Crossdeabb942019-02-11 14:11:09 -0800157 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800158 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
159 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
160 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
161 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800162 }
163
Colin Cross2cdd5df2019-02-25 10:25:24 -0800164 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800165 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
166 }
167}