Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.google.firebase.crashlytics.internal.analytics.AnalyticsEventLogger;
import com.google.firebase.crashlytics.internal.analytics.AnalyticsEventReceiver;
import com.google.firebase.crashlytics.internal.breadcrumbs.BreadcrumbSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -52,14 +53,20 @@ public class CrashlyticsAnalyticsListenerTest {
@Mock private AnalyticsEventLogger analyticsEventLogger;

private CrashlyticsAnalyticsListener listener;
private AutoCloseable mocks;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);

listener = new CrashlyticsAnalyticsListener();
}

@After
public void tearDown() throws Exception {
mocks.close();
}

@Test
public void testListenerCallsBreadcrumbReceiverWithEmptyParamsIfParamsIsNull() {
listener.setCrashlyticsOriginEventReceiver(crashlyticsOriginEventReceiver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import static org.mockito.Mockito.mock;

import androidx.annotation.NonNull;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.firebase.crashlytics.internal.model.StaticSessionData;
import com.google.firebase.inject.Deferred;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -35,10 +36,16 @@ public class CrashlyticsNativeComponentDeferredProxyTest {
private static final long TEST_START_TIME = 123;

@Mock private CrashlyticsNativeComponent component;
private AutoCloseable mocks;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
mocks.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -74,13 +75,19 @@ public class CrashlyticsRemoteConfigListenerTest {
@Captor private ArgumentCaptor<ArrayList<RolloutAssignment>> captor;

private CrashlyticsRemoteConfigListener listener;
private AutoCloseable mocks;

@Before
public void setup() throws Exception {
MockitoAnnotations.openMocks(this);
mocks = MockitoAnnotations.openMocks(this);
listener = new CrashlyticsRemoteConfigListener(userMetadata);
}

@After
public void tearDown() throws Exception {
mocks.close();
}

@Test
public void testListener_onChangeUpdateRCInteropClassToCrashlyticsClass() throws Exception {
RolloutsState rolloutsState = createInteropRolloutsStateWithSingleElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import com.google.firebase.crashlytics.internal.persistence.FileStore;
import junit.framework.TestCase;
import org.junit.After;

/** Base class for all our TestCases. */
public class CrashlyticsTestCase extends TestCase {
public class CrashlyticsTestCase {
protected Context getContext() {
return ApplicationProvider.getApplicationContext();
}

@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
new FileStore(getContext()).deleteAllCrashlyticsFiles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package com.google.firebase.crashlytics.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -23,13 +27,15 @@
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import org.junit.Test;

public class DevelopmentPlatformProviderTest extends CrashlyticsTestCase {
private static final String PACKAGE_NAME = "package.name";
private static final String UNITY_PLATFORM = "Unity";
private static final String UNITY_VERSION = "2.0.0";
private static final String FLUTTER_PLATFORM = "Flutter";

@Test
public void testDevelopmentPlatformInfo_withUnity_returnsPlatformAndVersion() throws Exception {
Context context = createMockContext(/* withUnityResource= */ true);

Expand All @@ -41,6 +47,7 @@ public void testDevelopmentPlatformInfo_withUnity_returnsPlatformAndVersion() th
assertEquals(UNITY_VERSION, provider.getDevelopmentPlatformVersion());
}

@Test
public void testDevelopmentPlatformInfo_withFlutter_returnsPlatformAndNoVersion() {
Context context = getContext(); // has asset DevelopmentPlatformProvider.FLUTTER_ASSET_FILE

Expand All @@ -51,6 +58,7 @@ public void testDevelopmentPlatformInfo_withFlutter_returnsPlatformAndNoVersion(
assertFalse(DevelopmentPlatformProvider.isUnity(context));
}

@Test
public void testDevelopmentPlatformInfo_unknownPlatform_returnsNull() throws Exception {
Context context = createMockContext(/* withUnityResource= */ false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
import com.google.firebase.inject.Deferred;
import com.google.firebase.remoteconfig.interop.FirebaseRemoteConfigInterop;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -33,9 +34,16 @@ public class RemoteConfigDeferredProxyTest {

@Mock private UserMetadata userMetadata;

private AutoCloseable mocks;

@Before
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
mocks = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
mocks.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BlockingAnalyticsEventLoggerTest {

@Mock private CrashlyticsOriginAnalyticsEventLogger mockLogger;

private AutoCloseable mocks;

private BlockingAnalyticsEventLogger blockingAnalyticsEventLogger;

@Before
Expand All @@ -49,12 +51,13 @@ public void setUp() throws Exception {
.setApiKey("API-KEY-API-KEY-API-KEY-API-KEY-API-KEY")
.setProjectId("project-id")
.build());
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);
}

@After
public void cleanUp() {
public void cleanUp() throws Exception {
FirebaseApp.clearInstancesForTest();
mocks.close();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@
public class BreadcrumbAnalyticsEventReceiverTest {

@Mock private BreadcrumbHandler mockBreadcrumbHandler;
private AutoCloseable mocks;

private BreadcrumbAnalyticsEventReceiver receiver;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);

receiver = new BreadcrumbAnalyticsEventReceiver();
}

@Test
public void testReceiverDropsEventsWhenHandlerIsNull() {
public void testReceiverDropsEventsWhenHandlerIsNull() throws Exception {
receiver.onEvent("event", new Bundle());
mocks.close();
// Should not fail.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.os.Bundle;
import com.google.firebase.analytics.connector.AnalyticsConnector;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -29,14 +30,20 @@ public class CrashlyticsOriginAnalyticsEventLoggerTest {
@Mock private AnalyticsConnector mockAnalyticsConnector;

private CrashlyticsOriginAnalyticsEventLogger logger;
private AutoCloseable mocks;

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mocks = MockitoAnnotations.openMocks(this);

logger = new CrashlyticsOriginAnalyticsEventLogger(mockAnalyticsConnector);
}

@After
public void tearDown() throws Exception {
mocks.close();
}

@Test
public void testLoggerCallsAnalyticsConnectorWithCrashlyticsOrigin() {
final String eventName = "_ae";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@

package com.google.firebase.crashlytics.internal.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
import org.junit.Test;

@SuppressLint("UnspecifiedRegisterReceiverFlag")
public class BatteryStateTest extends CrashlyticsTestCase {
// Tolerance for float comparisons.
static final float EPSILON = 0.0001f;
Expand All @@ -36,6 +43,7 @@ private Intent makeIntent(int status, int level, int scale) {
return intent;
}

@Test
public void testGetBatteryLevel() {
Context mockContext = mock(Context.class);
when(mockContext.registerReceiver(isNull(), any())).thenReturn(makeIntent(0, 50, 200));
Expand All @@ -47,6 +55,7 @@ public void testGetBatteryLevel() {
assertEquals(0.25f, batteryLevel, EPSILON);
}

@Test
public void testNullIntent() {
final Context mockContext = mock(Context.class);
when(mockContext.registerReceiver(isNull(), any())).thenReturn(null);
Expand All @@ -58,6 +67,7 @@ public void testNullIntent() {
assertEquals(1, state.getBatteryVelocity());
}

@Test
public void testTooManyReceivers() {
Context mockContext = mock(Context.class);
when(mockContext.registerReceiver(isNull(), any()))
Expand All @@ -70,6 +80,7 @@ public void testTooManyReceivers() {
assertEquals(1, state.getBatteryVelocity());
}

@Test
public void testEmptyIntent() {
final Context mockContext = mock(Context.class);
when(mockContext.registerReceiver(isNull(), any())).thenReturn(new Intent());
Expand All @@ -88,6 +99,7 @@ private void doVelocityTest(int velocity, Intent intent) {
assertEquals(velocity, state.getBatteryVelocity());
}

@Test
public void testVelocity() {
doVelocityTest(
BatteryState.VELOCITY_FULL, makeIntent(BatteryManager.BATTERY_STATUS_CHARGING, 1, 1));
Expand Down
Loading
Loading