Skip to content

Instantly share code, notes, and snippets.

@pedrovgs
Created February 21, 2019 12:17
Show Gist options
  • Select an option

  • Save pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b to your computer and use it in GitHub Desktop.

Select an option

Save pedrovgs/6a305ba4c5e3acfac854ce4c36558d9b to your computer and use it in GitHub Desktop.

Revisions

  1. pedrovgs created this gist Feb 21, 2019.
    35 changes: 35 additions & 0 deletions ExhaustiveIntentsTestRule.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    package com.aplazame.utils

    import android.app.Activity
    import androidx.test.espresso.intent.Intents
    import androidx.test.rule.ActivityTestRule

    class ExhaustiveIntentsTestRule<T : Activity> : ActivityTestRule<T> {

    private var isInitialized: Boolean = false

    constructor(activityClass: Class<T>) : super(activityClass)

    constructor(activityClass: Class<T>, initialTouchMode: Boolean) : super(activityClass, initialTouchMode)

    constructor(activityClass: Class<T>, initialTouchMode: Boolean, launchActivity: Boolean) : super(
    activityClass,
    initialTouchMode,
    launchActivity
    )

    override fun beforeActivityLaunched() {
    super.beforeActivityLaunched()
    Intents.init()
    isInitialized = true
    }

    override fun afterActivityFinished() {
    super.afterActivityFinished()
    if (isInitialized) {
    // Otherwise will throw a NPE if Intents.init() wasn't called.
    Intents.release()
    isInitialized = false
    }
    }
    }