xref: /aosp_15_r20/build/soong/android/apex_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// 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) {
23	const (
24		ForPrebuiltApex    = true
25		NotForPrebuiltApex = false
26	)
27	tests := []struct {
28		name        string
29		in          []ApexInfo
30		wantMerged  []ApexInfo
31		wantAliases [][2]string
32	}{
33		{
34			name: "single",
35			in: []ApexInfo{
36				{
37					ApexVariationName: "foo",
38					MinSdkVersion:     FutureApiLevel,
39					InApexVariants:    []string{"foo"},
40					ForPrebuiltApex:   NotForPrebuiltApex,
41				},
42			},
43			wantMerged: []ApexInfo{
44				{
45					ApexVariationName: "apex10000",
46					MinSdkVersion:     FutureApiLevel,
47					InApexVariants:    []string{"foo"},
48					ForPrebuiltApex:   NotForPrebuiltApex,
49				},
50			},
51			wantAliases: [][2]string{
52				{"foo", "apex10000"},
53			},
54		},
55		{
56			name: "merge",
57			in: []ApexInfo{
58				{
59					ApexVariationName: "foo",
60					MinSdkVersion:     FutureApiLevel,
61					InApexVariants:    []string{"foo"},
62					ForPrebuiltApex:   NotForPrebuiltApex,
63				},
64				{
65					ApexVariationName: "bar",
66					MinSdkVersion:     FutureApiLevel,
67					InApexVariants:    []string{"bar"},
68					ForPrebuiltApex:   NotForPrebuiltApex,
69				},
70			},
71			wantMerged: []ApexInfo{
72				{
73					ApexVariationName: "apex10000",
74					MinSdkVersion:     FutureApiLevel,
75					InApexVariants:    []string{"foo", "bar"},
76				}},
77			wantAliases: [][2]string{
78				{"foo", "apex10000"},
79				{"bar", "apex10000"},
80			},
81		},
82		{
83			name: "don't merge version",
84			in: []ApexInfo{
85				{
86					ApexVariationName: "foo",
87					MinSdkVersion:     FutureApiLevel,
88					InApexVariants:    []string{"foo"},
89					ForPrebuiltApex:   NotForPrebuiltApex,
90				},
91				{
92					ApexVariationName: "bar",
93					MinSdkVersion:     uncheckedFinalApiLevel(30),
94					InApexVariants:    []string{"bar"},
95					ForPrebuiltApex:   NotForPrebuiltApex,
96				},
97			},
98			wantMerged: []ApexInfo{
99				{
100					ApexVariationName: "apex10000",
101					MinSdkVersion:     FutureApiLevel,
102					InApexVariants:    []string{"foo"},
103					ForPrebuiltApex:   NotForPrebuiltApex,
104				},
105				{
106					ApexVariationName: "apex30",
107					MinSdkVersion:     uncheckedFinalApiLevel(30),
108					InApexVariants:    []string{"bar"},
109					ForPrebuiltApex:   NotForPrebuiltApex,
110				},
111			},
112			wantAliases: [][2]string{
113				{"foo", "apex10000"},
114				{"bar", "apex30"},
115			},
116		},
117		{
118			name: "merge updatable",
119			in: []ApexInfo{
120				{
121					ApexVariationName: "foo",
122					MinSdkVersion:     FutureApiLevel,
123					InApexVariants:    []string{"foo"},
124					ForPrebuiltApex:   NotForPrebuiltApex,
125				},
126				{
127					ApexVariationName: "bar",
128					MinSdkVersion:     FutureApiLevel,
129					Updatable:         true,
130					InApexVariants:    []string{"bar"},
131					ForPrebuiltApex:   NotForPrebuiltApex,
132				},
133			},
134			wantMerged: []ApexInfo{
135				{
136					ApexVariationName: "apex10000",
137					MinSdkVersion:     FutureApiLevel,
138					Updatable:         true,
139					InApexVariants:    []string{"foo", "bar"},
140					ForPrebuiltApex:   NotForPrebuiltApex,
141				},
142			},
143			wantAliases: [][2]string{
144				{"foo", "apex10000"},
145				{"bar", "apex10000"},
146			},
147		},
148		{
149			name: "don't merge when for prebuilt_apex",
150			in: []ApexInfo{
151				{
152					ApexVariationName: "foo",
153					MinSdkVersion:     FutureApiLevel,
154					InApexVariants:    []string{"foo"},
155					ForPrebuiltApex:   NotForPrebuiltApex,
156				},
157				{
158					ApexVariationName: "bar",
159					MinSdkVersion:     FutureApiLevel,
160					Updatable:         true,
161					InApexVariants:    []string{"bar"},
162					ForPrebuiltApex:   NotForPrebuiltApex,
163				},
164				// This one should not be merged in with the others because it is for
165				// a prebuilt_apex.
166				{
167					ApexVariationName: "baz",
168					MinSdkVersion:     FutureApiLevel,
169					Updatable:         true,
170					InApexVariants:    []string{"baz"},
171					ForPrebuiltApex:   ForPrebuiltApex,
172				},
173			},
174			wantMerged: []ApexInfo{
175				{
176					ApexVariationName: "apex10000",
177					MinSdkVersion:     FutureApiLevel,
178					Updatable:         true,
179					InApexVariants:    []string{"foo", "bar"},
180					ForPrebuiltApex:   NotForPrebuiltApex,
181				},
182				{
183					ApexVariationName: "baz",
184					MinSdkVersion:     FutureApiLevel,
185					Updatable:         true,
186					InApexVariants:    []string{"baz"},
187					ForPrebuiltApex:   ForPrebuiltApex,
188				},
189			},
190			wantAliases: [][2]string{
191				{"foo", "apex10000"},
192				{"bar", "apex10000"},
193			},
194		},
195		{
196			name: "merge different UsePlatformApis but don't allow using platform api",
197			in: []ApexInfo{
198				{
199					ApexVariationName: "foo",
200					MinSdkVersion:     FutureApiLevel,
201					InApexVariants:    []string{"foo"},
202					ForPrebuiltApex:   NotForPrebuiltApex,
203				},
204				{
205					ApexVariationName: "bar",
206					MinSdkVersion:     FutureApiLevel,
207					UsePlatformApis:   true,
208					InApexVariants:    []string{"bar"},
209					ForPrebuiltApex:   NotForPrebuiltApex,
210				},
211			},
212			wantMerged: []ApexInfo{
213				{
214					ApexVariationName: "apex10000",
215					MinSdkVersion:     FutureApiLevel,
216					InApexVariants:    []string{"foo", "bar"},
217					ForPrebuiltApex:   NotForPrebuiltApex,
218				},
219			},
220			wantAliases: [][2]string{
221				{"foo", "apex10000"},
222				{"bar", "apex10000"},
223			},
224		},
225		{
226			name: "merge same UsePlatformApis and allow using platform api",
227			in: []ApexInfo{
228				{
229					ApexVariationName: "foo",
230					MinSdkVersion:     FutureApiLevel,
231					UsePlatformApis:   true,
232					InApexVariants:    []string{"foo"},
233					ForPrebuiltApex:   NotForPrebuiltApex,
234				},
235				{
236					ApexVariationName: "bar",
237					MinSdkVersion:     FutureApiLevel,
238					UsePlatformApis:   true,
239					InApexVariants:    []string{"bar"},
240					ForPrebuiltApex:   NotForPrebuiltApex,
241				},
242			},
243			wantMerged: []ApexInfo{
244				{
245					ApexVariationName: "apex10000",
246					MinSdkVersion:     FutureApiLevel,
247					UsePlatformApis:   true,
248					InApexVariants:    []string{"foo", "bar"},
249					ForPrebuiltApex:   NotForPrebuiltApex,
250				},
251			},
252			wantAliases: [][2]string{
253				{"foo", "apex10000"},
254				{"bar", "apex10000"},
255			},
256		},
257	}
258
259	for _, tt := range tests {
260		t.Run(tt.name, func(t *testing.T) {
261			gotMerged, gotAliases := mergeApexVariations(tt.in)
262			if !reflect.DeepEqual(gotMerged, tt.wantMerged) {
263				t.Errorf("mergeApexVariations() gotMerged = %v, want %v", gotMerged, tt.wantMerged)
264			}
265			if !reflect.DeepEqual(gotAliases, tt.wantAliases) {
266				t.Errorf("mergeApexVariations() gotAliases = %v, want %v", gotAliases, tt.wantAliases)
267			}
268		})
269	}
270}
271