• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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"
23
24	"github.com/google/blueprint"
25)
26
27func intPtr(v int) *int {
28	return &v
29}
30
31func TestPrebuiltApis_SystemModulesCreation(t *testing.T) {
32	result := android.GroupFixturePreparers(
33		prepareForJavaTest,
34		FixtureWithPrebuiltApis(map[string][]string{
35			"31":      {},
36			"32":      {},
37			"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
53		// 32 and current both have public and module-lib system modules.
54		"sdk_public_32_system_modules",
55		"sdk_module-lib_32_system_modules",
56		"sdk_public_current_system_modules",
57		"sdk_module-lib_current_system_modules",
58	}
59	sort.Strings(expected)
60	android.AssertArrayString(t, "sdk system modules", expected, sdkSystemModules)
61}
62
63func TestPrebuiltApis_WithExtensions(t *testing.T) {
64	runTestWithBaseExtensionLevel := func(v int) (foo_input string, bar_input string) {
65		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"},
72				"32":      {"foo", "bar"},
73				"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()
81		return
82	}
83	// Here, the base extension level is 1, so extension level 2 is the latest
84	foo_input, bar_input := runTestWithBaseExtensionLevel(1)
85	android.AssertStringEquals(t, "Expected latest = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input)
86	android.AssertStringEquals(t, "Expected latest = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input)
87
88	// Here, the base extension level is 2, so 2 is not later than 32.
89	foo_input, bar_input = runTestWithBaseExtensionLevel(2)
90	android.AssertStringEquals(t, "Expected latest = api level 32", "prebuilts/sdk/32/public/api/foo.txt", foo_input)
91	android.AssertStringEquals(t, "Expected latest = api level 32", "prebuilts/sdk/32/public/api/bar.txt", bar_input)
92}
93