Skip to content
38 changes: 13 additions & 25 deletions scripts/smoke-test-android.ps1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup following a messy chain of merges.. sorry.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Write-Host "#####################################################"
$BuildDir = $(GetNewProjectBuildPath)
$ApkFileName = "test.apk"
$ProcessName = "io.sentry.unity.integrationtest"
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity"
$FallBackTestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity"
$TestActivityName = $null # Detected after install via dumpsys

$_ArtifactsPath = (Test-Path env:ARTIFACTS_PATH) ? $env:ARTIFACTS_PATH : (Join-Path $BuildDir "../test-artifacts/" $(Get-Date -Format "HHmmss"))

Expand Down Expand Up @@ -199,6 +198,15 @@ else
return 1
}

# Detect the launcher activity from the installed package
$dumpOutput = adb -s $device shell dumpsys package $ProcessName 2>&1 | Out-String
if ($dumpOutput -match "com.unity3d.player.UnityPlayerGameActivity") {
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerGameActivity"
} else {
$TestActivityName = "$ProcessName/com.unity3d.player.UnityPlayerActivity"
}
Write-Log "Detected activity: $TestActivityName"

function ProcessNewLogs([array]$newLogs, [ref]$lastLogCount, [array]$logCache) {
if ($newLogs) {
$currentLogs = @($newLogs) # Force array creation even for single line
Expand Down Expand Up @@ -227,21 +235,19 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
adb -s $device shell am force-stop $ProcessName
Start-Sleep -Milliseconds 500

$activityName = $TestActivityName

Write-Log "Setting configuration"

# Mark the full-screen notification as acknowledged
adb -s $device shell "settings put secure immersive_mode_confirmations confirmed"
adb -s $device shell "input keyevent KEYCODE_HOME"

Write-Log "Starting app '$activityName'"
Write-Log "Starting app '$TestActivityName'"

# Start the adb command as a background job so we can wait for it to finish with a timeout
$job = Start-Job -ScriptBlock {
param($device, $activityName, $Name)
& adb -s $device shell am start -n $activityName -e test $Name -W 2>&1
} -ArgumentList $device, $activityName, $Name
} -ArgumentList $device, $TestActivityName, $Name

# Wait for the job to complete or to timeout
$completed = Wait-Job $job -Timeout 60
Expand All @@ -252,27 +258,9 @@ function RunTest([string] $Name, [string] $SuccessString, [string] $FailureStrin
return $false
}

$output = Receive-Job $job
Receive-Job $job | Out-Null
Remove-Job $job

Write-Log "Checking if activity started"

# Check if the activity failed to start
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
{
$activityName = $FallBackTestActivityName
Write-Log "Trying fallback activity $activityName"

$output = & adb -s $device shell am start -n $activityName -e test $Name -W 2>&1

# Check if the fallback activity failed to start
if ($output -match "Error type 3" -or $output -match "Activity class \{$activityName\} does not exist.")
{
Write-Log "Activity does not exist"
return $false
}
}

Write-Log "Activity started successfully"

$appPID = PidOf $device $ProcessName
Expand Down
21 changes: 11 additions & 10 deletions src/Sentry.Unity.Android/SentryJava.cs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual "improvements".

Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void Init(SentryUnityOptions options)
try
{
using var sentry = new AndroidJavaClass("io.sentry.android.core.SentryAndroid");
using var context = new AndroidJavaClass("com.unity3d.player.UnityPlayer")
.GetStatic<AndroidJavaObject>("currentActivity");
using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using var context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

sentry.CallStatic("init", context, new AndroidOptionsConfiguration(androidOptions =>
{
Expand All @@ -120,9 +120,9 @@ public void Init(SentryUnityOptions options)
androidOptions.Call("setDist", options.Distribution);
androidOptions.Call("setEnvironment", options.Environment);

var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel");
using var sentryLevelClass = new AndroidJavaClass("io.sentry.SentryLevel");
var levelString = GetLevelString(options.DiagnosticLevel);
var sentryLevel = sentryLevelClass.GetStatic<AndroidJavaObject>(levelString);
using var sentryLevel = sentryLevelClass.GetStatic<AndroidJavaObject>(levelString);
androidOptions.Call("setDiagnosticLevel", sentryLevel);

if (options.SampleRate.HasValue)
Expand Down Expand Up @@ -267,7 +267,7 @@ public bool IsSentryJavaPresent()
{
try
{
_ = GetSentryJava();
using var _ = GetSentryJava();
}
catch (AndroidJavaException)
{
Expand Down Expand Up @@ -387,9 +387,9 @@ internal void RunJniSafe(Action action, [CallerMemberName] string actionName = "
{
action.Invoke();
}
catch (Exception)
catch (Exception e)
{
_logger?.LogError("Calling '{0}' failed.", actionName);
_logger?.LogError(e, "Calling '{0}' failed.", actionName);
}
}
else
Expand Down Expand Up @@ -423,9 +423,9 @@ private void SyncScope()
{
action.Invoke();
}
catch (Exception)
catch (Exception e)
{
_logger?.LogError("Calling '{0}' failed.", actionName);
_logger?.LogError(e, "Calling '{0}' failed.", actionName);
}
}
}
Expand All @@ -450,7 +450,8 @@ public void Close()

if (!MainThreadData.IsMainThread())
{
_logger?.LogError("Calling Close() on Android SDK requires running on MainThread");
_logger?.LogError("Calling Close() on Android SDK requires running on MainThread. " +
"Scope sync thread stopped but Java SDK was not closed.");
return;
}

Expand Down
1 change: 1 addition & 0 deletions test/IntegrationTest/Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ BeforeAll {
Write-Host "Running $Action..."

$extras = @("-e", "test", $Action)

$runResult = Invoke-DeviceApp -ExecutablePath $script:AndroidComponent -Arguments $extras

# Save result to JSON file
Expand Down
Loading