Skip to content

Commit a719db1

Browse files
dbnicholsonrtibbles
authored andcommitted
Factor out Flask app class on on-device test app
This should make it easier to track state than using globals.
1 parent b56460b commit a719db1

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

testapps/on_device_unit_tests/test_app/app_flask.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from flask import (
1313
Flask,
14+
current_app,
1415
render_template,
1516
request,
1617
Markup
@@ -26,12 +27,37 @@
2627
get_android_python_activity,
2728
set_device_orientation,
2829
setup_lifecycle_callbacks,
30+
skip_if_not_running_from_android_device,
2931
)
3032

3133

32-
app = Flask(__name__)
33-
setup_lifecycle_callbacks()
34-
service_running = False
34+
class App(Flask):
35+
def __init__(self, *args, **kwargs):
36+
super().__init__(*args, **kwargs)
37+
setup_lifecycle_callbacks()
38+
self.service_running = False
39+
40+
@property
41+
@skip_if_not_running_from_android_device
42+
def service(self):
43+
from jnius import autoclass
44+
45+
return autoclass('org.test.unit_tests_app.ServiceP4a_test_service')
46+
47+
@skip_if_not_running_from_android_device
48+
def service_start(self):
49+
activity = get_android_python_activity()
50+
self.service.start(activity, 'Some argument')
51+
self.service_running = True
52+
53+
@skip_if_not_running_from_android_device
54+
def service_stop(self):
55+
activity = get_android_python_activity()
56+
self.service.stop(activity)
57+
self.service_running = False
58+
59+
60+
app = App(__name__)
3561
TESTS_TO_PERFORM = dict()
3662
NON_ANDROID_DEVICE_MSG = 'Not running from Android device'
3763

@@ -53,34 +79,12 @@ def get_html_for_tested_modules(tested_modules, failed_tests):
5379
return Markup(modules_text)
5480

5581

56-
def get_test_service():
57-
from jnius import autoclass
58-
59-
return autoclass('org.test.unit_tests_app.ServiceP4a_test_service')
60-
61-
62-
def start_service():
63-
global service_running
64-
activity = get_android_python_activity()
65-
test_service = get_test_service()
66-
test_service.start(activity, 'Some argument')
67-
service_running = True
68-
69-
70-
def stop_service():
71-
global service_running
72-
activity = get_android_python_activity()
73-
test_service = get_test_service()
74-
test_service.stop(activity)
75-
service_running = False
76-
77-
7882
@app.route('/')
7983
def index():
8084
return render_template(
8185
'index.html',
8286
platform='Android' if RUNNING_ON_ANDROID else 'Desktop',
83-
service_running=service_running,
87+
service_running=current_app.service_running,
8488
)
8589

8690

@@ -179,7 +183,7 @@ def service():
179183

180184
action = args['action']
181185
if action == 'start':
182-
start_service()
186+
current_app.service_start()
183187
else:
184-
stop_service()
188+
current_app.service_stop()
185189
return ('', 204)

0 commit comments

Comments
 (0)