2020-04-10 18:40:16 +02:00
|
|
|
import os
|
|
|
|
import tempfile
|
2020-05-28 18:36:52 +02:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-04-10 18:40:16 +02:00
|
|
|
from buildozer.targets.android import TargetAndroid
|
2020-06-24 10:36:21 +02:00
|
|
|
from tests.targets.utils import (
|
|
|
|
init_buildozer,
|
|
|
|
patch_buildozer,
|
|
|
|
patch_buildozer_checkbin,
|
2020-06-24 20:26:12 +02:00
|
|
|
patch_buildozer_cmd,
|
|
|
|
patch_buildozer_file_exists,
|
2020-06-24 10:36:21 +02:00
|
|
|
)
|
2020-04-10 18:40:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def patch_buildozer_cmd_expect():
|
|
|
|
return patch_buildozer("cmd_expect")
|
|
|
|
|
|
|
|
|
|
|
|
def patch_buildozer_download():
|
|
|
|
return patch_buildozer("download")
|
|
|
|
|
|
|
|
|
|
|
|
def patch_buildozer_file_extract():
|
|
|
|
return patch_buildozer("file_extract")
|
|
|
|
|
|
|
|
|
|
|
|
def patch_os_isfile():
|
|
|
|
return mock.patch("os.path.isfile")
|
|
|
|
|
|
|
|
|
2020-04-13 19:12:38 +02:00
|
|
|
def patch_target_android(method):
|
|
|
|
return mock.patch(
|
|
|
|
"buildozer.targets.android.TargetAndroid.{method}".format(method=method)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-04-24 19:54:55 +02:00
|
|
|
def patch_platform(platform):
|
|
|
|
return mock.patch("buildozer.targets.android.platform", platform)
|
|
|
|
|
|
|
|
|
2020-06-24 10:36:21 +02:00
|
|
|
def init_target(temp_dir, options=None):
|
|
|
|
buildozer = init_buildozer(temp_dir, "android", options)
|
|
|
|
return TargetAndroid(buildozer)
|
2020-05-28 18:36:52 +02:00
|
|
|
|
2020-06-24 10:36:21 +02:00
|
|
|
|
|
|
|
def call_build_package(target_android):
|
|
|
|
"""
|
|
|
|
Call the build_package() method of the tested TargetAndroid instance,
|
|
|
|
patching the functions that would otherwise produce side-effects.
|
|
|
|
|
|
|
|
Return the mocked execute_build_package() method of the TargetAndroid
|
|
|
|
instance so that tests can easily check which command-line arguments
|
|
|
|
would be passed on to python-for-android's toolchain.
|
|
|
|
"""
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
expected_dist_dir = (
|
|
|
|
'{buildozer_dir}/android/platform/build-armeabi-v7a/dists/myapp__armeabi-v7a'.format(
|
|
|
|
buildozer_dir=buildozer.buildozer_dir)
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch_target_android('_update_libraries_references') as m_update_libraries_references, \
|
|
|
|
patch_target_android('_generate_whitelist') as m_generate_whitelist, \
|
|
|
|
mock.patch('buildozer.targets.android.TargetAndroid.execute_build_package') as m_execute_build_package, \
|
|
|
|
mock.patch('buildozer.targets.android.copyfile') as m_copyfile, \
|
|
|
|
mock.patch('buildozer.targets.android.os.listdir') as m_listdir:
|
|
|
|
m_listdir.return_value = ['30.0.0-rc2']
|
|
|
|
target_android.build_package()
|
|
|
|
|
|
|
|
assert m_listdir.call_count == 1
|
|
|
|
assert m_update_libraries_references.call_args_list == [
|
|
|
|
mock.call(expected_dist_dir)
|
|
|
|
]
|
|
|
|
assert m_generate_whitelist.call_args_list == [mock.call(expected_dist_dir)]
|
|
|
|
assert m_copyfile.call_args_list == [
|
|
|
|
mock.call(
|
|
|
|
'{expected_dist_dir}/bin/MyApplication-0.1-debug.apk'.format(
|
|
|
|
expected_dist_dir=expected_dist_dir
|
|
|
|
),
|
|
|
|
'{bin_dir}/myapp-0.1-armeabi-v7a-debug.apk'.format(bin_dir=buildozer.bin_dir),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
return m_execute_build_package
|
|
|
|
|
|
|
|
|
|
|
|
class TestTargetAndroid:
|
2020-04-10 18:40:16 +02:00
|
|
|
|
|
|
|
def setup_method(self):
|
2020-05-28 18:36:52 +02:00
|
|
|
"""
|
|
|
|
Create a temporary directory that will contain the spec file and will
|
|
|
|
serve as the root_dir.
|
|
|
|
"""
|
|
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
2020-04-10 18:40:16 +02:00
|
|
|
|
|
|
|
def tear_method(self):
|
2020-05-28 18:36:52 +02:00
|
|
|
"""
|
|
|
|
Remove the temporary directory created in self.setup_method.
|
|
|
|
"""
|
|
|
|
self.temp_dir.cleanup()
|
|
|
|
|
2020-04-10 18:40:16 +02:00
|
|
|
def test_init(self):
|
|
|
|
"""Tests init defaults."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
assert target_android._arch == "armeabi-v7a"
|
|
|
|
assert target_android._build_dir.endswith(
|
2020-04-10 18:40:16 +02:00
|
|
|
".buildozer/android/platform/build-armeabi-v7a"
|
|
|
|
)
|
2020-06-24 10:36:21 +02:00
|
|
|
assert target_android._p4a_bootstrap == "sdl2"
|
|
|
|
assert target_android._p4a_cmd.endswith(
|
2020-04-10 18:40:16 +02:00
|
|
|
"python -m pythonforandroid.toolchain "
|
|
|
|
)
|
2020-06-24 10:36:21 +02:00
|
|
|
assert target_android.build_mode == "debug"
|
2020-04-10 18:40:16 +02:00
|
|
|
assert (
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android.extra_p4a_args == (
|
2020-04-24 19:54:55 +02:00
|
|
|
' --color=always'
|
2020-09-13 17:25:14 +02:00
|
|
|
' --storage-dir="{buildozer_dir}/android/platform/build-armeabi-v7a" --ndk-api=21 --ignore-setup-py'.format(
|
2020-06-24 10:36:21 +02:00
|
|
|
buildozer_dir=buildozer.buildozer_dir)
|
2020-04-24 19:54:55 +02:00
|
|
|
)
|
2020-04-10 18:40:16 +02:00
|
|
|
)
|
2020-06-24 10:36:21 +02:00
|
|
|
assert target_android.p4a_apk_cmd == "apk --debug --bootstrap=sdl2"
|
|
|
|
assert target_android.platform_update is False
|
2020-04-10 18:40:16 +02:00
|
|
|
|
|
|
|
def test_init_positional_buildozer(self):
|
|
|
|
"""Positional `buildozer` argument is required."""
|
|
|
|
with pytest.raises(TypeError) as ex_info:
|
2020-04-13 17:26:17 +02:00
|
|
|
TargetAndroid()
|
2020-04-10 18:40:16 +02:00
|
|
|
assert ex_info.value.args == (
|
|
|
|
"__init__() missing 1 required positional argument: 'buildozer'",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_sdkmanager(self):
|
|
|
|
"""Tests the _sdkmanager() method."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
2020-04-10 18:40:16 +02:00
|
|
|
kwargs = {}
|
|
|
|
with patch_buildozer_cmd() as m_cmd, patch_buildozer_cmd_expect() as m_cmd_expect, patch_os_isfile() as m_isfile:
|
|
|
|
m_isfile.return_value = True
|
2020-06-24 10:36:21 +02:00
|
|
|
assert m_cmd.return_value == target_android._sdkmanager(**kwargs)
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_cmd.call_count == 1
|
|
|
|
assert m_cmd_expect.call_count == 0
|
|
|
|
assert m_isfile.call_count == 1
|
|
|
|
kwargs = {"return_child": True}
|
|
|
|
with patch_buildozer_cmd() as m_cmd, patch_buildozer_cmd_expect() as m_cmd_expect, patch_os_isfile() as m_isfile:
|
|
|
|
m_isfile.return_value = True
|
2020-06-24 10:36:21 +02:00
|
|
|
assert m_cmd_expect.return_value == target_android._sdkmanager(
|
2020-04-10 18:40:16 +02:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
assert m_cmd.call_count == 0
|
|
|
|
assert m_cmd_expect.call_count == 1
|
|
|
|
assert m_isfile.call_count == 1
|
|
|
|
|
|
|
|
def test_check_requirements(self):
|
|
|
|
"""Basic tests for the check_requirements() method."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
assert not hasattr(target_android, "adb_cmd")
|
|
|
|
assert not hasattr(target_android, "javac_cmd")
|
|
|
|
assert "PATH" not in buildozer.environ
|
2020-04-10 18:40:16 +02:00
|
|
|
with patch_buildozer_checkbin() as m_checkbin:
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android.check_requirements()
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_checkbin.call_args_list == [
|
|
|
|
mock.call("Git (git)", "git"),
|
|
|
|
mock.call("Cython (cython)", "cython"),
|
|
|
|
mock.call("Java compiler (javac)", "javac"),
|
|
|
|
mock.call("Java keytool (keytool)", "keytool"),
|
|
|
|
]
|
2020-06-24 10:36:21 +02:00
|
|
|
assert target_android.adb_cmd.endswith(
|
2020-04-10 18:40:16 +02:00
|
|
|
".buildozer/android/platform/android-sdk/platform-tools/adb"
|
|
|
|
)
|
2020-06-24 10:36:21 +02:00
|
|
|
assert target_android.javac_cmd == "javac"
|
|
|
|
assert target_android.keytool_cmd == "keytool"
|
|
|
|
assert "PATH" in buildozer.environ
|
2020-04-10 18:40:16 +02:00
|
|
|
|
|
|
|
def test_check_configuration_tokens(self):
|
|
|
|
"""Basic tests for the check_configuration_tokens() method."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
2020-04-10 18:40:16 +02:00
|
|
|
with mock.patch(
|
|
|
|
"buildozer.targets.android.Target.check_configuration_tokens"
|
|
|
|
) as m_check_configuration_tokens:
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android.check_configuration_tokens()
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_check_configuration_tokens.call_args_list == [mock.call([])]
|
|
|
|
|
2020-04-24 19:54:55 +02:00
|
|
|
@pytest.mark.parametrize("platform", ["linux", "darwin"])
|
|
|
|
def test_install_android_sdk(self, platform):
|
2020-04-10 18:40:16 +02:00
|
|
|
"""Basic tests for the _install_android_sdk() method."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
2020-04-10 18:40:16 +02:00
|
|
|
with patch_buildozer_file_exists() as m_file_exists, patch_buildozer_download() as m_download:
|
|
|
|
m_file_exists.return_value = True
|
2020-06-24 10:36:21 +02:00
|
|
|
sdk_dir = target_android._install_android_sdk()
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_file_exists.call_args_list == [
|
2020-06-24 10:36:21 +02:00
|
|
|
mock.call(target_android.android_sdk_dir)
|
2020-04-10 18:40:16 +02:00
|
|
|
]
|
|
|
|
assert m_download.call_args_list == []
|
|
|
|
assert sdk_dir.endswith(".buildozer/android/platform/android-sdk")
|
2020-04-24 19:54:55 +02:00
|
|
|
with patch_buildozer_file_exists() as m_file_exists, \
|
|
|
|
patch_buildozer_download() as m_download, \
|
|
|
|
patch_buildozer_file_extract() as m_file_extract, \
|
|
|
|
patch_platform(platform):
|
2020-04-10 18:40:16 +02:00
|
|
|
m_file_exists.return_value = False
|
2020-06-24 10:36:21 +02:00
|
|
|
sdk_dir = target_android._install_android_sdk()
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_file_exists.call_args_list == [
|
2020-06-24 10:36:21 +02:00
|
|
|
mock.call(target_android.android_sdk_dir)
|
2020-04-10 18:40:16 +02:00
|
|
|
]
|
2020-05-31 01:23:05 +02:00
|
|
|
platform_map = {"linux": "linux", "darwin": "mac"}
|
|
|
|
platform = platform_map[platform]
|
|
|
|
archive = "commandlinetools-{platform}-6514223_latest.zip".format(platform=platform)
|
2020-04-10 18:40:16 +02:00
|
|
|
assert m_download.call_args_list == [
|
|
|
|
mock.call(
|
2020-05-31 01:23:05 +02:00
|
|
|
"https://dl.google.com/android/repository/",
|
2020-04-24 19:54:55 +02:00
|
|
|
archive,
|
2020-04-10 18:40:16 +02:00
|
|
|
cwd=mock.ANY,
|
|
|
|
)
|
|
|
|
]
|
2020-04-24 19:54:55 +02:00
|
|
|
assert m_file_extract.call_args_list == [mock.call(archive, cwd=mock.ANY)]
|
2020-04-10 18:40:16 +02:00
|
|
|
assert sdk_dir.endswith(".buildozer/android/platform/android-sdk")
|
2020-04-13 19:12:38 +02:00
|
|
|
|
|
|
|
def test_build_package(self):
|
|
|
|
"""Basic tests for the build_package() method."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir)
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
m_execute_build_package = call_build_package(target_android)
|
2020-04-13 19:12:38 +02:00
|
|
|
assert m_execute_build_package.call_args_list == [
|
|
|
|
mock.call(
|
|
|
|
[
|
|
|
|
("--name", "'My Application'"),
|
|
|
|
("--version", "0.1"),
|
|
|
|
("--package", "org.test.myapp"),
|
|
|
|
("--minsdk", "21"),
|
|
|
|
("--ndk-api", "21"),
|
2020-06-24 10:36:21 +02:00
|
|
|
("--private", "{buildozer_dir}/android/app".format(buildozer_dir=buildozer.buildozer_dir)),
|
2020-04-13 19:12:38 +02:00
|
|
|
("--android-entrypoint", "org.kivy.android.PythonActivity"),
|
|
|
|
("--android-apptheme", "@android:style/Theme.NoTitleBar"),
|
|
|
|
("--orientation", "portrait"),
|
|
|
|
("--window",),
|
|
|
|
("debug",),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-05-28 18:36:52 +02:00
|
|
|
|
2020-05-30 00:47:18 +02:00
|
|
|
def test_numeric_version(self):
|
|
|
|
"""The `android.numeric_version` config should be passed to `build_package()`."""
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir, {
|
2020-05-30 00:47:18 +02:00
|
|
|
"android.numeric_version": "1234"
|
|
|
|
})
|
2020-06-24 10:36:21 +02:00
|
|
|
buildozer = target_android.buildozer
|
|
|
|
m_execute_build_package = call_build_package(target_android)
|
2020-05-30 00:47:18 +02:00
|
|
|
assert m_execute_build_package.call_args_list == [
|
|
|
|
mock.call(
|
|
|
|
[
|
|
|
|
("--name", "'My Application'"),
|
|
|
|
("--version", "0.1"),
|
|
|
|
("--package", "org.test.myapp"),
|
|
|
|
("--minsdk", "21"),
|
|
|
|
("--ndk-api", "21"),
|
2020-06-24 10:36:21 +02:00
|
|
|
("--private", "{buildozer_dir}/android/app".format(buildozer_dir=buildozer.buildozer_dir)),
|
2020-05-30 00:47:18 +02:00
|
|
|
("--android-entrypoint", "org.kivy.android.PythonActivity"),
|
|
|
|
("--android-apptheme", "@android:style/Theme.NoTitleBar"),
|
|
|
|
("--orientation", "portrait"),
|
|
|
|
("--window",),
|
|
|
|
("--numeric-version", "1234"),
|
|
|
|
("debug",),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2020-05-28 18:36:52 +02:00
|
|
|
def test_build_package_intent_filters(self):
|
|
|
|
"""
|
|
|
|
The build_package() method should honour the manifest.intent_filters
|
|
|
|
config option.
|
|
|
|
"""
|
|
|
|
filters_path = os.path.join(self.temp_dir.name, 'filters.xml')
|
|
|
|
|
|
|
|
with open(filters_path, 'w') as f:
|
|
|
|
f.write('<?xml version="1.0" encoding="utf-8"?>')
|
|
|
|
|
2020-06-24 10:36:21 +02:00
|
|
|
target_android = init_target(self.temp_dir, {
|
2020-05-28 18:36:52 +02:00
|
|
|
'android.manifest.intent_filters': 'filters.xml'
|
|
|
|
})
|
2020-06-24 10:36:21 +02:00
|
|
|
buildozer = target_android.buildozer
|
|
|
|
m_execute_build_package = call_build_package(target_android)
|
2020-05-28 18:36:52 +02:00
|
|
|
|
|
|
|
assert m_execute_build_package.call_args_list == [
|
2020-04-13 19:12:38 +02:00
|
|
|
mock.call(
|
2020-05-28 18:36:52 +02:00
|
|
|
[
|
|
|
|
('--name', "'My Application'"),
|
|
|
|
('--version', '0.1'),
|
|
|
|
('--package', 'org.test.myapp'),
|
|
|
|
('--minsdk', '21'),
|
|
|
|
('--ndk-api', '21'),
|
2020-06-24 10:36:21 +02:00
|
|
|
('--private', '{buildozer_dir}/android/app'.format(buildozer_dir=buildozer.buildozer_dir)),
|
2020-05-28 18:36:52 +02:00
|
|
|
('--android-entrypoint', 'org.kivy.android.PythonActivity'),
|
|
|
|
('--android-apptheme', '@android:style/Theme.NoTitleBar'),
|
|
|
|
('--orientation', 'portrait'),
|
|
|
|
('--window',),
|
|
|
|
('--intent-filters', os.path.realpath(filters_path)),
|
|
|
|
('debug',),
|
|
|
|
]
|
2020-04-13 19:12:38 +02:00
|
|
|
)
|
|
|
|
]
|
2020-08-14 15:35:13 +02:00
|
|
|
|
|
|
|
def test_allow_backup(self):
|
|
|
|
"""The `android.allow_backup` config should be passed to `build_package()`."""
|
|
|
|
target_android = init_target(self.temp_dir, {
|
|
|
|
"android.allow_backup": "false"
|
|
|
|
})
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
m_execute_build_package = call_build_package(target_android)
|
|
|
|
assert m_execute_build_package.call_args_list == [
|
|
|
|
mock.call(
|
|
|
|
[
|
|
|
|
("--name", "'My Application'"),
|
|
|
|
("--version", "0.1"),
|
|
|
|
("--package", "org.test.myapp"),
|
|
|
|
("--minsdk", "21"),
|
|
|
|
("--ndk-api", "21"),
|
|
|
|
("--private", "{buildozer_dir}/android/app".format(buildozer_dir=buildozer.buildozer_dir)),
|
|
|
|
("--android-entrypoint", "org.kivy.android.PythonActivity"),
|
|
|
|
("--android-apptheme", "@android:style/Theme.NoTitleBar"),
|
|
|
|
("--orientation", "portrait"),
|
|
|
|
("--window",),
|
|
|
|
("--allow-backup", "false"),
|
|
|
|
("debug",),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|
2020-08-16 11:00:19 +02:00
|
|
|
|
|
|
|
def test_backup_rules(self):
|
|
|
|
"""The `android.backup_rules` config should be passed to `build_package()`."""
|
|
|
|
target_android = init_target(self.temp_dir, {
|
|
|
|
"android.backup_rules": "backup_rules.xml"
|
|
|
|
})
|
|
|
|
buildozer = target_android.buildozer
|
|
|
|
m_execute_build_package = call_build_package(target_android)
|
|
|
|
assert m_execute_build_package.call_args_list == [
|
|
|
|
mock.call(
|
|
|
|
[
|
|
|
|
("--name", "'My Application'"),
|
|
|
|
("--version", "0.1"),
|
|
|
|
("--package", "org.test.myapp"),
|
|
|
|
("--minsdk", "21"),
|
|
|
|
("--ndk-api", "21"),
|
|
|
|
("--private", "{buildozer_dir}/android/app".format(buildozer_dir=buildozer.buildozer_dir)),
|
|
|
|
("--android-entrypoint", "org.kivy.android.PythonActivity"),
|
|
|
|
("--android-apptheme", "@android:style/Theme.NoTitleBar"),
|
|
|
|
("--orientation", "portrait"),
|
|
|
|
("--window",),
|
|
|
|
("--backup-rules", "{root_dir}/backup_rules.xml".format(root_dir=buildozer.root_dir)),
|
|
|
|
("debug",),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
]
|