blob: 1fbd7f10b95e6861b7778792d4f4f77562690c5d [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 Trafimovich6e827482020-06-12 14:32:24 +010047 OptionalUsesLibraries: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080048 UsesLibraries: nil,
49 LibraryPaths: nil,
50 Archs: []android.ArchType{android.Arm},
51 DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000052 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
53 DexPreoptImageLocations: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080054 PreoptBootClassPathDexFiles: nil,
55 PreoptBootClassPathDexLocations: nil,
56 PreoptExtractedApk: false,
57 NoCreateAppImage: false,
58 ForceCreateAppImage: false,
59 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080060 }
Colin Cross43f08db2018-11-12 10:13:39 -080061}
62
63func TestDexPreopt(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070064 t.Parallel()
Martin Stjernholm75a48d82020-01-10 20:32:59 +000065 config := android.TestConfig("out", nil, "", nil)
66 ctx := android.PathContextForTesting(config)
67 globalSoong := GlobalSoongConfigForTests(config)
68 global := GlobalConfigForTests(ctx)
69 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080070
Martin Stjernholm75a48d82020-01-10 20:32:59 +000071 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080072 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080073 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080074 }
75
Colin Crossdeabb942019-02-11 14:11:09 -080076 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080077 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
78 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080079 }
80
Colin Cross2cdd5df2019-02-25 10:25:24 -080081 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080082 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
83 }
84}
85
86func TestDexPreoptSystemOther(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070087 t.Parallel()
Martin Stjernholm75a48d82020-01-10 20:32:59 +000088 config := android.TestConfig("out", nil, "", nil)
89 ctx := android.PathContextForTesting(config)
90 globalSoong := GlobalSoongConfigForTests(config)
Anton Hansson4ec91dd2019-10-02 17:42:44 +010091 global := GlobalConfigForTests(ctx)
92 systemModule := testSystemModuleConfig(ctx, "Stest")
93 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
94 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -080095
96 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -080097
Anton Hansson4ec91dd2019-10-02 17:42:44 +010098 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000099 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100100 expectedPartition string
101 }
102 tests := []struct {
103 patterns []string
104 moduleTests []moduleTest
105 }{
106 {
107 patterns: []string{"app/%"},
108 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100109 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100110 {module: systemProductModule, expectedPartition: "system/product"},
111 {module: productModule, expectedPartition: "product"},
112 },
113 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000114 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100115 {
116 patterns: []string{"app/%", "product/app/%"},
117 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100118 {module: systemModule, expectedPartition: "system_other/system"},
119 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000120 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100121 },
122 },
Colin Cross43f08db2018-11-12 10:13:39 -0800123 }
124
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100125 for _, test := range tests {
126 global.PatternsOnSystemOther = test.patterns
127 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000128 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100129 if err != nil {
130 t.Fatal(err)
131 }
132
133 name := mt.module.Name
134 wantInstalls := android.RuleBuilderInstalls{
135 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
136 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
137 }
138
139 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000140 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100141 }
142 }
Colin Cross43f08db2018-11-12 10:13:39 -0800143 }
144
Colin Cross43f08db2018-11-12 10:13:39 -0800145}
146
147func TestDexPreoptProfile(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700148 t.Parallel()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000149 config := android.TestConfig("out", nil, "", nil)
150 ctx := android.PathContextForTesting(config)
151 globalSoong := GlobalSoongConfigForTests(config)
152 global := GlobalConfigForTests(ctx)
153 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800154
Colin Cross69f59a32019-02-15 10:39:37 -0800155 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800156
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000157 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800158 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800159 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800160 }
161
Colin Crossdeabb942019-02-11 14:11:09 -0800162 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800163 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
164 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
165 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
166 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800167 }
168
Colin Cross2cdd5df2019-02-25 10:25:24 -0800169 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800170 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
171 }
172}