Skip to content

Add service to webview test app #2598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions testapps/on_device_unit_tests/test_app/app_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@


app = Flask(__name__)
service_running = False
TESTS_TO_PERFORM = dict()
NON_ANDROID_DEVICE_MSG = 'Not running from Android device'

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


def get_test_service():
from jnius import autoclass

return autoclass('org.test.unit_tests_app.ServiceP4a_test_service')


def start_service():
global service_running
activity = get_android_python_activity()
test_service = get_test_service()
test_service.start(activity, 'Some argument')
service_running = True


def stop_service():
global service_running
activity = get_android_python_activity()
test_service = get_test_service()
test_service.stop(activity)
service_running = False


@app.route('/')
def index():
return render_template(
'index.html',
platform='Android' if RUNNING_ON_ANDROID else 'Desktop',
service_running=service_running,
)


Expand Down Expand Up @@ -139,3 +163,21 @@ def orientation():
direction = args['dir']
set_device_orientation(direction)
return ('', 204)


@app.route('/service')
def service():
if not RUNNING_ON_ANDROID:
print(NON_ANDROID_DEVICE_MSG, '...cancelled service.')
return (NON_ANDROID_DEVICE_MSG, 400)
args = request.args
if 'action' not in args:
print('ERROR: asked to manage service but no action specified')
return ('No action specified', 400)

action = args['action']
if action == 'start':
start_service()
else:
stop_service()
return ('', 204)
44 changes: 44 additions & 0 deletions testapps/on_device_unit_tests/test_app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,50 @@ <h3 class="text-underline">Android tests</h3>
}
</script>
</div>

<div>
<button onClick="start_service()">
start service
</button>

<button onClick="stop_service()">
stop service
</button>

<div id="service-status">
{{ 'Service started' if service_running else 'Service stopped' }}
</div>

<script>
function start_service() {
var request = new XMLHttpRequest();
request.onload = function() {
var elem = document.getElementById('service-status');
if (this.status >= 400)
elem.textContent = `Service start failed: ${this.statusText}`;
else
elem.textContent = 'Service started';
};
request.open('GET', 'service?action=start', true);
request.send();
}

function stop_service() {
var request = new XMLHttpRequest();
request.onload = function() {
var elem = document.getElementById('service-status');
if (this.status >= 400)
elem.textContent = `Service stop failed: ${this.statusText}`;
else
elem.textContent = 'Service stopped';
};
request.open('GET', 'service?action=stop', true);
request.send();
alert('Service stopped')
}
</script>
</div>

<br>
<br>
</div>
Expand Down