blob: 1de56a5cdc66487a1812a335b929c9b3c79f7103 [file] [log] [blame]
Colin Cross0ef08162019-05-01 15:50:51 -07001// Copyright 2019 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 java
16
17import (
18 "fmt"
19 "io"
Colin Cross9a642dc2019-05-30 11:17:23 -070020 "strconv"
Colin Cross0ef08162019-05-01 15:50:51 -070021 "strings"
22
23 "android/soong/android"
24)
25
26func init() {
27 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
28}
29
30var robolectricDefaultLibs = []string{
31 "robolectric_android-all-stub",
32 "Robolectric_all-target",
33 "mockito-robolectric-prebuilt",
34 "truth-prebuilt",
35}
36
37type robolectricProperties struct {
38 // The name of the android_app module that the tests will run against.
39 Instrumentation_for *string
40
41 Test_options struct {
42 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070043 Timeout *int64
Colin Cross9a642dc2019-05-30 11:17:23 -070044
45 // Number of shards to use when running the tests.
46 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070047 }
48}
49
50type robolectricTest struct {
51 Library
52
53 robolectricProperties robolectricProperties
54
Colin Cross9a642dc2019-05-30 11:17:23 -070055 libs []string
56 tests []string
Colin Cross0ef08162019-05-01 15:50:51 -070057}
58
59func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
60 r.Library.DepsMutator(ctx)
61
62 if r.robolectricProperties.Instrumentation_for != nil {
63 ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
64 } else {
65 ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
66 }
67
68 ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
69}
70
71func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
72 r.Library.GenerateAndroidBuildActions(ctx)
73
74 for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
75 r.libs = append(r.libs, ctx.OtherModuleName(dep))
76 }
Colin Cross9a642dc2019-05-30 11:17:23 -070077
78 // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
79 // annotated as a test and run it.
80 for _, src := range r.compiledJavaSrcs {
81 s := src.Rel()
82 if !strings.HasSuffix(s, "Test.java") {
83 continue
84 } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
85 continue
86 } else if strings.HasPrefix(s, "src/") {
87 s = strings.TrimPrefix(s, "src/")
88 }
89 r.tests = append(r.tests, s)
90 }
91}
92
93func shardTests(paths []string, shards int) [][]string {
94 if shards > len(paths) {
95 shards = len(paths)
96 }
97 if shards == 0 {
98 return nil
99 }
100 ret := make([][]string, 0, shards)
101 shardSize := (len(paths) + shards - 1) / shards
102 for len(paths) > shardSize {
103 ret = append(ret, paths[0:shardSize])
104 paths = paths[shardSize:]
105 }
106 if len(paths) > 0 {
107 ret = append(ret, paths)
108 }
109 return ret
Colin Cross0ef08162019-05-01 15:50:51 -0700110}
111
112func (r *robolectricTest) AndroidMk() android.AndroidMkData {
113 data := r.Library.AndroidMk()
114
115 data.Custom = func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
116 android.WriteAndroidMkData(w, data)
117
Colin Cross9a642dc2019-05-30 11:17:23 -0700118 if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
119 shards := shardTests(r.tests, int(*s))
120 for i, shard := range shards {
121 r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
122 }
123
124 // TODO: add rules to dist the outputs of the individual tests, or combine them together?
125 fmt.Fprintln(w, "")
126 fmt.Fprintln(w, ".PHONY:", "Run"+name)
127 fmt.Fprintln(w, "Run"+name, ": \\")
128 for i := range shards {
129 fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
130 }
131 fmt.Fprintln(w, "")
132 } else {
133 r.writeTestRunner(w, name, "Run"+name, r.tests)
Colin Cross0ef08162019-05-01 15:50:51 -0700134 }
Colin Cross0ef08162019-05-01 15:50:51 -0700135 }
136
137 return data
138}
139
Colin Cross9a642dc2019-05-30 11:17:23 -0700140func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
141 fmt.Fprintln(w, "")
142 fmt.Fprintln(w, "include $(CLEAR_VARS)")
143 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
144 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module)
145 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
146 fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
147 fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " "))
148 if t := r.robolectricProperties.Test_options.Timeout; t != nil {
149 fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
150 }
151 fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
152
153}
154
Colin Cross0ef08162019-05-01 15:50:51 -0700155// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
156// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
157// used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
Colin Cross9a642dc2019-05-30 11:17:23 -0700158//
159// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
160// it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
161// name, or match the package name when the prefix "src/" is removed.
Colin Cross0ef08162019-05-01 15:50:51 -0700162func RobolectricTestFactory() android.Module {
163 module := &robolectricTest{}
164
165 module.AddProperties(
166 &module.Module.properties,
167 &module.Module.protoProperties,
168 &module.robolectricProperties)
169
170 module.Module.dexpreopter.isTest = true
171
172 InitJavaModule(module, android.DeviceSupported)
173 return module
174}