xref: /aosp_15_r20/build/soong/android/test_suites_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1// Copyright 2024 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 android
16
17import (
18	"path/filepath"
19	"testing"
20)
21
22func TestBuildTestList(t *testing.T) {
23	t.Parallel()
24	ctx := GroupFixturePreparers(
25		prepareForFakeTestSuite,
26		FixtureRegisterWithContext(func(ctx RegistrationContext) {
27			ctx.RegisterParallelSingletonType("testsuites", testSuiteFilesFactory)
28		}),
29	).RunTestWithBp(t, `
30		fake_module {
31			name: "module1",
32			outputs: [
33				"Test1/Test1.config",
34				"Test1/Test1.apk",
35			],
36			test_suites: ["ravenwood-tests"],
37		}
38		fake_module {
39			name: "module2",
40			outputs: [
41				"Test2/Test21/Test21.config",
42				"Test2/Test21/Test21.apk",
43			],
44			test_suites: ["ravenwood-tests", "robolectric-tests"],
45		}
46		fake_module {
47			name: "module_without_config",
48			outputs: [
49				"BadTest/BadTest.jar",
50			],
51			test_suites: ["robolectric-tests"],
52		}
53	`)
54
55	config := ctx.SingletonForTests("testsuites")
56	allOutputs := config.AllOutputs()
57
58	wantContents := map[string]string{
59		"robolectric-tests.zip":      "",
60		"robolectric-tests_list.zip": "",
61		"robolectric-tests_list": `host/testcases/Test2/Test21/Test21.config
62`,
63		"ravenwood-tests.zip":      "",
64		"ravenwood-tests_list.zip": "",
65		"ravenwood-tests_list": `host/testcases/Test1/Test1.config
66host/testcases/Test2/Test21/Test21.config
67`,
68	}
69	for _, output := range allOutputs {
70		want, ok := wantContents[filepath.Base(output)]
71		if !ok {
72			t.Errorf("unexpected output: %q", output)
73			continue
74		}
75
76		got := ""
77		if want != "" {
78			got = ContentFromFileRuleForTests(t, ctx.TestContext, config.MaybeOutput(output))
79		}
80
81		if want != got {
82			t.Errorf("want %q, got %q", want, got)
83		}
84	}
85}
86
87type fake_module struct {
88	ModuleBase
89	props struct {
90		Outputs     []string
91		Test_suites []string
92	}
93}
94
95func fakeTestSuiteFactory() Module {
96	module := &fake_module{}
97	base := module.base()
98	module.AddProperties(&base.nameProperties, &module.props)
99	InitAndroidModule(module)
100	return module
101}
102
103var prepareForFakeTestSuite = GroupFixturePreparers(
104	FixtureRegisterWithContext(func(ctx RegistrationContext) {
105		ctx.RegisterModuleType("fake_module", fakeTestSuiteFactory)
106	}),
107)
108
109func (f *fake_module) GenerateAndroidBuildActions(ctx ModuleContext) {
110	for _, output := range f.props.Outputs {
111		ctx.InstallFile(pathForTestCases(ctx), output, nil)
112	}
113}
114
115func (f *fake_module) TestSuites() []string {
116	return f.props.Test_suites
117}
118