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

Add basic Integration Test for Compose flavor #303

Merged
merged 3 commits into from
Oct 31, 2024
Merged
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
6 changes: 5 additions & 1 deletion magellan-sample-migration/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
implementation libs.compose.material3

kaptTest libs.daggerCompiler
testImplementation(platform(libs.compose.bom))
testImplementation libs.junit
testImplementation libs.testCore
testImplementation libs.truth
Expand All @@ -99,9 +100,10 @@ dependencies {
testImplementation libs.truth
// testImplementation libs.espressoCore
testImplementation libs.compose.junit4
testImplementation libs.compose.manifest
debugImplementation libs.compose.manifest

kaptAndroidTest libs.daggerCompiler
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation libs.extJunit
androidTestImplementation libs.espressoCore
androidTestImplementation libs.espressoContrib
Expand All @@ -110,6 +112,8 @@ dependencies {
androidTestImplementation libs.testRunner
androidTestImplementation libs.testRules
androidTestImplementation libs.mockitoAndroid
androidTestImplementation libs.uiAutomator
androidTestImplementation libs.compose.junit4

androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestUtil 'androidx.test:orchestrator:1.4.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.wealthfront.magellan.sample.migration

import android.provider.Settings.Global.ANIMATOR_DURATION_SCALE
import android.provider.Settings.Global.TRANSITION_ANIMATION_SCALE
import android.provider.Settings.Global.WINDOW_ANIMATION_SCALE
import android.util.Log
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

private const val TRANSITION_ANIMATION_SETTINGS = "settings put global $TRANSITION_ANIMATION_SCALE"
private const val WINDOW_ANIMATION_SETTINGS = "settings put global $WINDOW_ANIMATION_SCALE"
private const val ANIMATOR_ANIMATION_SETTINGS = "settings put global $ANIMATOR_DURATION_SCALE"
private const val DISABLE_KEYBOARD_SETTINGS = "settings put secure show_ime_with_hard_keyboard"

class DisableAnimationsAndKeyboardRule : TestRule {

override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
override fun evaluate() {
disableAnimationsAndKeyboard()
try {
base.evaluate()
} finally {
enableAnimationsAndKeyboard()
}
}
}
}

private fun enableAnimationsAndKeyboard() {
Log.v(
DisableAnimationsAndKeyboardRule::class.java.simpleName,
"Enabling animations and keyboard"
)
executeUiDeviceCommand("$TRANSITION_ANIMATION_SETTINGS 1")
executeUiDeviceCommand("$WINDOW_ANIMATION_SETTINGS 1")
executeUiDeviceCommand("$ANIMATOR_ANIMATION_SETTINGS 1")
executeUiDeviceCommand("$DISABLE_KEYBOARD_SETTINGS 1")
}

private fun disableAnimationsAndKeyboard() {
Log.v(
DisableAnimationsAndKeyboardRule::class.java.simpleName,
"Disabling animations and keyboard"
)
executeUiDeviceCommand("$TRANSITION_ANIMATION_SETTINGS 0")
executeUiDeviceCommand("$WINDOW_ANIMATION_SETTINGS 0")
executeUiDeviceCommand("$ANIMATOR_ANIMATION_SETTINGS 0")
executeUiDeviceCommand("$DISABLE_KEYBOARD_SETTINGS 0")
}

private fun executeUiDeviceCommand(command: String) {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).executeShellCommand(command)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.CoroutineIdlingRule
import com.wealthfront.magellan.sample.migration.DisableAnimationsAndKeyboardRule
import com.wealthfront.magellan.sample.migration.MainActivity
import com.wealthfront.magellan.sample.migration.R
import com.wealthfront.magellan.sample.migration.TestAppComponent
Expand All @@ -26,6 +27,9 @@ import javax.inject.Inject

class NavigationTest {

@Rule @JvmField
val disableAnimationsAndKeyboardRule = DisableAnimationsAndKeyboardRule()

@Rule @JvmField
val coroutineIdlingRule = CoroutineIdlingRule()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.wealthfront.magellan.sample.migration.uitest

import android.app.Application
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.core.app.launchActivity
import com.wealthfront.magellan.sample.migration.AppComponentContainer
import com.wealthfront.magellan.sample.migration.CoroutineIdlingRule
import com.wealthfront.magellan.sample.migration.DisableAnimationsAndKeyboardRule
import com.wealthfront.magellan.sample.migration.MainActivity
import com.wealthfront.magellan.sample.migration.TestAppComponent
import com.wealthfront.magellan.sample.migration.api.DogApi
import com.wealthfront.magellan.sample.migration.api.DogBreedsResponse
import com.wealthfront.magellan.sample.migration.api.DogImageResponse
import com.wealthfront.magellan.sample.migration.coWhen
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import javax.inject.Inject

class NavigationTest {

@Rule @JvmField
val disableAnimationsAndKeyboardRule = DisableAnimationsAndKeyboardRule()

@Rule @JvmField
val coroutineIdlingRule = CoroutineIdlingRule()

@Rule @JvmField
val composeRule: ComposeTestRule = createEmptyComposeRule()

@Inject lateinit var api: DogApi

private lateinit var activityScenario: ActivityScenario<MainActivity>

@Before
fun setup() {
val context = ApplicationProvider.getApplicationContext<Application>()
((context as AppComponentContainer).injector() as TestAppComponent).inject(this)

coWhen { api.getAllBreeds() }
.thenReturn(DogBreedsResponse(message = mapOf("robotic" to emptyList()), status = "success"))
coWhen { api.getRandomImageForBreed("robotic") }.thenReturn(
DogImageResponse(message = "image-url", status = "success")
)
}

@Test
fun visitRetriever() {
activityScenario = launchActivity()

composeRule.onNodeWithText("robotic").performClick()
}
}
Loading