Search in sources :

Example 1 with DeviceCacheManager

use of com.google.firebase.perf.config.DeviceCacheManager in project firebase-android-sdk by firebase.

the class FirebasePerformanceTest method initializeFirebasePerformancePreferences.

/**
 * Creates a FirebasePerformance instance in a way that imitates the given manifest metadata or
 * sharedPreferences are present.
 *
 * @param metadataFireperfForceDeactivatedKey The value for the metadata key {@link
 *     #FIREPERF_FORCE_DEACTIVATED_KEY}. If null is passed, this key value pair will not be added
 *     to the bundle.
 * @param metadataFireperfEnabledKey The value for the metadata key {@link #FIREPERF_ENABLED_KEY}.
 *     If null is passed, this key value pair will not be added to the bundle.
 * @param sharedPreferencesEnabledDisabledKey The value for the sharedPreferences key {@link
 *     Constants#ENABLE_DISABLE}. If null is passed, this key value pair will not be added to the
 *     sharedPreferences.
 * @param firebaseRemoteConfigProvider The {@link Provider} for FirebaseRemoteConfig instance.
 * @return The FirebasePerformance instance initialized as if the given manifest metadata or
 *     sharedPreferences are present.
 */
private FirebasePerformance initializeFirebasePerformancePreferences(Boolean metadataFireperfForceDeactivatedKey, Boolean metadataFireperfEnabledKey, Boolean sharedPreferencesEnabledDisabledKey, Provider<RemoteConfigComponent> firebaseRemoteConfigProvider, Provider<TransportFactory> transportFactoryProvider) {
    DeviceCacheManager deviceCacheManager = new DeviceCacheManager(fakeDirectExecutorService);
    deviceCacheManager.setContext(ApplicationProvider.getApplicationContext());
    if (sharedPreferencesEnabledDisabledKey != null) {
        deviceCacheManager.setValue(Constants.ENABLE_DISABLE, sharedPreferencesEnabledDisabledKey);
    }
    spyConfigResolver.setDeviceCacheManager(deviceCacheManager);
    Bundle bundle = new Bundle();
    if (metadataFireperfEnabledKey != null) {
        bundle.putBoolean(FIREPERF_ENABLED_KEY, metadataFireperfEnabledKey);
    }
    if (metadataFireperfForceDeactivatedKey != null) {
        bundle.putBoolean(FIREPERF_FORCE_DEACTIVATED_KEY, metadataFireperfForceDeactivatedKey);
    }
    Context context = ApplicationProvider.getApplicationContext();
    shadowOf(context.getPackageManager()).getInternalMutablePackageInfo(context.getPackageName()).applicationInfo.metaData.putAll(bundle);
    return new FirebasePerformance(FirebaseApp.getInstance(), firebaseRemoteConfigProvider, mock(FirebaseInstallationsApi.class), transportFactoryProvider, spyRemoteConfigManager, spyConfigResolver, spySessionManager);
}
Also used : Context(android.content.Context) Bundle(android.os.Bundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FirebaseInstallationsApi(com.google.firebase.installations.FirebaseInstallationsApi)

Example 2 with DeviceCacheManager

use of com.google.firebase.perf.config.DeviceCacheManager in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method screenTrace_perfMonDeactivated_traceNotCreated.

@Test
public void screenTrace_perfMonDeactivated_traceNotCreated() {
    AppStateMonitor monitor = new AppStateMonitor(transportManager, clock);
    Activity activityWithNonHardwareAcceleratedView = createFakeActivity(/* isHardwareAccelerated =*/
    true);
    ConfigResolver configResolver = ConfigResolver.getInstance();
    configResolver.setDeviceCacheManager(new DeviceCacheManager(new FakeDirectExecutorService()));
    Bundle bundle = new Bundle();
    bundle.putBoolean("firebase_performance_collection_deactivated", true);
    configResolver.setMetadataBundle(new ImmutableBundle(bundle));
    // Developer has enabled Performance Monitoring during runtime.
    configResolver.setIsPerformanceCollectionEnabled(true);
    // Assert that screen trace has not been created.
    monitor.onActivityStarted(activityWithNonHardwareAcceleratedView);
    assertThat(monitor.getActivity2ScreenTrace()).isEmpty();
    // Confirm that this doesn't throw an exception.
    monitor.onActivityStopped(activityWithNonHardwareAcceleratedView);
}
Also used : ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Bundle(android.os.Bundle) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Activity(android.app.Activity) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Test(org.junit.Test)

Example 3 with DeviceCacheManager

use of com.google.firebase.perf.config.DeviceCacheManager in project firebase-android-sdk by firebase.

the class AppStateMonitorTest method setUp.

@Before
public void setUp() {
    currentTime = 0;
    initMocks(this);
    doAnswer((Answer<Timer>) invocationOnMock -> new Timer(currentTime)).when(clock).getTime();
    activity1 = createFakeActivity(/* isHardwareAccelerated= */
    true);
    activity2 = createFakeActivity(/* isHardwareAccelerated= */
    true);
    DeviceCacheManager.clearInstance();
    ConfigResolver.clearInstance();
    ConfigResolver configResolver = ConfigResolver.getInstance();
    configResolver.setDeviceCacheManager(new DeviceCacheManager(new FakeDirectExecutorService()));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Context(android.content.Context) TraceMetric(com.google.firebase.perf.v1.TraceMetric) ActivityController(org.robolectric.android.controller.ActivityController) Bundle(android.os.Bundle) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) ImmutableBundle(com.google.firebase.perf.util.ImmutableBundle) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Captor(org.mockito.Captor) Answer(org.mockito.stubbing.Answer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) Constants(com.google.firebase.perf.util.Constants) PerfSession(com.google.firebase.perf.v1.PerfSession) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) WeakReference(java.lang.ref.WeakReference) FirebasePerformanceTestBase(com.google.firebase.perf.FirebasePerformanceTestBase) GaugeManager(com.google.firebase.perf.session.gauges.GaugeManager) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Before(org.junit.Before) FirebasePerformanceInitializer(com.google.firebase.perf.FirebasePerformanceInitializer) Timer(com.google.firebase.perf.util.Timer) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) FOREGROUND_BACKGROUND(com.google.firebase.perf.v1.ApplicationProcessState.FOREGROUND_BACKGROUND) Clock(com.google.firebase.perf.util.Clock) Robolectric(org.robolectric.Robolectric) Trace(com.google.firebase.perf.metrics.Trace) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) TransportManager(com.google.firebase.perf.transport.TransportManager) Mockito.verify(org.mockito.Mockito.verify) SessionManager(com.google.firebase.perf.session.SessionManager) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) Mockito.never(org.mockito.Mockito.never) Application(android.app.Application) LayoutParams(android.view.WindowManager.LayoutParams) Assert(org.junit.Assert) Activity(android.app.Activity) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Timer(com.google.firebase.perf.util.Timer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) Before(org.junit.Before)

