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) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 32 | t.Parallel() |
Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 33 | result := android.GroupFixturePreparers( |
| 34 | prepareForJavaTest, |
| 35 | FixtureWithPrebuiltApis(map[string][]string{ |
| 36 | "31": {}, |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 37 | "32": {}, |
Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 38 | "current": {}, |
| 39 | }), |
| 40 | ).RunTest(t) |
| 41 | |
| 42 | sdkSystemModules := []string{} |
| 43 | result.VisitAllModules(func(module blueprint.Module) { |
| 44 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 45 | if strings.HasPrefix(name, "sdk_") && strings.HasSuffix(name, "_system_modules") { |
| 46 | sdkSystemModules = append(sdkSystemModules, name) |
| 47 | } |
| 48 | }) |
| 49 | sort.Strings(sdkSystemModules) |
| 50 | expected := []string{ |
| 51 | // 31 only has public system modules. |
| 52 | "sdk_public_31_system_modules", |
| 53 | |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 54 | // 32 and current both have public and module-lib system modules. |
| 55 | "sdk_public_32_system_modules", |
| 56 | "sdk_module-lib_32_system_modules", |
Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 57 | "sdk_public_current_system_modules", |
Paul Duffin | 004547f | 2021-10-29 13:50:24 +0100 | [diff] [blame] | 58 | "sdk_module-lib_current_system_modules", |
Paul Duffin | 6d448b7 | 2021-10-29 12:35:36 +0100 | [diff] [blame] | 59 | } |
| 60 | sort.Strings(expected) |
| 61 | android.AssertArrayString(t, "sdk system modules", expected, sdkSystemModules) |
| 62 | } |
Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 63 | |
| 64 | func TestPrebuiltApis_WithExtensions(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 65 | t.Parallel() |
Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 66 | runTestWithBaseExtensionLevel := func(v int) (foo_input, bar_input, baz_input string) { |
Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 67 | result := android.GroupFixturePreparers( |
| 68 | prepareForJavaTest, |
| 69 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 70 | variables.Platform_base_sdk_extension_version = intPtr(v) |
| 71 | }), |
| 72 | FixtureWithPrebuiltApisAndExtensions(map[string][]string{ |
| 73 | "31": {"foo"}, |
Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 74 | "32": {"foo", "bar", "baz"}, |
Anton Hansson | 3a3f169 | 2022-02-15 12:55:11 +0000 | [diff] [blame] | 75 | "current": {"foo", "bar"}, |
| 76 | }, map[string][]string{ |
| 77 | "1": {"foo"}, |
| 78 | "2": {"foo", "bar"}, |
| 79 | }), |
| 80 | ).RunTest(t) |
| 81 | foo_input = result.ModuleForTests("foo.api.public.latest", "").Rule("generator").Implicits[0].String() |
| 82 | 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] | 83 | 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] | 84 | return |
| 85 | } |
Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 86 | // Extension 2 is the latest for both foo and bar, finalized after the base extension version. |
| 87 | foo_input, bar_input, baz_input := runTestWithBaseExtensionLevel(1) |
| 88 | android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input) |
| 89 | android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input) |
| 90 | 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] | 91 | |
Anton Hansson | 7212dbe | 2022-09-20 17:08:49 +0000 | [diff] [blame] | 92 | // Extension 2 is the latest for both foo and bar, finalized together with 32 |
| 93 | foo_input, bar_input, baz_input = runTestWithBaseExtensionLevel(2) |
| 94 | android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input) |
| 95 | android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input) |
| 96 | android.AssertStringEquals(t, "Expected latest baz = api level 32", "prebuilts/sdk/32/public/api/baz.txt", baz_input) |
| 97 | |
| 98 | // Extension 3 is the current extension, but it has not yet been finalized. |
| 99 | foo_input, bar_input, baz_input = runTestWithBaseExtensionLevel(3) |
| 100 | android.AssertStringEquals(t, "Expected latest foo = extension level 2", "prebuilts/sdk/extensions/2/public/api/foo.txt", foo_input) |
| 101 | android.AssertStringEquals(t, "Expected latest bar = extension level 2", "prebuilts/sdk/extensions/2/public/api/bar.txt", bar_input) |
| 102 | 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] | 103 | } |
Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 104 | |
| 105 | func TestPrebuiltApis_WithIncrementalApi(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame^] | 106 | t.Parallel() |
Todd Lee | 2ec7e1c | 2023-08-25 18:02:13 +0000 | [diff] [blame] | 107 | runTestWithIncrementalApi := func() (foo_input, bar_input, baz_input string) { |
| 108 | result := android.GroupFixturePreparers( |
| 109 | prepareForJavaTest, |
| 110 | FixtureWithPrebuiltIncrementalApis(map[string][]string{ |
| 111 | "33.0": {"foo"}, |
| 112 | "33.1": {"foo", "bar", "baz"}, |
| 113 | "33.2": {"foo", "bar"}, |
| 114 | "current": {"foo", "bar"}, |
| 115 | }), |
| 116 | ).RunTest(t) |
| 117 | foo_input = result.ModuleForTests("foo.api.public.latest", "").Rule("generator").Implicits[0].String() |
| 118 | bar_input = result.ModuleForTests("bar.api.public.latest", "").Rule("generator").Implicits[0].String() |
| 119 | baz_input = result.ModuleForTests("baz.api.public.latest", "").Rule("generator").Implicits[0].String() |
| 120 | return |
| 121 | } |
| 122 | // 33.1 is the latest for baz, 33.2 is the latest for both foo & bar |
| 123 | foo_input, bar_input, baz_input := runTestWithIncrementalApi() |
| 124 | android.AssertStringEquals(t, "Expected latest foo = api level 33.2", "prebuilts/sdk/33.2/public/api/foo.txt", foo_input) |
| 125 | android.AssertStringEquals(t, "Expected latest bar = api level 33.2", "prebuilts/sdk/33.2/public/api/bar.txt", bar_input) |
| 126 | android.AssertStringEquals(t, "Expected latest baz = api level 33.1", "prebuilts/sdk/33.1/public/api/baz.txt", baz_input) |
| 127 | } |