• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15package aconfig
16
17import (
18	"strings"
19	"testing"
20
21	"android/soong/android"
22)
23
24func TestAconfigDeclarations(t *testing.T) {
25	bp := `
26		aconfig_declarations {
27			name: "module_name",
28			package: "com.example.package",
29			container: "com.android.foo",
30			exportable: true,
31			srcs: [
32				"foo.aconfig",
33				"bar.aconfig",
34			],
35		}
36	`
37	result := runTest(t, android.FixtureExpectsNoErrors, bp)
38
39	module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule)
40
41	// Check that the provider has the right contents
42	depData, _ := android.SingletonModuleProvider(result, module, android.AconfigDeclarationsProviderKey)
43	android.AssertStringEquals(t, "package", depData.Package, "com.example.package")
44	android.AssertStringEquals(t, "container", depData.Container, "com.android.foo")
45	android.AssertBoolEquals(t, "exportable", depData.Exportable, true)
46	if !strings.HasSuffix(depData.IntermediateCacheOutputPath.String(), "/intermediate.pb") {
47		t.Errorf("Missing intermediates proto path in provider: %s", depData.IntermediateCacheOutputPath.String())
48	}
49	if !strings.HasSuffix(depData.IntermediateDumpOutputPath.String(), "/intermediate.txt") {
50		t.Errorf("Missing intermediates text path in provider: %s", depData.IntermediateDumpOutputPath.String())
51	}
52}
53
54func TestAconfigDeclarationsWithExportableUnset(t *testing.T) {
55	bp := `
56		aconfig_declarations {
57			name: "module_name",
58			package: "com.example.package",
59			container: "com.android.foo",
60			srcs: [
61				"foo.aconfig",
62				"bar.aconfig",
63			],
64		}
65	`
66	result := runTest(t, android.FixtureExpectsNoErrors, bp)
67
68	module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule)
69	depData, _ := android.SingletonModuleProvider(result, module, android.AconfigDeclarationsProviderKey)
70	android.AssertBoolEquals(t, "exportable", depData.Exportable, false)
71}
72
73func TestAconfigDeclarationsWithContainer(t *testing.T) {
74	bp := `
75		aconfig_declarations {
76			name: "module_name",
77			package: "com.example.package",
78			container: "com.android.foo",
79			srcs: [
80				"foo.aconfig",
81			],
82		}
83	`
84	result := runTest(t, android.FixtureExpectsNoErrors, bp)
85
86	module := result.ModuleForTests("module_name", "")
87	rule := module.Rule("aconfig")
88	android.AssertStringEquals(t, "rule must contain container", rule.Args["container"], "--container com.android.foo")
89}
90
91func TestMandatoryProperties(t *testing.T) {
92	testCases := []struct {
93		name          string
94		expectedError string
95		bp            string
96	}{
97		{
98			name: "Srcs missing from aconfig_declarations",
99			bp: `
100				aconfig_declarations {
101					name: "my_aconfig_declarations_foo",
102					package: "com.example.package",
103					container: "otherapex",
104				}`,
105			expectedError: `missing source files`,
106		},
107		{
108			name: "Package missing from aconfig_declarations",
109			bp: `
110				aconfig_declarations {
111					name: "my_aconfig_declarations_foo",
112					container: "otherapex",
113					srcs: ["foo.aconfig"],
114				}`,
115			expectedError: `missing package property`,
116		},
117		{
118			name: "Container missing from aconfig_declarations",
119			bp: `
120				aconfig_declarations {
121					name: "my_aconfig_declarations_foo",
122					package: "com.example.package",
123					srcs: ["foo.aconfig"],
124				}`,
125			expectedError: `missing container property`,
126		},
127	}
128	for _, test := range testCases {
129		t.Run(test.name, func(t *testing.T) {
130			errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
131			android.GroupFixturePreparers(PrepareForTestWithAconfigBuildComponents).
132				ExtendWithErrorHandler(errorHandler).
133				RunTestWithBp(t, test.bp)
134		})
135	}
136}
137