Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame^] | 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory) |
| 27 | } |
| 28 | |
| 29 | var robolectricDefaultLibs = []string{ |
| 30 | "robolectric_android-all-stub", |
| 31 | "Robolectric_all-target", |
| 32 | "mockito-robolectric-prebuilt", |
| 33 | "truth-prebuilt", |
| 34 | } |
| 35 | |
| 36 | type 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 | |
| 46 | type robolectricTest struct { |
| 47 | Library |
| 48 | |
| 49 | robolectricProperties robolectricProperties |
| 50 | |
| 51 | libs []string |
| 52 | } |
| 53 | |
| 54 | func (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 | |
| 66 | func (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 | |
| 74 | func (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. |
| 98 | func 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 | } |