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 android 16 17import ( 18 "testing" 19) 20 21type defaultsTestProperties struct { 22 Foo []string 23} 24 25type defaultsTestModule struct { 26 ModuleBase 27 DefaultableModuleBase 28 properties defaultsTestProperties 29} 30 31func (d *defaultsTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { 32 ctx.Build(pctx, BuildParams{ 33 Rule: Touch, 34 Output: PathForModuleOut(ctx, "out"), 35 }) 36} 37 38func defaultsTestModuleFactory() Module { 39 module := &defaultsTestModule{} 40 module.AddProperties(&module.properties) 41 InitAndroidModule(module) 42 InitDefaultableModule(module) 43 return module 44} 45 46type defaultsTestDefaults struct { 47 ModuleBase 48 DefaultsModuleBase 49} 50 51func defaultsTestDefaultsFactory() Module { 52 defaults := &defaultsTestDefaults{} 53 defaults.AddProperties(&defaultsTestProperties{}) 54 InitDefaultsModule(defaults) 55 return defaults 56} 57 58var prepareForDefaultsTest = GroupFixturePreparers( 59 PrepareForTestWithDefaults, 60 FixtureRegisterWithContext(func(ctx RegistrationContext) { 61 ctx.RegisterModuleType("test", defaultsTestModuleFactory) 62 ctx.RegisterModuleType("defaults", defaultsTestDefaultsFactory) 63 }), 64) 65 66func TestDefaults(t *testing.T) { 67 bp := ` 68 defaults { 69 name: "transitive", 70 foo: ["transitive"], 71 } 72 73 defaults { 74 name: "defaults", 75 defaults: ["transitive"], 76 foo: ["defaults"], 77 } 78 79 test { 80 name: "foo", 81 defaults: ["defaults"], 82 foo: ["module"], 83 } 84 ` 85 86 result := GroupFixturePreparers( 87 prepareForDefaultsTest, 88 FixtureWithRootAndroidBp(bp), 89 ).RunTest(t) 90 91 foo := result.Module("foo", "").(*defaultsTestModule) 92 93 AssertDeepEquals(t, "foo", []string{"transitive", "defaults", "module"}, foo.properties.Foo) 94} 95 96func TestDefaultsAllowMissingDependencies(t *testing.T) { 97 bp := ` 98 defaults { 99 name: "defaults", 100 defaults: ["missing"], 101 foo: ["defaults"], 102 } 103 104 test { 105 name: "missing_defaults", 106 defaults: ["missing"], 107 foo: ["module"], 108 } 109 110 test { 111 name: "missing_transitive_defaults", 112 defaults: ["defaults"], 113 foo: ["module"], 114 } 115 ` 116 117 result := GroupFixturePreparers( 118 prepareForDefaultsTest, 119 PrepareForTestWithAllowMissingDependencies, 120 FixtureWithRootAndroidBp(bp), 121 ).RunTest(t) 122 123 missingDefaults := result.ModuleForTests("missing_defaults", "").Output("out") 124 missingTransitiveDefaults := result.ModuleForTests("missing_transitive_defaults", "").Output("out") 125 126 AssertSame(t, "missing_defaults rule", ErrorRule, missingDefaults.Rule) 127 128 AssertStringEquals(t, "missing_defaults", "module missing_defaults missing dependencies: missing\n", missingDefaults.Args["error"]) 129 130 // TODO: missing transitive defaults is currently not handled 131 _ = missingTransitiveDefaults 132} 133