Skip to content

Commit

Permalink
Tests: Fix unit tests so all tests pass (#1580)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 authored Aug 2, 2022
1 parent 2b031b9 commit 007bada
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 145 deletions.
1 change: 1 addition & 0 deletions src/opensles/AudioInputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>

#include "common/OboeDebug.h"
#include "oboe/AudioStreamBuilder.h"
#include "AudioInputStreamOpenSLES.h"
#include "AudioStreamOpenSLES.h"
Expand Down
1 change: 1 addition & 0 deletions src/opensles/AudioOutputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <SLES/OpenSLES_Android.h>
#include <common/AudioClock.h>

#include "common/OboeDebug.h"
#include "oboe/AudioStreamBuilder.h"
#include "AudioOutputStreamOpenSLES.h"
#include "AudioStreamOpenSLES.h"
Expand Down
1 change: 0 additions & 1 deletion src/opensles/AudioStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cassert>
#include <android/log.h>


#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <oboe/AudioStream.h>
Expand Down
24 changes: 12 additions & 12 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ include_directories(

# Build the test binary
add_executable(
testOboe
testAAudio.cpp
testUtilities.cpp
testFlowgraph.cpp
testStreamClosedMethods.cpp
testStreamWaitState.cpp
testXRunBehaviour.cpp
testStreamOpen.cpp
testStreamStates.cpp
testStreamFramesProcessed.cpp
testReturnStop.cpp
testStreamStop.cpp
testOboe
testAAudio.cpp
testFlowgraph.cpp
testReturnStop.cpp
testStreamClosedMethods.cpp
testStreamFramesProcessed.cpp
testStreamOpen.cpp
testStreamStates.cpp
testStreamStop.cpp
testStreamWaitState.cpp
testXRunBehaviour.cpp
testUtilities.cpp
)

target_link_libraries(testOboe gtest oboe)
2 changes: 2 additions & 0 deletions tests/UnitTestRunner/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
.DS_Store
/build
/captures
/assets
/jniLibs
.externalNativeBuild
6 changes: 3 additions & 3 deletions tests/UnitTestRunner/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 33
defaultConfig {
applicationId "com.google.oboe.tests.unittestrunner"
minSdkVersion 16
targetSdkVersion 28
minSdkVersion 21
targetSdkVersion 33
versionCode 1
versionName "1.0"
}
Expand Down
2 changes: 0 additions & 2 deletions tests/UnitTestRunner/app/src/main/assets/.gitignore

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -29,7 +26,7 @@
public class MainActivity extends AppCompatActivity {

private final String TAG = MainActivity.class.getName();
private static final String TEST_BINARY_FILEANAME = "testOboe";
private static final String TEST_BINARY_FILENAME = "testOboe.so";
private static final int APP_PERMISSION_REQUEST = 0;

private TextView outputText;
Expand Down Expand Up @@ -57,43 +54,18 @@ private void runCommand(){
}

private String executeBinary() {

AssetManager assetManager = getAssets();

StringBuffer output = new StringBuffer();
String abi = Build.CPU_ABI;
String extraStringForDebugBuilds = "-hwasan";
if (abi.endsWith(extraStringForDebugBuilds)) {
abi = abi.substring(0, abi.length() - extraStringForDebugBuilds.length());
}
String filesDir = getFilesDir().getPath();
String testBinaryPath = abi + "/" + TEST_BINARY_FILEANAME;
StringBuilder output = new StringBuilder();

try {
InputStream inStream = assetManager.open(testBinaryPath);
Log.d(TAG, "Opened " + testBinaryPath);

// Copy this file to an executable location
File outFile = new File(filesDir, TEST_BINARY_FILEANAME);

OutputStream outStream = new FileOutputStream(outFile);

byte[] buffer = new byte[1024];
int read;
while ((read = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, read);
String executablePath;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
executablePath = getApplicationInfo().nativeLibraryDir + "/" + TEST_BINARY_FILENAME;
} else {
executablePath = getExecutablePathFromAssets();
}
inStream.close();
outStream.flush();
outStream.close();
Log.d(TAG, "Copied " + testBinaryPath + " to " + filesDir);

String executablePath = filesDir + "/" + TEST_BINARY_FILEANAME;
Log.d(TAG, "Attempting to execute " + executablePath);

new File(executablePath).setExecutable(true, false);
Log.d(TAG, "Setting execute permission on " + executablePath);

Process process = Runtime.getRuntime().exec(executablePath);

BufferedReader stdInput = new BufferedReader(new
Expand All @@ -103,16 +75,16 @@ private String executeBinary() {
InputStreamReader(process.getErrorStream()));

// read the output from the command
String s = null;
String s;
while ((s = stdInput.readLine()) != null) {
Log.d(TAG, s);
output.append(s + "\n");
output.append(s).append("\n");
}

// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
Log.e(TAG, "ERROR: " + s);
output.append("ERROR: " + s + "\n");
output.append("ERROR: ").append(s).append("\n");
}

process.waitFor();
Expand All @@ -126,6 +98,51 @@ private String executeBinary() {
return output.toString();
}

// Legacy method to get asset path.
// This will not work on more recent Android releases.
private String getExecutablePathFromAssets() {
AssetManager assetManager = getAssets();

String abi = Build.SUPPORTED_ABIS[0];
String extraStringForDebugBuilds = "-hwasan";
if (abi.endsWith(extraStringForDebugBuilds)) {
abi = abi.substring(0, abi.length() - extraStringForDebugBuilds.length());
}
String filesDir = getFilesDir().getPath();
String testBinaryPath = abi + "/" + TEST_BINARY_FILENAME;

try {
InputStream inStream = assetManager.open(testBinaryPath);
Log.d(TAG, "Opened " + testBinaryPath);

// Copy this file to an executable location
File outFile = new File(filesDir, TEST_BINARY_FILENAME);

OutputStream outStream = new FileOutputStream(outFile);

byte[] buffer = new byte[1024];
int read;
while ((read = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, read);
}
inStream.close();
outStream.flush();
outStream.close();
Log.d(TAG, "Copied " + testBinaryPath + " to " + filesDir);

String executablePath = filesDir + "/" + TEST_BINARY_FILENAME;
Log.d(TAG, "Setting execute permission on " + executablePath);
boolean success = new File(executablePath).setExecutable(true, false);
if (!success) {
Log.d(TAG, "Could not set execute permission on " + executablePath);
}
return executablePath;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

private boolean isRecordPermissionGranted() {
return (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED);
Expand Down Expand Up @@ -169,19 +186,11 @@ class UnitTestCommand implements Runnable {
public void run() {
final String output = executeBinary();

runOnUiThread(new Runnable() {
@Override
public void run() {
outputText.setText(output);

// Scroll to the bottom so we can see the test result
scrollView.postDelayed(new Runnable() {
@Override
public void run() {
scrollView.scrollTo(0, outputText.getBottom());
}
}, 100);
}
runOnUiThread(() -> {
outputText.setText(output);

// Scroll to the bottom so we can see the test result
scrollView.postDelayed(() -> scrollView.scrollTo(0, outputText.getBottom()), 100);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTestRunner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.1.1'
classpath 'com.android.tools.build:gradle:7.2.1'
}
}

Expand Down
34 changes: 24 additions & 10 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ CMAKE=cmake
TEST_BINARY_FILENAME=testOboe
TEST_RUNNER_DIR=UnitTestRunner
TEST_RUNNER_PACKAGE_NAME=com.google.oboe.tests.unittestrunner
TEST_RUNNER_ASSET_DIR=${TEST_RUNNER_DIR}/app/src/main/assets
TEST_RUNNER_JNILIBS_DIR=${TEST_RUNNER_DIR}/app/src/main/jniLibs
TEST_RUNNER_ASSETS_DIR=${TEST_RUNNER_DIR}/app/src/main/assets

# Check prerequisites
if [ -z "$ANDROID_NDK" ]; then
Expand All @@ -68,7 +69,7 @@ fi
ABI=$(adb shell getprop ro.product.cpu.abi | tr -d '\n\r')

if [ -z "$ABI" ]; then
echo "No device ABI was set. Please ensure a device or emulator is running"
echo "No device ABI was set. Please ensure a device or emulator is running. You may need to unplug extra devices."
exit 1
fi

Expand All @@ -83,6 +84,11 @@ else
exit 1
fi

mkdir -p ${BUILD_DIR}

echo "Cleaning up previous build because swapping phones may result in stale binaries"
rm -r ${BUILD_DIR}

# Configure the build
echo "Building tests for ${ABI} using ${PLATFORM}"

Expand All @@ -95,9 +101,7 @@ CMAKE_ARGS="-H. \
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DCMAKE_VERBOSE_MAKEFILE=1"

mkdir -p ${BUILD_DIR}

cmake ${CMAKE_ARGS}
cmake ${CMAKE_ARGS}

# Perform the build
pushd ${BUILD_DIR}
Expand All @@ -112,11 +116,20 @@ pushd ${BUILD_DIR}

popd

# Copy the binary into the unit test runner app
mkdir ${TEST_RUNNER_ASSET_DIR}/${ABI}
DESTINATION_DIR=${TEST_RUNNER_ASSET_DIR}/${ABI}/${TEST_BINARY_FILENAME}
# Copy the binary into the jniLibs and assets folders of the unit test runner app
# The assets folder does not work after Android R for security reasons
# The jniLibs folder doesn't seem to work before Android O
# Thus, copy into both
mkdir ${TEST_RUNNER_JNILIBS_DIR}
mkdir ${TEST_RUNNER_JNILIBS_DIR}/${ABI}
DESTINATION_DIR=${TEST_RUNNER_JNILIBS_DIR}/${ABI}/${TEST_BINARY_FILENAME}
echo "Copying binary to ${DESTINATION_DIR}"
cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}.so
mkdir ${TEST_RUNNER_ASSETS_DIR}
mkdir ${TEST_RUNNER_ASSETS_DIR}/${ABI}
DESTINATION_DIR=${TEST_RUNNER_ASSETS_DIR}/${ABI}/${TEST_BINARY_FILENAME}
echo "Copying binary to ${DESTINATION_DIR}"
cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}
cp ${BUILD_DIR}/${TEST_BINARY_FILENAME} ${DESTINATION_DIR}.so

# Build and install the unit test runner app
pushd ${TEST_RUNNER_DIR}
Expand All @@ -141,4 +154,5 @@ echo "Starting app - Check your device for test results"
adb shell am start ${TEST_RUNNER_PACKAGE_NAME}/.MainActivity

sleep 1
adb logcat ${TEST_RUNNER_PACKAGE_NAME}
echo "Logging test logs and Oboe logs. Run adb logcat for complete logs."
adb logcat ${TEST_RUNNER_PACKAGE_NAME}.MainActivity:V OboeAudio:V *:S
Loading

0 comments on commit 007bada

Please sign in to comment.