aboutsummaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorColin Cross2019-05-02 11:37:33 -0500
committerGerrit Code Review2019-05-02 11:37:33 -0500
commit5346d0710826d56bb66c84e39723c9085a8ae337 (patch)
tree5728e7edbf9c1c08ad5e49ac1e15c63c0a449075 /java
parente7d52c779f8fbeda72f2f1f1dcb5555dc3921fec (diff)
parent0ef081672725e5cfe2b9ae7f644d4be0bab9f5e3 (diff)
downloadplatform-build-soong-5346d0710826d56bb66c84e39723c9085a8ae337.tar.gz
platform-build-soong-5346d0710826d56bb66c84e39723c9085a8ae337.tar.xz
platform-build-soong-5346d0710826d56bb66c84e39723c9085a8ae337.zip
Merge "Support robolectric_test"
Diffstat (limited to 'java')
-rw-r--r--java/robolectric.go110
1 files changed, 110 insertions, 0 deletions
diff --git a/java/robolectric.go b/java/robolectric.go
new file mode 100644
index 00000000..26f1e9d1
--- /dev/null
+++ b/java/robolectric.go
@@ -0,0 +1,110 @@
1// 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"
20 "strings"
21
22 "android/soong/android"
23)
24
25func init() {
26 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
27}
28
29var robolectricDefaultLibs = []string{
30 "robolectric_android-all-stub",
31 "Robolectric_all-target",
32 "mockito-robolectric-prebuilt",
33 "truth-prebuilt",
34}
35
36type robolectricProperties struct {
37 // The name of the android_app module that the tests will run against.
38 Instrumentation_for *string
39
40 Test_options struct {
41 // Timeout in seconds when running the tests.
42 Timeout *string
43 }
44}
45
46type robolectricTest struct {
47 Library
48
49 robolectricProperties robolectricProperties
50
51 libs []string
52}
53
54func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
55 r.Library.DepsMutator(ctx)
56
57 if r.robolectricProperties.Instrumentation_for != nil {
58 ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
59 } else {
60 ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
61 }
62
63 ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
64}
65
66func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
67 r.Library.GenerateAndroidBuildActions(ctx)
68
69 for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
70 r.libs = append(r.libs, ctx.OtherModuleName(dep))
71 }
72}
73
74func (r *robolectricTest) AndroidMk() android.AndroidMkData {
75 data := r.Library.AndroidMk()
76
77 data.Custom = func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
78 android.WriteAndroidMkData(w, data)
79
80 fmt.Fprintln(w, "")
81 fmt.Fprintln(w, "include $(CLEAR_VARS)")
82 fmt.Fprintln(w, "LOCAL_MODULE := Run"+name)
83 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", name)
84 fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
85 fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
86 if t := r.robolectricProperties.Test_options.Timeout; t != nil {
87 fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
88 }
89 fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
90 }
91
92 return data
93}
94
95// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
96// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
97// used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
98func RobolectricTestFactory() android.Module {
99 module := &robolectricTest{}
100
101 module.AddProperties(
102 &module.Module.properties,
103 &module.Module.protoProperties,
104 &module.robolectricProperties)
105
106 module.Module.dexpreopter.isTest = true
107
108 InitJavaModule(module, android.DeviceSupported)
109 return module
110}