1# Copyright 2024 The Android Open Source Project
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
15import uuid
16from mobly import base_test
17from mobly import test_runner
18from mobly import utils
19from mobly.asserts import assert_not_in
20from mobly.controllers import android_device
21
22
23class MultiDeviceInstrumentationTest(base_test.BaseTestClass):
24
25  def setup_class(self):
26    self.devices = self.register_controller(android_device)
27
28  def test_in_parallel(self):
29    test_id = str(uuid.uuid1())[:8]
30
31    def run_instrument_cmd(device_idx, device):
32      result = device.adb.shell((
33          f'am instrument -w -e position {device_idx} -e test_id {test_id} '
34          + 'android.test.wifi.nsd/androidx.test.runner.AndroidJUnitRunner'
35      ))
36      assert_not_in('FAIL', result.decode(), f'Failed in device {device_idx}')
37
38    utils.concurrent_exec(
39        run_instrument_cmd,
40        [*enumerate(self.devices)],
41        max_workers=2,
42        raise_on_exception=True,
43    )
44
45
46if __name__ == '__main__':
47  if '--' in sys.argv:
48    index = sys.argv.index('--')
49    sys.argv = sys.argv[:1] + sys.argv[index + 1 :]
50
51  test_runner.main()
52