| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 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 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "sort" | 
|  | 19 | "strings" | 
|  | 20 | "testing" | 
|  | 21 |  | 
|  | 22 | "android/soong/android" | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 23 |  | 
| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 24 | "github.com/google/blueprint" | 
|  | 25 | ) | 
|  | 26 |  | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 27 | func intPtr(v int) *int { | 
|  | 28 | return &v | 
|  | 29 | } | 
|  | 30 |  | 
| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 31 | func TestPrebuiltApis_SystemModulesCreation(t *testing.T) { | 
|  | 32 | result := android.GroupFixturePreparers( | 
|  | 33 | prepareForJavaTest, | 
|  | 34 | FixtureWithPrebuiltApis(map[string][]string{ | 
|  | 35 | "31":      {}, | 
| Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 36 | "32":      {}, | 
| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 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 |  | 
| Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 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", | 
| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 56 | "sdk_public_current_system_modules", | 
| Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 57 | "sdk_module-lib_current_system_modules", | 
| Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 58 | } | 
|  | 59 | sort.Strings(expected) | 
|  | 60 | android.AssertArrayString(t, "sdk system modules", expected, sdkSystemModules) | 
|  | 61 | } | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 62 |  | 
|  | 63 | func TestPrebuiltApis_WithExtensions(t *testing.T) { | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 64 | runTestWithBaseExtensionLevel := func(v int) (foo_input, bar_input, baz_input string) { | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 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"}, | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 72 | "32":      {"foo", "bar", "baz"}, | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 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() | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 81 | baz_input = result.ModuleForTests("baz.api.public.latest", "").Rule("generator").Implicits[0].String() | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 82 | return | 
|  | 83 | } | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 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) | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 89 |  | 
| Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 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) | 
| Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 101 | } | 
| Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | func 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 | } |