Skip to content

Commit

Permalink
feat(session)End to end test
Browse files Browse the repository at this point in the history
  • Loading branch information
YYChen01988 committed Apr 8, 2024
1 parent ae00e42 commit f348f0c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.bugsnag.android.internal.TaskType;

import android.app.Activity;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -104,18 +105,16 @@ private boolean shouldDiscardSession(boolean autoCaptured) {
return true;
} else {
Session existingSession = currentSession;
if (autoCaptured &&
existingSession != null &&
!existingSession.isAutoCaptured() &&
shouldSuppressFirstAutoSession) {

if (autoCaptured
&& existingSession != null
&& !existingSession.isAutoCaptured()
&& shouldSuppressFirstAutoSession) {
shouldSuppressFirstAutoSession = true;
return true;
}
}

return false;
}
}


void pauseSession() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MazerunnerApp : Application() {
super.onCreate()
triggerStartupAnrIfRequired()
setupNonSdkUsageStrictMode()
triggerManualSessionIfRequired()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.bugsnag.android.mazerunner

import android.app.Application
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.EndpointConfiguration

fun Application.triggerManualSessionIfRequired() {
val prefs = getSharedPreferences("SessionPreferences", android.content.Context.MODE_PRIVATE)
val manualSession = prefs.getBoolean("manualSession", false)

if (manualSession) {
val notifyEndpoint = prefs.getString("notify", null)
val sessionsEndpoint = prefs.getString("sessions", null)

// we remove the preferences so that we don't affect any future startup
prefs.edit()
.remove("notify")
.remove("sessions")
.commit()

// we have to startup Bugsnag at this point
val config = Configuration.load(this)
if (!notifyEndpoint.isNullOrBlank() && !sessionsEndpoint.isNullOrBlank()) {
config.endpoints = EndpointConfiguration(notifyEndpoint, sessionsEndpoint)
}

Bugsnag.start(this, config)
Bugsnag.startSession()
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
package com.bugsnag.android.mazerunner.scenarios

import android.content.Context
import android.content.Intent
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.createDefaultDelivery
import com.bugsnag.android.mazerunner.InterceptingDelivery
import kotlin.system.exitProcess

/**
* Sends an automated session payload to Bugsnag.
*/
internal class StartSessionAutoModeScenario(
config: Configuration,
context: Context,
eventMetadata: String?
) : Scenario(config, context, eventMetadata) {
override fun startScenario() {
context.applicationContext
.getSharedPreferences("SessionPreferences", Context.MODE_PRIVATE)
.edit()
.putBoolean("manualSession", MANUAL_START)
.putString("notify", config.endpoints.notify)
.putString("sessions", config.endpoints.sessions)
.commit()

init {
val baseDelivery = createDefaultDelivery()
var intercept = true
config.autoTrackSessions = true
config.delivery = InterceptingDelivery(baseDelivery) {
if (intercept) {
intercept = false
continueScenario()
}
}
exitProcess(0)
}

private fun continueScenario() {
Bugsnag.startSession()
registerActivityLifecycleCallbacks()
context.startActivity(Intent("com.bugsnag.android.mazerunner.UPDATE_CONTEXT"))
companion object {
private const val MANUAL_START = true
}
}
6 changes: 4 additions & 2 deletions features/smoke_tests/03_sessions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,7 @@ Feature: Session functionality smoke tests
And the event "user.name" equals "ManualSessionSmokeScenario"

Scenario: Start session in auto mode
When I run "StartSessionAutoModeScenario"
And I wait to receive a session
When I clear any error dialogue
And I run "StartSessionAutoModeScenario"
And I relaunch the app after a crash
Then I wait to receive a session

0 comments on commit f348f0c

Please sign in to comment.