Skip to content

Commit

Permalink
Remove ActivityTestRule from the Read.me + Replace FragmentTestActivi…
Browse files Browse the repository at this point in the history
…ty with FragmentScenario

Solve detekt issues
  • Loading branch information
sergio-sastre committed Aug 8, 2022
1 parent 0b81991 commit 2035688
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 141 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ See the example below:
@Test
fun shouldPassOnNoInternetScanTest() =
beforeTest {
activityTestRule.launchActivity(null)
// some things with the state
}.afterTest {
// some things with the state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,24 @@ The base class for all docloc screenshot tests.

Project-wide ScreenshotTestCase should be implemented as following:

open class ProductDocLocScreenshotTestCase(testName: String) : DocLocScreenshotTestCase(
File(testName), "comma-separated string of locales"
) {
class AdvancedScreenshotSampleTest : DocLocScreenshotTestCase(locales = "en,ru") {
private lateinit var view: ScreenshotSampleView

@get:Rule
val activityTestRule = ActivityTestRule(FragmentTestActivity::class.java, true, false)

protected lateinit var activity: FragmentTestActivity

@Before
open fun setUp() {
activity = activityTestRule.launchActivity(null)
}
}

Screenshoter test extends the project-wide class:

@ScreenShooterTest
class FeatureScreenshot : ProductDocLocScreenshotTestCase("feature_screenshot") {

@Test
fun featureScreen() {
val featureView = FeatureFragment.newInstance()
activity.setFragment(featureView)
val view = getUiSafeProxy<FeatureView>(featureView) // Explicit type is important and must be interface

view.showLoading()
captureScreenshot("screenshot_description")
}
}

As you might have noticed, activity test rule is launched with ``FragmentTestActivity``. It's a special per-project empty activity for test with ``setFragment(Fragment)`` method. E.g:

class FragmentTestActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fragment_container)
}

fun setFragment(fragment: Fragment) {
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.content_container, fragment, "")
fragmentTransaction.build()
}
@ScreenShooterTest
@Test
fun test() = before {
val scenario = launchFragmentInContainer<ScreenshotSampleFragment>()
scenario.onFragment {
view = getUiSafeProxy(it as ScreenshotSampleView)
}
}.after {
}.run {
step("1. Step 1") {
// ... [view] calls
captureScreenshot("Step 1")
}
...
}
}

