dexpreopt_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2018 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 dexpreopt
  15. import (
  16. "android/soong/android"
  17. "fmt"
  18. "testing"
  19. )
  20. func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
  21. return testModuleConfig(ctx, name, "system")
  22. }
  23. func testSystemProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
  24. return testModuleConfig(ctx, name, "system/product")
  25. }
  26. func testProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
  27. return testModuleConfig(ctx, name, "product")
  28. }
  29. func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleConfig {
  30. return &ModuleConfig{
  31. Name: name,
  32. DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name),
  33. BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)),
  34. DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)),
  35. UncompressedDex: false,
  36. HasApkLibraries: false,
  37. PreoptFlags: nil,
  38. ProfileClassListing: android.OptionalPath{},
  39. ProfileIsTextListing: false,
  40. EnforceUsesLibraries: false,
  41. OptionalUsesLibraries: nil,
  42. UsesLibraries: nil,
  43. LibraryPaths: nil,
  44. Archs: []android.ArchType{android.Arm},
  45. DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
  46. DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
  47. DexPreoptImageLocations: []string{},
  48. PreoptBootClassPathDexFiles: nil,
  49. PreoptBootClassPathDexLocations: nil,
  50. PreoptExtractedApk: false,
  51. NoCreateAppImage: false,
  52. ForceCreateAppImage: false,
  53. PresignedPrebuilt: false,
  54. }
  55. }
  56. func TestDexPreopt(t *testing.T) {
  57. config := android.TestConfig("out", nil, "", nil)
  58. ctx := android.PathContextForTesting(config)
  59. globalSoong := GlobalSoongConfigForTests(config)
  60. global := GlobalConfigForTests(ctx)
  61. module := testSystemModuleConfig(ctx, "test")
  62. rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. wantInstalls := android.RuleBuilderInstalls{
  67. {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
  68. {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
  69. }
  70. if rule.Installs().String() != wantInstalls.String() {
  71. t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
  72. }
  73. }
  74. func TestDexPreoptSystemOther(t *testing.T) {
  75. config := android.TestConfig("out", nil, "", nil)
  76. ctx := android.PathContextForTesting(config)
  77. globalSoong := GlobalSoongConfigForTests(config)
  78. global := GlobalConfigForTests(ctx)
  79. systemModule := testSystemModuleConfig(ctx, "Stest")
  80. systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
  81. productModule := testProductModuleConfig(ctx, "Ptest")
  82. global.HasSystemOther = true
  83. type moduleTest struct {
  84. module *ModuleConfig
  85. expectedPartition string
  86. }
  87. tests := []struct {
  88. patterns []string
  89. moduleTests []moduleTest
  90. }{
  91. {
  92. patterns: []string{"app/%"},
  93. moduleTests: []moduleTest{
  94. {module: systemModule, expectedPartition: "system_other/system"},
  95. {module: systemProductModule, expectedPartition: "system/product"},
  96. {module: productModule, expectedPartition: "product"},
  97. },
  98. },
  99. // product/app/% only applies to product apps inside the system partition
  100. {
  101. patterns: []string{"app/%", "product/app/%"},
  102. moduleTests: []moduleTest{
  103. {module: systemModule, expectedPartition: "system_other/system"},
  104. {module: systemProductModule, expectedPartition: "system_other/system/product"},
  105. {module: productModule, expectedPartition: "product"},
  106. },
  107. },
  108. }
  109. for _, test := range tests {
  110. global.PatternsOnSystemOther = test.patterns
  111. for _, mt := range test.moduleTests {
  112. rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. name := mt.module.Name
  117. wantInstalls := android.RuleBuilderInstalls{
  118. {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
  119. {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
  120. }
  121. if rule.Installs().String() != wantInstalls.String() {
  122. t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
  123. }
  124. }
  125. }
  126. }
  127. func TestDexPreoptProfile(t *testing.T) {
  128. config := android.TestConfig("out", nil, "", nil)
  129. ctx := android.PathContextForTesting(config)
  130. globalSoong := GlobalSoongConfigForTests(config)
  131. global := GlobalConfigForTests(ctx)
  132. module := testSystemModuleConfig(ctx, "test")
  133. module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
  134. rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. wantInstalls := android.RuleBuilderInstalls{
  139. {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
  140. {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
  141. {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
  142. {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
  143. }
  144. if rule.Installs().String() != wantInstalls.String() {
  145. t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
  146. }
  147. }