singleton_module_test.go 3.5 KB

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