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