Skip to content

Commit

Permalink
Merge branch 'CORE/refactor/add-compose-tests' into 'main'
Browse files Browse the repository at this point in the history
add compose routing tests

See merge request veepee/offerdiscovery/products/front-mobile/android/link-router!16
  • Loading branch information
Julio Cesar Bueno Cotta committed Jun 12, 2022
2 parents 583308a + 3e04b0e commit 293a413
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class AActivity : AppCompatActivity(R.layout.a_activity) {

@Composable
private fun ARootComposition(modifier: Modifier = Modifier) {

LinkRouterContainer(router = router) {
Column {
ComposableFor(
Expand Down
2 changes: 1 addition & 1 deletion Sample/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencyResolutionManagement {
library('material', 'com.google.android.material', 'material').versionRef(version('material', '1.6.0'))
library('test.junit', 'junit', 'junit').versionRef(version('junit', '4.13.1'))
library('test.androidx.test.core', 'androidx.test', 'core-ktx').versionRef(version('core-test', '1.4.0'))
library('test.robolectric', 'org.robolectric', 'robolectric').versionRef(version('robolectric', '4.6.1'))
library('test.robolectric', 'org.robolectric', 'robolectric').versionRef(version('robolectric', '4.8.1'))
library('test.mockito.inline', 'org.mockito', 'mockito-inline').versionRef(version('mockito-inline', '3.11.2'))
library('test.mockito.kotlin', 'org.mockito.kotlin', 'mockito-kotlin').versionRef(version('mockito-kotlin', '3.2.0'))
def composeVersion = version('compose', '1.2.0-beta03')
Expand Down
9 changes: 9 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ android {
composeOptions {
kotlinCompilerExtensionVersion '1.2.0-beta03'
}

testOptions {
unitTests {
includeAndroidResources = true
}
}
}

dependencies {
Expand All @@ -51,6 +57,9 @@ dependencies {
testImplementation libs.test.mockito.inline
testImplementation libs.test.mockito.kotlin
testImplementation libs.test.androidx.test.core

testImplementation libs.androidx.compose.ui.test
testImplementation libs.androidx.compose.ui.test.manifest
}

task sourceJar(type: Jar) {
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/java/com/veepee/vpcore/route/LinkRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class LinkRouterBuilder(
private val fragmentLinkRouterBuilder: FragmentLinkRouter.Builder,
private val deepLinkRouterBuilder: DeepLinkRouter.Builder,
private val composableLinkRouterBuilder: ComposableLinkRouter.Builder

) : LinkRouter.Builder {

override fun add(activityNameMapper: ActivityNameMapper<out ActivityName>): LinkRouter.Builder {
Expand Down Expand Up @@ -134,6 +135,7 @@ class LinkRouterBuilder(

override fun newBuilder(): LinkRouter.Builder {
return LinkRouterBuilder(

activityLinkRouterBuilder.newBuilder(),
fragmentLinkRouterBuilder.newBuilder(),
deepLinkRouterBuilder.newBuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ class ActivityLinkRouterTest {
}
val router = ActivityLinkRouterImpl(mappers, ChainFactoryImpl(listOf(interceptor)))
val activity: Activity = mock()
val intent1 = router.intentFor(activity, TestActivityALink)
assertEquals(intent1.component!!.className, TestActivityB::class.java.name)
val intent = router.intentFor(activity, TestActivityALink)
assertEquals(intent.component!!.className, TestActivityB::class.java.name)
}

private fun assertIntentFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import com.veepee.vpcore.route.activity.route.TestActivityBParameter
import com.veepee.vpcore.route.requireLinkParameter

internal class TestActivityB : Activity() {
private val param by lazy { intent.requireLinkParameter<TestActivityBParameter>() }
private val param by lazy { requireLinkParameter<TestActivityBParameter>() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.veepee.vpcore.route.composable.feature.TestComposablesNameMapper
import com.veepee.vpcore.route.composable.route.TestComposableALink
import com.veepee.vpcore.route.composable.route.TestComposableBLink
import com.veepee.vpcore.route.composable.route.TestComposableBParameter
import com.veepee.vpcore.route.link.compose.ComposableLink
import com.veepee.vpcore.route.link.compose.ComposableLinkRouterImpl
import com.veepee.vpcore.route.link.compose.ComposableName
import com.veepee.vpcore.route.link.compose.ComposableNameMapper
import com.veepee.vpcore.route.link.compose.NoComposableNameMapperException
import com.veepee.vpcore.route.link.compose.chain.ComposableLinkInterceptor
import com.veepee.vpcore.route.link.interceptor.Chain
import com.veepee.vpcore.route.link.interceptor.ChainFactoryImpl
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(
instrumentedPackages = ["androidx.loader.content"]
)
class ComposableLinkRouterTest {

@get:Rule
val composeTestRule = createComposeRule()
private val mappers: Set<ComposableNameMapper<out ComposableName>> =
setOf(TestComposablesNameMapper)

@Test
fun `should fail to route due to missing composable name mapper`() {
val router = ComposableLinkRouterImpl(emptySet(), ChainFactoryImpl(emptyList()))
Assert.assertThrows(NoComposableNameMapperException::class.java) {
composeTestRule.setContent {
router.ComposeFor(TestComposableALink())
}
}
}

@Test
fun `should route and display text within composable`() {
val router = ComposableLinkRouterImpl(mappers, ChainFactoryImpl(emptyList()))

composeTestRule.setContent {
router.ComposeFor(TestComposableALink())
}

composeTestRule
.onNodeWithText("Hello World")
.assertIsDisplayed()
}

@Test
fun `should route and display text given to composable`() {
val router = ComposableLinkRouterImpl(mappers, ChainFactoryImpl(emptyList()))

composeTestRule.setContent {
router.ComposeFor(TestComposableBLink(TestComposableBParameter("message")))
}

composeTestRule
.onNodeWithText("message")
.assertIsDisplayed()
}

@Test
fun `should intercept route and display other composable`() {
val router = ComposableLinkRouterImpl(
mappers,
ChainFactoryImpl(listOf(object : ComposableLinkInterceptor {
override fun intercept(
chain: Chain<ComposableNameMapper<out ComposableName>, ComposableLink<ComposableName>>,
mapper: ComposableNameMapper<out ComposableName>,
link: ComposableLink<ComposableName>
): ComposableLink<ComposableName> {
if (link is TestComposableBLink) {
return TestComposableALink()
}
return chain.next(mapper, link)
}
}))
)

composeTestRule.setContent {
router.ComposeFor(TestComposableBLink(TestComposableBParameter("message")))
}

composeTestRule
.onNodeWithText("Hello World")
.assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.feature

import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun TestComposableA(modifier: Modifier) {
BasicText(text = "Hello World", modifier)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.feature

import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

@Composable
fun TestComposableB(message: String, modifier: Modifier) {
BasicText(text = message, modifier)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.feature

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.veepee.vpcore.route.composable.route.TestComposableALink
import com.veepee.vpcore.route.composable.route.TestComposableBLink
import com.veepee.vpcore.route.composable.route.TestComposableName
import com.veepee.vpcore.route.link.compose.ComposableLink
import com.veepee.vpcore.route.link.compose.ComposableNameMapper

object TestComposablesNameMapper : ComposableNameMapper<TestComposableName> {
override val supportedNames: Array<TestComposableName> = TestComposableName.values()

@Composable
override fun Map(composableLink: ComposableLink<TestComposableName>, modifier: Modifier) {
when (composableLink) {
is TestComposableALink -> TestComposableA(modifier = modifier)
is TestComposableBLink -> TestComposableB(
composableLink.parameter.message,
modifier = modifier
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.route

import com.veepee.vpcore.route.link.compose.ComposableLink
import com.veepee.vpcore.route.link.compose.ComposableParameter

class TestComposableALink : ComposableLink<TestComposableName> {
override val composableName: TestComposableName = TestComposableName.TestComposableA
override val parameter: ComposableParameter? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.route

import com.veepee.vpcore.route.link.compose.ComposableLink
import com.veepee.vpcore.route.link.compose.ComposableParameter

class TestComposableBLink(
override val parameter: TestComposableBParameter
) : ComposableLink<TestComposableName> {
override val composableName: TestComposableName = TestComposableName.TestComposableB
}

data class TestComposableBParameter(val message: String) : ComposableParameter
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2021, Veepee
*
* Permission to use, copy, modify, and/or distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright notice
* and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
* THIS SOFTWARE.
*/
package com.veepee.vpcore.route.composable.route

import com.veepee.vpcore.route.link.compose.ComposableName

enum class TestComposableName : ComposableName {
TestComposableA,
TestComposableB,
}
6 changes: 4 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ dependencyResolutionManagement {
library('material.components', 'com.google.android.material', 'material').versionRef(version('material', '1.6.0'))
library('test.junit', 'junit', 'junit').versionRef(version('junit', '4.13.1'))
library('test.androidx.test.core', 'androidx.test', 'core-ktx').versionRef(version('core-test', '1.4.0'))
library('test.robolectric', 'org.robolectric', 'robolectric').versionRef(version('robolectric', '4.6.1'))
library('test.robolectric', 'org.robolectric', 'robolectric').versionRef(version('robolectric', '4.8.1'))
library('test.mockito.inline', 'org.mockito', 'mockito-inline').versionRef(version('mockito-inline', '3.11.2'))
library('test.mockito.kotlin', 'org.mockito.kotlin', 'mockito-kotlin').versionRef(version('mockito-kotlin', '3.2.0'))

def composeVersion = version('compose', '1.2.0-beta03')
library('androidx.compose.foundation', 'androidx.compose.foundation', 'foundation').versionRef(composeVersion)

library('androidx.compose.ui.test', 'androidx.compose.ui', 'ui-test-junit4').versionRef(composeVersion)
library('androidx.compose.ui.test.manifest', 'androidx.compose.ui', 'ui-test-manifest').versionRef(composeVersion)
}
}
}

0 comments on commit 293a413

Please sign in to comment.