From 6d448b7a0a2a05125e3f118051db4f8db5f72ca8 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 29 Oct 2021 12:35:36 +0100 Subject: [PATCH 1/2] Add test for prebuilt_apis creation of system modules Previously, there were no tests for this (outside uses in other tests). This change adds a test that uses the fixture preparers that create a prebuilt_api in order to make prebuilt APIs available. That ensures that both the prebuilt_api is working as expected and the preparer creates a realistic test environment. Bug: 204189791 Test: m nothing Change-Id: I57352aa00f7b268e5286be92f177764dd63ba7e8 --- java/Android.bp | 1 + java/prebuilt_apis_test.go | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 java/prebuilt_apis_test.go diff --git a/java/Android.bp b/java/Android.bp index 78d0cc1a9..8835b4456 100644 --- a/java/Android.bp +++ b/java/Android.bp @@ -93,6 +93,7 @@ bootstrap_go_package { "platform_bootclasspath_test.go", "platform_compat_config_test.go", "plugin_test.go", + "prebuilt_apis_test.go", "rro_test.go", "sdk_test.go", "sdk_library_test.go", diff --git a/java/prebuilt_apis_test.go b/java/prebuilt_apis_test.go new file mode 100644 index 000000000..7c2e52600 --- /dev/null +++ b/java/prebuilt_apis_test.go @@ -0,0 +1,52 @@ +// Copyright 2021 Google Inc. All rights reserved. +// +// 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 java + +import ( + "sort" + "strings" + "testing" + + "android/soong/android" + "github.com/google/blueprint" +) + +func TestPrebuiltApis_SystemModulesCreation(t *testing.T) { + result := android.GroupFixturePreparers( + prepareForJavaTest, + FixtureWithPrebuiltApis(map[string][]string{ + "31": {}, + "current": {}, + }), + ).RunTest(t) + + sdkSystemModules := []string{} + result.VisitAllModules(func(module blueprint.Module) { + name := android.RemoveOptionalPrebuiltPrefix(module.Name()) + if strings.HasPrefix(name, "sdk_") && strings.HasSuffix(name, "_system_modules") { + sdkSystemModules = append(sdkSystemModules, name) + } + }) + sort.Strings(sdkSystemModules) + expected := []string{ + // 31 only has public system modules. + "sdk_public_31_system_modules", + + // current only has public system modules. + "sdk_public_current_system_modules", + } + sort.Strings(expected) + android.AssertArrayString(t, "sdk system modules", expected, sdkSystemModules) +} From 1cad3a53db591d5f40d4a7d7ac883fd10763fd98 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Fri, 29 Oct 2021 13:30:59 +0100 Subject: [PATCH 2/2] Make prebuilt_api test environment realistic Previously, the fixture preparer for prebuilt_apis would add a core-for-system-modules.jar file in every API directory even though currently they only exist in the public API directories. This change makes the test environment more realistic by only creating them for the public API. Rather than hard code that into the test code (which would duplicate the hard coding in the decodeSdkDep func) this extracts a function that is used by both. That ensures that any changes to that func will be reflected in both the test and runtime behavior. Bug: 204189791 Test: m nothing Change-Id: I346ac9c0dcf407c61de16b6027663a05821bcf62 --- java/sdk.go | 9 ++++++++- java/testing.go | 24 ++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/java/sdk.go b/java/sdk.go index 3c6b23463..42ed14f74 100644 --- a/java/sdk.go +++ b/java/sdk.go @@ -60,6 +60,12 @@ func defaultJavaLanguageVersion(ctx android.EarlyModuleContext, s android.SdkSpe } } +// systemModuleKind returns the kind of system modules to use. +func systemModuleKind() android.SdkKind { + // Currently, every sdk version uses the system modules for the public API. + return android.SdkPublic +} + func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext) sdkDep { sdkVersion := sdkContext.SdkVersion(ctx) if !sdkVersion.Valid() { @@ -105,7 +111,8 @@ func decodeSdkDep(ctx android.EarlyModuleContext, sdkContext android.SdkContext) var systemModules string if defaultJavaLanguageVersion(ctx, sdkVersion).usesJavaModules() { - systemModules = "sdk_public_" + sdkVersion.ApiLevel.String() + "_system_modules" + systemModuleKind := systemModuleKind() + systemModules = fmt.Sprintf("sdk_%s_%s_system_modules", systemModuleKind, sdkVersion.ApiLevel) } return sdkDep{ diff --git a/java/testing.go b/java/testing.go index 0a6a4fabb..fafc8d760 100644 --- a/java/testing.go +++ b/java/testing.go @@ -159,8 +159,7 @@ func FixtureWithPrebuiltApis(release2Modules map[string][]string) android.Fixtur `, strings.Join(android.SortedStringKeys(release2Modules), `", "`)) for release, modules := range release2Modules { - libs := append([]string{"android", "core-for-system-modules"}, modules...) - mockFS.Merge(prebuiltApisFilesForLibs([]string{release}, libs)) + mockFS.Merge(prebuiltApisFilesForModules([]string{release}, modules)) } return android.GroupFixturePreparers( android.FixtureAddTextFile(path, bp), @@ -168,16 +167,25 @@ func FixtureWithPrebuiltApis(release2Modules map[string][]string) android.Fixtur ) } -func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte { +func prebuiltApisFilesForModules(apiLevels []string, modules []string) map[string][]byte { + libs := append([]string{"android"}, modules...) + fs := make(map[string][]byte) for _, level := range apiLevels { - for _, lib := range sdkLibs { - for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} { - fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil + for _, sdkKind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkModule, android.SdkSystemServer, android.SdkTest} { + // A core-for-system-modules file must only be created for the sdk kind that supports it. + if sdkKind == systemModuleKind() { + fs[fmt.Sprintf("prebuilts/sdk/%s/%s/core-for-system-modules.jar", level, sdkKind)] = nil + } + + for _, lib := range libs { + // Create a jar file for every library. + fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, sdkKind, lib)] = nil + // No finalized API files for "current" if level != "current" { - fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil - fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil + fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, sdkKind, lib)] = nil + fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, sdkKind, lib)] = nil } } }