From e223801839fd4f924892a6e0ffc1b726d7cd5712 Mon Sep 17 00:00:00 2001 From: Shobhit Agarwal Date: Sun, 14 Jan 2024 02:22:26 +0530 Subject: [PATCH] Add "active survey id" to all the crash logs (#2182) * Add custom key to crash logs * Fix failing unit tests ParameterizedRobolectricTestRunner initializes the GroundApplication class but for some reason doesn't apply the test only modules. --- .../android/ground/GroundApplication.kt | 21 ++++++++++++++----- .../com/google/android/ground/BaseHiltTest.kt | 2 -- .../room/converter/ValueJsonConverterTest.kt | 5 ++++- .../ground/system/GeocodingManagerTest.kt | 2 -- .../tasks/point/LatLngConverterTest.kt | 7 ++++++- .../ui/home/mapcontainer/LoiCardUtilTest.kt | 5 ++++- .../ui/map/gms/mog/MogImageMetadataTest.kt | 3 --- .../ui/map/gms/mog/MogTileMetadataTest.kt | 3 --- .../ground/ui/map/gms/mog/MogTileTest.kt | 3 --- .../ui/map/gms/mog/PixelCoordinatesTest.kt | 3 --- .../mog/SeekableBufferedInputStreamTest.kt | 3 --- .../ui/map/gms/mog/SeekableInputStreamTest.kt | 3 --- .../ui/map/gms/mog/TiffTagDataTypeTest.kt | 8 +++---- .../ground/ui/map/gms/mog/TiffTagTest.kt | 9 +++----- .../ui/map/gms/mog/TileCoordinatesTest.kt | 3 --- 15 files changed, 36 insertions(+), 44 deletions(-) diff --git a/ground/src/main/java/com/google/android/ground/GroundApplication.kt b/ground/src/main/java/com/google/android/ground/GroundApplication.kt index 7b5f37309c..cc61e4998a 100644 --- a/ground/src/main/java/com/google/android/ground/GroundApplication.kt +++ b/ground/src/main/java/com/google/android/ground/GroundApplication.kt @@ -21,7 +21,9 @@ import android.util.Log import androidx.hilt.work.HiltWorkerFactory import androidx.multidex.MultiDexApplication import androidx.work.Configuration +import com.google.android.ground.repository.SurveyRepository import com.google.firebase.crashlytics.FirebaseCrashlytics +import com.google.firebase.crashlytics.setCustomKeys import dagger.hilt.android.HiltAndroidApp import javax.inject.Inject import timber.log.Timber @@ -29,10 +31,11 @@ import timber.log.Timber @HiltAndroidApp class GroundApplication : MultiDexApplication(), Configuration.Provider { + @Inject lateinit var crashReportingTree: CrashReportingTree @Inject lateinit var workerFactory: HiltWorkerFactory init { - Timber.plant(if (isReleaseBuild()) CrashReportingTree() else Timber.DebugTree()) + Timber.plant(if (isReleaseBuild()) crashReportingTree else Timber.DebugTree()) } private fun isReleaseBuild(): Boolean = BuildConfig.BUILD_TYPE.contentEquals("release") @@ -59,12 +62,20 @@ class GroundApplication : MultiDexApplication(), Configuration.Provider { } /** Reports any error with priority more than "info" to Crashlytics. */ - private class CrashReportingTree : Timber.Tree() { + class CrashReportingTree @Inject constructor(private val surveyRepository: SurveyRepository) : + Timber.Tree() { + + private fun getSelectedSurveyId(): String = surveyRepository.selectedSurveyId ?: "" + override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { if (priority > Log.INFO) { - val crashlytics = FirebaseCrashlytics.getInstance() - crashlytics.log(message) - if (t != null && priority == Log.ERROR) crashlytics.recordException(t) + with(FirebaseCrashlytics.getInstance()) { + log(message) + if (t != null && priority == Log.ERROR) { + setCustomKeys { key("selectedSurveyId", getSelectedSurveyId()) } + recordException(t) + } + } } } } diff --git a/ground/src/test/java/com/google/android/ground/BaseHiltTest.kt b/ground/src/test/java/com/google/android/ground/BaseHiltTest.kt index 012f28bf7f..44cb968855 100644 --- a/ground/src/test/java/com/google/android/ground/BaseHiltTest.kt +++ b/ground/src/test/java/com/google/android/ground/BaseHiltTest.kt @@ -21,7 +21,6 @@ import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltTestApplication import javax.annotation.OverridingMethodsMustInvokeSuper import javax.inject.Inject -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestDispatcher import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest @@ -33,7 +32,6 @@ import org.mockito.junit.MockitoRule import org.robolectric.annotation.Config /** Injects Hilt dependencies during setUp. */ -@OptIn(ExperimentalCoroutinesApi::class) @Config(application = HiltTestApplication::class) open class BaseHiltTest { /** Required for injecting hilt dependencies using @Inject annotation. */ diff --git a/ground/src/test/java/com/google/android/ground/persistence/local/room/converter/ValueJsonConverterTest.kt b/ground/src/test/java/com/google/android/ground/persistence/local/room/converter/ValueJsonConverterTest.kt index 1f522cff66..751bfd8c55 100644 --- a/ground/src/test/java/com/google/android/ground/persistence/local/room/converter/ValueJsonConverterTest.kt +++ b/ground/src/test/java/com/google/android/ground/persistence/local/room/converter/ValueJsonConverterTest.kt @@ -15,6 +15,7 @@ */ package com.google.android.ground.persistence.local.room.converter +import com.google.android.ground.BaseHiltTest import com.google.android.ground.model.geometry.Coordinates import com.google.android.ground.model.geometry.LinearRing import com.google.android.ground.model.geometry.Point @@ -32,6 +33,7 @@ import com.google.android.ground.ui.datacollection.tasks.point.DropPinTaskResult import com.google.android.ground.ui.datacollection.tasks.polygon.DrawAreaTaskResult import com.google.common.truth.Truth.assertThat import com.sharedtest.FakeData +import dagger.hilt.android.testing.HiltAndroidTest import java.util.Date import kotlinx.collections.immutable.persistentListOf import org.json.JSONArray @@ -39,12 +41,13 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.ParameterizedRobolectricTestRunner +@HiltAndroidTest @RunWith(ParameterizedRobolectricTestRunner::class) class ValueJsonConverterTest( private val task: Task, private val value: Value, private val input: Any -) { +) : BaseHiltTest() { @Test fun testToJsonObject() { diff --git a/ground/src/test/java/com/google/android/ground/system/GeocodingManagerTest.kt b/ground/src/test/java/com/google/android/ground/system/GeocodingManagerTest.kt index a4c38e7512..f7f33546dc 100644 --- a/ground/src/test/java/com/google/android/ground/system/GeocodingManagerTest.kt +++ b/ground/src/test/java/com/google/android/ground/system/GeocodingManagerTest.kt @@ -27,7 +27,6 @@ import dagger.hilt.android.testing.UninstallModules import java.util.* import javax.inject.Inject import kotlin.test.assertEquals -import kotlinx.coroutines.ExperimentalCoroutinesApi import org.junit.Test import org.junit.runner.RunWith import org.mockito.ArgumentMatchers.anyInt @@ -40,7 +39,6 @@ import org.robolectric.ParameterizedRobolectricTestRunner @HiltAndroidTest @RunWith(ParameterizedRobolectricTestRunner::class) @UninstallModules(SystemModule::class) -@OptIn(ExperimentalCoroutinesApi::class) class GeocodingManagerTest( private val expectedAreaName: String, private val message: String, diff --git a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/point/LatLngConverterTest.kt b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/point/LatLngConverterTest.kt index 67f5694fb1..367db28251 100644 --- a/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/point/LatLngConverterTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/datacollection/tasks/point/LatLngConverterTest.kt @@ -16,15 +16,18 @@ package com.google.android.ground.ui.datacollection.tasks.point +import com.google.android.ground.BaseHiltTest import com.google.android.ground.model.geometry.Coordinates import com.google.android.ground.ui.datacollection.tasks.point.LatLngConverter.formatCoordinates import com.google.common.truth.Truth.assertThat +import dagger.hilt.android.testing.HiltAndroidTest import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +@HiltAndroidTest @RunWith(RobolectricTestRunner::class) -class LatLngConverterTest { +class LatLngConverterTest : BaseHiltTest() { @Test fun testProcessCoordinates_ne() { @@ -37,11 +40,13 @@ class LatLngConverterTest { assertThat(formatCoordinates(Coordinates(-10.555, 10.555))) .isEqualTo("10°33'18\" S 10°33'18\" E") } + @Test fun testProcessCoordinates_nw() { assertThat(formatCoordinates(Coordinates(10.555, -10.555))) .isEqualTo("10°33'18\" N 10°33'18\" W") } + @Test fun testProcessCoordinates_sw() { assertThat(formatCoordinates(Coordinates(-10.555, -10.555))) diff --git a/ground/src/test/java/com/google/android/ground/ui/home/mapcontainer/LoiCardUtilTest.kt b/ground/src/test/java/com/google/android/ground/ui/home/mapcontainer/LoiCardUtilTest.kt index ecccb9a8a2..c3f4f13895 100644 --- a/ground/src/test/java/com/google/android/ground/ui/home/mapcontainer/LoiCardUtilTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/home/mapcontainer/LoiCardUtilTest.kt @@ -17,17 +17,20 @@ package com.google.android.ground.ui.home.mapcontainer import android.content.Context import androidx.test.core.app.ApplicationProvider +import com.google.android.ground.BaseHiltTest import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getDisplayLoiName import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getJobName import com.google.android.ground.ui.home.mapcontainer.cards.LoiCardUtil.getSubmissionsText import com.google.common.truth.Truth.assertThat import com.sharedtest.FakeData +import dagger.hilt.android.testing.HiltAndroidTest import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner +@HiltAndroidTest @RunWith(RobolectricTestRunner::class) -class LoiCardUtilTest { +class LoiCardUtilTest : BaseHiltTest() { private val context: Context = ApplicationProvider.getApplicationContext() diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogImageMetadataTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogImageMetadataTest.kt index 0c54086a88..3dd2fa8023 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogImageMetadataTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogImageMetadataTest.kt @@ -18,10 +18,7 @@ package com.google.android.ground.ui.map.gms.mog import com.google.common.truth.Truth.assertThat import org.junit.Assert.assertThrows import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class MogImageMetadataTest { // TileCountX = 4, TileCountY = 4 diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileMetadataTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileMetadataTest.kt index 58981e3fa1..5c587d6b1f 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileMetadataTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileMetadataTest.kt @@ -17,10 +17,7 @@ package com.google.android.ground.ui.map.gms.mog import org.junit.Assert.assertThrows import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class MogTileMetadataTest { private val testMogTileMetadata = diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileTest.kt index 2bc7e09311..511cee2ee0 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/MogTileTest.kt @@ -17,10 +17,7 @@ package com.google.android.ground.ui.map.gms.mog import com.google.common.truth.Truth.assertThat import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class MogTileTest { @Test diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/PixelCoordinatesTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/PixelCoordinatesTest.kt index aea9f09f64..ee08ff4d34 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/PixelCoordinatesTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/PixelCoordinatesTest.kt @@ -18,10 +18,7 @@ package com.google.android.ground.ui.map.gms.mog import com.google.android.gms.maps.model.LatLng import kotlin.test.assertEquals import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class PixelCoordinatesTest { @Test fun `atZoom() at lower zoom reduces coords`() { diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableBufferedInputStreamTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableBufferedInputStreamTest.kt index 2e295c0a66..7c81ece6f0 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableBufferedInputStreamTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableBufferedInputStreamTest.kt @@ -19,10 +19,7 @@ package com.google.android.ground.ui.map.gms.mog import java.io.ByteArrayInputStream import kotlin.test.assertEquals import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class SeekableBufferedInputStreamTest { private val testSourceStream = ByteArrayInputStream(byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableInputStreamTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableInputStreamTest.kt index d0ccf1f610..89c303b53c 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableInputStreamTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/SeekableInputStreamTest.kt @@ -20,10 +20,7 @@ import java.io.ByteArrayInputStream import kotlin.test.assertEquals import org.junit.Before import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class SeekableInputStreamTest { private val testSourceStream = ByteArrayInputStream(byteArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)) diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagDataTypeTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagDataTypeTest.kt index 98dbdbfe0d..7200e01200 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagDataTypeTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagDataTypeTest.kt @@ -18,9 +18,9 @@ package com.google.android.ground.ui.map.gms.mog import com.google.common.truth.Truth.assertThat import org.junit.Test import org.junit.runner.RunWith -import org.robolectric.ParameterizedRobolectricTestRunner +import org.junit.runners.Parameterized -@RunWith(ParameterizedRobolectricTestRunner::class) +@RunWith(Parameterized::class) class TiffTagDataTypeTest( private val dataType: TiffTagDataType, private val sizeInBytes: Int, @@ -35,9 +35,7 @@ class TiffTagDataTypeTest( companion object { @JvmStatic - @ParameterizedRobolectricTestRunner.Parameters( - name = "{0} should be at position {2} with size {1}" - ) + @Parameterized.Parameters(name = "{0} should be at position {2} with size {1}") fun data() = listOf( arrayOf(TiffTagDataType.BYTE, 1, 1), diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagTest.kt index c7f3a06311..04c672e5fc 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TiffTagTest.kt @@ -17,22 +17,19 @@ package com.google.android.ground.ui.map.gms.mog import com.google.common.truth.Truth.assertWithMessage import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class TiffTagTest { @Test fun testUniqueId() { assertWithMessage("TiffTag enum contains non-unique ids") - .that(TiffTag.values()) + .that(TiffTag.entries.toTypedArray()) .hasLength(TiffTag.byId.size) } @Test fun testIsArray_true() { - TiffTag.values() + TiffTag.entries .filter { it == TiffTag.TileByteCounts || it == TiffTag.TileOffsets || it == TiffTag.JPEGTables } @@ -41,7 +38,7 @@ class TiffTagTest { @Test fun testIsArray_false() { - TiffTag.values() + TiffTag.entries .filter { it != TiffTag.TileByteCounts && it != TiffTag.TileOffsets && it != TiffTag.JPEGTables } diff --git a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TileCoordinatesTest.kt b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TileCoordinatesTest.kt index b32c1ec023..254eabb1fb 100644 --- a/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TileCoordinatesTest.kt +++ b/ground/src/test/java/com/google/android/ground/ui/map/gms/mog/TileCoordinatesTest.kt @@ -19,10 +19,7 @@ import com.google.android.ground.model.geometry.Coordinates import com.google.android.ground.ui.map.Bounds import com.google.common.truth.Truth.assertThat import org.junit.Test -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -@RunWith(RobolectricTestRunner::class) class TileCoordinatesTest { @Test