Search in sources :

Example 6 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 7 with FOREGROUND_BACKGROUND

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

the class AppStateMonitorTest method backgroundTrace_perfMonDeactivated_traceCreated.

@Test
public void backgroundTrace_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 background.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    monitor.onActivityStopped(activity1);
    // activity1 goes to foreground.
    currentTime = 2;
    monitor.onActivityResumed(activity1);
    assertThat(monitor.isForeground()).isTrue();
    // Background 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 background.
    currentTime = 3;
    monitor.onActivityStopped(activity1);
    // activity1 goes to foreground.
    currentTime = 4;
    monitor.onActivityResumed(activity1);
    assertThat(monitor.isForeground()).isTrue();
    // Background 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 8 with FOREGROUND_BACKGROUND

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

the class AppStateMonitorTest method testAppStateCallbackWithNetworkRequestMetricBuilder.

@Test
public void testAppStateCallbackWithNetworkRequestMetricBuilder() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    // registerForAppState() is called by NetworkRequestMetricBuilder's constructor.
    NetworkRequestMetricBuilder builder = new NetworkRequestMetricBuilder(mock(TransportManager.class), monitor, mock(GaugeManager.class));
    Assert.assertEquals(ApplicationProcessState.BACKGROUND, builder.getAppState());
    // activity1 comes to foreground.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    Assert.assertTrue(monitor.isForeground());
    // builder is updated through AppStateCallback.
    Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    // activity1 goes to background.
    currentTime = 2;
    monitor.onActivityStopped(activity1);
    Assert.assertFalse(monitor.isForeground());
    // Foreground session trace.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // builder is updated again.
    Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
    // unregisterForAppState() is called by NetworkRequestMetricBuilder.build().
    builder.build();
    Assert.assertEquals(FOREGROUND_BACKGROUND, builder.getAppState());
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) GaugeManager(com.google.firebase.perf.session.gauges.GaugeManager) TransportManager(com.google.firebase.perf.transport.TransportManager) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) Test(org.junit.Test)

Example 9 with FOREGROUND_BACKGROUND

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

the class AppStateMonitorTest method backgroundTrace_perfMonEnabledAtRuntime_traceCreated.

@Test
public void backgroundTrace_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 background.
    currentTime = 1;
    monitor.onActivityResumed(activity1);
    monitor.onActivityStopped(activity1);
    // activity1 goes to foreground.
    currentTime = 2;
    monitor.onActivityResumed(activity1);
    assertThat(monitor.isForeground()).isTrue();
    // Background 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 background.
    currentTime = 3;
    monitor.onActivityStopped(activity1);
    // Foreground trace is created because Performance Monitoring is enabled.
    verify(transportManager, times(1)).log(any(TraceMetric.class), eq(FOREGROUND_BACKGROUND));
    // activity1 goes to foreground.
    currentTime = 4;
    monitor.onActivityResumed(activity1);
    assertThat(monitor.isForeground()).isTrue();
    // Background trace is 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 10 with FOREGROUND_BACKGROUND

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

the class AppStateMonitorTest method testAppStateCallbackWithTrace.

@Test
public void testAppStateCallbackWithTrace() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    Trace trace = new Trace("TRACE_1", transportManager, clock, monitor);
    // Trace is not started yet, default state is APPLICATION_PROCESS_STATE_UNKNOWN
    Assert.assertEquals(ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN, trace.getAppState());
    // activity1 comes to foreground.
    currentTime = 1;
    // registerForAppState() is called by Trace.start().
    trace.start();
    // Trace started, get state from AppStateMonitor.
    Assert.assertEquals(ApplicationProcessState.BACKGROUND, trace.getAppState());
    monitor.onActivityResumed(activity1);
    Assert.assertTrue(monitor.isForeground());
    Assert.assertEquals(FOREGROUND_BACKGROUND, trace.getAppState());
    verify(transportManager, times(0)).log(argTraceMetric.capture(), nullable(ApplicationProcessState.class));
    // activity1 goes to background.
    currentTime = 2;
    monitor.onActivityStopped(activity1);
    Assert.assertFalse(monitor.isForeground());
    // Foreground session trace.
    verify(transportManager, times(1)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    // Trace is updated through AppStatCallback.
    Assert.assertEquals(FOREGROUND_BACKGROUND, trace.getAppState());
    // unregisterForAppState() is called by Trace.stop()
    trace.stop();
    // trace has been through FOREGROUND_BACKGROUND
    Assert.assertEquals(FOREGROUND_BACKGROUND, trace.getAppState());
    // a TraceMetric is sent for this trace object.
    verify(transportManager, times(2)).log(argTraceMetric.capture(), eq(FOREGROUND_BACKGROUND));
    TraceMetric metric = argTraceMetric.getValue();
    Assert.assertEquals("TRACE_1", metric.getName());
}
Also used : Trace(com.google.firebase.perf.metrics.Trace) 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