Skip to content

Commit 9289aa7

Browse files
committed
Make tests happy
1 parent 7c4b39a commit 9289aa7

File tree

4 files changed

+11
-150
lines changed

4 files changed

+11
-150
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void onAppStartSpansSent() {
155155
}
156156

157157
public boolean shouldSendStartMeasurements() {
158-
return shouldSendStartMeasurements;
158+
return shouldSendStartMeasurements && appLaunchedInForeground;
159159
}
160160

161161
public void restartAppStart(final long uptimeMillis) {
@@ -344,7 +344,7 @@ public void onActivityDestroyed(@NonNull Activity activity) {
344344
// as the next Activity is considered like a new app start
345345
if (remainingActivities == 0 && !activity.isChangingConfigurations()) {
346346
appLaunchedInForeground = false;
347-
shouldSendStartMeasurements = true;
347+
shouldSendStartMeasurements = false;
348348
firstDrawDone.set(false);
349349
}
350350
}

sentry-android-core/src/test/java/io/sentry/android/core/ActivityLifecycleIntegrationTest.kt

Lines changed: 7 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import org.robolectric.shadow.api.Shadow
5454
import org.robolectric.shadows.ShadowActivityManager
5555
import java.util.Date
5656
import java.util.concurrent.Future
57-
import java.util.concurrent.TimeUnit
5857
import kotlin.test.AfterTest
5958
import kotlin.test.BeforeTest
6059
import kotlin.test.Test
@@ -94,7 +93,10 @@ class ActivityLifecycleIntegrationTest {
9493

9594
whenever(hub.options).thenReturn(options)
9695

97-
AppStartMetrics.getInstance().isAppLaunchedInForeground = true
96+
val metrics = AppStartMetrics.getInstance()
97+
metrics.isAppLaunchedInForeground = true
98+
metrics.appStartTimeSpan.start()
99+
98100
// We let the ActivityLifecycleIntegration create the proper transaction here
99101
val optionCaptor = argumentCaptor<TransactionOptions>()
100102
val contextCaptor = argumentCaptor<TransactionContext>()
@@ -594,45 +596,6 @@ class ActivityLifecycleIntegrationTest {
594596
verify(ttfdReporter, never()).registerFullyDrawnListener(any())
595597
}
596598

597-
@Test
598-
fun `App start is Cold when savedInstanceState is null`() {
599-
val sut = fixture.getSut()
600-
fixture.options.tracesSampleRate = 1.0
601-
sut.register(fixture.hub, fixture.options)
602-
603-
val activity = mock<Activity>()
604-
sut.onActivityCreated(activity, null)
605-
606-
assertEquals(AppStartType.COLD, AppStartMetrics.getInstance().appStartType)
607-
}
608-
609-
@Test
610-
fun `App start is Warm when savedInstanceState is not null`() {
611-
val sut = fixture.getSut()
612-
fixture.options.tracesSampleRate = 1.0
613-
sut.register(fixture.hub, fixture.options)
614-
615-
val activity = mock<Activity>()
616-
val bundle = Bundle()
617-
sut.onActivityCreated(activity, bundle)
618-
619-
assertEquals(AppStartType.WARM, AppStartMetrics.getInstance().appStartType)
620-
}
621-
622-
@Test
623-
fun `Do not overwrite App start type after set`() {
624-
val sut = fixture.getSut()
625-
fixture.options.tracesSampleRate = 1.0
626-
sut.register(fixture.hub, fixture.options)
627-
628-
val activity = mock<Activity>()
629-
val bundle = Bundle()
630-
sut.onActivityCreated(activity, bundle)
631-
sut.onActivityCreated(activity, null)
632-
633-
assertEquals(AppStartType.WARM, AppStartMetrics.getInstance().appStartType)
634-
}
635-
636599
@Test
637600
fun `When firstActivityCreated is false, start transaction with given appStartTime`() {
638601
val sut = fixture.getSut()
@@ -883,86 +846,6 @@ class ActivityLifecycleIntegrationTest {
883846
)
884847
}
885848

886-
@Test
887-
fun `When firstActivityCreated is false and bundle is not null, start app start warm span with given appStartTime`() {
888-
val sut = fixture.getSut()
889-
fixture.options.tracesSampleRate = 1.0
890-
sut.register(fixture.hub, fixture.options)
891-
sut.setFirstActivityCreated(false)
892-
893-
val date = SentryNanotimeDate(Date(1), 0)
894-
setAppStartTime(date)
895-
896-
val activity = mock<Activity>()
897-
sut.onActivityCreated(activity, fixture.bundle)
898-
899-
val span = fixture.transaction.children.first()
900-
assertEquals(span.operation, "app.start.warm")
901-
assertEquals(span.description, "Warm Start")
902-
assertEquals(span.startDate.nanoTimestamp(), date.nanoTimestamp())
903-
}
904-
905-
@Test
906-
fun `When firstActivityCreated is false and bundle is not null, start app start cold span with given appStartTime`() {
907-
val sut = fixture.getSut()
908-
fixture.options.tracesSampleRate = 1.0
909-
sut.register(fixture.hub, fixture.options)
910-
sut.setFirstActivityCreated(false)
911-
912-
val date = SentryNanotimeDate(Date(1), 0)
913-
setAppStartTime(date)
914-
915-
val activity = mock<Activity>()
916-
sut.onActivityCreated(activity, null)
917-
918-
val span = fixture.transaction.children.first()
919-
assertEquals(span.operation, "app.start.cold")
920-
assertEquals(span.description, "Cold Start")
921-
assertEquals(span.startDate.nanoTimestamp(), date.nanoTimestamp())
922-
}
923-
924-
@Test
925-
fun `When firstActivityCreated is false and app started more than 1 minute ago, start app with Warm start`() {
926-
val sut = fixture.getSut()
927-
fixture.options.tracesSampleRate = 1.0
928-
sut.register(fixture.hub, fixture.options)
929-
sut.setFirstActivityCreated(false)
930-
931-
val date = SentryNanotimeDate(Date(1), 0)
932-
val duration = TimeUnit.MINUTES.toMillis(1) + 2
933-
val durationNanos = TimeUnit.MILLISECONDS.toNanos(duration)
934-
val stopDate = SentryNanotimeDate(Date(duration), durationNanos)
935-
setAppStartTime(date, stopDate)
936-
937-
val activity = mock<Activity>()
938-
sut.onActivityCreated(activity, null)
939-
940-
val span = fixture.transaction.children.first()
941-
assertEquals(span.operation, "app.start.warm")
942-
assertEquals(span.description, "Warm Start")
943-
assertEquals(span.startDate.nanoTimestamp(), date.nanoTimestamp())
944-
}
945-
946-
@Test
947-
fun `When firstActivityCreated is false and app started in background, start app with Warm start`() {
948-
val sut = fixture.getSut()
949-
AppStartMetrics.getInstance().isAppLaunchedInForeground = false
950-
fixture.options.tracesSampleRate = 1.0
951-
sut.register(fixture.hub, fixture.options)
952-
sut.setFirstActivityCreated(false)
953-
954-
val date = SentryNanotimeDate(Date(1), 0)
955-
setAppStartTime(date)
956-
957-
val activity = mock<Activity>()
958-
sut.onActivityCreated(activity, null)
959-
960-
val span = fixture.transaction.children.first()
961-
assertEquals(span.operation, "app.start.warm")
962-
assertEquals(span.description, "Warm Start")
963-
assertEquals(span.startDate.nanoTimestamp(), date.nanoTimestamp())
964-
}
965-
966849
@Test
967850
fun `When firstActivityCreated is true, start transaction but not with given appStartTime`() {
968851
val sut = fixture.getSut()
@@ -1467,7 +1350,6 @@ class ActivityLifecycleIntegrationTest {
14671350
assertEquals(startDate.nanoTimestamp(), sut.getProperty<SentryDate>("lastPausedTime").nanoTimestamp())
14681351

14691352
sut.onActivityCreated(activity, null)
1470-
assertNotNull(sut.appStartSpan)
14711353

14721354
sut.onActivityPostCreated(activity, null)
14731355
assertTrue(activityLifecycleSpan.onCreate.hasStopped())
@@ -1556,15 +1438,6 @@ class ActivityLifecycleIntegrationTest {
15561438
// lastPausedUptimeMillis is set to current SystemClock.uptimeMillis()
15571439
val lastUptimeMillis = sut.getProperty<Long>("lastPausedUptimeMillis")
15581440
assertNotEquals(0, lastUptimeMillis)
1559-
1560-
sut.onActivityCreated(activity, null)
1561-
// AppStartMetrics app start time is set to Activity preCreated timestamp
1562-
assertEquals(lastUptimeMillis, appStartMetrics.appStartTimeSpan.startUptimeMs)
1563-
// AppStart type is considered warm
1564-
assertEquals(AppStartType.WARM, appStartMetrics.appStartType)
1565-
1566-
// Activity appStart span timestamp is the same of AppStartMetrics.appStart timestamp
1567-
assertEquals(sut.appStartSpan!!.startDate.nanoTimestamp(), appStartMetrics.getAppStartTimeSpanWithFallback(fixture.options).startTimestamp!!.nanoTimestamp())
15681441
}
15691442

15701443
private fun SentryTracer.isFinishing() = getProperty<Any>("finishStatus").getProperty<Boolean>("isFinishing")
@@ -1578,6 +1451,9 @@ class ActivityLifecycleIntegrationTest {
15781451

15791452
private fun setAppStartTime(date: SentryDate = SentryNanotimeDate(Date(1), 0), stopDate: SentryDate? = null) {
15801453
// set by SentryPerformanceProvider so forcing it here
1454+
AppStartMetrics.getInstance().appStartType = AppStartType.COLD
1455+
AppStartMetrics.getInstance().isAppLaunchedInForeground = true
1456+
15811457
val sdkAppStartTimeSpan = AppStartMetrics.getInstance().sdkInitTimeSpan
15821458
val appStartTimeSpan = AppStartMetrics.getInstance().appStartTimeSpan
15831459
val millis = DateUtils.nanosToMillis(date.nanoTimestamp().toDouble()).toLong()

sentry-android-core/src/test/java/io/sentry/android/core/PerformanceAndroidEventProcessorTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class PerformanceAndroidEventProcessorTest {
7575
null,
7676
null
7777
).also {
78-
AppStartMetrics.getInstance().onActivityCreated(mock(), mock())
78+
AppStartMetrics.getInstance().onActivityCreated(mock(), if (coldStart) null else mock())
7979
}
8080

8181
@BeforeTest
@@ -225,6 +225,7 @@ class PerformanceAndroidEventProcessorTest {
225225
fun `adds app start metrics to app start txn`() {
226226
// given some app start metrics
227227
val appStartMetrics = AppStartMetrics.getInstance()
228+
appStartMetrics.isAppLaunchedInForeground = true
228229
appStartMetrics.appStartType = AppStartType.COLD
229230
appStartMetrics.appStartTimeSpan.setStartedAt(123)
230231
appStartMetrics.appStartTimeSpan.setStoppedAt(456)

sentry-android-core/src/test/java/io/sentry/android/core/performance/AppStartMetricsTest.kt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,6 @@ class AppStartMetricsTest {
144144
assertFalse(timeSpan.hasStarted())
145145
}
146146

147-
@Test
148-
fun `if app is launched in background with perfV2, appStartTimeSpanWithFallback returns an empty span`() {
149-
val appStartTimeSpan = AppStartMetrics.getInstance().appStartTimeSpan
150-
appStartTimeSpan.start()
151-
assertTrue(appStartTimeSpan.hasStarted())
152-
AppStartMetrics.getInstance().isAppLaunchedInForeground = false
153-
AppStartMetrics.getInstance().onActivityCreated(mock(), mock())
154-
155-
val options = SentryAndroidOptions().apply {
156-
isEnablePerformanceV2 = true
157-
}
158-
159-
val timeSpan = AppStartMetrics.getInstance().getAppStartTimeSpanWithFallback(options)
160-
assertFalse(timeSpan.hasStarted())
161-
}
162-
163147
@Test
164148
fun `if app start span is at most 1 minute, appStartTimeSpanWithFallback returns the app start span`() {
165149
val appStartTimeSpan = AppStartMetrics.getInstance().appStartTimeSpan

0 commit comments

Comments
 (0)