Search in sources :

Example 1 with FOREGROUND_BACKGROUND

use of com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method foreGroundTrace_perfMonDeactivated_traceCreated.

@Test
public void foreGroundTrace_perfMonDeactivated_traceCreated() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // Firebase Performance is deactivated at build time.
    Bundle bundle = new Bundle();
    bundle.putBoolean("firebase_performance_collection_deactivated", true);
    ConfigResolver.getInstance().setMetadataBundle(new ImmutableBundle(bundle));
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    // activity1 goes to background.
    currentTime = 2;
    monitor.onActivityStopped(activity1);
    assertThat(monitor.isForeground()).isFalse();
    // Foreground trace is not created because Performance Monitoring is deactivated at build time.
    verify(transportManager, never()).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
    // Developer enabled Performance Monitoring during runtime.
    ConfigResolver.getInstance().setIsPerformanceCollectionEnabled(true);
    // activity1 comes to foreground.
    currentTime = 3;
    monitor.onActivityResumed(activity1);
    // activity1 goes to background.
    currentTime = 4;
    monitor.onActivityStopped(activity1);
    assertThat(monitor.isForeground()).isFalse();
    // Foreground trace is not created because deactivation takes higher priority.
    verify(transportManager, never()).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) Bundle(android.os.Bundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Test(org.junit.Test)

Example 2 with FOREGROUND_BACKGROUND

use of com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method foregroundBackgroundEvent_activityStateChanges_fgBgEventsCreated.

@Test
public void foregroundBackgroundEvent_activityStateChanges_fgBgEventsCreated() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.incrementCount("counter1", 10);
    monitor.onActivityResumed(activity1);
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    // activity1 goes to background.
    currentTime = 2;
    monitor.incrementCount("counter2", 20);
    monitor.onActivityStopped(activity1);
    Assert.assertEquals(currentTime, monitor.getPauseTime().getMicros());
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    TraceMetric metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.FOREGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(2, metric.getCountersCount());
    Assert.assertEquals(1, metric.getPerfSessionsCount());
    Map<String, Long> counters = metric.getCountersMap();
    Assert.assertEquals(10, (long) counters.get("counter1"));
    Assert.assertEquals(20, (long) counters.get("counter2"));
    Assert.assertEquals(monitor.getResumeTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getResumeTime().getDurationMicros(monitor.getPauseTime()), metric.getDurationUs());
    // Verify bug 36457047 fix, onActivityStopped() is called twice.
    monitor.onActivityStopped(activity1);
    Assert.assertEquals(2, monitor.getPauseTime().getMicros());
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    // log() should NOT be called again on second onActivityStopped() call.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to foreground.
    currentTime = 3;
    monitor.incrementCount("counter3", 30);
    monitor.onActivityResumed(activity1);
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(1, metric.getCountersCount());
    assertThat(metric.getCountersMap()).containsEntry("counter3", 30L);
    Assert.assertEquals(monitor.getPauseTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getPauseTime().getDurationMicros(monitor.getResumeTime()), metric.getDurationUs());
    Assert.assertEquals(1, metric.getPerfSessionsCount());
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Example 3 with FOREGROUND_BACKGROUND

use of com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method updatePerfSession_isAfterSendingForegroundOrBackgroundSession.

@Test
public void updatePerfSession_isAfterSendingForegroundOrBackgroundSession() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    monitor.registerForAppState(SessionManager.getInstance().getAppStateCallback());
    monitor.setStopTime(new Timer(currentTime));
    monitor.setIsColdStart(false);
    // Mandatory due to circular dependencies of singletons AppStateMonitor and SessionManager
    AppStateMonitor.getInstance().setIsColdStart(false);
    // Foreground -> Background, sends _fs
    PerfSession currentSession = SessionManager.getInstance().perfSession().build();
    monitor.onActivityResumed(activity1);
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    PerfSession sentSession = argTraceMetric.getValue().getPerfSessions(0);
    Assert.assertEquals(currentSession, sentSession);
    // Background -> Foreground, sends _bs
    currentSession = SessionManager.getInstance().perfSession().build();
    monitor.onActivityStopped(activity1);
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    sentSession = argTraceMetric.getValue().getPerfSessions(0);
    Assert.assertEquals(currentSession, sentSession);
}
Also used : Timer(com.google.firebase.perf.util.Timer) PerfSession(com.google.firebase.perf.v1.PerfSession) Test(org.junit.Test)

