Skip to content

Commit 19972b4

Browse files
committed
[test] Add unittest for pythonforandroid.recommendations
1 parent 736c639 commit 19972b4

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

tests/test_recommendations.py

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import unittest
2+
from os.path import join
3+
from sys import version as py_version
4+
5+
try:
6+
from unittest import mock
7+
except ImportError:
8+
# `Python 2` or lower than `Python 3.3` does not
9+
# have the `unittest.mock` module built-in
10+
import mock
11+
from pythonforandroid.recommendations import (
12+
check_ndk_api,
13+
check_ndk_version,
14+
check_target_api,
15+
read_ndk_version,
16+
MAX_NDK_VERSION,
17+
RECOMMENDED_NDK_VERSION,
18+
RECOMMENDED_TARGET_API,
19+
MIN_NDK_API,
20+
MIN_NDK_VERSION,
21+
NDK_DOWNLOAD_URL,
22+
ARMEABI_MAX_TARGET_API,
23+
MIN_TARGET_API,
24+
)
25+
from pythonforandroid.util import BuildInterruptingException
26+
running_in_py2 = int(py_version[0]) < 3
27+
28+
29+
class TestRecommendations(unittest.TestCase):
30+
"""
31+
An inherited class of `unittest.TestCase`to test the module
32+
:mod:`~pythonforandroid.recommendations`.
33+
"""
34+
35+
def setUp(self):
36+
self.ndk_dir = "/opt/android/android-ndk"
37+
38+
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
39+
@mock.patch("pythonforandroid.recommendations.read_ndk_version")
40+
def test_check_ndk_version_greater_than_recommended(self, mock_read_ndk):
41+
mock_read_ndk.return_value.version = [MAX_NDK_VERSION + 1, 0, 5232133]
42+
with self.assertLogs(level="INFO") as cm:
43+
check_ndk_version(self.ndk_dir)
44+
mock_read_ndk.assert_called_once_with(self.ndk_dir)
45+
self.assertEqual(
46+
cm.output,
47+
[
48+
"INFO:p4a:[INFO]: Found NDK version {ndk_current}".format(
49+
ndk_current=MAX_NDK_VERSION + 1
50+
),
51+
"WARNING:p4a:[WARNING]:"
52+
" Maximum recommended NDK version is {ndk_recommended}".format(
53+
ndk_recommended=RECOMMENDED_NDK_VERSION
54+
),
55+
"WARNING:p4a:[WARNING]:"
56+
" Newer NDKs may not be fully supported by p4a.",
57+
],
58+
)
59+
60+
@mock.patch("pythonforandroid.recommendations.read_ndk_version")
61+
def test_check_ndk_version_lower_than_recommended(self, mock_read_ndk):
62+
mock_read_ndk.return_value.version = [MIN_NDK_VERSION - 1, 0, 5232133]
63+
with self.assertRaises(BuildInterruptingException) as e:
64+
check_ndk_version(self.ndk_dir)
65+
self.assertEqual(
66+
e.exception.args[0],
67+
"Unsupported NDK version detected {ndk_current}"
68+
"\n* Note: Minimum supported NDK version is {ndk_min}".format(
69+
ndk_current=MIN_NDK_VERSION - 1, ndk_min=MIN_NDK_VERSION
70+
),
71+
)
72+
mock_read_ndk.assert_called_once_with(self.ndk_dir)
73+
74+
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
75+
def test_check_ndk_version_error(self):
76+
"""
77+
Test that a fake ndk dir give us two messages:
78+
- first should be an `INFO` log
79+
- second should be an `WARNING` log
80+
"""
81+
with self.assertLogs(level="INFO") as cm:
82+
check_ndk_version(self.ndk_dir)
83+
self.assertEqual(
84+
cm.output,
85+
[
86+
"INFO:p4a:[INFO]: Could not determine NDK version, "
87+
"no source.properties in the NDK dir",
88+
"WARNING:p4a:[WARNING]: Unable to read the ndk version, "
89+
"assuming that you are using an NDK greater than 17 (the "
90+
"minimum ndk required to use p4a successfully).\n"
91+
"Note: If you got build errors, consider to download the "
92+
"recommended ndk version which is 17c and try it again (after "
93+
"removing all the files generated with this build). To "
94+
"download the android NDK visit the following "
95+
"page: {download_url}".format(download_url=NDK_DOWNLOAD_URL),
96+
],
97+
)
98+
99+
@mock.patch("pythonforandroid.recommendations.open")
100+
def test_read_ndk_version(self, mock_open_src_prop):
101+
mock_open_src_prop.side_effect = [
102+
mock.mock_open(
103+
read_data="Pkg.Revision = 17.2.4988734"
104+
).return_value
105+
]
106+
version = read_ndk_version(self.ndk_dir)
107+
mock_open_src_prop.assert_called_once_with(
108+
join(self.ndk_dir, "source.properties")
109+
)
110+
assert version == "17.2.4988734"
111+
112+
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
113+
@mock.patch("pythonforandroid.recommendations.open")
114+
def test_read_ndk_version_error(self, mock_open_src_prop):
115+
mock_open_src_prop.side_effect = [
116+
mock.mock_open(read_data="").return_value
117+
]
118+
with self.assertLogs(level="INFO") as cm:
119+
version = read_ndk_version(self.ndk_dir)
120+
self.assertEqual(
121+
cm.output,
122+
[
123+
"INFO:p4a:[INFO]: Could not parse "
124+
"$NDK_DIR/source.properties, not checking NDK version"
125+
],
126+
)
127+
mock_open_src_prop.assert_called_once_with(
128+
join(self.ndk_dir, "source.properties")
129+
)
130+
assert version is None
131+
132+
def test_check_target_api_error_arch_armeabi(self):
133+
134+
with self.assertRaises(BuildInterruptingException) as e:
135+
check_target_api(RECOMMENDED_TARGET_API, "armeabi")
136+
self.assertEqual(
137+
e.exception.args[0],
138+
"Asked to build for armeabi architecture with API {ndk_api}, but "
139+
"API {max_target_api} or greater does not support armeabi".format(
140+
ndk_api=RECOMMENDED_TARGET_API,
141+
max_target_api=ARMEABI_MAX_TARGET_API,
142+
),
143+
)
144+
145+
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
146+
def test_check_target_api_warning_target_api(self):
147+
148+
with self.assertLogs(level="INFO") as cm:
149+
check_target_api(MIN_TARGET_API - 1, MIN_TARGET_API)
150+
self.assertEqual(
151+
cm.output,
152+
[
153+
"WARNING:p4a:[WARNING]: Target API 25 < 26",
154+
"WARNING:p4a:[WARNING]: Target APIs lower than 26 are no "
155+
"longer supported on Google Play, and are not recommended. "
156+
"Note that the Target API can be higher than your device "
157+
"Android version, and should usually be as high as possible.",
158+
],
159+
)
160+
161+
def test_check_ndk_api_error_android_api(self):
162+
"""
163+
Given an `android api` greater than an `ndk_api`, we should get an
164+
`BuildInterruptingException`.
165+
"""
166+
ndk_api = MIN_NDK_API + 1
167+
android_api = MIN_NDK_API
168+
with self.assertRaises(BuildInterruptingException) as e:
169+
check_ndk_api(ndk_api, android_api)
170+
self.assertEqual(
171+
e.exception.args[0],
172+
"Target NDK API is {ndk_api}, higher than the target Android "
173+
"API {android_api}.".format(
174+
ndk_api=ndk_api, android_api=android_api
175+
),
176+
)
177+
178+
@unittest.skipIf(running_in_py2, "`assertLogs` requires Python 3.4+")
179+
def test_check_ndk_api_warning_old_ndk(self):
180+
"""
181+
Given an `android api` lower than the supported by p4a, we should
182+
get an `BuildInterruptingException`.
183+
"""
184+
ndk_api = MIN_NDK_API - 1
185+
android_api = RECOMMENDED_TARGET_API
186+
with self.assertLogs(level="INFO") as cm:
187+
check_ndk_api(ndk_api, android_api)
188+
self.assertEqual(
189+
cm.output,
190+
["WARNING:p4a:[WARNING]: NDK API less than 21 is not supported"],
191+
)

0 commit comments

Comments
 (0)