|
| 1 | + |
| 2 | +import mock |
| 3 | +from mock import MagicMock |
| 4 | +import os |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | + |
| 9 | + |
| 10 | +# Import the tested android._ctypes_library_finder module, |
| 11 | +# making sure android._android won't crash us! |
| 12 | +# (since android._android is android-only / not compilable on desktop) |
| 13 | +android_module_folder = os.path.abspath(os.path.join( |
| 14 | + os.path.dirname(__file__), |
| 15 | + "..", "pythonforandroid", "recipes", "android", "src" |
| 16 | +)) |
| 17 | +sys.path.insert(0, android_module_folder) |
| 18 | +sys.modules['android._android'] = MagicMock() |
| 19 | +import android._ctypes_library_finder |
| 20 | +sys.path.remove(android_module_folder) |
| 21 | + |
| 22 | + |
| 23 | +@mock.patch.dict('sys.modules', jnius=MagicMock()) |
| 24 | +def test_get_activity_lib_dir(): |
| 25 | + import jnius # should get us our fake module |
| 26 | + |
| 27 | + # Short test that it works when activity doesn't exist: |
| 28 | + jnius.autoclass = MagicMock() |
| 29 | + jnius.autoclass.return_value = None |
| 30 | + assert android._ctypes_library_finder.get_activity_lib_dir( |
| 31 | + "JavaClass" |
| 32 | + ) is None |
| 33 | + assert mock.call("JavaClass") in jnius.autoclass.call_args_list |
| 34 | + |
| 35 | + # Comprehensive test that verifies getApplicationInfo() call: |
| 36 | + activity = MagicMock() |
| 37 | + app_context = activity.getApplicationContext() |
| 38 | + app_context.getPackageName.return_value = "test.package" |
| 39 | + app_info = app_context.getPackageManager().getApplicationInfo() |
| 40 | + app_info.nativeLibraryDir = '/testpath' |
| 41 | + |
| 42 | + def pick_class(name): |
| 43 | + cls = MagicMock() |
| 44 | + if name == "JavaClass": |
| 45 | + cls.mActivity = activity |
| 46 | + elif name == "android.content.pm.PackageManager": |
| 47 | + # Manager class: |
| 48 | + cls.GET_SHARED_LIBRARY_FILES = 1024 |
| 49 | + return cls |
| 50 | + |
| 51 | + jnius.autoclass = MagicMock(side_effect=pick_class) |
| 52 | + assert android._ctypes_library_finder.get_activity_lib_dir( |
| 53 | + "JavaClass" |
| 54 | + ) == "/testpath" |
| 55 | + assert mock.call("JavaClass") in jnius.autoclass.call_args_list |
| 56 | + assert mock.call("test.package", 1024) in ( |
| 57 | + app_context.getPackageManager().getApplicationInfo.call_args_list |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +@mock.patch.dict('sys.modules', jnius=MagicMock()) |
| 62 | +def test_find_library(): |
| 63 | + test_d = tempfile.mkdtemp(prefix="p4a-android-ctypes-test-libdir-") |
| 64 | + try: |
| 65 | + with open(os.path.join(test_d, "mymadeuplib.so.5"), "w"): |
| 66 | + pass |
| 67 | + import jnius # should get us our fake module |
| 68 | + |
| 69 | + # Test with mActivity returned: |
| 70 | + jnius.autoclass = MagicMock() |
| 71 | + jnius.autoclass().mService = None |
| 72 | + app_context = jnius.autoclass().mActivity.getApplicationContext() |
| 73 | + app_info = app_context.getPackageManager().getApplicationInfo() |
| 74 | + app_info.nativeLibraryDir = '/doesnt-exist-testpath' |
| 75 | + assert android._ctypes_library_finder.find_library( |
| 76 | + "mymadeuplib" |
| 77 | + ) is None |
| 78 | + assert mock.call("org.kivy.android.PythonActivity") in ( |
| 79 | + jnius.autoclass.call_args_list |
| 80 | + ) |
| 81 | + app_info.nativeLibraryDir = test_d |
| 82 | + assert os.path.normpath(android._ctypes_library_finder.find_library( |
| 83 | + "mymadeuplib" |
| 84 | + )) == os.path.normpath(os.path.join(test_d, "mymadeuplib.so.5")) |
| 85 | + |
| 86 | + # Test with mService returned: |
| 87 | + jnius.autoclass = MagicMock() |
| 88 | + jnius.autoclass().mActivity = None |
| 89 | + app_context = jnius.autoclass().mService.getApplicationContext() |
| 90 | + app_info = app_context.getPackageManager().getApplicationInfo() |
| 91 | + app_info.nativeLibraryDir = '/doesnt-exist-testpath' |
| 92 | + assert android._ctypes_library_finder.find_library( |
| 93 | + "mymadeuplib" |
| 94 | + ) is None |
| 95 | + app_info.nativeLibraryDir = test_d |
| 96 | + assert os.path.normpath(android._ctypes_library_finder.find_library( |
| 97 | + "mymadeuplib" |
| 98 | + )) == os.path.normpath(os.path.join(test_d, "mymadeuplib.so.5")) |
| 99 | + finally: |
| 100 | + shutil.rmtree(test_d) |
| 101 | + |
| 102 | + |
| 103 | +def test_does_libname_match_filename(): |
| 104 | + assert android._ctypes_library_finder.does_libname_match_filename( |
| 105 | + "mylib", "mylib.so" |
| 106 | + ) |
| 107 | + assert not android._ctypes_library_finder.does_libname_match_filename( |
| 108 | + "mylib", "amylib.so" |
| 109 | + ) |
| 110 | + assert not android._ctypes_library_finder.does_libname_match_filename( |
| 111 | + "mylib", "mylib.txt" |
| 112 | + ) |
| 113 | + assert not android._ctypes_library_finder.does_libname_match_filename( |
| 114 | + "mylib", "mylib" |
| 115 | + ) |
| 116 | + assert android._ctypes_library_finder.does_libname_match_filename( |
| 117 | + "mylib", "libmylib.test.so.1.2.3" |
| 118 | + ) |
| 119 | + assert not android._ctypes_library_finder.does_libname_match_filename( |
| 120 | + "mylib", "libtest.mylib.so" |
| 121 | + ) |
| 122 | + assert android._ctypes_library_finder.does_libname_match_filename( |
| 123 | + "mylib", "mylib.so.5" |
| 124 | + ) |
0 commit comments