blob: 347bf7d98d2e2ff37ce8c5c2876d740065145824 [file] [log] [blame]
Colin Crossaede88c2020-08-11 12:17:01 -07001// Copyright 2020 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 android
16
17import (
18 "reflect"
19 "testing"
20)
21
22func Test_mergeApexVariations(t *testing.T) {
Paul Duffin064b70c2020-11-02 17:32:38 +000023 const (
24 ForPrebuiltApex = true
25 NotForPrebuiltApex = false
26 )
Colin Crossaede88c2020-08-11 12:17:01 -070027 tests := []struct {
28 name string
29 in []ApexInfo
30 wantMerged []ApexInfo
31 wantAliases [][2]string
32 }{
33 {
34 name: "single",
35 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -070036 {
37 ApexVariationName: "foo",
38 MinSdkVersion: FutureApiLevel,
39 InApexVariants: []string{"foo"},
40 InApexModules: []string{"foo"},
41 ForPrebuiltApex: NotForPrebuiltApex,
42 },
Colin Crossaede88c2020-08-11 12:17:01 -070043 },
44 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -070045 {
46 ApexVariationName: "apex10000",
47 MinSdkVersion: FutureApiLevel,
48 InApexVariants: []string{"foo"},
49 InApexModules: []string{"foo"},
50 ForPrebuiltApex: NotForPrebuiltApex,
51 },
Colin Crossaede88c2020-08-11 12:17:01 -070052 },
53 wantAliases: [][2]string{
54 {"foo", "apex10000"},
55 },
56 },
57 {
58 name: "merge",
59 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -070060 {
61 ApexVariationName: "foo",
62 MinSdkVersion: FutureApiLevel,
63 InApexVariants: []string{"foo"},
64 InApexModules: []string{"foo"},
65 ForPrebuiltApex: NotForPrebuiltApex,
66 },
67 {
68 ApexVariationName: "bar",
69 MinSdkVersion: FutureApiLevel,
70 InApexVariants: []string{"bar"},
71 InApexModules: []string{"bar"},
72 ForPrebuiltApex: NotForPrebuiltApex,
73 },
Colin Crossaede88c2020-08-11 12:17:01 -070074 },
75 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -070076 {
77 ApexVariationName: "apex10000",
78 MinSdkVersion: FutureApiLevel,
79 InApexVariants: []string{"foo", "bar"},
80 InApexModules: []string{"foo", "bar"},
81 }},
Colin Crossaede88c2020-08-11 12:17:01 -070082 wantAliases: [][2]string{
Liz Kammer7eed5382022-04-21 11:13:45 -040083 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -070084 {"bar", "apex10000"},
Colin Crossaede88c2020-08-11 12:17:01 -070085 },
86 },
87 {
88 name: "don't merge version",
89 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -070090 {
91 ApexVariationName: "foo",
92 MinSdkVersion: FutureApiLevel,
93 InApexVariants: []string{"foo"},
94 InApexModules: []string{"foo"},
95 ForPrebuiltApex: NotForPrebuiltApex,
96 },
97 {
98 ApexVariationName: "bar",
99 MinSdkVersion: uncheckedFinalApiLevel(30),
100 InApexVariants: []string{"bar"},
101 InApexModules: []string{"bar"},
102 ForPrebuiltApex: NotForPrebuiltApex,
103 },
Colin Crossaede88c2020-08-11 12:17:01 -0700104 },
105 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700106 {
107 ApexVariationName: "apex10000",
108 MinSdkVersion: FutureApiLevel,
109 InApexVariants: []string{"foo"},
110 InApexModules: []string{"foo"},
111 ForPrebuiltApex: NotForPrebuiltApex,
112 },
113 {
114 ApexVariationName: "apex30",
115 MinSdkVersion: uncheckedFinalApiLevel(30),
116 InApexVariants: []string{"bar"},
117 InApexModules: []string{"bar"},
118 ForPrebuiltApex: NotForPrebuiltApex,
119 },
Colin Crossaede88c2020-08-11 12:17:01 -0700120 },
121 wantAliases: [][2]string{
Colin Crossaede88c2020-08-11 12:17:01 -0700122 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -0700123 {"bar", "apex30"},
Colin Crossaede88c2020-08-11 12:17:01 -0700124 },
125 },
126 {
127 name: "merge updatable",
128 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700129 {
130 ApexVariationName: "foo",
131 MinSdkVersion: FutureApiLevel,
132 InApexVariants: []string{"foo"},
133 InApexModules: []string{"foo"},
134 ForPrebuiltApex: NotForPrebuiltApex,
135 },
136 {
137 ApexVariationName: "bar",
138 MinSdkVersion: FutureApiLevel,
139 Updatable: true,
140 InApexVariants: []string{"bar"},
141 InApexModules: []string{"bar"},
142 ForPrebuiltApex: NotForPrebuiltApex,
143 },
Colin Crossaede88c2020-08-11 12:17:01 -0700144 },
145 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700146 {
147 ApexVariationName: "apex10000",
148 MinSdkVersion: FutureApiLevel,
149 Updatable: true,
150 InApexVariants: []string{"foo", "bar"},
151 InApexModules: []string{"foo", "bar"},
152 ForPrebuiltApex: NotForPrebuiltApex,
153 },
Colin Crossaede88c2020-08-11 12:17:01 -0700154 },
155 wantAliases: [][2]string{
Colin Crossaede88c2020-08-11 12:17:01 -0700156 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -0700157 {"bar", "apex10000"},
Colin Crossaede88c2020-08-11 12:17:01 -0700158 },
159 },
160 {
Paul Duffin064b70c2020-11-02 17:32:38 +0000161 name: "don't merge when for prebuilt_apex",
162 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700163 {
164 ApexVariationName: "foo",
165 MinSdkVersion: FutureApiLevel,
166 InApexVariants: []string{"foo"},
167 InApexModules: []string{"foo"},
168 ForPrebuiltApex: NotForPrebuiltApex,
169 },
170 {
171 ApexVariationName: "bar",
172 MinSdkVersion: FutureApiLevel,
173 Updatable: true,
174 InApexVariants: []string{"bar"},
175 InApexModules: []string{"bar"},
176 ForPrebuiltApex: NotForPrebuiltApex,
177 },
Paul Duffin064b70c2020-11-02 17:32:38 +0000178 // This one should not be merged in with the others because it is for
179 // a prebuilt_apex.
Colin Cross9132ced2024-04-15 15:32:38 -0700180 {
181 ApexVariationName: "baz",
182 MinSdkVersion: FutureApiLevel,
183 Updatable: true,
184 InApexVariants: []string{"baz"},
185 InApexModules: []string{"baz"},
186 ForPrebuiltApex: ForPrebuiltApex,
187 },
Paul Duffin064b70c2020-11-02 17:32:38 +0000188 },
189 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700190 {
191 ApexVariationName: "apex10000",
192 MinSdkVersion: FutureApiLevel,
193 Updatable: true,
194 InApexVariants: []string{"foo", "bar"},
195 InApexModules: []string{"foo", "bar"},
196 ForPrebuiltApex: NotForPrebuiltApex,
197 },
198 {
199 ApexVariationName: "baz",
200 MinSdkVersion: FutureApiLevel,
201 Updatable: true,
202 InApexVariants: []string{"baz"},
203 InApexModules: []string{"baz"},
204 ForPrebuiltApex: ForPrebuiltApex,
205 },
Paul Duffin064b70c2020-11-02 17:32:38 +0000206 },
207 wantAliases: [][2]string{
Paul Duffin064b70c2020-11-02 17:32:38 +0000208 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -0700209 {"bar", "apex10000"},
Paul Duffin064b70c2020-11-02 17:32:38 +0000210 },
211 },
Jiyong Park9477c262021-06-22 20:23:05 +0900212 {
Jiyong Parkd4a00632022-04-12 12:23:20 +0900213 name: "merge different UsePlatformApis but don't allow using platform api",
Jiyong Park9477c262021-06-22 20:23:05 +0900214 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700215 {
216 ApexVariationName: "foo",
217 MinSdkVersion: FutureApiLevel,
218 InApexVariants: []string{"foo"},
219 InApexModules: []string{"foo"},
220 ForPrebuiltApex: NotForPrebuiltApex,
221 },
222 {
223 ApexVariationName: "bar",
224 MinSdkVersion: FutureApiLevel,
225 UsePlatformApis: true,
226 InApexVariants: []string{"bar"},
227 InApexModules: []string{"bar"},
228 ForPrebuiltApex: NotForPrebuiltApex,
229 },
Jiyong Park9477c262021-06-22 20:23:05 +0900230 },
231 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700232 {
233 ApexVariationName: "apex10000",
234 MinSdkVersion: FutureApiLevel,
235 InApexVariants: []string{"foo", "bar"},
236 InApexModules: []string{"foo", "bar"},
237 ForPrebuiltApex: NotForPrebuiltApex,
238 },
Jiyong Park9477c262021-06-22 20:23:05 +0900239 },
240 wantAliases: [][2]string{
Jiyong Parkd4a00632022-04-12 12:23:20 +0900241 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -0700242 {"bar", "apex10000"},
Jiyong Parkd4a00632022-04-12 12:23:20 +0900243 },
244 },
245 {
246 name: "merge same UsePlatformApis and allow using platform api",
247 in: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700248 {
249 ApexVariationName: "foo",
250 MinSdkVersion: FutureApiLevel,
251 UsePlatformApis: true,
252 InApexVariants: []string{"foo"},
253 InApexModules: []string{"foo"},
254 ForPrebuiltApex: NotForPrebuiltApex,
255 },
256 {
257 ApexVariationName: "bar",
258 MinSdkVersion: FutureApiLevel,
259 UsePlatformApis: true,
260 InApexVariants: []string{"bar"},
261 InApexModules: []string{"bar"},
262 ForPrebuiltApex: NotForPrebuiltApex,
263 },
Jiyong Parkd4a00632022-04-12 12:23:20 +0900264 },
265 wantMerged: []ApexInfo{
Colin Cross9132ced2024-04-15 15:32:38 -0700266 {
267 ApexVariationName: "apex10000",
268 MinSdkVersion: FutureApiLevel,
269 UsePlatformApis: true,
270 InApexVariants: []string{"foo", "bar"},
271 InApexModules: []string{"foo", "bar"},
272 ForPrebuiltApex: NotForPrebuiltApex,
273 },
Jiyong Parkd4a00632022-04-12 12:23:20 +0900274 },
275 wantAliases: [][2]string{
Jiyong Park9477c262021-06-22 20:23:05 +0900276 {"foo", "apex10000"},
Colin Cross9132ced2024-04-15 15:32:38 -0700277 {"bar", "apex10000"},
Jiyong Park9477c262021-06-22 20:23:05 +0900278 },
279 },
Colin Crossaede88c2020-08-11 12:17:01 -0700280 }
Paul Duffin064b70c2020-11-02 17:32:38 +0000281
Colin Crossaede88c2020-08-11 12:17:01 -0700282 for _, tt := range tests {
283 t.Run(tt.name, func(t *testing.T) {
Colin Cross9132ced2024-04-15 15:32:38 -0700284 gotMerged, gotAliases := mergeApexVariations(tt.in)
Colin Crossaede88c2020-08-11 12:17:01 -0700285 if !reflect.DeepEqual(gotMerged, tt.wantMerged) {
286 t.Errorf("mergeApexVariations() gotMerged = %v, want %v", gotMerged, tt.wantMerged)
287 }
288 if !reflect.DeepEqual(gotAliases, tt.wantAliases) {
289 t.Errorf("mergeApexVariations() gotAliases = %v, want %v", gotAliases, tt.wantAliases)
290 }
291 })
292 }
293}