Example 4 with FOREGROUND_BACKGROUND

use of com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method foregroundTrace_perfMonEnabledAtRuntime_traceCreated.

@Test
public void foregroundTrace_perfMonEnabledAtRuntime_traceCreated() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // Firebase Performance is disabled at build time.
    Bundle bundle = new Bundle();
    bundle.putBoolean("firebase_performance_collection_enabled", false);
    ConfigResolver.getInstance().setMetadataBundle(new ImmutableBundle(bundle));
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    // activity1 goes to background.
    currentTime = 2;
    monitor.onActivityStopped(activity1);
    assertThat(monitor.isForeground()).isFalse();
    // Foreground trace is not created because Performance Monitoring is disabled at build time.
    verify(transportManager, never()).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
    // Developer enabled Performance Monitoring during runtime.
    ConfigResolver.getInstance().setIsPerformanceCollectionEnabled(true);
    // activity1 comes to foreground.
    currentTime = 3;
    monitor.onActivityResumed(activity1);
    // Background trace has been created because Performance Monitoring is enabled.
    verify(transportManager, times(1)).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to background.
    currentTime = 4;
    monitor.onActivityStopped(activity1);
    assertThat(monitor.isForeground()).isFalse();
    // Foreground trace has been created because Performance Monitoring is enabled.
    verify(transportManager, times(2)).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
}
Also used : TraceMetric(com.google.firebase.perf.v1.TraceMetric) Bundle(android.os.Bundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Test(org.junit.Test)

Example 5 with FOREGROUND_BACKGROUND

use of com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method testTwoActivities.

@Test
public void testTwoActivities() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    currentTime = 2;
    monitor.onActivityResumed(activity2);
    // second activity becomes visible does not change resumeTime.
    Assert.assertEquals(1, monitor.getResumeTime().getMicros());
    // two activities visible.
    Assert.assertEquals(2, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to background.
    currentTime = 3;
    monitor.onActivityStopped(activity1);
    Assert.assertNull(monitor.getPauseTime());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // activity2 goes to background.
    currentTime = 4;
    monitor.onActivityStopped(activity2);
    // pauseTime updated.
    Assert.assertEquals(4, monitor.getPauseTime().getMicros());
    // no activity visible.
    Assert.assertEquals(0, monitor.getResumed().size());
    Assert.assertFalse(monitor.isForeground());
    // send foreground trace log.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    TraceMetric metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.FOREGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(monitor.getResumeTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getResumeTime().getDurationMicros(monitor.getPauseTime()), metric.getDurationUs());
    // activity1 goes to foreground.
    currentTime = 5;
    monitor.onActivityResumed(activity1);
    // resumeTime updated.
    Assert.assertEquals(currentTime, monitor.getResumeTime().getMicros());
    Assert.assertEquals(1, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    // send background trace.
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    metric = argTraceMetric.getValue();
    Assert.assertEquals(Constants.TraceNames.BACKGROUND_TRACE_NAME.toString(), metric.getName());
    Assert.assertEquals(monitor.getPauseTime().getMicros(), metric.getClientStartTimeUs());
    Assert.assertEquals(monitor.getPauseTime().getDurationMicros(monitor.getResumeTime()), metric.getDurationUs());
    // activity2 goes to foreground.
    currentTime = 6;
    monitor.onActivityResumed(activity2);
    // resumeTime does not change because this is second activity becomes visible.
    Assert.assertEquals(5, monitor.getResumeTime().getMicros());
    // two activities are visible.
    Assert.assertEquals(2, monitor.getResumed().size());
    Assert.assertTrue(monitor.isForeground());
    // no new event log.
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) TraceMetric(com.google.firebase.perf.v1.TraceMetric) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 TraceMetric (com.google.firebase.perf.v1.TraceMetric)8 Bundle (android.os.Bundle)5 ImmutableBundle (com.google.firebase.perf.util.ImmutableBundle)5 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)4 NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)1 Trace (com.google.firebase.perf.metrics.Trace)1 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)1 TransportManager (com.google.firebase.perf.transport.TransportManager)1 Timer (com.google.firebase.perf.util.Timer)1 PerfSession (com.google.firebase.perf.v1.PerfSession)1