Skip to content

Commit

Permalink
update test scenarios to include auto session tracking, rework how bu…
Browse files Browse the repository at this point in the history
…gsnag is initialised to allow greater configurability from test harness
  • Loading branch information
fractalwrench committed Feb 22, 2018
1 parent 2634763 commit 2c60c8f
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 14 deletions.
15 changes: 15 additions & 0 deletions features/auto_session.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Feature: Android support

Scenario: Automatic Session Tracking
When I run "AutoSessionScenario" with the defaults
And I wait for 1 seconds
Then I should receive a request
And the request is a valid for the session tracking API
And the "Bugsnag-API-Key" header equals "a35a2a72bd230ac0aa0f52715bbdc6aa"
And the payload field "notifier.name" equals "Android Bugsnag Notifier"
And the payload field "sessions" is an array with 1 element
And the session "user.id" equals "123"
And the session "user.email" equals "user@example.com"
And the session "user.name" equals "Joe Bloggs"
And the session "id" is not null
And the session "startedAt" is not null
2 changes: 1 addition & 1 deletion features/manual_session.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Android support

Scenario: Test handled Android Exception
Scenario: Manual Session Tracking
When I run "ManualSessionScenario" with the defaults
Then I should receive a request
And the request is a valid for the session tracking API
Expand Down
1 change: 1 addition & 0 deletions mazerunner/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

</intent-filter>
</activity>
<activity android:name=".SecondActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.util.Log
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration

class MainActivity : Activity() {
Expand All @@ -18,7 +17,6 @@ class MainActivity : Activity() {

override fun onResume() {
super.onResume()
initialiseBugsnag()
enqueueTestCase()
}

Expand All @@ -35,18 +33,16 @@ class MainActivity : Activity() {
private fun executeTestCase() {
val eventType = intent.getStringExtra("EVENT_TYPE")
Log.d("Bugsnag", "Received test case, executing " + eventType)
val testCase = factory.testCaseForName(eventType)
val testCase = factory.testCaseForName(eventType, prepareConfig(), this)
testCase.run()
}

private fun initialiseBugsnag() {
private fun prepareConfig(): Configuration {
val config = Configuration(intent.getStringExtra("BUGSNAG_API_KEY"))
val port = intent.getStringExtra("BUGSNAG_PORT")
config.endpoint = "${findHostname()}:$port"
config.sessionEndpoint = "${findHostname()}:$port"

Bugsnag.init(this, config)
Bugsnag.setLoggingEnabled(true)
return config
}

private fun findHostname(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bugsnag.android.mazerunner

import android.app.Activity
import android.os.Bundle

class SecondActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.bugsnag.android.mazerunner

import android.content.Context
import com.bugsnag.android.Configuration
import com.bugsnag.android.mazerunner.scenarios.Scenario

internal class TestCaseFactory {

fun testCaseForName(eventType: String?): Scenario {
fun testCaseForName(eventType: String?, config: Configuration, context: Context): Scenario {
val clz = Class.forName("com.bugsnag.android.mazerunner.scenarios.$eventType")
return clz.newInstance() as Scenario
val constructor = clz.constructors[0]
return constructor.newInstance(config, context) as Scenario
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.flushAllSessions
import com.bugsnag.android.mazerunner.SecondActivity

/**
* Sends a manual session payload to Bugsnag.
*/
internal class AutoSessionScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
config.setAutoCaptureSessions(true)
super.run()
Bugsnag.setUser("123", "user@example.com", "Joe Bloggs")
context.startActivity(Intent(context, SecondActivity::class.java))
flushAllSessions()
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.bugsnag.android.mazerunner.scenarios

import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration

/**
* Sends a handled exception to Bugsnag, which does not include session data.
*/
internal class HandledExceptionScenario : Scenario() {
internal class HandledExceptionScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
super.run()
Bugsnag.notify(generateException())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.bugsnag.android.mazerunner.scenarios

import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.flushAllSessions

/**
* Sends a manual session payload to Bugsnag.
*/
internal class ManualSessionScenario : Scenario() {
internal class ManualSessionScenario(config: Configuration,
context: Context) : Scenario(config, context) {

override fun run() {
super.run()
Bugsnag.setUser("123", "user@example.com", "Joe Bloggs")
Bugsnag.startSession()
flushAllSessions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.bugsnag.android.mazerunner.scenarios

import android.content.Context
import com.bugsnag.android.Bugsnag
import com.bugsnag.android.Configuration
import com.bugsnag.android.NetworkException

abstract internal class Scenario {
abstract internal class Scenario(protected val config: Configuration,
protected val context: Context) {

abstract fun run()
open fun run() {
Bugsnag.init(context, config)
Bugsnag.setLoggingEnabled(true)
}

/**
* Sets a NOP implementation for the Session Tracking API, preventing delivery
Expand Down

0 comments on commit 2c60c8f

Please sign in to comment.