| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 1 | // Copyright 2018 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 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 15 | package aconfig | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 16 |  | 
|  | 17 | import ( | 
| LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 18 | "slices" | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 19 | "strings" | 
|  | 20 | "testing" | 
|  | 21 |  | 
|  | 22 | "android/soong/android" | 
|  | 23 | ) | 
|  | 24 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 25 | func TestAconfigDeclarations(t *testing.T) { | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 26 | bp := ` | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 27 | aconfig_declarations { | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 28 | name: "module_name", | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 29 | package: "com.example.package", | 
| Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 30 | container: "com.android.foo", | 
| Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 31 | exportable: true, | 
| Zhi Dou | 8a35a6f | 2023-07-19 18:04:24 +0000 | [diff] [blame] | 32 | srcs: [ | 
|  | 33 | "foo.aconfig", | 
|  | 34 | "bar.aconfig", | 
|  | 35 | ], | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 36 | } | 
|  | 37 | ` | 
|  | 38 | result := runTest(t, android.FixtureExpectsNoErrors, bp) | 
|  | 39 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 40 | module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | // Check that the provider has the right contents | 
| LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 43 | depData, _ := android.SingletonModuleProvider(result, module, android.AconfigDeclarationsProviderKey) | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 44 | android.AssertStringEquals(t, "package", depData.Package, "com.example.package") | 
| Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 45 | android.AssertStringEquals(t, "container", depData.Container, "com.android.foo") | 
| Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 46 | android.AssertBoolEquals(t, "exportable", depData.Exportable, true) | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 47 | if !strings.HasSuffix(depData.IntermediateCacheOutputPath.String(), "/intermediate.pb") { | 
|  | 48 | t.Errorf("Missing intermediates proto path in provider: %s", depData.IntermediateCacheOutputPath.String()) | 
|  | 49 | } | 
|  | 50 | if !strings.HasSuffix(depData.IntermediateDumpOutputPath.String(), "/intermediate.txt") { | 
|  | 51 | t.Errorf("Missing intermediates text path in provider: %s", depData.IntermediateDumpOutputPath.String()) | 
| Joe Onorato | 175073c | 2023-06-01 14:42:59 -0700 | [diff] [blame] | 52 | } | 
|  | 53 | } | 
| Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 54 |  | 
|  | 55 | func TestAconfigDeclarationsWithExportableUnset(t *testing.T) { | 
|  | 56 | bp := ` | 
|  | 57 | aconfig_declarations { | 
|  | 58 | name: "module_name", | 
|  | 59 | package: "com.example.package", | 
|  | 60 | container: "com.android.foo", | 
|  | 61 | srcs: [ | 
|  | 62 | "foo.aconfig", | 
|  | 63 | "bar.aconfig", | 
|  | 64 | ], | 
|  | 65 | } | 
|  | 66 | ` | 
|  | 67 | result := runTest(t, android.FixtureExpectsNoErrors, bp) | 
|  | 68 |  | 
|  | 69 | module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule) | 
|  | 70 | depData, _ := android.SingletonModuleProvider(result, module, android.AconfigDeclarationsProviderKey) | 
|  | 71 | android.AssertBoolEquals(t, "exportable", depData.Exportable, false) | 
|  | 72 | } | 
| Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 73 |  | 
|  | 74 | func TestAconfigDeclarationsWithContainer(t *testing.T) { | 
|  | 75 | bp := ` | 
|  | 76 | aconfig_declarations { | 
|  | 77 | name: "module_name", | 
|  | 78 | package: "com.example.package", | 
|  | 79 | container: "com.android.foo", | 
|  | 80 | srcs: [ | 
|  | 81 | "foo.aconfig", | 
|  | 82 | ], | 
|  | 83 | } | 
|  | 84 | ` | 
|  | 85 | result := runTest(t, android.FixtureExpectsNoErrors, bp) | 
|  | 86 |  | 
|  | 87 | module := result.ModuleForTests("module_name", "") | 
|  | 88 | rule := module.Rule("aconfig") | 
|  | 89 | android.AssertStringEquals(t, "rule must contain container", rule.Args["container"], "--container com.android.foo") | 
|  | 90 | } | 
|  | 91 |  | 
| Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame] | 92 | func TestMandatoryProperties(t *testing.T) { | 
|  | 93 | testCases := []struct { | 
|  | 94 | name          string | 
|  | 95 | expectedError string | 
|  | 96 | bp            string | 
|  | 97 | }{ | 
|  | 98 | { | 
|  | 99 | name: "Srcs missing from aconfig_declarations", | 
|  | 100 | bp: ` | 
|  | 101 | aconfig_declarations { | 
|  | 102 | name: "my_aconfig_declarations_foo", | 
|  | 103 | package: "com.example.package", | 
|  | 104 | container: "otherapex", | 
|  | 105 | }`, | 
|  | 106 | expectedError: `missing source files`, | 
|  | 107 | }, | 
|  | 108 | { | 
|  | 109 | name: "Package missing from aconfig_declarations", | 
|  | 110 | bp: ` | 
|  | 111 | aconfig_declarations { | 
|  | 112 | name: "my_aconfig_declarations_foo", | 
|  | 113 | container: "otherapex", | 
|  | 114 | srcs: ["foo.aconfig"], | 
|  | 115 | }`, | 
|  | 116 | expectedError: `missing package property`, | 
|  | 117 | }, | 
|  | 118 | { | 
|  | 119 | name: "Container missing from aconfig_declarations", | 
|  | 120 | bp: ` | 
|  | 121 | aconfig_declarations { | 
|  | 122 | name: "my_aconfig_declarations_foo", | 
|  | 123 | package: "com.example.package", | 
|  | 124 | srcs: ["foo.aconfig"], | 
|  | 125 | }`, | 
|  | 126 | expectedError: `missing container property`, | 
|  | 127 | }, | 
|  | 128 | } | 
|  | 129 | for _, test := range testCases { | 
|  | 130 | t.Run(test.name, func(t *testing.T) { | 
|  | 131 | errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError) | 
|  | 132 | android.GroupFixturePreparers(PrepareForTestWithAconfigBuildComponents). | 
|  | 133 | ExtendWithErrorHandler(errorHandler). | 
|  | 134 | RunTestWithBp(t, test.bp) | 
|  | 135 | }) | 
|  | 136 | } | 
| Yu Liu | eeff222 | 2024-03-19 23:07:34 +0000 | [diff] [blame] | 137 | } | 
| LaMont Jones | 21d04d9 | 2024-06-11 11:28:54 -0700 | [diff] [blame] | 138 |  | 
|  | 139 | func TestAssembleFileName(t *testing.T) { | 
|  | 140 | testCases := []struct { | 
|  | 141 | name          string | 
|  | 142 | releaseConfig string | 
|  | 143 | path          string | 
|  | 144 | expectedValue string | 
|  | 145 | }{ | 
|  | 146 | { | 
|  | 147 | name:          "active release config", | 
|  | 148 | path:          "file.path", | 
|  | 149 | expectedValue: "file.path", | 
|  | 150 | }, | 
|  | 151 | { | 
|  | 152 | name:          "release config FOO", | 
|  | 153 | releaseConfig: "FOO", | 
|  | 154 | path:          "file.path", | 
|  | 155 | expectedValue: "file-FOO.path", | 
|  | 156 | }, | 
|  | 157 | } | 
|  | 158 | for _, test := range testCases { | 
|  | 159 | actualValue := assembleFileName(test.releaseConfig, test.path) | 
|  | 160 | if actualValue != test.expectedValue { | 
|  | 161 | t.Errorf("Expected %q found %q", test.expectedValue, actualValue) | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | func TestGenerateAndroidBuildActions(t *testing.T) { | 
|  | 167 | testCases := []struct { | 
|  | 168 | name         string | 
|  | 169 | buildFlags   map[string]string | 
|  | 170 | bp           string | 
|  | 171 | errorHandler android.FixtureErrorHandler | 
|  | 172 | }{ | 
|  | 173 | { | 
|  | 174 | name: "generate extra", | 
|  | 175 | buildFlags: map[string]string{ | 
|  | 176 | "RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS": "config2", | 
|  | 177 | "RELEASE_ACONFIG_VALUE_SETS":            "aconfig_value_set-config1", | 
|  | 178 | "RELEASE_ACONFIG_VALUE_SETS_config2":    "aconfig_value_set-config2", | 
|  | 179 | }, | 
|  | 180 | bp: ` | 
|  | 181 | aconfig_declarations { | 
|  | 182 | name: "module_name", | 
|  | 183 | package: "com.example.package", | 
|  | 184 | container: "com.android.foo", | 
|  | 185 | srcs: [ | 
|  | 186 | "foo.aconfig", | 
|  | 187 | "bar.aconfig", | 
|  | 188 | ], | 
|  | 189 | } | 
|  | 190 | aconfig_value_set { | 
|  | 191 | name: "aconfig_value_set-config1", | 
|  | 192 | values: [] | 
|  | 193 | } | 
|  | 194 | aconfig_value_set { | 
|  | 195 | name: "aconfig_value_set-config2", | 
|  | 196 | values: [] | 
|  | 197 | } | 
|  | 198 | `, | 
|  | 199 | }, | 
|  | 200 | } | 
|  | 201 | for _, test := range testCases { | 
|  | 202 | fixture := PrepareForTest(t, addBuildFlagsForTest(test.buildFlags)) | 
|  | 203 | if test.errorHandler != nil { | 
|  | 204 | fixture = fixture.ExtendWithErrorHandler(test.errorHandler) | 
|  | 205 | } | 
|  | 206 | result := fixture.RunTestWithBp(t, test.bp) | 
|  | 207 | module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule) | 
|  | 208 | depData, _ := android.SingletonModuleProvider(result, module, android.AconfigReleaseDeclarationsProviderKey) | 
|  | 209 | expectedKeys := []string{""} | 
|  | 210 | for _, rc := range strings.Split(test.buildFlags["RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS"], " ") { | 
|  | 211 | expectedKeys = append(expectedKeys, rc) | 
|  | 212 | } | 
|  | 213 | slices.Sort(expectedKeys) | 
|  | 214 | actualKeys := []string{} | 
|  | 215 | for rc := range depData { | 
|  | 216 | actualKeys = append(actualKeys, rc) | 
|  | 217 | } | 
|  | 218 | slices.Sort(actualKeys) | 
|  | 219 | android.AssertStringEquals(t, "provider keys", strings.Join(expectedKeys, " "), strings.Join(actualKeys, " ")) | 
|  | 220 | for _, rc := range actualKeys { | 
|  | 221 | if !strings.HasSuffix(depData[rc].IntermediateCacheOutputPath.String(), assembleFileName(rc, "/intermediate.pb")) { | 
|  | 222 | t.Errorf("Incorrect intermediates proto path in provider for release config %s: %s", rc, depData[rc].IntermediateCacheOutputPath.String()) | 
|  | 223 | } | 
|  | 224 | if !strings.HasSuffix(depData[rc].IntermediateDumpOutputPath.String(), assembleFileName(rc, "/intermediate.txt")) { | 
|  | 225 | t.Errorf("Incorrect intermediates text path in provider for release config %s: %s", rc, depData[rc].IntermediateDumpOutputPath.String()) | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 | } | 
|  | 229 | } |