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, bar_input, baz_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", "baz"}, 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 baz_input = result.ModuleForTests("baz.api.public.latest", "").Rule("generator").Implicits[0].String() 82 return 83 } 84 // 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) 89 90 // 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) 101} 102 103func TestPrebuiltApis_WithIncrementalApi(t *testing.T) { 104 runTestWithIncrementalApi := func() (foo_input, bar_input, baz_input string) { 105 result := android.GroupFixturePreparers( 106 prepareForJavaTest, 107 FixtureWithPrebuiltIncrementalApis(map[string][]string{ 108 "33.0": {"foo"}, 109 "33.1": {"foo", "bar", "baz"}, 110 "33.2": {"foo", "bar"}, 111 "current": {"foo", "bar"}, 112 }), 113 ).RunTest(t) 114 foo_input = result.ModuleForTests("foo.api.public.latest", "").Rule("generator").Implicits[0].String() 115 bar_input = result.ModuleForTests("bar.api.public.latest", "").Rule("generator").Implicits[0].String() 116 baz_input = result.ModuleForTests("baz.api.public.latest", "").Rule("generator").Implicits[0].String() 117 return 118 } 119 // 33.1 is the latest for baz, 33.2 is the latest for both foo & bar 120 foo_input, bar_input, baz_input := runTestWithIncrementalApi() 121 android.AssertStringEquals(t, "Expected latest foo = api level 33.2", "prebuilts/sdk/33.2/public/api/foo.txt", foo_input) 122 android.AssertStringEquals(t, "Expected latest bar = api level 33.2", "prebuilts/sdk/33.2/public/api/bar.txt", bar_input) 123 android.AssertStringEquals(t, "Expected latest baz = api level 33.1", "prebuilts/sdk/33.1/public/api/baz.txt", baz_input) 124} 125