1#!/usr/bin/env python3
2#
3# Copyright 2023, 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"""A collection of integration test cases for atest."""
18
19import os
20from typing import Any, Callable
21import atest_integration_test
22
23# Note: The following constants should ideally be imported from their
24#       corresponding prod source code, but this makes local execution of the
25#       integration test harder due to some special dependencies in the prod
26#       code. Therefore we copy the definition here for now in favor of easier
27#       local integration test execution. If value changes in the source code
28#       breaking the integration test becomes a problem in the future, we can
29#       reconsider importing these constants.
30# Log prefix for dry-run run command. Defined in atest/atest_main.py
31_DRY_RUN_COMMAND_LOG_PREFIX = 'Internal run command from dry-run: '
32
33
34class CommandVerificationTests(atest_integration_test.AtestTestCase):
35  """Checks atest tradefed commands."""
36
37  @atest_integration_test.run_in_parallel
38  def test_animator_test(self):
39    """Verify that the test's command runs correctly."""
40    atest_cmd = 'AnimatorTest'
41    expected_cmd = (
42        'atest_tradefed.sh template/atest_device_test_base --template:map'
43        ' test=atest --template:map log_saver=template/log/atest_log_saver'
44        ' --no-enable-granular-attempts --module CtsAnimationTestCases'
45        ' --atest-include-filter'
46        ' CtsAnimationTestCases:android.animation.cts.AnimatorTest'
47        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
48        ' VERBOSE --no-early-device-release --test-arg'
49        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
50        ' --enable-parameterized-modules --exclude-module-parameters'
51        ' secondary_user --exclude-module-parameters instant_app'
52        ' --exclude-module-parameters multi_abi'
53    )
54    self._verify_atest_internal_runner_command(
55        atest_cmd,
56        self._assert_equivalent_cmds,
57        expected_cmd=expected_cmd,
58    )
59
60  @atest_integration_test.run_in_parallel
61  def test_cts_animation_test_cases_animator_test(self):
62    """Verify that the test's command runs correctly."""
63    atest_cmd = 'CtsAnimationTestCases:AnimatorTest'
64    expected_cmd = (
65        'atest_tradefed.sh template/atest_device_test_base --template:map'
66        ' test=atest --template:map log_saver=template/log/atest_log_saver'
67        ' --no-enable-granular-attempts --module CtsAnimationTestCases'
68        ' --atest-include-filter'
69        ' CtsAnimationTestCases:android.animation.cts.AnimatorTest'
70        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
71        ' VERBOSE --no-early-device-release --test-arg'
72        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
73        ' --enable-parameterized-modules --exclude-module-parameters multi_abi'
74        ' --exclude-module-parameters instant_app --exclude-module-parameters'
75        ' secondary_user'
76    )
77    self._verify_atest_internal_runner_command(
78        atest_cmd,
79        self._assert_equivalent_cmds,
80        expected_cmd=expected_cmd,
81    )
82
83  @atest_integration_test.run_in_parallel
84  def test_cts_sample_device_cases_shared_prefs_test(self):
85    """Verify that the test's command runs correctly."""
86    atest_cmd = (
87        'CtsSampleDeviceTestCases:SampleDeviceTest#testSharedPreferences'
88    )
89    expected_cmd = (
90        'atest_tradefed.sh template/atest_device_test_base --template:map'
91        ' test=atest'
92        ' --template:map log_saver=template/log/atest_log_saver'
93        ' --no-enable-granular-attempts --include-filter'
94        ' CtsSampleDeviceTestCases --atest-include-filter'
95        ' CtsSampleDeviceTestCases:android.sample.cts.SampleDeviceTest#testSharedPreferences'
96        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
97        ' VERBOSE --no-early-device-release --test-arg'
98        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
99    )
100    self._verify_atest_internal_runner_command(
101        atest_cmd,
102        self._assert_equivalent_cmds,
103        expected_cmd=expected_cmd,
104    )
105
106  @atest_integration_test.run_in_parallel
107  def test_cts_sample_device_cases_android_sample_test(self):
108    """Verify that the test's command runs correctly."""
109    atest_cmd = 'CtsSampleDeviceTestCases:android.sample.cts'
110    expected_cmd = (
111        'atest_tradefed.sh template/atest_device_test_base --template:map'
112        ' test=atest'
113        ' --template:map log_saver=template/log/atest_log_saver'
114        ' --no-enable-granular-attempts --include-filter'
115        ' CtsSampleDeviceTestCases --atest-include-filter'
116        ' CtsSampleDeviceTestCases:android.sample.cts --skip-loading-config-jar'
117        ' --log-level-display VERBOSE --log-level VERBOSE'
118        ' --no-early-device-release --test-arg'
119        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
120    )
121    self._verify_atest_internal_runner_command(
122        atest_cmd,
123        self._assert_equivalent_cmds,
124        expected_cmd=expected_cmd,
125    )
126
127  @atest_integration_test.run_in_parallel
128  def test_cts_sample_device_cases_device_report_log_test(self):
129    """Verify that the test's command runs correctly."""
130    atest_cmd = (
131        'CtsSampleDeviceTestCases:android.sample.cts.SampleDeviceReportLogTest'
132    )
133    expected_cmd = (
134        'atest_tradefed.sh template/atest_device_test_base --template:map'
135        ' test=atest'
136        ' --template:map log_saver=template/log/atest_log_saver'
137        ' --no-enable-granular-attempts --include-filter'
138        ' CtsSampleDeviceTestCases --atest-include-filter'
139        ' CtsSampleDeviceTestCases:android.sample.cts.SampleDeviceReportLogTest'
140        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
141        ' VERBOSE --no-early-device-release --test-arg'
142        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
143    )
144    self._verify_atest_internal_runner_command(
145        atest_cmd,
146        self._assert_equivalent_cmds,
147        expected_cmd=expected_cmd,
148    )
149
150  @atest_integration_test.run_in_parallel
151  def test_cts_animation_cases_sample_device_cases_test(self):
152    """Verify that the test's command runs correctly."""
153    atest_cmd = 'CtsAnimationTestCases CtsSampleDeviceTestCases'
154    expected_cmd = (
155        'atest_tradefed.sh template/atest_device_test_base --template:map'
156        ' test=atest'
157        ' --template:map log_saver=template/log/atest_log_saver'
158        ' --no-enable-granular-attempts --include-filter CtsAnimationTestCases'
159        ' --include-filter CtsSampleDeviceTestCases --skip-loading-config-jar'
160        ' --log-level-display VERBOSE --log-level VERBOSE'
161        ' --no-early-device-release --test-arg'
162        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
163    )
164    self._verify_atest_internal_runner_command(
165        atest_cmd,
166        self._assert_equivalent_cmds,
167        expected_cmd=expected_cmd,
168    )
169
170  @atest_integration_test.run_in_parallel
171  def test_hello_world_tests_test(self):
172    """Verify that the test's command runs correctly."""
173    atest_cmd = 'HelloWorldTests'
174    expected_cmd = (
175        'atest_tradefed.sh template/atest_device_test_base --template:map'
176        ' test=atest --template:map log_saver=template/log/atest_log_saver'
177        ' --no-enable-granular-attempts --include-filter HelloWorldTests'
178        ' --include-filter hallo-welt --skip-loading-config-jar'
179        ' --log-level-display VERBOSE --log-level VERBOSE'
180        ' --no-early-device-release'
181    )
182    self._verify_atest_internal_runner_command(
183        atest_cmd,
184        self._assert_equivalent_cmds,
185        expected_cmd=expected_cmd,
186    )
187
188  @atest_integration_test.run_in_parallel
189  def test_perinstance_camerahidl_config_injection_test(self):
190    """Verify that the test's command runs correctly."""
191    atest_cmd = (
192        'PerInstance/CameraHidlTest#'
193        'configureInjectionStreamsAvailableOutputs/0_internal_0'
194    )
195    expected_cmd = (
196        'atest_tradefed.sh template/atest_device_test_base --template:map'
197        ' test=atest --template:map log_saver=template/log/atest_log_saver'
198        ' --no-enable-granular-attempts --include-filter'
199        ' VtsHalCameraProviderV2_4TargetTest --atest-include-filter'
200        ' VtsHalCameraProviderV2_4TargetTest:PerInstance/CameraHidlTest.configureInjectionStreamsAvailableOutputs/0_internal_0'
201        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
202        ' VERBOSE --no-early-device-release'
203    )
204    self._verify_atest_internal_runner_command(
205        atest_cmd,
206        self._assert_equivalent_cmds,
207        expected_cmd=expected_cmd,
208    )
209
210  @atest_integration_test.run_in_parallel
211  def test_vts_hal_camera_provider_config_injection_test(self):
212    """Verify that the test's command runs correctly."""
213    atest_cmd = (
214        'VtsHalCameraProviderV2_4TargetTest:PerInstance/'
215        'CameraHidlTest#configureInjectionStreamsAvailableOutputs/'
216        '0_internal_0'
217    )
218    expected_cmd = (
219        'atest_tradefed.sh template/atest_device_test_base --template:map'
220        ' test=atest --template:map log_saver=template/log/atest_log_saver'
221        ' --no-enable-granular-attempts --include-filter'
222        ' VtsHalCameraProviderV2_4TargetTest --atest-include-filter'
223        ' VtsHalCameraProviderV2_4TargetTest:PerInstance/CameraHidlTest.configureInjectionStreamsAvailableOutputs/0_internal_0'
224        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
225        ' VERBOSE --no-early-device-release'
226    )
227    self._verify_atest_internal_runner_command(
228        atest_cmd,
229        self._assert_equivalent_cmds,
230        expected_cmd=expected_cmd,
231    )
232
233  @atest_integration_test.run_in_parallel
234  def test_android_animation_cts_test(self):
235    """Verify that the test's command runs correctly."""
236    atest_cmd = 'android.animation.cts'
237    expected_cmd = (
238        'atest_tradefed.sh template/atest_device_test_base --template:map'
239        ' test=atest --template:map log_saver=template/log/atest_log_saver'
240        ' --no-enable-granular-attempts --module CtsAnimationTestCases'
241        ' --atest-include-filter CtsAnimationTestCases:android.animation.cts'
242        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
243        ' VERBOSE --no-early-device-release --test-arg'
244        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
245        ' --enable-parameterized-modules --exclude-module-parameters multi_abi'
246        ' --exclude-module-parameters instant_app --exclude-module-parameters'
247        ' secondary_user'
248    )
249    self._verify_atest_internal_runner_command(
250        atest_cmd,
251        self._assert_equivalent_cmds,
252        expected_cmd=expected_cmd,
253    )
254
255  @atest_integration_test.run_in_parallel
256  def test_android_sample_cts_device_report_log_test(self):
257    """Verify that the test's command runs correctly."""
258    atest_cmd = 'android.sample.cts.SampleDeviceReportLogTest'
259    expected_cmd = (
260        'atest_tradefed.sh template/atest_device_test_base --template:map'
261        ' test=atest'
262        ' --template:map log_saver=template/log/atest_log_saver'
263        ' --no-enable-granular-attempts --include-filter'
264        ' CtsSampleDeviceTestCases --atest-include-filter'
265        ' CtsSampleDeviceTestCases:android.sample.cts.SampleDeviceReportLogTest'
266        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
267        ' VERBOSE --no-early-device-release --test-arg'
268        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
269    )
270    self._verify_atest_internal_runner_command(
271        atest_cmd,
272        self._assert_equivalent_cmds,
273        expected_cmd=expected_cmd,
274    )
275
276  @atest_integration_test.run_in_parallel
277  def test_android_sample_cts_shared_prefs_test(self):
278    """Verify that the test's command runs correctly."""
279    atest_cmd = 'android.sample.cts.SampleDeviceTest#testSharedPreferences'
280    expected_cmd = (
281        'atest_tradefed.sh template/atest_device_test_base --template:map'
282        ' test=atest'
283        ' --template:map log_saver=template/log/atest_log_saver'
284        ' --no-enable-granular-attempts --include-filter'
285        ' CtsSampleDeviceTestCases --atest-include-filter'
286        ' CtsSampleDeviceTestCases:android.sample.cts.SampleDeviceTest#testSharedPreferences'
287        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
288        ' VERBOSE --no-early-device-release --test-arg'
289        ' com.android.tradefed.testtype.AndroidJUnitTest:exclude-annotation:android.platform.test.annotations.AppModeInstant'
290    )
291    self._verify_atest_internal_runner_command(
292        atest_cmd,
293        self._assert_equivalent_cmds,
294        expected_cmd=expected_cmd,
295    )
296
297  @atest_integration_test.run_in_parallel
298  def test_hello_world_test(self):
299    """Verify that the test's command runs correctly."""
300    atest_cmd = 'hello_world_test'
301    expected_cmd = (
302        'atest_tradefed.sh template/atest_device_test_base --template:map'
303        ' test=atest --template:map log_saver=template/log/atest_log_saver'
304        ' --no-enable-granular-attempts --include-filter hello_world_test'
305        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
306        ' VERBOSE --no-early-device-release'
307    )
308    self._verify_atest_internal_runner_command(
309        atest_cmd,
310        self._assert_equivalent_cmds,
311        expected_cmd=expected_cmd,
312    )
313
314  @atest_integration_test.run_in_parallel
315  def test_native_benchmark_test(self):
316    """Verify that the test's command runs correctly."""
317    atest_cmd = 'native-benchmark'
318    expected_cmd = (
319        'atest_tradefed.sh template/atest_device_test_base --template:map'
320        ' test=atest --template:map log_saver=template/log/atest_log_saver'
321        ' --no-enable-granular-attempts --include-filter native-benchmark'
322        ' --log-level-display VERBOSE --log-level VERBOSE'
323        ' --no-early-device-release'
324    )
325    self._verify_atest_internal_runner_command(
326        atest_cmd,
327        self._assert_equivalent_cmds,
328        expected_cmd=expected_cmd,
329    )
330
331  @atest_integration_test.run_in_parallel
332  def test_platform_native_example_test(self):
333    """Verify that the test's command runs correctly."""
334    atest_cmd = 'platform_testing/tests/example/native'
335    expected_cmd = (
336        'atest_tradefed.sh template/atest_device_test_base --template:map'
337        ' test=atest --template:map log_saver=template/log/atest_log_saver'
338        ' --no-enable-granular-attempts --include-filter hello_world_test'
339        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
340        ' VERBOSE --no-early-device-release'
341    )
342    self._verify_atest_internal_runner_command(
343        atest_cmd,
344        self._assert_equivalent_cmds,
345        expected_cmd=expected_cmd,
346    )
347
348  @atest_integration_test.run_in_parallel
349  def test_platform_android_example_test(self):
350    """Verify that the test's command runs correctly."""
351    atest_cmd = 'platform_testing/tests/example/native/Android.bp'
352    expected_cmd = (
353        'atest_tradefed.sh template/atest_device_test_base --template:map'
354        ' test=atest --template:map log_saver=template/log/atest_log_saver'
355        ' --no-enable-granular-attempts --include-filter hello_world_test'
356        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
357        ' VERBOSE --no-early-device-release'
358    )
359    self._verify_atest_internal_runner_command(
360        atest_cmd,
361        self._assert_equivalent_cmds,
362        expected_cmd=expected_cmd,
363    )
364
365  @atest_integration_test.run_in_parallel
366  def test_tf_core_config_native_benchmark_test(self):
367    """Verify that the test's command runs correctly."""
368    atest_cmd = 'tools/tradefederation/core/res/config/native-benchmark.xml'
369    expected_cmd = (
370        'atest_tradefed.sh template/atest_device_test_base --template:map'
371        ' test=atest --template:map log_saver=template/log/atest_log_saver'
372        ' --no-enable-granular-attempts --include-filter native-benchmark'
373        ' --log-level-display VERBOSE --log-level VERBOSE'
374        ' --no-early-device-release'
375    )
376    self._verify_atest_internal_runner_command(
377        atest_cmd,
378        self._assert_equivalent_cmds,
379        expected_cmd=expected_cmd,
380    )
381
382  @atest_integration_test.run_in_parallel
383  def test_quick_access_wallet_robo_test(self):
384    """Verify that the test's command runs correctly."""
385    test_cmd = 'QuickAccessWalletRoboTests'
386    expected_cmd = (
387        'atest_tradefed.sh template/atest_device_test_base --template:map'
388        ' test=atest --template:map log_saver=template/log/atest_log_saver'
389        ' --no-enable-granular-attempts --include-filter'
390        ' QuickAccessWalletRoboTests --skip-loading-config-jar'
391        ' --log-level-display VERBOSE --log-level VERBOSE'
392        ' --no-early-device-release'
393    )
394    self._verify_atest_internal_runner_command(
395        test_cmd,
396        self._assert_equivalent_cmds,
397        expected_cmd=expected_cmd,
398    )
399
400  @atest_integration_test.run_in_parallel
401  def test_quick_access_wallet_robo_host_test(self):
402    """Verify that the test's command runs correctly."""
403    atest_cmd = 'QuickAccessWalletRoboTests --host'
404    expected_cmd = (
405        'atest_tradefed.sh template/atest_deviceless_test_base --template:map'
406        ' test=atest --template:map log_saver=template/log/atest_log_saver'
407        ' --no-enable-granular-attempts --include-filter'
408        ' QuickAccessWalletRoboTests --skip-loading-config-jar'
409        ' --log-level-display VERBOSE --log-level VERBOSE'
410        ' --no-early-device-release -n --prioritize-host-config'
411        ' --skip-host-arch-check'
412    )
413    self._verify_atest_internal_runner_command(
414        atest_cmd,
415        self._assert_equivalent_cmds,
416        expected_cmd=expected_cmd,
417    )
418
419  @atest_integration_test.run_in_parallel
420  def test_cts_wifi_aware_cases_test(self):
421    """Verify that the test's command runs correctly."""
422    atest_cmd = 'CtsWifiAwareTestCases'
423    expected_cmd = (
424        'atest_tradefed.sh template/atest_device_test_base --template:map'
425        ' test=atest --template:map log_saver=template/log/atest_log_saver'
426        ' --no-enable-granular-attempts --include-filter CtsWifiAwareTestCases'
427        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
428        ' VERBOSE --no-early-device-release --replicate-parent-setup'
429        ' --multi-device-count 2'
430    )
431    self._verify_atest_internal_runner_command(
432        atest_cmd,
433        self._assert_equivalent_cmds,
434        expected_cmd=expected_cmd,
435    )
436
437  @atest_integration_test.run_in_parallel
438  def test_pts_bot_test(self):
439    """Verify that the test's command runs correctly."""
440    atest_cmd = 'pts-bot:PAN/GN/MISC/UUID/BV-01-C'
441    expected_cmd = (
442        'atest_tradefed.sh template/atest_device_test_base --template:map'
443        ' test=atest --template:map log_saver=template/log/atest_log_saver'
444        ' --no-enable-granular-attempts --include-filter pts-bot'
445        ' --atest-include-filter pts-bot:PAN/GN/MISC/UUID/BV-01-C'
446        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
447        ' VERBOSE --no-early-device-release'
448    )
449    self._verify_atest_internal_runner_command(
450        atest_cmd,
451        self._assert_equivalent_cmds,
452        expected_cmd=expected_cmd,
453    )
454
455  @atest_integration_test.run_in_parallel
456  def test_tee_ui_utils_test(self):
457    """Verify that the test's command runs correctly."""
458    atest_cmd = 'TeeUIUtilsTest'
459    expected_cmd = (
460        'atest_tradefed.sh template/atest_device_test_base --template:map'
461        ' test=atest --template:map log_saver=template/log/atest_log_saver'
462        ' --no-enable-granular-attempts --include-filter teeui_unit_tests'
463        ' --atest-include-filter teeui_unit_tests:TeeUIUtilsTest.*'
464        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
465        ' VERBOSE --no-early-device-release'
466    )
467    self._verify_atest_internal_runner_command(
468        atest_cmd,
469        self._assert_equivalent_cmds,
470        expected_cmd=expected_cmd,
471    )
472
473  @atest_integration_test.run_in_parallel
474  def test_tee_ui_utils_intersect_convext_obj_test(self):
475    """Verify that the test's command runs correctly."""
476    atest_cmd = (
477        'TeeUIUtilsTest#intersectTest,ConvexObjectConstruction,'
478        'ConvexObjectLineIntersection'
479    )
480    expected_cmd = (
481        'atest_tradefed.sh template/atest_device_test_base --template:map'
482        ' test=atest --template:map log_saver=template/log/atest_log_saver'
483        ' --no-enable-granular-attempts --include-filter teeui_unit_tests'
484        ' --atest-include-filter'
485        ' teeui_unit_tests:TeeUIUtilsTest.ConvexObjectConstruction:TeeUIUtilsTest.ConvexObjectLineIntersection:TeeUIUtilsTest.intersectTest'
486        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
487        ' VERBOSE --no-early-device-release'
488    )
489    self._verify_atest_internal_runner_command(
490        atest_cmd,
491        self._assert_equivalent_cmds,
492        expected_cmd=expected_cmd,
493    )
494
495  @atest_integration_test.run_in_parallel
496  def test_cts_activity_mgr_register_ui_change_test(self):
497    """Verify that the test's command runs correctly."""
498    atest_cmd = (
499        'CtsSecurityTestCases:android.security.cts.'
500        'ActivityManagerTest#testActivityManager_'
501        'registerUidChangeObserver_allPermission'
502    )
503    expected_cmd = (
504        'atest_tradefed.sh template/atest_device_test_base --template:map'
505        ' test=atest --template:map log_saver=template/log/atest_log_saver'
506        ' --no-enable-granular-attempts --module CtsSecurityTestCases'
507        ' --atest-include-filter'
508        ' CtsSecurityTestCases:android.security.cts.ActivityManagerTest#testActivityManager_registerUidChangeObserver_allPermission'
509        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
510        ' VERBOSE --no-early-device-release --enable-parameterized-modules'
511        ' --exclude-module-parameters instant_app --exclude-module-parameters'
512        ' secondary_user --exclude-module-parameters multi_abi'
513    )
514    self._verify_atest_internal_runner_command(
515        atest_cmd,
516        self._assert_equivalent_cmds,
517        expected_cmd=expected_cmd,
518    )
519
520  @atest_integration_test.run_in_parallel
521  def test_cts_activity_mgr_register_ui_change_java_test(self):
522    """Verify that the test's command runs correctly."""
523    atest_cmd = (
524        'cts/tests/tests/security/src/android/security/cts/'
525        'ActivityManagerTest.java#testActivityManager_'
526        'registerUidChangeObserver_allPermission'
527    )
528    expected_cmd = (
529        'atest_tradefed.sh template/atest_device_test_base --template:map'
530        ' test=atest --template:map log_saver=template/log/atest_log_saver'
531        ' --no-enable-granular-attempts --module CtsSecurityTestCases'
532        ' --atest-include-filter'
533        ' CtsSecurityTestCases:android.security.cts.ActivityManagerTest#testActivityManager_registerUidChangeObserver_allPermission'
534        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
535        ' VERBOSE --no-early-device-release --enable-parameterized-modules'
536        ' --exclude-module-parameters instant_app --exclude-module-parameters'
537        ' secondary_user --exclude-module-parameters multi_abi'
538    )
539    self._verify_atest_internal_runner_command(
540        atest_cmd,
541        self._assert_equivalent_cmds,
542        expected_cmd=expected_cmd,
543    )
544
545  @atest_integration_test.run_in_parallel
546  def test_permission_memory_footprint_apps_size_kt_test(self):
547    """Verify that the test's command runs correctly."""
548    atest_cmd = (
549        'cts/tests/tests/security/src/android/security/cts/'
550        'PermissionMemoryFootprintTest.kt#'
551        'checkAppsCantIncreasePermissionSizeAfterCreating'
552    )
553    expected_cmd = (
554        'atest_tradefed.sh template/atest_device_test_base --template:map'
555        ' test=atest --template:map log_saver=template/log/atest_log_saver'
556        ' --no-enable-granular-attempts --module CtsSecurityTestCases'
557        ' --atest-include-filter'
558        ' CtsSecurityTestCases:android.security.cts.PermissionMemoryFootprintTest#checkAppsCantIncreasePermissionSizeAfterCreating'
559        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
560        ' VERBOSE --no-early-device-release --enable-parameterized-modules'
561        ' --exclude-module-parameters instant_app --exclude-module-parameters'
562        ' multi_abi --exclude-module-parameters secondary_user'
563    )
564    self._verify_atest_internal_runner_command(
565        atest_cmd,
566        self._assert_equivalent_cmds,
567        expected_cmd=expected_cmd,
568    )
569
570  @atest_integration_test.run_in_parallel
571  def test_permission_memory_footprint_test(self):
572    """Verify that the test's command runs correctly."""
573    atest_cmd = 'android.security.cts.PermissionMemoryFootprintTest'
574    expected_cmd = (
575        'atest_tradefed.sh template/atest_device_test_base --template:map'
576        ' test=atest --template:map log_saver=template/log/atest_log_saver'
577        ' --no-enable-granular-attempts --module CtsSecurityTestCases'
578        ' --atest-include-filter'
579        ' CtsSecurityTestCases:android.security.cts.PermissionMemoryFootprintTest'
580        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
581        ' VERBOSE --no-early-device-release --enable-parameterized-modules'
582        ' --exclude-module-parameters multi_abi --exclude-module-parameters'
583        ' instant_app --exclude-module-parameters secondary_user'
584    )
585    self._verify_atest_internal_runner_command(
586        atest_cmd,
587        self._assert_equivalent_cmds,
588        expected_cmd=expected_cmd,
589    )
590
591  @atest_integration_test.run_in_parallel
592  def test_permission_memory_footprint_apps_size_test(self):
593    """Verify that the test's command runs correctly."""
594    atest_cmd = (
595        'android.security.cts.PermissionMemoryFootprintTest#'
596        'checkAppsCantIncreasePermissionSizeAfterCreating'
597    )
598    expected_cmd = (
599        'atest_tradefed.sh template/atest_device_test_base --template:map'
600        ' test=atest --template:map log_saver=template/log/atest_log_saver'
601        ' --no-enable-granular-attempts --module CtsSecurityTestCases'
602        ' --atest-include-filter'
603        ' CtsSecurityTestCases:android.security.cts.PermissionMemoryFootprintTest#checkAppsCantIncreasePermissionSizeAfterCreating'
604        ' --skip-loading-config-jar --log-level-display VERBOSE --log-level'
605        ' VERBOSE --no-early-device-release --enable-parameterized-modules'
606        ' --exclude-module-parameters secondary_user'
607        ' --exclude-module-parameters multi_abi --exclude-module-parameters'
608        ' instant_app'
609    )
610    self._verify_atest_internal_runner_command(
611        atest_cmd,
612        self._assert_equivalent_cmds,
613        expected_cmd=expected_cmd,
614    )
615
616  @classmethod
617  @atest_integration_test.setup_parallel_in_build_env
618  def setUpClassForParallelInBuild(cls):
619    """Setup method for parallel run in build env.
620
621    This setup executes an initial run of atest on a simple method,
622    disregarding the results. This is needed so that atest will build any
623    dependencies required before the parallel run starts. Without this
624    setup, on certain build environment the parallel run may trigger
625    parallel soong build which will result in lock timeout.
626    """
627    cls.run_atest_command(
628        'hello_world_test --dry-run -cit',
629        atest_integration_test.StepInput(
630            os.environ, os.environ['ANDROID_BUILD_TOP'], cls.get_config(), {}
631        ),
632        include_device_serial=False,
633        print_output=False,
634    ).check_returncode()
635
636  def _sanitize_runner_command(self, cmd: str) -> str:
637    """Sanitize an atest runner command by removing non-essential args."""
638    remove_args_starting_with = [
639        '--skip-all-system-status-check',
640        '--atest-log-file-path',
641        'LD_LIBRARY_PATH=',
642        '--proto-output-file=',
643        '--log-root-path',
644    ]
645    remove_args_with_values = ['-s', '--serial']
646    build_command = 'build/soong/soong_ui.bash'
647    original_args = cmd.split()
648    result_args = []
649    for arg in original_args:
650      if arg == build_command:
651        result_args.append(f'./{build_command}')
652        continue
653      if not any(
654          (arg.startswith(prefix) for prefix in remove_args_starting_with)
655      ):
656        result_args.append(arg)
657    for arg in remove_args_with_values:
658      while arg in result_args:
659        idx = result_args.index(arg)
660        # Delete value index first.
661        del result_args[idx + 1]
662        del result_args[idx]
663
664    return ' '.join(result_args)
665
666  def _assert_equivalent_cmds(
667      self,
668      atest_cmd: str,
669      actual_cmd: str,
670      expected_cmd: str,
671  ) -> None:
672    """Assert that the expected command is equivalent to the actual command.
673
674    Non-essential arguments such as log directory and serial will be ignored.
675
676    Args:
677        atest_cmd: The atest command string that is being tested.
678        actual_cmd: The actual atest internal runner command string.
679        expected_cmd: The expected atest internal runner command string.
680
681    Returns:
682    """
683    actual_cmd = self._sanitize_runner_command(actual_cmd)
684    expected_cmd = self._sanitize_runner_command(expected_cmd)
685
686    self.assertEqual(
687        set(actual_cmd.split()),
688        set(expected_cmd.split()),
689        'Unexpected atest internal runner command generated for the'
690        ' atest command `%s`.\nActual:\n`%s`\nExpected:\n`%s`'
691        % (atest_cmd, actual_cmd, expected_cmd),
692    )
693
694  def _verify_atest_internal_runner_command(
695      self,
696      atest_cmd: str,
697      assertion_func: Callable[str, None],
698      **assertion_func_params: dict[str, Any],
699  ) -> None:
700    """Verifies atest's runner command using the provided assertion function.
701
702    Args:
703        atest_cmd: The atest command to execute. Note: Do not add the atest
704          binary to the beginning of the command.
705        assertion_func: A function that takes a test command string and an atest
706          internal command string and runs assertions on it.
707        **assertion_func_params: Parameters for the assertion function.
708    """
709    script = self.create_atest_script()
710
711    def build_step(
712        step_in: atest_integration_test.StepInput,
713    ) -> atest_integration_test.StepOutput:
714      result = self.run_atest_command(
715          atest_cmd + ' --dry-run -cit', step_in, include_device_serial=False
716      )
717      result.check_returncode()
718      runner_cmd = result.get_atest_log_values_from_prefix(
719          _DRY_RUN_COMMAND_LOG_PREFIX
720      )[0]
721
722      step_out = self.create_step_output()
723      step_out.set_snapshot_include_paths([])
724      step_out.add_snapshot_obj('runner_cmd', runner_cmd)
725      return step_out
726
727    def test_step(step_in: atest_integration_test.StepInput) -> None:
728      runner_cmd = step_in.get_obj('runner_cmd')
729      assertion_func(atest_cmd, runner_cmd, **assertion_func_params)
730
731    script.add_build_step(build_step)
732    script.add_test_step(test_step)
733    script.run()
734
735
736if __name__ == '__main__':
737  atest_integration_test.main()
738