Example 4 with DeviceCacheManager

use of com.google.firebase.perf.config.DeviceCacheManager in project firebase-android-sdk by firebase.

the class PerfSessionTest method setUp.

@Before
public void setUp() {
    initMocks(this);
    DeviceCacheManager.clearInstance();
    ConfigResolver.clearInstance();
    appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().commit();
    ConfigResolver configResolver = ConfigResolver.getInstance();
    configResolver.setApplicationContext(appContext);
    configResolver.setDeviceCacheManager(new DeviceCacheManager(new FakeDirectExecutorService()));
}
Also used : ConfigResolver(com.google.firebase.perf.config.ConfigResolver) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) Before(org.junit.Before)

Example 5 with DeviceCacheManager

use of com.google.firebase.perf.config.DeviceCacheManager in project firebase-android-sdk by firebase.

the class TraceTest method setUp.

@Before
public void setUp() {
    currentTime = 1;
    initMocks(this);
    doAnswer((Answer<Timer>) invocationOnMock -> new Timer(currentTime)).when(mockClock).getTime();
    arguments = ArgumentCaptor.forClass(TraceMetric.class);
    DeviceCacheManager.clearInstance();
    ConfigResolver.clearInstance();
    appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit().clear().commit();
    ConfigResolver configResolver = ConfigResolver.getInstance();
    configResolver.setDeviceCacheManager(new DeviceCacheManager(new FakeDirectExecutorService()));
    configResolver.setApplicationContext(appContext);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) Context(android.content.Context) TraceMetric(com.google.firebase.perf.v1.TraceMetric) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ArgumentMatchers.nullable(org.mockito.ArgumentMatchers.nullable) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Random(java.util.Random) PREFS_NAME(com.google.firebase.perf.util.Constants.PREFS_NAME) Parcel(android.os.Parcel) AppStateMonitor(com.google.firebase.perf.application.AppStateMonitor) Answer(org.mockito.stubbing.Answer) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) Constants(com.google.firebase.perf.util.Constants) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) PerfSession(com.google.firebase.perf.session.PerfSession) FirebasePerformanceTestBase(com.google.firebase.perf.FirebasePerformanceTestBase) GaugeManager(com.google.firebase.perf.session.gauges.GaugeManager) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Before(org.junit.Before) Timer(com.google.firebase.perf.util.Timer) Clock(com.google.firebase.perf.util.Clock) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) TransportManager(com.google.firebase.perf.transport.TransportManager) Mockito.verify(org.mockito.Mockito.verify) SessionManager(com.google.firebase.perf.session.SessionManager) RobolectricTestRunner(org.robolectric.RobolectricTestRunner) Mockito.never(org.mockito.Mockito.never) Mockito.mock(org.mockito.Mockito.mock) ConfigResolver(com.google.firebase.perf.config.ConfigResolver) Timer(com.google.firebase.perf.util.Timer) TraceMetric(com.google.firebase.perf.v1.TraceMetric) DeviceCacheManager(com.google.firebase.perf.config.DeviceCacheManager) FakeDirectExecutorService(com.google.testing.timing.FakeDirectExecutorService) Before(org.junit.Before)

Aggregations

DeviceCacheManager (com.google.firebase.perf.config.DeviceCacheManager)5 ConfigResolver (com.google.firebase.perf.config.ConfigResolver)4 FakeDirectExecutorService (com.google.testing.timing.FakeDirectExecutorService)4 Context (android.content.Context)3 Bundle (android.os.Bundle)3 ImmutableBundle (com.google.firebase.perf.util.ImmutableBundle)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Activity (android.app.Activity)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 FirebasePerformanceTestBase (com.google.firebase.perf.FirebasePerformanceTestBase)2 SessionManager (com.google.firebase.perf.session.SessionManager)2 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)2 TransportManager (com.google.firebase.perf.transport.TransportManager)2 Clock (com.google.firebase.perf.util.Clock)2 Constants (com.google.firebase.perf.util.Constants)2 Timer (com.google.firebase.perf.util.Timer)2 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)2 TraceMetric (com.google.firebase.perf.v1.TraceMetric)2 RunWith (org.junit.runner.RunWith)2