1#!/usr/bin/env python3
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
18import os
19import subprocess
20import unittest
21import time
22
23def run_cmd(cmd, ignore_error=False):
24    print("Running command:", cmd)
25    p = subprocess.Popen(cmd, shell=True)
26    p.communicate()
27    if not ignore_error and p.returncode:
28        raise subprocess.CalledProcessError(p.returncode, cmd)
29    return p.returncode
30
31class TestHidlJava(unittest.TestCase):
32    pass
33
34def cleanup(cmd):
35    binary = cmd.split()[0]
36    run_cmd("adb shell killall %s >/dev/null 2>&1" % binary, ignore_error=True)
37
38def make_test(client, server):
39    def test(self):
40        try:
41            env = "CLASSPATH=/data/framework/hidl_test_java_java.jar"
42
43            cleanup(client)
44            cleanup(server)
45            run_cmd("adb shell \"( %s %s -s ) </dev/null >/dev/null 2>&1 &\"" % (env, server))
46            time.sleep(2)
47            run_cmd("adb shell %s %s -c" % (env, client))
48        finally:
49            cleanup(client)
50            cleanup(server)
51    return test
52
53def has_bitness(bitness):
54    return 0 == run_cmd("echo '[[ \"$(getprop ro.product.cpu.abilist%s)\" != \"\" ]]' | adb shell sh" % bitness, ignore_error=True)
55
56if __name__ == '__main__':
57    cmds = ["app_process /data/framework com.android.commands.hidl_test_java.HidlTestJava"]
58
59    if has_bitness(32):
60        cmds += ["/data/nativetest/hidl_test_java_native/hidl_test_java_native"]
61
62    if has_bitness(64):
63        cmds += ["/data/nativetest64/hidl_test_java_native/hidl_test_java_native"]
64
65    assert len(cmds) >= 2
66
67    def short_name(cmd):
68        if "app" in cmd:
69            return "java"
70        if "64" in cmd:
71            return "64"
72        return "32"
73
74    for client in cmds:
75        for server in cmds:
76            test_name = 'test_%s_to_%s' % (short_name(client), short_name(server))
77            test = make_test(client, server)
78            setattr(TestHidlJava, test_name, test)
79
80    suite = unittest.TestLoader().loadTestsFromTestCase(TestHidlJava)
81    unittest.TextTestRunner(verbosity=2).run(suite)
82