abstract class [DocLocScreenshotTestCase](index.md)(**screenshotsDirectory**: [File](https://docs.oracle.com/javase/8/docs/api/java/io/File.html), **screenshotDirectoryProvider**: [ScreenshotDirectoryProvider](../../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-directory-provider/index.md), **screenshotNameProvider**: [ScreenshotNameProvider](../../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-name-provider/index.md), **changeSystemLocale**: [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html), **locales**: [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?, **kaspressoBuilder**: [Kaspresso.Builder](../../com.kaspersky.kaspresso.kaspresso/-kaspresso/-builder/index.md)) : [TestCase](../-test-case/index.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
| Name| Summary|
|---|---|
| [BaseTestCase](-base-test-case/index.md)| [androidJvm] <br>Brief description <br><br><br>The base class for all parametrized test cases. Extend this class with a single base project-wide inheritor of [TestCase](-test-case/index.md) as a parent for all actual project-wide test cases. Nesting test cases are not recommended, use [com.kaspersky.kaspresso.testcases.api.scenario.Scenario](../com.kaspersky.kaspresso.testcases.api.scenario/-scenario/index.md) instead.<br><br> <br>Content <br>abstract class [BaseTestCase](-base-test-case/index.md)<[InitData](-base-test-case/index.md), [Data](-base-test-case/index.md)>(**kaspressoBuilder**: [Kaspresso.Builder](../com.kaspersky.kaspresso.kaspresso/-kaspresso/-builder/index.md), **dataProducer**: ([InitData](-base-test-case/index.md).() -> [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)?) -> [Data](-base-test-case/index.md), **mainSectionEnrichers**: [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<[MainSectionEnricher](../com.kaspersky.kaspresso.enricher/-main-section-enricher/index.md)<[Data](-base-test-case/index.md)>>) : [TestAssistantsProvider](../com.kaspersky.kaspresso.testcases.core.testassistants/-test-assistants-provider/index.md) <br><br><br>
| [DocLocScreenshotTestCase](-doc-loc-screenshot-test-case/index.md)| [androidJvm] <br>Brief description <br><br><br><br><br>The base class for all docloc screenshot tests.<br><br><br><br>Project-wide ScreenshotTestCase should be implemented as following:<br><br> open class ProductDocLocScreenshotTestCase(testName: String) : DocLocScreenshotTestCase( <br> File(testName), "comma-separated string of locales" <br> ) { <br> <br> @get:Rule <br> val activityTestRule = ActivityTestRule(FragmentTestActivity::class.java, true, false) <br> <br> protected lateinit var activity: FragmentTestActivity <br> <br> @Before <br> open fun setUp() { <br> activity = activityTestRule.launchActivity(null) <br> } <br> }<br><br>Screenshoter test extends the project-wide class:<br><br> @ScreenShooterTest <br> class FeatureScreenshot : ProductDocLocScreenshotTestCase("feature_screenshot") { <br> <br> @Test <br> fun featureScreen() { <br> val featureView = FeatureFragment.newInstance() <br> activity.setFragment(featureView) <br> val view = getUiSafeProxy<FeatureView>(featureView) // Explicit type is important and must be interface <br> <br> view.showLoading() <br> captureScreenshot("screenshot_description") <br> } <br> }<br><br>As you might have noticed, activity test rule is launched with ``FragmentTestActivity``. It's a special per-project empty activity for test with ``setFragment(Fragment)`` method. E.g:<br><br> class FragmentTestActivity : AppCompatActivity() { <br> <br> override fun onCreate(savedInstanceState: Bundle?) { <br> super.onCreate(savedInstanceState) <br> setContentView(R.layout.activity_fragment_container) <br> } <br> <br> fun setFragment(fragment: Fragment) { <br> val fragmentTransaction = supportFragmentManager.beginTransaction() <br> fragmentTransaction.replace(R.id.content_container, fragment, "") <br> fragmentTransaction.build() <br> } <br> }<br><br> <br>Content <br>abstract class [DocLocScreenshotTestCase](-doc-loc-screenshot-test-case/index.md)(**screenshotsDirectory**: [File](https://docs.oracle.com/javase/8/docs/api/java/io/File.html), **screenshotDirectoryProvider**: [ScreenshotDirectoryProvider](../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-directory-provider/index.md), **screenshotNameProvider**: [ScreenshotNameProvider](../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-name-provider/index.md), **changeSystemLocale**: [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html), **locales**: [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?, **kaspressoBuilder**: [Kaspresso.Builder](../com.kaspersky.kaspresso.kaspresso/-kaspresso/-builder/index.md)) : [TestCase](-test-case/index.md) <br><br><br>
| [DocLocScreenshotTestCase](-doc-loc-screenshot-test-case/index.md)| [androidJvm] <br>Brief description <br><br><br><br><br>The base class for all docloc screenshot tests.<br><br><br><br>Project-wide ScreenshotTestCase should be implemented as following:<br><br> class AdvancedScreenshotSampleTest(DocLocScreenshotTestCase(locales = "en,ru"){ <br> <br> protected lateinit var view: ScreenshotSampleView <br> <br> @ScreenShooterTest <br> @Test <br> fun featureScreen() = before { <br> val scenario = launchFragmentInContainer<ScreenshotSampleFragment>() <br> scenario.onFragment { <br> view = getUiSafeProxy(it as ScreenshotSampleView) // Explicit type is important and must be interface <br> } <br> }.after { <br>}.run { <br> view.showLoading() <br> captureScreenshot("screenshot_description") <br> } <br> }<br><br> <br>Content <br>abstract class [DocLocScreenshotTestCase](-doc-loc-screenshot-test-case/index.md)(**screenshotsDirectory**: [File](https://docs.oracle.com/javase/8/docs/api/java/io/File.html), **screenshotDirectoryProvider**: [ScreenshotDirectoryProvider](../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-directory-provider/index.md), **screenshotNameProvider**: [ScreenshotNameProvider](../com.kaspersky.kaspresso.device.screenshots.screenshotfiles/-screenshot-name-provider/index.md), **changeSystemLocale**: [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html), **locales**: [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)?, **kaspressoBuilder**: [Kaspresso.Builder](../com.kaspersky.kaspresso.kaspresso/-kaspresso/-builder/index.md)) : [TestCase](-test-case/index.md) <br><br><br>
| [TestCase](-test-case/index.md)| [androidJvm] <br>Brief description <br><br><br>The base class for all test cases. Extend this class with a single base project-wide inheritor of [TestCase](-test-case/index.md) as a parent for all actual project-wide test cases. Nesting test cases are not permitted because they may produce an exception caused by re-initialization of the [Kaspresso](../com.kaspersky.kaspresso.kaspresso/-kaspresso/index.md), use [com.kaspersky.kaspresso.testcases.api.scenario.Scenario](../com.kaspersky.kaspresso.testcases.api.scenario/-scenario/index.md) instead.<br><br> <br>Content <br>abstract class [TestCase](-test-case/index.md)(**kaspressoBuilder**: [Kaspresso.Builder](../com.kaspersky.kaspresso.kaspresso/-kaspresso/-builder/index.md)) : [BaseTestCase](-base-test-case/index.md)<[Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html), [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html)> <br><br><br>

Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,3 @@ class ComplexComposeTest : TestCase() {
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class ContinuouslyTest : TestCase() {
}
}


@Test
fun testDialogNotPresentAfterAndroidO() {
// Don`t allow to run this test on Android < Oreo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class UiContinuouslyTest : TestCase() {
}
}


@Test
fun testDialogNotPresentAfterAndroidO() {
// Don`t allow to run this test on Android < Oreo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.kaspersky.kaspressample.docloc_tests.advanced

import android.Manifest
import android.graphics.Color
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.docloc.FragmentTestActivity
import com.kaspersky.kaspressample.docloc.ScreenshotSampleFragment
import com.kaspersky.kaspressample.docloc.ScreenshotSampleView
import com.kaspersky.kaspresso.annotations.ScreenShooterTest
Expand All @@ -17,8 +16,6 @@ import org.junit.Test
* For more information see DocLoc wiki page.
*/
class AdvancedScreenshotSampleTest : DocLocScreenshotTestCase(locales = "en,ru") {

private lateinit var fragment: ScreenshotSampleFragment
private lateinit var view: ScreenshotSampleView

@get:Rule
Expand All @@ -27,16 +24,12 @@ class AdvancedScreenshotSampleTest : DocLocScreenshotTestCase(locales = "en,ru")
Manifest.permission.READ_EXTERNAL_STORAGE
)

@get:Rule
val activityRule = activityScenarioRule<FragmentTestActivity>()

@ScreenShooterTest
@Test
fun test() = before {
fragment = ScreenshotSampleFragment()
view = getUiSafeProxy(fragment as ScreenshotSampleView)
activityRule.scenario.onActivity { activity ->
activity.setFragment(fragment)
val scenario = launchFragmentInContainer<ScreenshotSampleFragment>()
scenario.onFragment {
view = getUiSafeProxy(it as ScreenshotSampleView)
}
}.after {
}.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.kaspersky.kaspressample.docloc_tests.advanced

import android.Manifest
import android.graphics.Color
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.rule.GrantPermissionRule
import com.kaspersky.kaspressample.docloc.FragmentTestActivity
import com.kaspersky.kaspressample.docloc.ScreenshotSampleFragment
import com.kaspersky.kaspressample.docloc.ScreenshotSampleView
import com.kaspersky.kaspresso.annotations.ScreenShooterTest
Expand All @@ -21,8 +20,6 @@ class AdvancedScreenshotSampleTestLegacy : DocLocScreenshotTestCase(
screenshotsDirectory = File("screenshots"),
locales = "en,ru"
) {

private lateinit var fragment: ScreenshotSampleFragment
private lateinit var view: ScreenshotSampleView

@get:Rule
Expand All @@ -31,16 +28,12 @@ class AdvancedScreenshotSampleTestLegacy : DocLocScreenshotTestCase(
Manifest.permission.READ_EXTERNAL_STORAGE
)

@get:Rule
val activityRule = activityScenarioRule<FragmentTestActivity>()

@ScreenShooterTest
@Test
fun test() = before {
fragment = ScreenshotSampleFragment()
view = getUiSafeProxy(fragment as ScreenshotSampleView)
activityRule.scenario.onActivity { activity ->
activity.setFragment(fragment)
val scenario = launchFragmentInContainer<ScreenshotSampleFragment>()
scenario.onFragment {
view = getUiSafeProxy(it as ScreenshotSampleView)
}
}.after {
}.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ class InitTransformDataTest : BaseParametrizedTest() {
}
}
}

4 changes: 0 additions & 4 deletions samples/kaspresso-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
android:name=".continuously.ContinuouslySampleActivity"
android:label="@string/main_screen_continuously_sample_button"/>

<activity
android:name=".docloc.FragmentTestActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"/>

<activity
android:name=".device.DeviceSampleActivity"
android:label="@string/device_sample_title"/>
Expand Down

This file was deleted.

6 changes: 2 additions & 4 deletions wiki/02_Wrapper_over_UiAutomator.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,11 @@ class KautomatorMeasureTest : TestCase(
)

@get:Rule
val activityTestRule = ActivityTestRule(MainActivity::class.java, true, false)
val activityRule = activityScenarioRule<MainActivity>()

@Test
fun test() =
before {
activityTestRule.launchActivity(null)
}.after { }.run {
before { }.after { }.run {

======> UI Automator: 0 minutes, 1 seconds and 252 millis
======> UI Automator boost: 0 minutes, 0 seconds and 310 millis
Expand Down
1 change: 0 additions & 1 deletion wiki/04_How_to_write_autotests.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ All of the above mentioned inspired us to create the test's structure like below
@Test
fun shouldPassOnNoInternetScanTest() =
before {
activityTestRule.launchActivity(null)
// some things with the state
}.after {
// some things with the state
Expand Down
3 changes: 1 addition & 2 deletions wiki/05_Device.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ Device instance is available in `BaseTestContext` scope and `BaseTestCase` via `
@Test
fun test() =
run {
step("Open Simple Screen") {
activityTestRule.launchActivity(null)
step("On Simple Screen opened") {
======> device.screenshots.take("Additional_screenshot") <======

MainScreen {
Expand Down
3 changes: 1 addition & 2 deletions wiki/06_AdbServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ In Kaspresso, we wrap `AdbTerminal` into a special interface `AdbServer`.
@Test
fun test() =
run {
step("Open Simple Screen") {
activityTestRule.launchActivity(null)
step("On Simple Screen opened") {
======> adbServer.performShell("input text 1") <======

MainScreen {
Expand Down
53 changes: 11 additions & 42 deletions wiki/07_DocLoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,53 +132,22 @@ In most cases, there is no need to launch certain activity, do a lot of steps be
Also, when you use Model-View-Presenter architectural pattern, you are able to control UI state
directly through the View interface. So, there is no need to interact with the application interface and wait for changes.

First create a base test activity with `setFragment(Fragment)` method in your application:

```kotlin
class FragmentTestActivity : AppCompatActivity() {

fun setFragment(fragment: Fragment) = with(supportFragmentManager.beginTransaction()) {
replace(android.R.id.content, fragment)
commit()
}
}
```

Then add a base product screenshot test case:

```kotlin
open class ProductDocLocScreenshotTestCase : DocLocScreenshotTestCase(
locales = "en,ru"
) {

@get:Rule
val activityTestRule = ActivityTestRule(FragmentTestActivity::class.java, false, true)

protected val activity: FragmentTestActivity
get() = activityTestRule.activity

}
```

This test case would run your `FragmentTestActivity` on startup. Now you are able to write your screenshooter tests.
For example, create a new test class which extends `ProductDocLocScreenshotTestCase`:
We can achieve that by using a `FragmentScenario`, like in the example below:

```kotlin
@RunWith(AndroidJUnit4::class)
class AdvancedScreenshotSampleTest : ProductDocLocScreenshotTestCase() {

private lateinit var fragment: FeatureFragment
private lateinit var view: FeatureView
class AdvancedScreenshotSampleTest : DocLocScreenshotTestCase(locales = "en,ru") {
private lateinit var view: ScreenshotSampleView

@ScreenShooterTest
@Test
fun test() {
before {
fragment = FeatureFragment()
view = getUiSafeProxy(fragment as FeatureView)
activity.setFragment(fragment)
}.after {
}.run {
fun test() = before {
val scenario = launchFragmentInContainer<ScreenshotSampleFragment>()
scenario.onFragment {
view = getUiSafeProxy(it as ScreenshotSampleView)
}
}.after {
}.run {

step("1. Step 1") {
// ... [view] calls
Expand All @@ -201,7 +170,7 @@ class AdvancedScreenshotSampleTest : ProductDocLocScreenshotTestCase() {
}
```

As you might notice, the `getUiSafeProxy` method called to get an instance of `FeatureView`.
As you might notice, the `getUiSafeProxy` method called to get an instance of `ScreenshotSampleView`.
This method wraps your View interface and returns a proxy on it.
The proxy guarantees that all the methods of the View interface you called, will be invoked on the main thread.
There is also `getUiSafeProxyFromImplementation` which wraps an implementation rather than an interface.
Expand Down
2 changes: 1 addition & 1 deletion wiki/08_Kaspresso-Robolectric.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class FailingSharedTest : TestCase() {
)

@get:Rule
val activityTestRule = ActivityTestRule(DeviceSampleActivity::class.java, false, true)
val activityRule = activityScenarioRule<DeviceSampleActivity>()

@Test
fun exploitSampleTest() =
Expand Down

0 comments on commit 2035688

Please sign in to comment.