1// Copyright 2023 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 aidl_library
16
17import (
18	"android/soong/android"
19	"testing"
20)
21
22func TestAidlLibrary(t *testing.T) {
23	t.Parallel()
24	ctx := android.GroupFixturePreparers(
25		PrepareForTestWithAidlLibrary,
26		android.MockFS{
27			"package_bar/Android.bp": []byte(`
28			aidl_library {
29					name: "bar",
30					srcs: ["x/y/Bar.aidl"],
31					strip_import_prefix: "x",
32				}
33			`),
34		}.AddToFixture(),
35		android.MockFS{
36			"package_foo/Android.bp": []byte(`
37			aidl_library {
38					name: "foo",
39					srcs: ["a/b/Foo.aidl"],
40					hdrs: ["a/Header.aidl"],
41					strip_import_prefix: "a",
42					deps: ["bar"],
43				}
44			`),
45		}.AddToFixture(),
46	).RunTest(t).TestContext
47
48	foo := ctx.ModuleForTests("foo", "").Module().(*AidlLibrary)
49	actualInfo, _ := android.SingletonModuleProvider(ctx, foo, AidlLibraryProvider)
50
51	android.AssertArrayString(
52		t,
53		"aidl include dirs",
54		[]string{"package_foo/a", "package_bar/x"},
55		android.Paths(actualInfo.IncludeDirs.ToList()).Strings(),
56	)
57
58	android.AssertPathsRelativeToTopEquals(
59		t,
60		"aidl srcs paths",
61		[]string{"package_foo/a/b/Foo.aidl"},
62		actualInfo.Srcs,
63	)
64
65	android.AssertPathsRelativeToTopEquals(
66		t,
67		"aidl hdrs paths",
68		[]string{"package_foo/a/Header.aidl"},
69		actualInfo.Hdrs.ToList(),
70	)
71}
72
73func TestAidlLibraryWithoutStripImportPrefix(t *testing.T) {
74	t.Parallel()
75	ctx := android.GroupFixturePreparers(
76		PrepareForTestWithAidlLibrary,
77		android.MockFS{
78			"package_bar/Android.bp": []byte(`
79			aidl_library {
80					name: "bar",
81					srcs: ["x/y/Bar.aidl"],
82					hdrs: ["BarHeader.aidl"],
83				}
84			`),
85		}.AddToFixture(),
86		android.MockFS{
87			"package_foo/Android.bp": []byte(`
88			aidl_library {
89					name: "foo",
90					srcs: ["a/b/Foo.aidl"],
91					deps: ["bar"],
92				}
93			`),
94		}.AddToFixture(),
95	).RunTest(t).TestContext
96
97	foo := ctx.ModuleForTests("foo", "").Module().(*AidlLibrary)
98	actualInfo, _ := android.SingletonModuleProvider(ctx, foo, AidlLibraryProvider)
99
100	android.AssertArrayString(
101		t,
102		"aidl include dirs",
103		[]string{"package_foo", "package_bar"},
104		android.Paths(actualInfo.IncludeDirs.ToList()).Strings(),
105	)
106
107	android.AssertPathsRelativeToTopEquals(
108		t,
109		"aidl srcs paths",
110		[]string{"package_foo/a/b/Foo.aidl"},
111		actualInfo.Srcs,
112	)
113
114	android.AssertPathsRelativeToTopEquals(
115		t,
116		"aidl hdrs paths",
117		[]string{"package_bar/BarHeader.aidl"},
118		actualInfo.Hdrs.ToList(),
119	)
120}
121
122func TestAidlLibraryWithNoSrcsHdrsDeps(t *testing.T) {
123	t.Parallel()
124	android.GroupFixturePreparers(
125		PrepareForTestWithAidlLibrary,
126		android.MockFS{
127			"package_bar/Android.bp": []byte(`
128			aidl_library {
129					name: "bar",
130				}
131			`),
132		}.AddToFixture(),
133	).
134		ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("at least srcs or hdrs prop must be non-empty")).
135		RunTest(t)
136}
137