blob: 2b843532504c616ff5afe6204cecde700926f421 [file] [log] [blame]
Paul Duffin6d448b72021-10-29 12:35:36 +01001// Copyright 2021 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 "sort"
19 "strings"
20 "testing"
21
22 "android/soong/android"
Anton Hansson3a3f1692022-02-15 12:55:11 +000023
Paul Duffin6d448b72021-10-29 12:35:36 +010024 "github.com/google/blueprint"
25)
26
Anton Hansson3a3f1692022-02-15 12:55:11 +000027func intPtr(v int) *int {
28 return &v
29}
30
Paul Duffin6d448b72021-10-29 12:35:36 +010031func TestPrebuiltApis_SystemModulesCreation(t *testing.T) {
32 result := android.GroupFixturePreparers(
33 prepareForJavaTest,
34 FixtureWithPrebuiltApis(map[string][]string{
35 "31": {},
Paul Duffin004547f2021-10-29 13:50:24 +010036 "32": {},
Paul Duffin6d448b72021-10-29 12:35:36 +010037 "current": {},
38 }),
39 ).RunTest(t)
40
41 sdkSystemModules := []string{}
42 result.VisitAllModules(func(module blueprint.Module) {
43 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
44 if strings.HasPrefix(name, "sdk_") && strings.HasSuffix(name, "_system_modules") {
45 sdkSystemModules = append(sdkSystemModules, name)
46 }
47 })
48 sort.Strings(sdkSystemModules)
49 expected := []string{
50 // 31 only has public system modules.
51 "sdk_public_31_system_modules",
52
Paul Duffin004547f2021-10-29 13:50:24 +010053 // 32 and current both have public and module-lib system modules.
54 "sdk_public_32_system_modules",
55 "sdk_module-lib_32_system_modules",
Paul Duffin6d448b72021-10-29 12:35:36 +010056 "sdk_public_current_system_modules",
Paul Duffin004547f2021-10-29 13:50:24 +010057 "sdk_module-lib_current_system_modules",
Paul Duffin6d448b72021-10-29 12:35:36 +010058 }
59 sort.Strings(expected)
60 android.AssertArrayString(t, "sdk system modules", expected, sdkSystemModules)
61}
Anton Hansson3a3f1692022-02-15 12:55:11 +000062
63func TestPrebuiltApis_WithExtensions(t *testing.T) {
Anton Hansson7212dbe2022-09-20 17:08:49 +000064 runTestWithBaseExtensionLevel := func(v int) (foo_input, bar_input, baz_input string) {
Anton Hansson3a3f1692022-02-15 12:55:11 +000065 result := android.GroupFixturePreparers(
66 prepareForJavaTest,
67 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
68 variables.Platform_base_sdk_extension_version = intPtr(v)
69 }),
70 FixtureWithPrebuiltApisAndExtensions(map[string][]string{
71 "31": {"foo"},
Anton Hansson7212dbe2022-09-20 17:08:49 +000072 "32": {"foo", "bar", "baz"},
Anton Hansson3a3f1692022-02-15 12:55:11 +000073 "current": {"foo", "bar"},
74 }, map[string][]string{
75 "1": {"foo"},
76 "2": {"foo", "bar"},
77 }),
78 ).RunTest(t)
79 foo_input = result.ModuleForTests("foo.api.public.latest", "").Rule("generator").Implicits[0].String()
80 bar_input = result.ModuleForTests("bar.api.public.latest", "").Rule("generator").Implicits[0].String()
Anton Hansson7212dbe2022-09-20 17:08:49 +000081 baz_input = result.ModuleForTests("baz.api.public.latest", "").Rule("generator").Implicits[0].String()
Anton Hansson3a3f1692022-02-15 12:55:11 +000082 return
83 }
Anton Hansson7212dbe2022-09-20 17:08:49 +000084 // Extension 2 is the latest for both foo and bar, finalized after the base extension version.
85 foo_input, bar_input, baz_input := runTestWithBaseExtensionLevel(1)
86 android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input)
87 android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input)
88 android.AssertStringEquals(t, "Expected latest baz = api level 32", "prebuilts/sdk/32/public/api/baz.txt", baz_input)
Anton Hansson3a3f1692022-02-15 12:55:11 +000089
Anton Hansson7212dbe2022-09-20 17:08:49 +000090 // Extension 2 is the latest for both foo and bar, finalized together with 32
91 foo_input, bar_input, baz_input = runTestWithBaseExtensionLevel(2)
92 android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input)
93 android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input)
94 android.AssertStringEquals(t, "Expected latest baz = api level 32", "prebuilts/sdk/32/public/api/baz.txt", baz_input)
95
96 // Extension 3 is the current extension, but it has not yet been finalized.
97 foo_input, bar_input, baz_input = runTestWithBaseExtensionLevel(3)
98 android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input)
99 android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input)
100 android.AssertStringEquals(t, "Expected latest baz = api level 32", "prebuilts/sdk/32/public/api/baz.txt", baz_input)
Anton Hansson3a3f1692022-02-15 12:55:11 +0000101}