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 java
16
17import (
18	"fmt"
19	"reflect"
20	"testing"
21
22	"android/soong/android"
23)
24
25func TestAndroidAppSet(t *testing.T) {
26	ctx, _ := testJava(t, `
27		android_app_set {
28			name: "foo",
29			set: "prebuilts/apks/app.apks",
30			prerelease: true,
31		}`)
32	module := ctx.ModuleForTests("foo", "android_common")
33	const packedSplitApks = "foo.zip"
34	params := module.Output(packedSplitApks)
35	if params.Rule == nil {
36		t.Errorf("expected output %s is missing", packedSplitApks)
37	}
38	if s := params.Args["allow-prereleased"]; s != "true" {
39		t.Errorf("wrong allow-prereleased value: '%s', expected 'true'", s)
40	}
41	if s := params.Args["partition"]; s != "system" {
42		t.Errorf("wrong partition value: '%s', expected 'system'", s)
43	}
44	mkEntries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
45	actualInstallFile := mkEntries.EntryMap["LOCAL_APK_SET_INSTALL_FILE"]
46	expectedInstallFile := []string{"foo.apk"}
47	if !reflect.DeepEqual(actualInstallFile, expectedInstallFile) {
48		t.Errorf("Unexpected LOCAL_APK_SET_INSTALL_FILE value: '%s', expected: '%s',",
49			actualInstallFile, expectedInstallFile)
50	}
51}
52
53func TestAndroidAppSet_Variants(t *testing.T) {
54	bp := `
55		android_app_set {
56			name: "foo",
57			set: "prebuilts/apks/app.apks",
58		}`
59	testCases := []struct {
60		name            string
61		targets         []android.Target
62		aaptPrebuiltDPI []string
63		sdkVersion      int
64		expected        map[string]string
65	}{
66		{
67			name: "One",
68			targets: []android.Target{
69				{Os: android.Android, Arch: android.Arch{ArchType: android.X86}},
70			},
71			aaptPrebuiltDPI: []string{"ldpi", "xxhdpi"},
72			sdkVersion:      29,
73			expected: map[string]string{
74				"abis":              "X86",
75				"allow-prereleased": "false",
76				"screen-densities":  "LDPI,XXHDPI",
77				"sdk-version":       "29",
78				"stem":              "foo",
79			},
80		},
81		{
82			name: "Two",
83			targets: []android.Target{
84				{Os: android.Android, Arch: android.Arch{ArchType: android.X86_64}},
85				{Os: android.Android, Arch: android.Arch{ArchType: android.X86}},
86			},
87			aaptPrebuiltDPI: nil,
88			sdkVersion:      30,
89			expected: map[string]string{
90				"abis":              "X86_64,X86",
91				"allow-prereleased": "false",
92				"screen-densities":  "all",
93				"sdk-version":       "30",
94				"stem":              "foo",
95			},
96		},
97	}
98
99	for _, test := range testCases {
100		ctx := android.GroupFixturePreparers(
101			PrepareForTestWithJavaDefaultModules,
102			android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
103				variables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
104				variables.Platform_sdk_version = &test.sdkVersion
105			}),
106			android.FixtureModifyConfig(func(config android.Config) {
107				config.Targets[android.Android] = test.targets
108			}),
109		).RunTestWithBp(t, bp)
110
111		module := ctx.ModuleForTests("foo", "android_common")
112		const packedSplitApks = "foo.zip"
113		params := module.Output(packedSplitApks)
114		for k, v := range test.expected {
115			t.Run(test.name, func(t *testing.T) {
116				android.AssertStringEquals(t, fmt.Sprintf("arg value for `%s`", k), v, params.Args[k])
117			})
118		}
119	}
120}
121