Commit 4a6951dc authored by Beatriz Rizental's avatar Beatriz Rizental Committed by Pier Angelo Vendrame
Browse files

TB 43243: [android] Implement Android launch test

Also remove exit call from terminate function.
It causes all espresso tests to crash on exit and otherwise doesn't do anything.
parent c5bd3772
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
package org.mozilla.fenix.torbrowser

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

@RunWith(AndroidJUnit4::class)
class LaunchTest {

    @get:Rule
    var rule = HomeActivityScenarioRule()

    @Test
    fun appLaunchesWithoutCrash() {
        val device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        device.waitForIdle()

        // Simulate a 30-second delay
        val latch = CountDownLatch(1)
        Thread {
            try {
                Thread.sleep(30_000)
                latch.countDown()
            } catch (e: InterruptedException) {
                e.printStackTrace()
            }
        }.start()

        latch.await(30, TimeUnit.SECONDS)

        // If we got here, the app did not crash. Test passed.
    }
}
+33 −0
Original line number Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.torbrowser

import androidx.test.ext.junit.rules.ActivityScenarioRule
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.ext.components

class HomeActivityScenarioRule : TestRule {
    private val delegate = ActivityScenarioRule<HomeActivity>(HomeActivity::class.java)

    val scenario get() = delegate.scenario

    // ActivityScenarioRule waits for the DESTROYED lifecycle state during teardown, which means
    // HomeActivity.onDestroy runs synchronously before the rule returns. onDestroy calls shutDown() ->
    // exitProcess(0) when isFinishing, which kills the instrumentation process. Setting this flag
    // prevents that code path so tests can report their results normally.
    override fun apply(base: Statement, description: Description): Statement {
        return delegate.apply(object : Statement() {
            override fun evaluate() {
                delegate.scenario.onActivity { activity ->
                    activity.applicationContext.components.notificationsDelegate.shouldShutDownWithOnDestroyWhenIsFinishing = false
                }
                base.evaluate()
            }
        }, description)
    }
}