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