mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/crash/CrashTest.kt
author Cosmin Sabou <csabou@mozilla.com>
Wed, 21 Nov 2018 17:46:44 +0200
changeset 447478 3bd6b94b23e5dc644606c0f7f708f9008ccf0939
parent 447475 bae43dfd3700460a55f3559eabbc888975f3a3c0
child 447890 0bb898d00ed7b9d1bdaf3b4320cbc2ac5bca00fc
permissions -rw-r--r--
Backed out changeset bae43dfd3700 (bug 1485045) for toolchains gradle bustages. CLOSED TREE

package org.mozilla.geckoview.test.crash

import android.content.Intent
import android.os.Message
import android.os.Messenger
import android.support.test.InstrumentationRegistry
import android.support.test.filters.MediumTest
import android.support.test.rule.ServiceTestRule
import android.support.test.runner.AndroidJUnit4
import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.notNullValue
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.Timeout
import org.junit.runner.RunWith
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.test.util.Environment
import java.io.File

@RunWith(AndroidJUnit4::class)
@MediumTest
class CrashTest {
    companion object {
        val DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS = 90000L
        val DEFAULT_X86_DEVICE_TIMEOUT_MILLIS = 60000L
        val DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS = 180000L
        val DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS = 60000L
    }

    lateinit var messenger: Messenger

    @get:Rule val rule = ServiceTestRule()

    @get:Rule val timeoutRule = Timeout.millis(getTimeoutMillis())

    fun getTimeoutMillis(): Long {
        val env = Environment()
        if ("x86" == env.cpuArch) {
            return if (env.isEmulator)
                CrashTest.DEFAULT_X86_EMULATOR_TIMEOUT_MILLIS
            else
                CrashTest.DEFAULT_X86_DEVICE_TIMEOUT_MILLIS
        }
        return if (env.isEmulator)
            CrashTest.DEFAULT_ARM_EMULATOR_TIMEOUT_MILLIS
        else
            CrashTest.DEFAULT_ARM_DEVICE_TIMEOUT_MILLIS
    }

    @Before
    fun setup() {
        CrashTestHandler.queue.clear()

        val context = InstrumentationRegistry.getTargetContext();
        val binder = rule.bindService(Intent(context, RemoteGeckoService::class.java))
        messenger = Messenger(binder)
        assertThat("messenger should not be null", binder, notNullValue())
    }

    fun assertCrashIntent(intent: Intent, fatal: Boolean) {
        assertThat("Action should match",
                intent.action, equalTo(GeckoRuntime.ACTION_CRASHED))
        assertThat("Dump file should exist",
                File(intent.getStringExtra(GeckoRuntime.EXTRA_MINIDUMP_PATH)).exists(),
                equalTo(true))
        assertThat("Extras file should exist",
                File(intent.getStringExtra(GeckoRuntime.EXTRA_EXTRAS_PATH)).exists(),
                equalTo(true))
        assertThat("Dump should be succcesful",
                intent.getBooleanExtra(GeckoRuntime.EXTRA_MINIDUMP_SUCCESS, false),
                equalTo(true))

        assertThat("Fatality should match",
                intent.getBooleanExtra(GeckoRuntime.EXTRA_CRASH_FATAL, !fatal), equalTo(fatal))
    }

    @Test
    fun crashParent() {
        messenger.send(Message.obtain(null, RemoteGeckoService.CMD_CRASH_PARENT_NATIVE))
        assertCrashIntent(CrashTestHandler.queue.take(), true)
    }

    @Test
    fun crashContent() {
        messenger.send(Message.obtain(null, RemoteGeckoService.CMD_CRASH_CONTENT_NATIVE))
        assertCrashIntent(CrashTestHandler.queue.take(), false)
    }
}