Skip to content

Commit fff7013

Browse files
authored
Merge pull request #2598 from dbnicholson/webview-testapp-service
Add service to webview test app
2 parents eeea8a1 + 175d282 commit fff7013

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

testapps/on_device_unit_tests/test_app/app_flask.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030

3131
app = Flask(__name__)
32+
service_running = False
3233
TESTS_TO_PERFORM = dict()
3334
NON_ANDROID_DEVICE_MSG = 'Not running from Android device'
3435

@@ -50,11 +51,34 @@ def get_html_for_tested_modules(tested_modules, failed_tests):
5051
return Markup(modules_text)
5152

5253

54+
def get_test_service():
55+
from jnius import autoclass
56+
57+
return autoclass('org.test.unit_tests_app.ServiceP4a_test_service')
58+
59+
60+
def start_service():
61+
global service_running
62+
activity = get_android_python_activity()
63+
test_service = get_test_service()
64+
test_service.start(activity, 'Some argument')
65+
service_running = True
66+
67+
68+
def stop_service():
69+
global service_running
70+
activity = get_android_python_activity()
71+
test_service = get_test_service()
72+
test_service.stop(activity)
73+
service_running = False
74+
75+
5376
@app.route('/')
5477
def index():
5578
return render_template(
5679
'index.html',
5780
platform='Android' if RUNNING_ON_ANDROID else 'Desktop',
81+
service_running=service_running,
5882
)
5983

6084

@@ -139,3 +163,21 @@ def orientation():
139163
direction = args['dir']
140164
set_device_orientation(direction)
141165
return ('', 204)
166+
167+
168+
@app.route('/service')
169+
def service():
170+
if not RUNNING_ON_ANDROID:
171+
print(NON_ANDROID_DEVICE_MSG, '...cancelled service.')
172+
return (NON_ANDROID_DEVICE_MSG, 400)
173+
args = request.args
174+
if 'action' not in args:
175+
print('ERROR: asked to manage service but no action specified')
176+
return ('No action specified', 400)
177+
178+
action = args['action']
179+
if action == 'start':
180+
start_service()
181+
else:
182+
stop_service()
183+
return ('', 204)

testapps/on_device_unit_tests/test_app/templates/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,50 @@ <h3 class="text-underline">Android tests</h3>
9090
}
9191
</script>
9292
</div>
93+
94+
<div>
95+
<button onClick="start_service()">
96+
start service
97+
</button>
98+
99+
<button onClick="stop_service()">
100+
stop service
101+
</button>
102+
103+
<div id="service-status">
104+
{{ 'Service started' if service_running else 'Service stopped' }}
105+
</div>
106+
107+
<script>
108+
function start_service() {
109+
var request = new XMLHttpRequest();
110+
request.onload = function() {
111+
var elem = document.getElementById('service-status');
112+
if (this.status >= 400)
113+
elem.textContent = `Service start failed: ${this.statusText}`;
114+
else
115+
elem.textContent = 'Service started';
116+
};
117+
request.open('GET', 'service?action=start', true);
118+
request.send();
119+
}
120+
121+
function stop_service() {
122+
var request = new XMLHttpRequest();
123+
request.onload = function() {
124+
var elem = document.getElementById('service-status');
125+
if (this.status >= 400)
126+
elem.textContent = `Service stop failed: ${this.statusText}`;
127+
else
128+
elem.textContent = 'Service stopped';
129+
};
130+
request.open('GET', 'service?action=stop', true);
131+
request.send();
132+
alert('Service stopped')
133+
}
134+
</script>
135+
</div>
136+
93137
<br>
94138
<br>
95139
</div>

0 commit comments

Comments
 (0)