blob: 3b8c6b2138aaa33d07cb85666411cbf6f322dcd9 [file] [log] [blame]
Colin Cross9aed5bc2020-12-28 15:15:34 -08001// 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
15package android
16
17import (
Colin Cross9aed5bc2020-12-28 15:15:34 -080018 "testing"
19)
20
21type testSingletonModule struct {
22 SingletonModuleBase
23 ops []string
24}
25
26func (tsm *testSingletonModule) GenerateAndroidBuildActions(ctx ModuleContext) {
27 tsm.ops = append(tsm.ops, "GenerateAndroidBuildActions")
28}
29
30func (tsm *testSingletonModule) GenerateSingletonBuildActions(ctx SingletonContext) {
31 tsm.ops = append(tsm.ops, "GenerateSingletonBuildActions")
32}
33
34func (tsm *testSingletonModule) MakeVars(ctx MakeVarsContext) {
35 tsm.ops = append(tsm.ops, "MakeVars")
36}
37
38func testSingletonModuleFactory() SingletonModule {
39 tsm := &testSingletonModule{}
40 InitAndroidSingletonModule(tsm)
41 return tsm
42}
43
Paul Duffind6597002021-03-16 23:36:24 +000044var prepareForSingletonModuleTest = GroupFixturePreparers(
Colin Cross9aed5bc2020-12-28 15:15:34 -080045 // Enable Kati output to test SingletonModules with MakeVars.
Paul Duffind6597002021-03-16 23:36:24 +000046 PrepareForTestWithAndroidMk,
47 FixtureRegisterWithContext(func(ctx RegistrationContext) {
48 ctx.RegisterSingletonModuleType("test_singleton_module", testSingletonModuleFactory)
Paul Duffind6597002021-03-16 23:36:24 +000049 }),
Martin Stjernholm1ebef5b2022-02-10 23:34:28 +000050 PrepareForTestWithMakevars,
Paul Duffind6597002021-03-16 23:36:24 +000051)
Colin Cross9aed5bc2020-12-28 15:15:34 -080052
53func TestSingletonModule(t *testing.T) {
54 bp := `
55 test_singleton_module {
56 name: "test_singleton_module",
57 }
58 `
Paul Duffin30ac3e72021-03-20 00:36:14 +000059 result := GroupFixturePreparers(
60 prepareForSingletonModuleTest,
61 FixtureWithRootAndroidBp(bp),
62 ).RunTest(t)
Colin Cross9aed5bc2020-12-28 15:15:34 -080063
Paul Duffind6597002021-03-16 23:36:24 +000064 ops := result.ModuleForTests("test_singleton_module", "").Module().(*testSingletonModule).ops
Colin Cross9aed5bc2020-12-28 15:15:34 -080065 wantOps := []string{"GenerateAndroidBuildActions", "GenerateSingletonBuildActions", "MakeVars"}
Paul Duffind6597002021-03-16 23:36:24 +000066 AssertDeepEquals(t, "operations", wantOps, ops)
Colin Cross9aed5bc2020-12-28 15:15:34 -080067}
68
69func 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 Duffind6597002021-03-16 23:36:24 +000079
Paul Duffin30ac3e72021-03-20 00:36:14 +000080 prepareForSingletonModuleTest.
Paul Duffind6597002021-03-16 23:36:24 +000081 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{
82 `\QDuplicate SingletonModule "test_singleton_module", previously used in\E`,
Paul Duffin30ac3e72021-03-20 00:36:14 +000083 })).RunTestWithBp(t, bp)
Colin Cross9aed5bc2020-12-28 15:15:34 -080084}
85
86func TestUnusedSingletonModule(t *testing.T) {
Paul Duffin30ac3e72021-03-20 00:36:14 +000087 result := GroupFixturePreparers(
Paul Duffind6597002021-03-16 23:36:24 +000088 prepareForSingletonModuleTest,
Paul Duffin30ac3e72021-03-20 00:36:14 +000089 ).RunTest(t)
Colin Cross9aed5bc2020-12-28 15:15:34 -080090
Paul Duffind6597002021-03-16 23:36:24 +000091 singleton := result.SingletonForTests("test_singleton_module").Singleton()
Colin Cross9aed5bc2020-12-28 15:15:34 -080092 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
Colin Cross9aed5bc2020-12-28 15:15:34 -080099func TestVariantSingletonModule(t *testing.T) {
Colin Cross74dc5982024-01-17 15:00:46 -0800100 if testing.Short() {
101 t.Skip("test fails with data race enabled")
102 }
Colin Cross9aed5bc2020-12-28 15:15:34 -0800103 bp := `
104 test_singleton_module {
105 name: "test_singleton_module",
106 }
107 `
108
Paul Duffin30ac3e72021-03-20 00:36:14 +0000109 GroupFixturePreparers(
110 prepareForSingletonModuleTest,
111 FixtureRegisterWithContext(func(ctx RegistrationContext) {
112 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
Colin Crossd27205e2024-09-12 22:41:37 -0700113 ctx.Transition("test_singleton_module_mutator", &testTransitionMutator{
114 split: func(ctx BaseModuleContext) []string {
115 return []string{"a", "b"}
116 },
117 })
Paul Duffin30ac3e72021-03-20 00:36:14 +0000118 })
119 }),
120 ).
Paul Duffind6597002021-03-16 23:36:24 +0000121 ExtendWithErrorHandler(FixtureExpectsAllErrorsToMatchAPattern([]string{
122 `\QGenerateAndroidBuildActions already called for variant\E`,
123 })).
Paul Duffin30ac3e72021-03-20 00:36:14 +0000124 RunTestWithBp(t, bp)
Colin Cross9aed5bc2020-12-28 15:15:34 -0800125}