Commit 28731381 authored by Roger Yang's avatar Roger Yang
Browse files

Closes #5508: fix Intermittent test failure in CrashReporter

parent f6b3b770
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ import android.content.Intent
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import mozilla.components.lib.crash.service.CrashReporterService
import mozilla.components.support.test.any
import mozilla.components.support.test.eq
@@ -34,12 +36,14 @@ import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf
import java.lang.reflect.Modifier

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class CrashReporterTest {
    private val testDispatcher = TestCoroutineDispatcher()
    private val scope = TestCoroutineScope(testDispatcher)

    @ExperimentalCoroutinesApi
    @get:Rule
    val coroutinesTestRule = MainCoroutineRule()
    val coroutinesTestRule = MainCoroutineRule(testDispatcher)

    @Before
    fun setUp() {
@@ -72,7 +76,8 @@ class CrashReporterTest {

        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.NEVER
            shouldPrompt = CrashReporter.Prompt.NEVER,
            scope = scope
        ).install(testContext))

        val crash: Crash.UncaughtExceptionCrash = mock()
@@ -91,7 +96,8 @@ class CrashReporterTest {

        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.ALWAYS
            shouldPrompt = CrashReporter.Prompt.ALWAYS,
            scope = scope
        ).install(testContext))

        val crash: Crash.UncaughtExceptionCrash = mock()
@@ -110,7 +116,8 @@ class CrashReporterTest {

        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.ONLY_NATIVE_CRASH
            shouldPrompt = CrashReporter.Prompt.ONLY_NATIVE_CRASH,
            scope = scope
        ).install(testContext))

        val crash: Crash.UncaughtExceptionCrash = mock()
@@ -129,7 +136,8 @@ class CrashReporterTest {

        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.ONLY_NATIVE_CRASH
            shouldPrompt = CrashReporter.Prompt.ONLY_NATIVE_CRASH,
            scope = scope
        ).install(testContext))

        val crash: Crash.NativeCodeCrash = mock()
@@ -158,7 +166,8 @@ class CrashReporterTest {

        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.ALWAYS
            shouldPrompt = CrashReporter.Prompt.ALWAYS,
            scope = scope
        ).install(testContext))

        reporter.enabled = false
@@ -178,7 +187,8 @@ class CrashReporterTest {
        val service = mock<CrashReporterService>()
        val reporter = spy(CrashReporter(
            services = listOf(service),
            shouldPrompt = CrashReporter.Prompt.NEVER
            shouldPrompt = CrashReporter.Prompt.NEVER,
            scope = scope
        ).install(testContext))

        doReturn(mock<Job>()).`when`(reporter).submitReport(crash)
+15 −2
Original line number Diff line number Diff line
@@ -5,23 +5,34 @@
package mozilla.components.lib.crash.handler

import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import mozilla.components.lib.crash.Crash
import mozilla.components.lib.crash.CrashReporter
import mozilla.components.lib.crash.service.CrashReporterService
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class ExceptionHandlerTest {
    private val testDispatcher = TestCoroutineDispatcher()
    private val scope = TestCoroutineScope(testDispatcher)

    @get:Rule
    val coroutinesTestRule = MainCoroutineRule(testDispatcher)

    @Test
    fun `ExceptionHandler forwards crashes to CrashReporter`() {
@@ -42,7 +53,8 @@ class ExceptionHandlerTest {

        val crashReporter = spy(CrashReporter(
            shouldPrompt = CrashReporter.Prompt.NEVER,
            services = listOf(service)
            services = listOf(service),
            scope = scope
        ))

        val handler = ExceptionHandler(
@@ -77,7 +89,8 @@ class ExceptionHandlerTest {

                    override fun report(throwable: Throwable) {
                    }
                })
                }),
                scope = scope
        ).install(testContext)

        val handler = ExceptionHandler(
+14 −3
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ActivityScenario.launch
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import mozilla.components.lib.crash.Crash
@@ -24,10 +25,12 @@ import mozilla.components.lib.crash.prompt.CrashReporterActivity.Companion.SHARE
import mozilla.components.lib.crash.service.CrashReporterService
import mozilla.components.support.test.mock
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
@@ -37,6 +40,11 @@ import org.mockito.MockitoAnnotations.initMocks
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class CrashReporterActivityTest {
    private val testDispatcher = TestCoroutineDispatcher()
    private val scope = TestCoroutineScope(testDispatcher)

    @get:Rule
    val coroutinesTestRule = MainCoroutineRule(testDispatcher)

    @Mock
    lateinit var service: CrashReporterService
@@ -50,7 +58,8 @@ class CrashReporterActivityTest {
    fun `Pressing close button sends report`() = runBlockingTest {
        CrashReporter(
            shouldPrompt = CrashReporter.Prompt.ALWAYS,
            services = listOf(service)
            services = listOf(service),
            scope = scope
        ).install(testContext)

        val crash = Crash.UncaughtExceptionCrash(RuntimeException("Hello World"), arrayListOf())
@@ -72,7 +81,8 @@ class CrashReporterActivityTest {
    fun `Pressing restart button sends report`() = runBlockingTest {
        CrashReporter(
            shouldPrompt = CrashReporter.Prompt.ALWAYS,
            services = listOf(service)
            services = listOf(service),
            scope = scope
        ).install(testContext)

        val crash = Crash.UncaughtExceptionCrash(RuntimeException("Hello World"), arrayListOf())
@@ -114,7 +124,8 @@ class CrashReporterActivityTest {
    fun `Sending crash report saves checkbox state`() = runBlockingTest {
        CrashReporter(
            shouldPrompt = CrashReporter.Prompt.ALWAYS,
            services = listOf(service)
            services = listOf(service),
            scope = scope
        ).install(testContext)

        val crash = Crash.UncaughtExceptionCrash(RuntimeException("Hello World"), arrayListOf())