-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
143d40e
commit d534647
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
testcommons/src/main/kotlin/net/natura/testcommons/actions/TextFieldActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.natura.testcommons.actions | ||
|
||
import android.view.View | ||
import android.widget.EditText | ||
import androidx.test.espresso.UiController | ||
import androidx.test.espresso.ViewAction | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom | ||
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility | ||
import com.natura.android.textfield.TextField | ||
import net.natura.testcommons.R | ||
import org.hamcrest.CoreMatchers.allOf | ||
import org.hamcrest.Matcher | ||
|
||
fun typeValueInTextField(text: String) = object : ViewAction { | ||
|
||
override fun getDescription(): String = "type $text in TextField" | ||
|
||
override fun getConstraints(): Matcher<View> { | ||
return allOf( | ||
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE), | ||
isAssignableFrom(TextField::class.java) | ||
) | ||
} | ||
|
||
override fun perform(uiController: UiController?, view: View?) { | ||
val editText = (view as TextField).findViewById<EditText>(R.id.text_field_input_value) | ||
editText.setText(text) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
testcommons/src/main/kotlin/net/natura/testcommons/assertions/TextFieldAssertions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.natura.testcommons.assertions | ||
|
||
import androidx.test.espresso.ViewAssertion | ||
import com.natura.android.textfield.TextField | ||
import org.junit.Assert.assertEquals | ||
|
||
fun hasErrorInTextField(expectedText: String) = ViewAssertion { view, noMatchingViewException -> | ||
noMatchingViewException?.let { throw it } | ||
|
||
if (view !is TextField) { | ||
throw AssertionError("View is not an instance of TextField") | ||
} | ||
|
||
assertEquals(TextField.State.ERROR, view.state) | ||
assertEquals(expectedText, view.error) | ||
} |
25 changes: 25 additions & 0 deletions
25
...commons/src/main/kotlin/net/natura/testcommons/extensions/InstantTaskExecutorExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.natura.testcommons.extensions | ||
|
||
import androidx.arch.core.executor.ArchTaskExecutor | ||
import androidx.arch.core.executor.TaskExecutor | ||
import org.junit.jupiter.api.extension.AfterEachCallback | ||
import org.junit.jupiter.api.extension.BeforeEachCallback | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
|
||
class InstantTaskExecutorExtension : BeforeEachCallback, AfterEachCallback { | ||
|
||
override fun beforeEach(context: ExtensionContext?) { | ||
ArchTaskExecutor.getInstance() | ||
.setDelegate(object : TaskExecutor() { | ||
override fun executeOnDiskIO(runnable: Runnable) = runnable.run() | ||
|
||
override fun postToMainThread(runnable: Runnable) = runnable.run() | ||
|
||
override fun isMainThread(): Boolean = true | ||
}) | ||
} | ||
|
||
override fun afterEach(context: ExtensionContext?) { | ||
ArchTaskExecutor.getInstance().setDelegate(null) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
testcommons/src/main/kotlin/net/natura/testcommons/matchers/DrawableMatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.natura.testcommons.matchers | ||
|
||
import android.view.View | ||
import android.widget.ImageView | ||
import androidx.annotation.DrawableRes | ||
import androidx.core.graphics.drawable.toBitmap | ||
import org.hamcrest.Description | ||
import org.hamcrest.TypeSafeMatcher | ||
|
||
fun withDrawable(@DrawableRes id: Int) = object : TypeSafeMatcher<View>() { | ||
|
||
override fun describeTo(description: Description) { | ||
description.appendText("ImageView has drawable with id $id") | ||
} | ||
|
||
override fun matchesSafely(view: View): Boolean { | ||
val context = view.context | ||
val expectedBitmap = context.getDrawable(id)?.toBitmap() | ||
|
||
return view is ImageView && view.drawable.toBitmap().sameAs(expectedBitmap) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
testcommons/src/main/kotlin/net/natura/testcommons/rules/IntentsRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.natura.testcommons.rules | ||
|
||
import androidx.test.espresso.intent.Intents | ||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
|
||
class IntentsRule : TestRule { | ||
|
||
override fun apply(base: Statement?, description: Description?) = object : Statement() { | ||
override fun evaluate() { | ||
try { | ||
Intents.init() | ||
base?.evaluate() | ||
} finally { | ||
Intents.release() | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
testcommons/src/main/kotlin/net/natura/testcommons/rules/KoinRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package net.natura.testcommons.rules | ||
|
||
import org.junit.rules.TestRule | ||
import org.junit.runner.Description | ||
import org.junit.runners.model.Statement | ||
import org.koin.core.context.startKoin | ||
import org.koin.core.context.stopKoin | ||
import org.koin.core.module.Module | ||
|
||
class KoinRule(private val injectedModule: Module? = null) : TestRule { | ||
|
||
override fun apply(base: Statement?, description: Description?) = object : Statement() { | ||
override fun evaluate() { | ||
try { | ||
startKoin { | ||
injectedModule?.let(::modules) | ||
} | ||
|
||
base?.evaluate() | ||
} finally { | ||
stopKoin() | ||
} | ||
} | ||
|
||
} | ||
} |