xref: /aosp_15_r20/build/soong/tradefed_modules/test_suite_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.
14package tradefed_modules
15
16import (
17	"android/soong/android"
18	"android/soong/java"
19	"encoding/json"
20	"slices"
21	"testing"
22)
23
24func TestTestSuites(t *testing.T) {
25	t.Parallel()
26	ctx := android.GroupFixturePreparers(
27		java.PrepareForTestWithJavaDefaultModules,
28		android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
29	).RunTestWithBp(t, `
30		android_test {
31			name: "TestModule1",
32			sdk_version: "current",
33		}
34
35		android_test {
36			name: "TestModule2",
37			sdk_version: "current",
38		}
39
40		test_suite {
41			name: "my-suite",
42			description: "a test suite",
43			tests: [
44				"TestModule1",
45				"TestModule2",
46			]
47		}
48	`)
49	manifestPath := ctx.ModuleForTests("my-suite", "android_common").Output("out/soong/test_suites/my-suite/my-suite.json")
50	var actual testSuiteManifest
51	if err := json.Unmarshal([]byte(android.ContentFromFileRuleForTests(t, ctx.TestContext, manifestPath)), &actual); err != nil {
52		t.Errorf("failed to unmarshal manifest: %v", err)
53	}
54	slices.Sort(actual.Files)
55
56	expected := testSuiteManifest{
57		Name: "my-suite",
58		Files: []string{
59			"target/testcases/TestModule1/TestModule1.config",
60			"target/testcases/TestModule1/arm64/TestModule1.apk",
61			"target/testcases/TestModule2/TestModule2.config",
62			"target/testcases/TestModule2/arm64/TestModule2.apk",
63		},
64	}
65
66	android.AssertDeepEquals(t, "manifests differ", expected, actual)
67}
68
69func TestTestSuitesWithNested(t *testing.T) {
70	t.Parallel()
71	ctx := android.GroupFixturePreparers(
72		java.PrepareForTestWithJavaDefaultModules,
73		android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
74	).RunTestWithBp(t, `
75		android_test {
76			name: "TestModule1",
77			sdk_version: "current",
78		}
79
80		android_test {
81			name: "TestModule2",
82			sdk_version: "current",
83		}
84
85		android_test {
86			name: "TestModule3",
87			sdk_version: "current",
88		}
89
90		test_suite {
91			name: "my-child-suite",
92			description: "a child test suite",
93			tests: [
94				"TestModule1",
95				"TestModule2",
96			]
97		}
98
99		test_suite {
100			name: "my-all-tests-suite",
101			description: "a parent test suite",
102			tests: [
103				"TestModule1",
104				"TestModule3",
105				"my-child-suite",
106			]
107		}
108	`)
109	manifestPath := ctx.ModuleForTests("my-all-tests-suite", "android_common").Output("out/soong/test_suites/my-all-tests-suite/my-all-tests-suite.json")
110	var actual testSuiteManifest
111	if err := json.Unmarshal([]byte(android.ContentFromFileRuleForTests(t, ctx.TestContext, manifestPath)), &actual); err != nil {
112		t.Errorf("failed to unmarshal manifest: %v", err)
113	}
114	slices.Sort(actual.Files)
115
116	expected := testSuiteManifest{
117		Name: "my-all-tests-suite",
118		Files: []string{
119			"target/testcases/TestModule1/TestModule1.config",
120			"target/testcases/TestModule1/arm64/TestModule1.apk",
121			"target/testcases/TestModule2/TestModule2.config",
122			"target/testcases/TestModule2/arm64/TestModule2.apk",
123			"target/testcases/TestModule3/TestModule3.config",
124			"target/testcases/TestModule3/arm64/TestModule3.apk",
125		},
126	}
127
128	android.AssertDeepEquals(t, "manifests differ", expected, actual)
129}
130
131func TestTestSuitesNotInstalledInTestcases(t *testing.T) {
132	t.Parallel()
133	android.GroupFixturePreparers(
134		java.PrepareForTestWithJavaDefaultModules,
135		android.FixtureRegisterWithContext(RegisterTestSuiteBuildComponents),
136	).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{
137		`"SomeHostTest" is not installed in testcases`,
138	})).RunTestWithBp(t, `
139			java_test_host {
140				name: "SomeHostTest",
141				srcs: ["a.java"],
142			}
143			test_suite {
144				name: "my-suite",
145				description: "a test suite",
146				tests: [
147					"SomeHostTest",
148				]
149			}
150	`)
151}
152