Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 1 | // Copyright 2021 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 android |
| 16 | |
| 17 | import ( |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 18 | "testing" |
| 19 | ) |
| 20 | |
| 21 | type testSingletonModule struct { |
| 22 | SingletonModuleBase |
| 23 | ops []string |
| 24 | } |
| 25 | |
| 26 | func (tsm *testSingletonModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 27 | tsm.ops = append(tsm.ops, "GenerateAndroidBuildActions") |
| 28 | } |
| 29 | |
| 30 | func (tsm *testSingletonModule) GenerateSingletonBuildActions(ctx SingletonContext) { |
| 31 | tsm.ops = append(tsm.ops, "GenerateSingletonBuildActions") |
| 32 | } |
| 33 | |
| 34 | func (tsm *testSingletonModule) MakeVars(ctx MakeVarsContext) { |
| 35 | tsm.ops = append(tsm.ops, "MakeVars") |
| 36 | } |
| 37 | |
| 38 | func testSingletonModuleFactory() SingletonModule { |
| 39 | tsm := &testSingletonModule{} |
| 40 | InitAndroidSingletonModule(tsm) |
| 41 | return tsm |
| 42 | } |
| 43 | |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 44 | var prepareForSingletonModuleTest = GroupFixturePreparers( |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 45 | // Enable Kati output to test SingletonModules with MakeVars. |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 46 | PrepareForTestWithAndroidMk, |
| 47 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 48 | ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory) |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 49 | }), |
Martin Stjernholm | 1ebef5b | 2022-02-10 23:34:28 +0000 | [diff] [blame] | 50 | PrepareForTestWithMakevars, |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 51 | ) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 52 | |
| 53 | func TestSingletonModule(t *testing.T) { |
| 54 | bp := ` |
| 55 | test_singleton_module { |
| 56 | name: "test_singleton_module", |
| 57 | } |
| 58 | ` |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 59 | result := GroupFixturePreparers( |
| 60 | prepareForSingletonModuleTest, |
| 61 | FixtureWithRootAndroidBp(bp), |
| 62 | ).RunTest(t) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 63 | |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 64 | ops := result.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 65 | wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"} |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 66 | AssertDeepEquals(t, "operations", wantOps, ops) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func TestDuplicateSingletonModule(t *testing.T) { |
| 70 | bp := ` |
| 71 | test_singleton_module { |
| 72 | name: "test_singleton_module", |
| 73 | } |
| 74 | |
| 75 | test_singleton_module { |
| 76 | name: "test_singleton_module2", |
| 77 | } |
| 78 | ` |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 79 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 80 | prepareForSingletonModuleTest. |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 81 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{ |
| 82 | `\QDuplicate SingletonModule "test_singleton_module", previously used in\E`, |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 83 | })).RunTestWithBp(t, bp) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func TestUnusedSingletonModule(t *testing.T) { |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 87 | result := GroupFixturePreparers( |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 88 | prepareForSingletonModuleTest, |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 89 | ).RunTest(t) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 90 | |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 91 | singleton := result.SingletonForTests("test_singleton_module").Singleton() |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 92 | sm := singleton.(*singletonModuleSingletonAdaptor).sm |
| 93 | ops := sm.(*testSingletonModule).ops |
| 94 | if ops != nil { |
| 95 | t.Errorf("Expected no operations, got %q", ops) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func testVariantSingletonModuleMutator(ctx BottomUpMutatorContext) { |
| 100 | if _, ok := ctx.Module().(*testSingletonModule); ok { |
| 101 | ctx.CreateVariations("a", "b") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestVariantSingletonModule(t *testing.T) { |
| 106 | bp := ` |
| 107 | test_singleton_module { |
| 108 | name: "test_singleton_module", |
| 109 | } |
| 110 | ` |
| 111 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 112 | GroupFixturePreparers( |
| 113 | prepareForSingletonModuleTest, |
| 114 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 115 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 116 | ctx.BottomUp("test_singleton_module_mutator", testVariantSingletonModuleMutator) |
| 117 | }) |
| 118 | }), |
| 119 | ). |
Paul Duffin | d659700 | 2021-03-16 23:36:24 +0000 | [diff] [blame] | 120 | ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{ |
| 121 | `\QGenerateAndroidBuildActions already called for variant\E`, |
| 122 | })). |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 123 | RunTestWithBp(t, bp) |
Colin Cross | 9aed5bc | 2020-12-28 15:15:34 -0800 | [diff] [blame] | 124 | } |