blob: 4130788f093f06f9649a742cff55284cab15e79c [file]
// Copyright (C) 2024 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"android/soong/android"
"android/soong/java"
"fmt"
"slices"
"testing"
"github.com/google/blueprint/proptools"
)
var prepareForTestWithCombinedApis = android.GroupFixturePreparers(
android.FixtureRegisterWithContext(registerBuildComponents),
java.PrepareForTestWithJavaDefaultModules,
android.FixtureAddTextFile("a/Android.bp", gatherRequiredDepsForTest()),
java.PrepareForTestWithJavaSdkLibraryFiles,
android.FixtureMergeMockFs(android.MockFS{
"a/api/current.txt": nil,
"a/api/removed.txt": nil,
"a/api/system-current.txt": nil,
"a/api/system-removed.txt": nil,
"a/api/test-current.txt": nil,
"a/api/test-removed.txt": nil,
"a/api/module-lib-current.txt": nil,
"a/api/module-lib-removed.txt": nil,
}),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.Allow_missing_dependencies = proptools.BoolPtr(true)
}),
android.PrepareForTestWithHostTools("metalava"),
)
func getAllTestSdkLibraries() []string {
modules := slices.Clone(non_updatable_modules)
for _, flagInfo := range moduleFlagInfos {
modules = append(modules, flagInfo.moduleName)
}
return modules
}
func gatherRequiredDepsForTest() string {
var bp string
extraLibraryModules := []string{
"android_test_frameworks_core_stubs_current",
"android_test_frameworks_core_stubs_current.from-text",
"android_test_frameworks_core_stubs_current.from-source",
"android_stubs_current_exportable.from-source",
"android_system_stubs_current_exportable.from-source",
"android_test_stubs_current_exportable.from-source",
"android_module_lib_stubs_current_exportable.from-source",
"android_system_server_stubs_current_exportable.from-source",
}
extraSdkLibraryModules := getAllTestSdkLibraries()
extraFilegroupModules := []string{
"non-updatable-current.txt",
"non-updatable-removed.txt",
"non-updatable-system-current.txt",
"non-updatable-system-removed.txt",
"non-updatable-test-current.txt",
"non-updatable-test-removed.txt",
"non-updatable-module-lib-current.txt",
"non-updatable-module-lib-removed.txt",
"non-updatable-system-server-current.txt",
"non-updatable-system-server-removed.txt",
"non-updatable-exportable-current.txt",
"non-updatable-exportable-removed.txt",
"non-updatable-exportable-system-current.txt",
"non-updatable-exportable-system-removed.txt",
"non-updatable-exportable-test-current.txt",
"non-updatable-exportable-test-removed.txt",
"non-updatable-exportable-module-lib-current.txt",
"non-updatable-exportable-module-lib-removed.txt",
"non-updatable-exportable-system-server-current.txt",
"non-updatable-exportable-system-server-removed.txt",
}
for _, extra := range extraLibraryModules {
bp += fmt.Sprintf(`
java_library {
name: "%s",
srcs: ["a.java"],
sdk_version: "none",
system_modules: "stable-core-platform-api-stubs-system-modules",
compile_dex: true,
}
`, extra)
}
for _, extra := range extraSdkLibraryModules {
bp += fmt.Sprintf(`
java_sdk_library {
name: "%s",
srcs: ["a.java"],
public: {
enabled: true,
},
system: {
enabled: true,
},
test: {
enabled: true,
},
module_lib: {
enabled: true,
},
api_packages: [
"foo",
],
sdk_version: "core_current",
compile_dex: true,
annotations_enabled: true,
}
`, extra)
}
for _, extra := range extraFilegroupModules {
bp += fmt.Sprintf(`
filegroup {
name: "%[1]s",
}
`, extra)
}
bp += fmt.Sprintf(`
java_defaults {
name: "android.jar_defaults",
}
`)
return bp
}
func TestCombinedApisDefaults(t *testing.T) {
testNonUpdatableModules := append(getAllTestSdkLibraries(), "framework-foo", "framework-bar")
result := android.GroupFixturePreparers(
prepareForTestWithCombinedApis,
java.FixtureWithLastReleaseApis(testNonUpdatableModules...),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.VendorVars = map[string]map[string]string{
"boolean_var": {
"for_testing": "true",
},
}
}),
).RunTestWithBp(t, `
java_sdk_library {
name: "framework-foo",
srcs: ["a.java"],
public: {
enabled: true,
},
system: {
enabled: true,
},
test: {
enabled: true,
},
module_lib: {
enabled: true,
},
api_packages: [
"foo",
],
sdk_version: "core_current",
annotations_enabled: true,
}
java_sdk_library {
name: "framework-bar",
srcs: ["a.java"],
public: {
enabled: true,
},
system: {
enabled: true,
},
test: {
enabled: true,
},
module_lib: {
enabled: true,
},
api_packages: [
"foo",
],
sdk_version: "core_current",
annotations_enabled: true,
}
combined_apis {
name: "foo",
bootclasspath: [
"framework-bar",
] + select(boolean_var_for_testing(), {
true: [
"framework-foo",
],
default: [],
}),
}
`)
subModuleDependsOnSelectAppendedModule := java.CheckModuleHasDependency(t,
result.TestContext, "foo-current.txt", "android_common", "framework-foo")
android.AssertBoolEquals(t, "Submodule expected to depend on the select-appended module",
true, subModuleDependsOnSelectAppendedModule)
android.AssertBoolEquals(t, "all-updatable-modules-system-stubs should depend on framework-bar.stubs.system",
true, java.CheckModuleHasDependency(t, result.TestContext, "all-updatable-modules-system-stubs", "android_common", "framework-bar.stubs.system"))
android.AssertBoolEquals(t, "all-updatable-modules-system-stubs.from-source should depend on framework-bar.stubs.system.from-source",
true, java.CheckModuleHasDependency(t, result.TestContext, "all-updatable-modules-system-stubs.from-source", "android_common", "framework-bar.stubs.system.from-source"))
}
func TestGetNonUpdatableModules(t *testing.T) {
// Test case: RELEASE_TELECOM_MAINLINE_MODULE is disabled
t.Run("telecom mainline module disabled", func(t *testing.T) {
extraSdkLibraryModules := getAllTestSdkLibraries()
result := android.GroupFixturePreparers(
prepareForTestWithCombinedApis,
java.FixtureWithLastReleaseApis(extraSdkLibraryModules...),
).RunTest(t)
nonUpdatableModules := getNonUpdatableModules(result.Config)
if !slices.Contains(nonUpdatableModules, telecom) {
t.Errorf("Expected non-updatable modules to contain '%s' when the release flag is disabled, but it was not found.", telecom)
}
})
// Test case: RELEASE_TELECOM_MAINLINE_MODULE is enabled
t.Run("telecom mainline module enabled", func(t *testing.T) {
extraSdkLibraryModules := getAllTestSdkLibraries()
result := android.GroupFixturePreparers(
prepareForTestWithCombinedApis,
java.FixtureWithLastReleaseApis(extraSdkLibraryModules...),
android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
variables.BuildFlags = map[string]string{
"RELEASE_TELECOM_MAINLINE_MODULE": "true",
}
}),
).RunTest(t)
nonUpdatableModules := getNonUpdatableModules(result.Config)
if slices.Contains(nonUpdatableModules, telecom) {
t.Errorf("Expected non-updatable modules to not contain '%s' when the release flag is enabled, but it was found.", telecom)
}
})
}