blob: 34a0e16db2f0f24f9354e5abf26f6a9ef279c182 [file] [log] [blame]
Ulya Trafimovich5f364b62020-06-30 12:39:01 +01001#!/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
19import sys
20import unittest
21
22import construct_context as cc
23
24sys.dont_write_bytecode = True
25
Spandan Dasec555f12021-08-25 18:42:40 +000026
Jiakai Zhanga4496782023-05-17 16:57:30 +010027CONTEXT_JSON = {
28 '28': [
29 {
30 'Name': 'z',
31 'Optional': False,
32 'Host': 'out/zdir/z.jar',
33 'Device': '/system/z.jar',
34 'Subcontexts': [],
35 },
36 ],
37 '29': [
38 {
39 'Name': 'x',
40 'Optional': False,
41 'Host': 'out/xdir/x.jar',
42 'Device': '/system/x.jar',
43 'Subcontexts': [],
44 },
45 {
46 'Name': 'y',
47 'Optional': False,
48 'Host': 'out/ydir/y.jar',
49 'Device': '/product/y.jar',
50 'Subcontexts': [],
51 },
52 ],
53 'any': [
54 {
55 'Name': 'a',
56 'Optional': False,
57 'Host': 'out/adir/a.jar',
58 'Device': '/system/a.jar',
59 'Subcontexts': [
60 { # Not installed optional, being the only child.
61 'Name': 'a1',
62 'Optional': True,
63 'Host': 'out/a1dir/a1.jar',
64 'Device': '/product/a1.jar',
65 'Subcontexts': [],
66 },
67 ],
68 },
69 {
70 'Name': 'b',
71 'Optional': True,
72 'Host': 'out/bdir/b.jar',
73 'Device': '/product/b.jar',
74 'Subcontexts': [
75 { # Not installed but required.
76 'Name': 'b1',
77 'Optional': False,
78 'Host': 'out/b1dir/b1.jar',
79 'Device': '/product/b1.jar',
80 'Subcontexts': [],
81 },
82 { # Installed optional.
83 'Name': 'b2',
84 'Optional': True,
85 'Host': 'out/b2dir/b2.jar',
86 'Device': '/product/b2.jar',
87 'Subcontexts': [],
88 },
89 { # Not installed optional.
90 'Name': 'b3',
91 'Optional': True,
92 'Host': 'out/b3dir/b3.jar',
93 'Device': '/product/b3.jar',
94 'Subcontexts': [],
95 },
96 { # Installed optional with one more level of nested deps.
97 'Name': 'b4',
98 'Optional': True,
99 'Host': 'out/b4dir/b4.jar',
100 'Device': '/product/b4.jar',
101 'Subcontexts': [
102 {
103 'Name': 'b41',
104 'Optional': True,
105 'Host': 'out/b41dir/b41.jar',
106 'Device': '/product/b41.jar',
107 'Subcontexts': [],
108 },
109 {
110 'Name': 'b42',
111 'Optional': True,
112 'Host': 'out/b42dir/b42.jar',
113 'Device': '/product/b42.jar',
114 'Subcontexts': [],
115 },
116 ],
117 },
118 ],
119 },
120 { # Not installed optional, at the top-level.
121 'Name': 'c',
122 'Optional': True,
123 'Host': 'out/cdir/c.jar',
124 'Device': '/product/c.jar',
125 'Subcontexts': [],
126 },
127 ],
128}
Spandan Dasec555f12021-08-25 18:42:40 +0000129
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100130
Jiakai Zhanga4496782023-05-17 16:57:30 +0100131PRODUCT_PACKAGES = ['a', 'b', 'b2', 'b4', 'b41', 'b42', 'x', 'y', 'z']
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100132
Jiakai Zhanga4496782023-05-17 16:57:30 +0100133
134def construct_context_args(target_sdk):
135 return cc.construct_context_args(target_sdk, CONTEXT_JSON, PRODUCT_PACKAGES)
136
137
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100138class ConstructContextTest(unittest.TestCase):
Jiakai Zhanga4496782023-05-17 16:57:30 +0100139 def test_construct_context_27(self):
140 actual = construct_context_args('27')
141 # The order matters.
142 expected = (
143 'class_loader_context_arg='
144 '--class-loader-context=PCL[]{'
145 'PCL[out/xdir/x.jar]#'
146 'PCL[out/ydir/y.jar]#'
147 'PCL[out/zdir/z.jar]#'
148 'PCL[out/adir/a.jar]#'
149 'PCL[out/bdir/b.jar]{'
150 'PCL[out/b1dir/b1.jar]#'
151 'PCL[out/b2dir/b2.jar]#'
152 'PCL[out/b4dir/b4.jar]{'
153 'PCL[out/b41dir/b41.jar]#'
154 'PCL[out/b42dir/b42.jar]'
155 '}'
156 '}'
157 '}'
158 ' ; '
159 'stored_class_loader_context_arg='
160 '--stored-class-loader-context=PCL[]{'
161 'PCL[/system/x.jar]#'
162 'PCL[/product/y.jar]#'
163 'PCL[/system/z.jar]#'
164 'PCL[/system/a.jar]#'
165 'PCL[/product/b.jar]{'
166 'PCL[/product/b1.jar]#'
167 'PCL[/product/b2.jar]#'
168 'PCL[/product/b4.jar]{'
169 'PCL[/product/b41.jar]#'
170 'PCL[/product/b42.jar]'
171 '}'
172 '}'
173 '}')
174 self.assertEqual(actual, expected)
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100175
Spandan Dasec555f12021-08-25 18:42:40 +0000176 def test_construct_context_28(self):
Jiakai Zhanga4496782023-05-17 16:57:30 +0100177 actual = construct_context_args('28')
178 expected = (
179 'class_loader_context_arg='
180 '--class-loader-context=PCL[]{'
181 'PCL[out/xdir/x.jar]#'
182 'PCL[out/ydir/y.jar]#'
183 'PCL[out/adir/a.jar]#'
184 'PCL[out/bdir/b.jar]{'
185 'PCL[out/b1dir/b1.jar]#'
186 'PCL[out/b2dir/b2.jar]#'
187 'PCL[out/b4dir/b4.jar]{'
188 'PCL[out/b41dir/b41.jar]#'
189 'PCL[out/b42dir/b42.jar]'
190 '}'
191 '}'
192 '}'
Spandan Dasec555f12021-08-25 18:42:40 +0000193 ' ; '
Jiakai Zhanga4496782023-05-17 16:57:30 +0100194 'stored_class_loader_context_arg='
195 '--stored-class-loader-context=PCL[]{'
196 'PCL[/system/x.jar]#'
197 'PCL[/product/y.jar]#'
198 'PCL[/system/a.jar]#'
199 'PCL[/product/b.jar]{'
200 'PCL[/product/b1.jar]#'
201 'PCL[/product/b2.jar]#'
202 'PCL[/product/b4.jar]{'
203 'PCL[/product/b41.jar]#'
204 'PCL[/product/b42.jar]'
205 '}'
206 '}'
207 '}')
208 self.assertEqual(actual, expected)
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100209
Spandan Dasec555f12021-08-25 18:42:40 +0000210 def test_construct_context_29(self):
Jiakai Zhanga4496782023-05-17 16:57:30 +0100211 actual = construct_context_args('29')
212 expected = (
213 'class_loader_context_arg='
214 '--class-loader-context=PCL[]{'
215 'PCL[out/adir/a.jar]#'
216 'PCL[out/bdir/b.jar]{'
217 'PCL[out/b1dir/b1.jar]#'
218 'PCL[out/b2dir/b2.jar]#'
219 'PCL[out/b4dir/b4.jar]{'
220 'PCL[out/b41dir/b41.jar]#'
221 'PCL[out/b42dir/b42.jar]'
222 '}'
223 '}'
224 '}'
Spandan Dasec555f12021-08-25 18:42:40 +0000225 ' ; '
Jiakai Zhanga4496782023-05-17 16:57:30 +0100226 'stored_class_loader_context_arg='
227 '--stored-class-loader-context=PCL[]{'
228 'PCL[/system/a.jar]#'
229 'PCL[/product/b.jar]{'
230 'PCL[/product/b1.jar]#'
231 'PCL[/product/b2.jar]#'
232 'PCL[/product/b4.jar]{'
233 'PCL[/product/b41.jar]#'
234 'PCL[/product/b42.jar]'
235 '}'
236 '}'
237 '}')
238 self.assertEqual(actual, expected)
Spandan Dasec555f12021-08-25 18:42:40 +0000239
240 def test_construct_context_S(self):
Jiakai Zhanga4496782023-05-17 16:57:30 +0100241 actual = construct_context_args('S')
242 expected = (
243 'class_loader_context_arg='
244 '--class-loader-context=PCL[]{'
245 'PCL[out/adir/a.jar]#'
246 'PCL[out/bdir/b.jar]{'
247 'PCL[out/b1dir/b1.jar]#'
248 'PCL[out/b2dir/b2.jar]#'
249 'PCL[out/b4dir/b4.jar]{'
250 'PCL[out/b41dir/b41.jar]#'
251 'PCL[out/b42dir/b42.jar]'
252 '}'
253 '}'
254 '}'
Spandan Dasec555f12021-08-25 18:42:40 +0000255 ' ; '
Jiakai Zhanga4496782023-05-17 16:57:30 +0100256 'stored_class_loader_context_arg='
257 '--stored-class-loader-context=PCL[]{'
258 'PCL[/system/a.jar]#'
259 'PCL[/product/b.jar]{'
260 'PCL[/product/b1.jar]#'
261 'PCL[/product/b2.jar]#'
262 'PCL[/product/b4.jar]{'
263 'PCL[/product/b41.jar]#'
264 'PCL[/product/b42.jar]'
265 '}'
266 '}'
267 '}')
268 self.assertEqual(actual, expected)
269
Ulya Trafimovich5f364b62020-06-30 12:39:01 +0100270
271if __name__ == '__main__':
Spandan Dasec555f12021-08-25 18:42:40 +0000272 unittest.main(verbosity=2)