#!/bin/bash --posix
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eux

# Unset TESTBRIDGE_TEST_ONLY environment variable set by Bazel's --test_filter
# flag so that JUnit3 doesn't filter out the Android test suite class. Instead,
# forward this variable as a Java flag with the same name.
if [ -z "${TESTBRIDGE_TEST_ONLY+1}" ]; then
  ANDROID_TESTBRIDGE_TEST_ONLY=""
else
  ANDROID_TESTBRIDGE_TEST_ONLY=${TESTBRIDGE_TEST_ONLY}
  unset TESTBRIDGE_TEST_ONLY
fi

function join_paths() {
  local base_dir=$1
  local sep=$2
  shift 2
  local paths=$@

  local result=""
  for path in $paths
  do
    result=${base_dir}/${path}${sep}${result}
  done
  echo ${result}
}

test_label="%test_label%"
test_entry_point="%test_entry_point%"
log_levels="%log_levels%"
WORKSPACE_DIR="${TEST_SRCDIR}/%workspace%"
adb="${WORKSPACE_DIR}/%adb%"
aapt="${WORKSPACE_DIR}/%aapt%"
device_script="${WORKSPACE_DIR}/%device_script%"

data_deps="%data_deps%"
data_deps=$(join_paths ${WORKSPACE_DIR} "," ${data_deps})

device_broker_type="%device_broker_type%"

target_apk="%target_apk%"

instrumentation_apk="%instrumentation_apk%"

support_apks="%support_apks%"
support_apks=$(join_paths ${WORKSPACE_DIR} "," ${support_apks})

apks_to_install="${support_apks}${target_apk},${instrumentation_apk}"

declare -A device_script_fixtures=( %device_script_fixtures% )

host_service_fixture="%host_service_fixture%"
host_service_fixture_services="%host_service_fixture_services%"

fixture_args="%fixture_args%"

test_suite_property_name='%test_suite_property_name%'

argv=$(cat <<END
--aapt=${aapt} \
--adb=${adb} \
--device_broker_type=${device_broker_type} \
--device_script=${device_script} \
--data_deps=${data_deps} \
--test_label=${test_label} \
--apks_to_install=${apks_to_install} \
--fixture_scripts=$(printf "%s," "${!device_script_fixtures[@]}") \
--hermetic_server_script=${host_service_fixture} \
--hermetic_servers=${host_service_fixture_services} \
--data_deps=$(printf "%s," "${!device_script_fixtures[@]}") \
--test_filter=${ANDROID_TESTBRIDGE_TEST_ONLY} \
$@
END
)

# Export the default DISPLAY variable to :0 if DISPLAY is not set, which is the
# case in sandboxing. This will allow Android emulators to show up by default.
# The DISPLAY variable can also be set with --action_env.
# e.g. `bazel test //... --action_env=DISPLAY=:42`
# To disable DISPLAY, set
# android_instrumentation_test.args = [ "--enable_display=false" ]
if [[ -z "${DISPLAY-}" ]]; then
  export DISPLAY=:0
fi

$test_entry_point \
    --wrapper_script_flag=--jvm_flag=-D$test_suite_property_name=com.google.android.apps.common.testing.suite.AndroidDeviceTestSuite \
    --wrapper_script_flag=--jvm_flag=-Dargv="$argv" \
    $argv
