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					InApexModules:     []string{"foo"},
41					ForPrebuiltApex:   NotForPrebuiltApex,
42				},
43			},
44			wantMerged: []ApexInfo{
45				{
46					ApexVariationName: "apex10000",
47					MinSdkVersion:     FutureApiLevel,
48					InApexVariants:    []string{"foo"},
49					InApexModules:     []string{"foo"},
50					ForPrebuiltApex:   NotForPrebuiltApex,
51				},
52			},
53			wantAliases: [][2]string{
54				{"foo", "apex10000"},
55			},
56		},
57		{
58			name: "merge",
59			in: []ApexInfo{
60				{
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				},
74			},
75			wantMerged: []ApexInfo{
76				{
77					ApexVariationName: "apex10000",
78					MinSdkVersion:     FutureApiLevel,
79					InApexVariants:    []string{"foo", "bar"},
80					InApexModules:     []string{"foo", "bar"},
81				}},
82			wantAliases: [][2]string{
83				{"foo", "apex10000"},
84				{"bar", "apex10000"},
85			},
86		},
87		{
88			name: "don't merge version",
89			in: []ApexInfo{
90				{
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				},
104			},
105			wantMerged: []ApexInfo{
106				{
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				},
120			},
121			wantAliases: [][2]string{
122				{"foo", "apex10000"},
123				{"bar", "apex30"},
124			},
125		},
126		{
127			name: "merge updatable",
128			in: []ApexInfo{
129				{
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				},
144			},
145			wantMerged: []ApexInfo{
146				{
147					ApexVariationName: "apex10000",
148					MinSdkVersion:     FutureApiLevel,
149					Updatable:         true,
150					InApexVariants:    []string{"foo", "bar"},
151					InApexModules:     []string{"foo", "bar"},
152					ForPrebuiltApex:   NotForPrebuiltApex,
153				},
154			},
155			wantAliases: [][2]string{
156				{"foo", "apex10000"},
157				{"bar", "apex10000"},
158			},
159		},
160		{
161			name: "don't merge when for prebuilt_apex",
162			in: []ApexInfo{
163				{
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				},
178				// This one should not be merged in with the others because it is for
179				// a prebuilt_apex.
180				{
181					ApexVariationName: "baz",
182					MinSdkVersion:     FutureApiLevel,
183					Updatable:         true,
184					InApexVariants:    []string{"baz"},
185					InApexModules:     []string{"baz"},
186					ForPrebuiltApex:   ForPrebuiltApex,
187				},
188			},
189			wantMerged: []ApexInfo{
190				{
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				},
206			},
207			wantAliases: [][2]string{
208				{"foo", "apex10000"},
209				{"bar", "apex10000"},
210			},
211		},
212		{
213			name: "merge different UsePlatformApis but don't allow using platform api",
214			in: []ApexInfo{
215				{
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				},
230			},
231			wantMerged: []ApexInfo{
232				{
233					ApexVariationName: "apex10000",
234					MinSdkVersion:     FutureApiLevel,
235					InApexVariants:    []string{"foo", "bar"},
236					InApexModules:     []string{"foo", "bar"},
237					ForPrebuiltApex:   NotForPrebuiltApex,
238				},
239			},
240			wantAliases: [][2]string{
241				{"foo", "apex10000"},
242				{"bar", "apex10000"},
243			},
244		},
245		{
246			name: "merge same UsePlatformApis and allow using platform api",
247			in: []ApexInfo{
248				{
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				},
264			},
265			wantMerged: []ApexInfo{
266				{
267					ApexVariationName: "apex10000",
268					MinSdkVersion:     FutureApiLevel,
269					UsePlatformApis:   true,
270					InApexVariants:    []string{"foo", "bar"},
271					InApexModules:     []string{"foo", "bar"},
272					ForPrebuiltApex:   NotForPrebuiltApex,
273				},
274			},
275			wantAliases: [][2]string{
276				{"foo", "apex10000"},
277				{"bar", "apex10000"},
278			},
279		},
280	}
281
282	for _, tt := range tests {
283		t.Run(tt.name, func(t *testing.T) {
284			gotMerged, gotAliases := mergeApexVariations(tt.in)
285			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}
294