Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 1 | # Copyright 2018 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | ################################################ |
| 16 | # Script to build and run the Oboe tests on an attached Android device or emulator |
| 17 | # |
| 18 | # Prerequisites: |
| 19 | # - CMake on PATH |
| 20 | # - ANDROID_NDK environment variable is set to your Android NDK location |
| 21 | # e.g. /Library/Android/sdk/ndk-bundle |
| 22 | # - Android device or emulator attached and accessible via adb |
| 23 | # |
Don Turner | d5a82b7 | 2018-07-12 18:09:15 +0100 | [diff] [blame] | 24 | # Instructions: |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 25 | # - Run this script |
| 26 | # - Check the test results on your target device |
Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 27 | # |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 28 | # What does the script do? |
| 29 | # - Builds a test binary for the target architecture |
| 30 | # - Copies the test binary into the UnitTestRunner app |
| 31 | # - Builds, installs and runs the app on the target device |
Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 32 | # |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 33 | # The initial run may take some time as GTest is built, subsequent runs should be much faster. |
| 34 | # |
| 35 | # If you want to perform a clean build just delete the 'build' folder and re-run this script. You will need to do |
| 36 | # this if you change target architectures (e.g. when changing between real device and emulator) |
| 37 | # |
| 38 | # Why is running the tests so convoluted? |
| 39 | # The tests require the RECORDING permission and on many devices (e.g Samsung) the adb user does not have this |
| 40 | # permission (and `run-as` is broken). This means that the test binary must be executed by an app which has this |
| 41 | # permission, hence the need for the UnitTestRunner app. |
Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 42 | # |
| 43 | ################################################ |
| 44 | |
| 45 | #!/bin/bash |
| 46 | |
| 47 | # Directories, paths and filenames |
| 48 | BUILD_DIR=build |
| 49 | CMAKE=cmake |
| 50 | TEST_BINARY_FILENAME=testOboe |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 51 | TEST_RUNNER_DIR=UnitTestRunner |
| 52 | TEST_RUNNER_PACKAGE_NAME=com.google.oboe.tests.unittestrunner |
| 53 | TEST_RUNNER_ASSET_DIR=${TEST_RUNNER_DIR}/app/src/main/assets |
Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 54 | |
| 55 | # Check prerequisites |
| 56 | if [ -z "$ANDROID_NDK" ]; then |
| 57 | echo "Please set ANDROID_NDK to the Android NDK folder" |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
| 61 | if [ ! $(type -P ${CMAKE}) ]; then |
| 62 | echo "${CMAKE} was not found on your path. You can install it using Android Studio using Tools->Android->SDK Manager->SDK Tools." |
| 63 | exit 1 |
| 64 | fi |
| 65 | |
| 66 | # Get the device ABI |
| 67 | ABI=$(adb shell getprop ro.product.cpu.abi | tr -d '\n\r') |
| 68 | |
| 69 | if [ -z "$ABI" ]; then |
| 70 | echo "No device ABI was set. Please ensure a device or emulator is running" |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | echo "Device/emulator architecture is $ABI" |
| 75 | |
| 76 | if [ ${ABI} == "arm64-v8a" ] || [ ${ABI} == "x86_64" ]; then |
| 77 | PLATFORM=android-21 |
| 78 | elif [ ${ABI} == "armeabi-v7a" ] || [ ${ABI} == "x86" ]; then |
| 79 | PLATFORM=android-16 |
| 80 | else |
| 81 | echo "Unrecognised ABI: ${ABI}. Supported ABIs are: arm64-v8a, armeabi-v7a, x86_64, x86. If you feel ${ABI} should be supported please file an issue on github.com/google/oboe" |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | # Configure the build |
| 86 | echo "Building tests for ${ABI} using ${PLATFORM}" |
| 87 | |
| 88 | CMAKE_ARGS="-H. \ |
| 89 | -B${BUILD_DIR} \ |
| 90 | -DANDROID_ABI=${ABI} \ |
| 91 | -DANDROID_PLATFORM=${PLATFORM} \ |
| 92 | -DCMAKE_BUILD_TYPE=RelWithDebInfo \ |
| 93 | -DCMAKE_CXX_FLAGS=-std=c++11 \ |
| 94 | -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \ |
| 95 | -DCMAKE_VERBOSE_MAKEFILE=1" |
| 96 | |
| 97 | mkdir -p ${BUILD_DIR} |
| 98 | |
| 99 | cmake ${CMAKE_ARGS} |
| 100 | |
| 101 | # Perform the build |
| 102 | pushd ${BUILD_DIR} |
| 103 | make -j5 |
| 104 | |
| 105 | if [ $? -eq 0 ]; then |
| 106 | echo "Tests built successfully" |
| 107 | else |
| 108 | echo "Building tests FAILED" |
| 109 | exit 1 |
| 110 | fi |
| 111 | |
| 112 | popd |
| 113 | |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 114 | # Copy the binary into the unit test runner app |
| 115 | mkdir ${TEST_RUNNER_ASSET_DIR}/${ABI} |
| 116 | DESTINATION_DIR=${TEST_RUNNER_ASSET_DIR}/${ABI}/${TEST_BINARY_FILENAME} |
| 117 | echo "Copying binary to ${DESTINATION_DIR}" |
| 118 | cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR} |
Don Turner | fda6701 | 2018-01-19 18:10:25 +0000 | [diff] [blame] | 119 | |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 120 | # Build and install the unit test runner app |
| 121 | pushd ${TEST_RUNNER_DIR} |
| 122 | echo "Building test runner app" |
| 123 | ./gradlew assembleDebug |
| 124 | echo "Installing to device" |
| 125 | ./gradlew installDebug |
| 126 | popd |
Don Turner | ff649d0 | 2018-07-04 18:35:02 +0100 | [diff] [blame] | 127 | |
Don Turner | ae0a477 | 2018-09-20 18:46:44 +0100 | [diff] [blame] | 128 | echo "Starting app - Check your device for test results" |
Don Turner | 6dd6439 | 2018-09-20 19:14:13 +0100 | [diff] [blame^] | 129 | adb shell am start ${TEST_RUNNER_PACKAGE_NAME}/.MainActivity |
| 130 | |
| 131 | sleep 1 |
| 132 | adb logcat --pid=`adb shell pidof -s ${TEST_RUNNER_PACKAGE_NAME}` |