Skip to content

Commit

Permalink
Fixed android tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gosha212 committed May 12, 2024
1 parent dce0939 commit 144fd3d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule

private const val INTENT_LAUNCH_ARGS_KEY = "launchArgs"
private const val ACTIVITY_LAUNCH_TIMEOUT = 10000L

class ActivityLaunchHelper
@JvmOverloads
constructor(
class ActivityLaunchHelper @JvmOverloads constructor(
private val clazz: Class<out Activity>,
private val launchArgs: LaunchArgs = LaunchArgs(),
private val intentsFactory: LaunchIntentsFactory = LaunchIntentsFactory(),
private val notificationDataParserGen: (String) -> NotificationDataParser = { path -> NotificationDataParser(path) }
private val notificationDataParserGen: (String) -> NotificationDataParser = { path -> NotificationDataParser(path) },
private val activityScenarioWrapperManager: ActivityScenarioWrapperManager = ActivityScenarioWrapperManager()
) {

private val activityScenarioRules: MutableList<ActivityScenario<Activity>> = mutableListOf()
private val activityScenarioRules: MutableList<ActivityScenarioWrapper> = mutableListOf()

fun launchActivityUnderTest() {
val intent = extractInitialIntent()
activityScenarioRules.add(ActivityScenario.launch(intent))
activityScenarioRules.add(activityScenarioWrapperManager.launch(intent))
}

fun launchMainActivity() {
Expand Down Expand Up @@ -58,7 +56,7 @@ constructor(
}

private fun launchActivitySync(intent: Intent) {
activityScenarioRules.add(ActivityScenario.launch(intent))
activityScenarioRules.add(activityScenarioWrapperManager.launch(intent))
}

private val appContext: Context
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wix.detox

import android.app.Activity
import android.content.Intent
import androidx.test.core.app.ActivityScenario

class ActivityScenarioWrapper private constructor(private val activityScenario: ActivityScenario<Activity>) {

fun close() {
activityScenario.close()
}

companion object {
fun launch(clazz: Class<Activity>): ActivityScenarioWrapper {
return ActivityScenarioWrapper(ActivityScenario.launch(clazz))
}

fun launch(intent: Intent): ActivityScenarioWrapper {
return ActivityScenarioWrapper(ActivityScenario.launch(intent))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.wix.detox

import android.app.Activity
import android.content.Intent

class ActivityScenarioWrapperManager {

fun launch(clazz: Class<Activity>): ActivityScenarioWrapper {
return ActivityScenarioWrapper.launch(clazz)
}

fun launch(intent: Intent): ActivityScenarioWrapper {
return ActivityScenarioWrapper.launch(intent)
}
}
1 change: 1 addition & 0 deletions detox/android/detox/src/full/java/com/wix/detox/Detox.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static void runTests(Class<? extends Activity> clazz, @NonNull final Cont

sActivityLaunchHelper = new ActivityLaunchHelper(clazz);
DetoxMain.run(context, sActivityLaunchHelper);
sActivityLaunchHelper.close();
}

public static void launchMainActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import org.mockito.kotlin.*
import androidx.test.rule.ActivityTestRule
import org.junit.runner.RunWith
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
Expand All @@ -23,34 +22,36 @@ class ActivityLaunchHelperTest {
private lateinit var intent: Intent
private lateinit var launchArgsAsBundle: Bundle
private lateinit var notificationDataAsBundle: Bundle
private lateinit var testRule: ActivityTestRule<Activity>
private lateinit var testClazz: Class<Activity>
private lateinit var launchArgs: LaunchArgs
private lateinit var intentsFactory: LaunchIntentsFactory
private lateinit var notificationDataParser: NotificationDataParser
private lateinit var activityScenarioWrapperManager: ActivityScenarioWrapperManager

private fun uut() = ActivityLaunchHelper(testRule, launchArgs, intentsFactory, { notificationDataParser })
private fun uut() = ActivityLaunchHelper(testClazz, launchArgs, intentsFactory, { notificationDataParser }, activityScenarioWrapperManager)

@Before
fun setup() {
intent = Intent()
launchArgsAsBundle = mock()
notificationDataAsBundle = mock()

testRule = mock()
testClazz = Activity::class.java
launchArgs = mock() {
on { asIntentBundle() }.thenReturn(launchArgsAsBundle)
}
intentsFactory = mock()
notificationDataParser = mock() {
on { toBundle() }.thenReturn(notificationDataAsBundle)
}
activityScenarioWrapperManager = mock()
}

@Test
fun `default-activity -- should launch using test rule, with a clean intent`() {
givenCleanLaunch()
uut().launchActivityUnderTest()
verify(testRule).launchActivity(eq(intent))
verify(activityScenarioWrapperManager).launch(eq(intent))
}

@Test
Expand All @@ -64,7 +65,7 @@ class ActivityLaunchHelperTest {
fun `default activity, with a url -- should launch based on the url`() {
givenLaunchWithInitialURL()
uut().launchActivityUnderTest()
verify(testRule).launchActivity(eq(intent))
verify(activityScenarioWrapperManager).launch(eq(intent))
verify(intentsFactory).intentWithUrl(initialURL, true)
}

Expand All @@ -79,7 +80,7 @@ class ActivityLaunchHelperTest {
fun `default activity, with notification data -- should launch with the data as bundle`() {
givenLaunchWithNotificationData()
uut().launchActivityUnderTest()
verify(testRule).launchActivity(eq(intent))
verify(activityScenarioWrapperManager).launch(eq(intent))
verify(intentsFactory).intentWithNotificationData(any(), eq(notificationDataAsBundle), eq(true))
}

Expand Down

0 comments on commit 144fd3d

Please sign in to comment.