Skip to content

Commit

Permalink
Add "active survey id" to all the crash logs (#2182)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
shobhitagarwal1612 authored Jan 13, 2024
1 parent f3159c6 commit e223801
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ 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

@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")
Expand All @@ -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)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,19 +33,21 @@ 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
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -41,7 +38,7 @@ class TiffTagTest {

@Test
fun testIsArray_false() {
TiffTag.values()
TiffTag.entries
.filter {
it != TiffTag.TileByteCounts && it != TiffTag.TileOffsets && it != TiffTag.JPEGTables
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e223801

Please sign in to comment.