Skip to content

Commit

Permalink
Merge pull request #303 from wealthfront/fix-import
Browse files Browse the repository at this point in the history
Add basic Integration Test for Compose flavor
  • Loading branch information
cmathew authored Oct 31, 2024
2 parents 2376ca4 + f18cd47 commit 64893c8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
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()
}
}

0 comments on commit 64893c8

Please sign in to comment.