construct_context_test.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2020 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Unit tests for construct_context.py."""
  18. import sys
  19. import unittest
  20. import construct_context as cc
  21. sys.dont_write_bytecode = True
  22. CONTEXT_JSON = {
  23. '28': [
  24. {
  25. 'Name': 'z',
  26. 'Optional': False,
  27. 'Host': 'out/zdir/z.jar',
  28. 'Device': '/system/z.jar',
  29. 'Subcontexts': [],
  30. },
  31. ],
  32. '29': [
  33. {
  34. 'Name': 'x',
  35. 'Optional': False,
  36. 'Host': 'out/xdir/x.jar',
  37. 'Device': '/system/x.jar',
  38. 'Subcontexts': [],
  39. },
  40. {
  41. 'Name': 'y',
  42. 'Optional': False,
  43. 'Host': 'out/ydir/y.jar',
  44. 'Device': '/product/y.jar',
  45. 'Subcontexts': [],
  46. },
  47. ],
  48. 'any': [
  49. {
  50. 'Name': 'a',
  51. 'Optional': False,
  52. 'Host': 'out/adir/a.jar',
  53. 'Device': '/system/a.jar',
  54. 'Subcontexts': [
  55. { # Not installed optional, being the only child.
  56. 'Name': 'a1',
  57. 'Optional': True,
  58. 'Host': 'out/a1dir/a1.jar',
  59. 'Device': '/product/a1.jar',
  60. 'Subcontexts': [],
  61. },
  62. ],
  63. },
  64. {
  65. 'Name': 'b',
  66. 'Optional': True,
  67. 'Host': 'out/bdir/b.jar',
  68. 'Device': '/product/b.jar',
  69. 'Subcontexts': [
  70. { # Not installed but required.
  71. 'Name': 'b1',
  72. 'Optional': False,
  73. 'Host': 'out/b1dir/b1.jar',
  74. 'Device': '/product/b1.jar',
  75. 'Subcontexts': [],
  76. },
  77. { # Installed optional.
  78. 'Name': 'b2',
  79. 'Optional': True,
  80. 'Host': 'out/b2dir/b2.jar',
  81. 'Device': '/product/b2.jar',
  82. 'Subcontexts': [],
  83. },
  84. { # Not installed optional.
  85. 'Name': 'b3',
  86. 'Optional': True,
  87. 'Host': 'out/b3dir/b3.jar',
  88. 'Device': '/product/b3.jar',
  89. 'Subcontexts': [],
  90. },
  91. { # Installed optional with one more level of nested deps.
  92. 'Name': 'b4',
  93. 'Optional': True,
  94. 'Host': 'out/b4dir/b4.jar',
  95. 'Device': '/product/b4.jar',
  96. 'Subcontexts': [
  97. {
  98. 'Name': 'b41',
  99. 'Optional': True,
  100. 'Host': 'out/b41dir/b41.jar',
  101. 'Device': '/product/b41.jar',
  102. 'Subcontexts': [],
  103. },
  104. {
  105. 'Name': 'b42',
  106. 'Optional': True,
  107. 'Host': 'out/b42dir/b42.jar',
  108. 'Device': '/product/b42.jar',
  109. 'Subcontexts': [],
  110. },
  111. ],
  112. },
  113. ],
  114. },
  115. { # Not installed optional, at the top-level.
  116. 'Name': 'c',
  117. 'Optional': True,
  118. 'Host': 'out/cdir/c.jar',
  119. 'Device': '/product/c.jar',
  120. 'Subcontexts': [],
  121. },
  122. ],
  123. }
  124. PRODUCT_PACKAGES = ['a', 'b', 'b2', 'b4', 'b41', 'b42', 'x', 'y', 'z']
  125. def construct_context_args(target_sdk):
  126. return cc.construct_context_args(target_sdk, CONTEXT_JSON, PRODUCT_PACKAGES)
  127. class ConstructContextTest(unittest.TestCase):
  128. def test_construct_context_27(self):
  129. actual = construct_context_args('27')
  130. # The order matters.
  131. expected = (
  132. 'class_loader_context_arg='
  133. '--class-loader-context=PCL[]{'
  134. 'PCL[out/xdir/x.jar]#'
  135. 'PCL[out/ydir/y.jar]#'
  136. 'PCL[out/zdir/z.jar]#'
  137. 'PCL[out/adir/a.jar]#'
  138. 'PCL[out/bdir/b.jar]{'
  139. 'PCL[out/b1dir/b1.jar]#'
  140. 'PCL[out/b2dir/b2.jar]#'
  141. 'PCL[out/b4dir/b4.jar]{'
  142. 'PCL[out/b41dir/b41.jar]#'
  143. 'PCL[out/b42dir/b42.jar]'
  144. '}'
  145. '}'
  146. '}'
  147. ' ; '
  148. 'stored_class_loader_context_arg='
  149. '--stored-class-loader-context=PCL[]{'
  150. 'PCL[/system/x.jar]#'
  151. 'PCL[/product/y.jar]#'
  152. 'PCL[/system/z.jar]#'
  153. 'PCL[/system/a.jar]#'
  154. 'PCL[/product/b.jar]{'
  155. 'PCL[/product/b1.jar]#'
  156. 'PCL[/product/b2.jar]#'
  157. 'PCL[/product/b4.jar]{'
  158. 'PCL[/product/b41.jar]#'
  159. 'PCL[/product/b42.jar]'
  160. '}'
  161. '}'
  162. '}')
  163. self.assertEqual(actual, expected)
  164. def test_construct_context_28(self):
  165. actual = construct_context_args('28')
  166. expected = (
  167. 'class_loader_context_arg='
  168. '--class-loader-context=PCL[]{'
  169. 'PCL[out/xdir/x.jar]#'
  170. 'PCL[out/ydir/y.jar]#'
  171. 'PCL[out/adir/a.jar]#'
  172. 'PCL[out/bdir/b.jar]{'
  173. 'PCL[out/b1dir/b1.jar]#'
  174. 'PCL[out/b2dir/b2.jar]#'
  175. 'PCL[out/b4dir/b4.jar]{'
  176. 'PCL[out/b41dir/b41.jar]#'
  177. 'PCL[out/b42dir/b42.jar]'
  178. '}'
  179. '}'
  180. '}'
  181. ' ; '
  182. 'stored_class_loader_context_arg='
  183. '--stored-class-loader-context=PCL[]{'
  184. 'PCL[/system/x.jar]#'
  185. 'PCL[/product/y.jar]#'
  186. 'PCL[/system/a.jar]#'
  187. 'PCL[/product/b.jar]{'
  188. 'PCL[/product/b1.jar]#'
  189. 'PCL[/product/b2.jar]#'
  190. 'PCL[/product/b4.jar]{'
  191. 'PCL[/product/b41.jar]#'
  192. 'PCL[/product/b42.jar]'
  193. '}'
  194. '}'
  195. '}')
  196. self.assertEqual(actual, expected)
  197. def test_construct_context_29(self):
  198. actual = construct_context_args('29')
  199. expected = (
  200. 'class_loader_context_arg='
  201. '--class-loader-context=PCL[]{'
  202. 'PCL[out/adir/a.jar]#'
  203. 'PCL[out/bdir/b.jar]{'
  204. 'PCL[out/b1dir/b1.jar]#'
  205. 'PCL[out/b2dir/b2.jar]#'
  206. 'PCL[out/b4dir/b4.jar]{'
  207. 'PCL[out/b41dir/b41.jar]#'
  208. 'PCL[out/b42dir/b42.jar]'
  209. '}'
  210. '}'
  211. '}'
  212. ' ; '
  213. 'stored_class_loader_context_arg='
  214. '--stored-class-loader-context=PCL[]{'
  215. 'PCL[/system/a.jar]#'
  216. 'PCL[/product/b.jar]{'
  217. 'PCL[/product/b1.jar]#'
  218. 'PCL[/product/b2.jar]#'
  219. 'PCL[/product/b4.jar]{'
  220. 'PCL[/product/b41.jar]#'
  221. 'PCL[/product/b42.jar]'
  222. '}'
  223. '}'
  224. '}')
  225. self.assertEqual(actual, expected)
  226. def test_construct_context_S(self):
  227. actual = construct_context_args('S')
  228. expected = (
  229. 'class_loader_context_arg='
  230. '--class-loader-context=PCL[]{'
  231. 'PCL[out/adir/a.jar]#'
  232. 'PCL[out/bdir/b.jar]{'
  233. 'PCL[out/b1dir/b1.jar]#'
  234. 'PCL[out/b2dir/b2.jar]#'
  235. 'PCL[out/b4dir/b4.jar]{'
  236. 'PCL[out/b41dir/b41.jar]#'
  237. 'PCL[out/b42dir/b42.jar]'
  238. '}'
  239. '}'
  240. '}'
  241. ' ; '
  242. 'stored_class_loader_context_arg='
  243. '--stored-class-loader-context=PCL[]{'
  244. 'PCL[/system/a.jar]#'
  245. 'PCL[/product/b.jar]{'
  246. 'PCL[/product/b1.jar]#'
  247. 'PCL[/product/b2.jar]#'
  248. 'PCL[/product/b4.jar]{'
  249. 'PCL[/product/b41.jar]#'
  250. 'PCL[/product/b42.jar]'
  251. '}'
  252. '}'
  253. '}')
  254. self.assertEqual(actual, expected)
  255. if __name__ == '__main__':
  256. unittest.main(verbosity=2)