Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options support in ActivityStarter #371

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import com.badoo.ribs.android.activitystarter.ActivityStarter
import com.badoo.ribs.android.activitystarter.OptionsCreator
import com.badoo.ribs.android.requestcode.RequestCodeClient
import com.badoo.ribs.minimal.reactive.Relay
import com.badoo.ribs.minimal.reactive.Source
Expand All @@ -18,13 +19,14 @@ class TestActivityStarter : ActivityStarter {
override fun events(client: RequestCodeClient): Source<ActivityStarter.ActivityResultEvent> =
events

override fun startActivity(createIntent: Context.() -> Intent) {
override fun startActivity(createOptions: OptionsCreator?, createIntent: Context.() -> Intent) {
startedIntents += createIntent(mock(Context::class.java))
}

override fun startActivityForResult(
client: RequestCodeClient,
requestCode: Int,
createOptions: OptionsCreator?,
createIntent: Context.() -> Intent
) {
startedIntents += createIntent(mock(Context::class.java))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.Activity.RESULT_OK
import android.app.Instrumentation
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import androidx.test.InstrumentationRegistry.getTargetContext
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.Intents.intending
Expand All @@ -27,7 +28,7 @@ import org.hamcrest.CoreMatchers.allOf
import org.junit.Rule
import org.junit.Test


@Suppress("FunctionNaming")
class ActivityStarterTest {

@get:Rule
Expand All @@ -42,6 +43,28 @@ class ActivityStarterTest {
intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
}

@Test
fun startActivity_startsTargetActivityWithOptions_hasOptions() {
val basicOptions = createTestBundle()
activityRule.activity.activityStarter.startActivity({ basicOptions }) {
Intent(this, OtherActivity::class.java)
}

intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
val lastStartedOptionValue = activityRule.activity.lastStartedOptions!!.get(KEY_TEST_OPTION)
assertThat(lastStartedOptionValue).isEqualTo(VALUE_TEST_OPTION)
}

@Test
fun startActivity_startsTargetActivityWithoutOptions_hasNoOptions() {
activityRule.activity.activityStarter.startActivity {
Intent(this, OtherActivity::class.java)
}

intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
assertThat(activityRule.activity.lastStartedOptions).isNull()
}

@Test
fun startActivity_intentDataIsDeliveredToTargetActivity() {
activityRule.activity.activityStarter.startActivity {
Expand All @@ -66,6 +89,38 @@ class ActivityStarterTest {
intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
}

@Test
fun startActivityForResult_startsTargetActivityWithOptions_hasOptions() {
activityRule.activity.activityStarter.events(identifiable).rx2().subscribeOnTestObserver()

val basicOptions = createTestBundle()
activityRule
.activity
.activityStarter
.startActivityForResult(identifiable, requestCode = 1, { basicOptions }) {
Intent(this, OtherActivity::class.java)
}

intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
val lastStartedOptionValue = activityRule.activity.lastStartedOptions!!.get(KEY_TEST_OPTION)
assertThat(lastStartedOptionValue).isEqualTo(VALUE_TEST_OPTION)
}

@Test
fun startActivityForResult_startsTargetActivityWithoutOptions_hasNoOptions() {
activityRule.activity.activityStarter.events(identifiable).rx2().subscribeOnTestObserver()

activityRule
.activity
.activityStarter
.startActivityForResult(identifiable, requestCode = 1) {
Intent(this, OtherActivity::class.java)
}

intended(hasComponent(ComponentName(getTargetContext(), OtherActivity::class.java)))
assertThat(activityRule.activity.lastStartedOptions).isNull()
}

@Test
fun startActivityForResult_startActivityThatReturnsOkResult_returnsOkResultCode() {
givenResultForActivity<OtherActivity>(resultCode = RESULT_OK)
Expand Down Expand Up @@ -185,9 +240,17 @@ class ActivityStarterTest {
}
}

private fun createTestBundle(): Bundle =
Bundle()
.apply {
putString(KEY_TEST_OPTION, VALUE_TEST_OPTION)
}

companion object {
private val identifiable = TestRequestCodeClient()
private val collisionIdentifiable1 = TestRequestCodeClient(requestCodeClientId = "Siblings")
private val collisionIdentifiable2 = TestRequestCodeClient(requestCodeClientId = "Teheran")
private const val KEY_TEST_OPTION = "KeyTestOption"
private const val VALUE_TEST_OPTION = "ValueTestOption"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TestActivity : AppCompatActivity() {

var ignoreActivityStarts: Boolean = false
var lastStartedRequestCode: Int = -1
var lastStartedOptions: Bundle? = null

private val activityBoundary: ActivityBoundary by lazy {
ActivityBoundary(
Expand All @@ -46,13 +47,19 @@ class TestActivity : AppCompatActivity() {
requestCodeRegistry.onSaveInstanceState(outState)
}

override fun startActivityForResult(intent: Intent?, requestCode: Int) {
override fun startActivityForResult(intent: Intent?, requestCode: Int, options: Bundle?) {
if (!ignoreActivityStarts) {
super.startActivityForResult(intent, requestCode)
super.startActivityForResult(intent, requestCode, options)
}
lastStartedOptions = options
lastStartedRequestCode = requestCode
}

override fun startActivity(intent: Intent?, options: Bundle?) {
lastStartedOptions = options
super.startActivity(intent, options)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
activityBoundary.onActivityResult(requestCode, resultCode, data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ class ActivityBoundary(
requestCodeRegistry
)

override fun startActivity(createIntent: Context.() -> Intent) {
activityStarterHost.startActivity(activityStarterHost.context.createIntent())
override fun startActivity(createOptions: OptionsCreator?, createIntent: Context.() -> Intent) {
activityStarterHost.startActivity(activityStarterHost.context.createIntent(), createOptions)
}

override fun startActivityForResult(
client: RequestCodeClient,
requestCode: Int,
createOptions: OptionsCreator?,
createIntent: Context.() -> Intent
) {
activityStarterHost.startActivityForResult(
activityStarterHost.context.createIntent(),
client.forgeExternalRequestCode(requestCode)
client.forgeExternalRequestCode(requestCode),
createOptions
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import com.badoo.ribs.android.requestcode.RequestCodeClient
*/
interface ActivityStarter : RequestCodeBasedEventStream<ActivityResultEvent> {

fun startActivity(createIntent: Context.() -> Intent)
fun startActivity(createOptions: OptionsCreator? = null, createIntent: Context.() -> Intent)

fun startActivityForResult(client: RequestCodeClient, requestCode: Int, createIntent: Context.() -> Intent)
fun startActivityForResult(
client: RequestCodeClient,
requestCode: Int,
createOptions: OptionsCreator? = null,
createIntent: Context.() -> Intent
)

data class ActivityResultEvent(
override val requestCode: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,41 @@ interface ActivityStarterHost {

val context: Context

fun startActivity(intent: Intent)
fun startActivity(intent: Intent, createOptions: OptionsCreator? = null)

fun startActivityForResult(intent: Intent, requestCode: Int)
fun startActivityForResult(intent: Intent, requestCode: Int, createOptions: OptionsCreator? = null)

class ActivityHost(private val activity: Activity) : ActivityStarterHost {
override val context: Context
get() = activity

override fun startActivity(intent: Intent) {
activity.startActivity(intent)
override fun startActivity(intent: Intent, createOptions: OptionsCreator?) {
activity.startActivity(intent, createOptions?.invoke(activity))
}

override fun startActivityForResult(intent: Intent, requestCode: Int) {
activity.startActivityForResult(intent, requestCode)
override fun startActivityForResult(
intent: Intent,
requestCode: Int,
createOptions: OptionsCreator?
) {
activity.startActivityForResult(intent, requestCode, createOptions?.invoke(activity))
}
}

class FragmentHost(private val fragment: Fragment) : ActivityStarterHost {
override val context: Context
get() = fragment.requireContext()

override fun startActivity(intent: Intent) {
fragment.startActivity(intent)
override fun startActivity(intent: Intent, createOptions: OptionsCreator?) {
fragment.startActivity(intent, createOptions?.invoke(fragment.requireActivity()))
}

override fun startActivityForResult(intent: Intent, requestCode: Int) {
fragment.startActivityForResult(intent, requestCode)
override fun startActivityForResult(
intent: Intent,
requestCode: Int,
createOptions: OptionsCreator?
) {
fragment.startActivityForResult(intent, requestCode, createOptions?.invoke(fragment.requireActivity()))
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.badoo.ribs.android.activitystarter

import android.app.Activity
import android.os.Bundle

typealias OptionsCreator = Activity.() -> Bundle?
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Instrumentation.ActivityResult
import android.content.Context
import android.content.Intent
import com.badoo.ribs.android.activitystarter.ActivityStarter
import com.badoo.ribs.android.activitystarter.OptionsCreator
import com.badoo.ribs.android.requestcode.RequestCodeClient
import com.badoo.ribs.minimal.reactive.Relay
import com.badoo.ribs.minimal.reactive.Source
Expand All @@ -22,11 +23,16 @@ class TestActivityStarter : ActivityStarter {

private val eventsRelay: Relay<Pair<RequestCodeClient, ActivityStarter.ActivityResultEvent>> = Relay()

override fun startActivity(createIntent: Context.() -> Intent) {
override fun startActivity(createOptions: OptionsCreator?, createIntent: Context.() -> Intent) {
intents += testContext.createIntent()
}

override fun startActivityForResult(client: RequestCodeClient, requestCode: Int, createIntent: Context.() -> Intent) {
override fun startActivityForResult(
client: RequestCodeClient,
requestCode: Int,
createOptions: OptionsCreator?,
createIntent: Context.() -> Intent
) {
val intent = testContext.createIntent()

intents += intent
Expand Down