blob: 2ea6401793565a972d16369b7536cbda5be9327e [file] [log] [blame]
Paul Duffinb432df92021-03-22 22:09:42 +00001// Copyright (C) 2021 The Android Open Source Project
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 apex
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/dexpreopt"
22 "android/soong/java"
23 "github.com/google/blueprint"
24)
25
26// Contains tests for platform_bootclasspath logic from java/platform_bootclasspath.go that requires
27// apexes.
28
29var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers(
30 java.PrepareForTestWithDexpreopt,
31 PrepareForTestWithApexBuildComponents,
32)
33
34func TestPlatformBootclasspathDependencies(t *testing.T) {
35 result := android.GroupFixturePreparers(
36 prepareForTestWithPlatformBootclasspath,
37 prepareForTestWithArtApex,
38 prepareForTestWithMyapex,
39 // Configure some libraries in the art and framework boot images.
40 dexpreopt.FixtureSetArtBootJars("com.android.art:baz", "com.android.art:quuz"),
41 dexpreopt.FixtureSetBootJars("platform:foo"),
42 dexpreopt.FixtureSetUpdatableBootJars("myapex:bar"),
43 java.PrepareForTestWithJavaSdkLibraryFiles,
44 java.FixtureWithLastReleaseApis("foo"),
45 ).RunTestWithBp(t, `
46 apex {
47 name: "com.android.art",
48 key: "com.android.art.key",
49 bootclasspath_fragments: [
50 "art-bootclasspath-fragment",
51 ],
52 updatable: false,
53 }
54
55 apex_key {
56 name: "com.android.art.key",
57 public_key: "com.android.art.avbpubkey",
58 private_key: "com.android.art.pem",
59 }
60
61 bootclasspath_fragment {
62 name: "art-bootclasspath-fragment",
63 apex_available: [
64 "com.android.art",
65 ],
66 contents: [
67 "baz",
68 "quuz",
69 ],
70 }
71
72 java_library {
73 name: "baz",
74 apex_available: [
75 "com.android.art",
76 ],
77 srcs: ["b.java"],
78 installable: true,
79 }
80
81 // Add a java_import that is not preferred and so won't have an appropriate apex variant created
82 // for it to make sure that the platform_bootclasspath doesn't try and add a dependency onto it.
83 java_import {
84 name: "baz",
85 apex_available: [
86 "com.android.art",
87 ],
88 jars: ["b.jar"],
89 }
90
91 java_library {
92 name: "quuz",
93 apex_available: [
94 "com.android.art",
95 ],
96 srcs: ["b.java"],
97 installable: true,
98 }
99
100 apex {
101 name: "myapex",
102 key: "myapex.key",
103 java_libs: [
104 "bar",
105 ],
106 updatable: false,
107 }
108
109 apex_key {
110 name: "myapex.key",
111 public_key: "testkey.avbpubkey",
112 private_key: "testkey.pem",
113 }
114
115 java_sdk_library {
116 name: "foo",
117 srcs: ["b.java"],
118 }
119
120 java_library {
121 name: "bar",
122 srcs: ["b.java"],
123 installable: true,
124 apex_available: ["myapex"],
125 permitted_packages: ["bar"],
126 }
127
128 platform_bootclasspath {
129 name: "myplatform-bootclasspath",
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100130
131 fragments: [
132 {
133 apex: "com.android.art",
134 module: "art-bootclasspath-fragment",
135 },
136 ],
Paul Duffinb432df92021-03-22 22:09:42 +0000137 }
138`,
139 )
140
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100141 java.CheckPlatformBootclasspathModules(t, result, "myplatform-bootclasspath", []string{
142 "com.android.art:baz",
143 "com.android.art:quuz",
144 "platform:foo",
145 "myapex:bar",
146 })
147
148 java.CheckPlatformBootclasspathFragments(t, result, "myplatform-bootclasspath", []string{
149 `com.android.art:art-bootclasspath-fragment`,
150 })
151
Paul Duffinb432df92021-03-22 22:09:42 +0000152 // Make sure that the myplatform-bootclasspath has the correct dependencies.
153 CheckModuleDependencies(t, result.TestContext, "myplatform-bootclasspath", "android_common", []string{
154 `platform:dex2oatd`,
155 `com.android.art:baz`,
156 `com.android.art:quuz`,
157 `platform:foo`,
158 `myapex:bar`,
Paul Duffin62d8c3b2021-04-07 20:35:11 +0100159 `com.android.art:art-bootclasspath-fragment`,
Paul Duffinb432df92021-03-22 22:09:42 +0000160 })
161}
162
163// CheckModuleDependencies checks the dependencies of the selected module against the expected list.
164//
165// The expected list must be a list of strings of the form "<apex>:<module>", where <apex> is the
166// name of the apex, or platform is it is not part of an apex and <module> is the module name.
167func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
168 t.Helper()
169 module := ctx.ModuleForTests(name, variant).Module()
170 modules := []android.Module{}
171 ctx.VisitDirectDeps(module, func(m blueprint.Module) {
172 modules = append(modules, m.(android.Module))
173 })
174
175 pairs := java.ApexNamePairsFromModules(ctx, modules)
176 android.AssertDeepEquals(t, "module dependencies", expected, pairs)
177}