Skip to content

Commit

Permalink
Create compose base (#439)
Browse files Browse the repository at this point in the history
* Crete compose base

* Rollback files

* Move test to barista-compose module

* Methods with StringRes working

* Update assertIsNotDisplayed

* Add assertIsDisplayed and assertIsNotDisplayed to SemanticsNodeInteraction

* Remove node version of assertIsDisplayed() and assertIsNotDisplayed()

* Rename methods to assertDisplayed() and assertNotDisplayed() to match Barista API

* Reorder params so we can have same signatures

* Simplify tests

* Remove unused composable

* Remove compose from sample

* Remove unneded files

* Make sure build on Github CI runs compose tests

* Cleanup test

* Cleanup test

* Update barista compose to allow publish
  • Loading branch information
alorma authored Sep 23, 2021
1 parent 403c97f commit 3f0e105
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ jobs:
arch: x86
profile: pixel_2
disable-animations: true
script: ./gradlew :sample:connectedCheck
script: ./gradlew :sample:connectedCheck :barista-compose:connectedCheck
1 change: 1 addition & 0 deletions barista-compose/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
50 changes: 50 additions & 0 deletions barista-compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
plugins {
id("com.android.library")
kotlin("android")
}

apply(from = "../config/android-quality.gradle")

ext["PUBLISH_ARTIFACT_ID"] = "barista-compose"

apply(from = "${rootProject.projectDir}/scripts/publish-module.gradle")

android {
compileSdk = 31

defaultConfig {
minSdk = 21
targetSdk = 31

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["clearPackageData"] = "true"
}

buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = rootProject.extra["compose_version"] as String
}

packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
api("androidx.test.ext:junit:1.1.3")
api("androidx.compose.ui:ui-test-junit4:1.0.2")

debugImplementation("androidx.compose.ui:ui:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.compose.material:material:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.compose.ui:ui-tooling-preview:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
debugImplementation("androidx.activity:activity-compose:1.3.1")
debugImplementation("androidx.compose.ui:ui-test-junit4:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.compose.ui:ui-test-manifest:${rootProject.extra["compose_version"]}")
debugImplementation("androidx.compose.ui:ui-tooling:${rootProject.extra["compose_version"]}")

}
69 changes: 69 additions & 0 deletions barista-compose/src/androidTest/java/ComposeAssertionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.alorma.barista_compose.assertion

import androidx.compose.ui.res.stringResource
import androidx.compose.ui.test.junit4.createComposeRule
import com.adevinta.android.barista.sample.TextComposable
import com.alorma.barista_compose.R
import org.junit.Rule
import org.junit.Test

class ComposeAssertionsTest {

@get:Rule
val composeTestRule = createComposeRule()

// assertDisplayed tests
@Test
fun assertDisplayed_StringTest() {
composeTestRule.setContent {
TextComposable("Hello world")
}

composeTestRule.assertDisplayed("Hello world")
}

@Test
fun assertDisplayed_ResourceTest() {
composeTestRule.setContent {
TextComposable(stringResource(R.string.app_name))
}

composeTestRule.assertDisplayed(R.string.app_name)
}

@Test(expected = AssertionError::class)
fun assertDisplayed_ResourceTest_fail() {
composeTestRule.setContent {
TextComposable(stringResource(R.string.app_name))
}

composeTestRule.assertDisplayed(R.string.next)
}

// assertNotDisplayed tests
@Test
fun assertNotDisplayed_StringTest() {
val text = "Hello world"
composeTestRule.setContent { TextComposable(text) }

composeTestRule.assertNotDisplayed("next")
}

@Test
fun assertNotDisplayed_ResourceTest() {
composeTestRule.setContent {
TextComposable(stringResource(R.string.app_name))
}

composeTestRule.assertNotDisplayed(R.string.next)
}

@Test(expected = AssertionError::class)
fun assertNotDisplayed_ResourceTest_fail() {
composeTestRule.setContent {
TextComposable(stringResource(R.string.next))
}

composeTestRule.assertNotDisplayed(R.string.next)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.adevinta.android.barista.sample

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun TextComposable(
text: String
) {
Box(modifier = Modifier.padding(16.dp)) {
Text(text = text)
}
}
5 changes: 5 additions & 0 deletions barista-compose/src/debug/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Barista</string>
<string name="next">Next</string>
</resources>
6 changes: 6 additions & 0 deletions barista-compose/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alorma.barista_compose"
>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.alorma.barista_compose.assertion

import androidx.annotation.StringRes
import androidx.compose.ui.test.SemanticsNodeInteraction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.onNodeWithText
import com.alorma.barista_compose.assertion.internal.resources

fun ComposeTestRule.assertDisplayed(
text: String,
useUnmergedTree: Boolean = false,
substring: Boolean = false,
ignoreCase: Boolean = false
): SemanticsNodeInteraction {
return onNodeWithText(
text = text,
substring = substring,
ignoreCase = ignoreCase,
useUnmergedTree = useUnmergedTree
).assertIsDisplayed()
}

fun ComposeTestRule.assertDisplayed(
@StringRes textRes: Int,
useUnmergedTree: Boolean = false,
substring: Boolean = false,
ignoreCase: Boolean = false,
): SemanticsNodeInteraction {
return assertDisplayed(resources().getString(textRes), useUnmergedTree, substring, ignoreCase)
}

fun ComposeTestRule.assertNotDisplayed(
text: String,
useUnmergedTree: Boolean = false,
substring: Boolean = false,
ignoreCase: Boolean = false
) {
return onNodeWithText(text, substring, ignoreCase, useUnmergedTree).assertDoesNotExist()
}

fun ComposeTestRule.assertNotDisplayed(
@StringRes textRes: Int,
useUnmergedTree: Boolean = false,
substring: Boolean = false,
ignoreCase: Boolean = false
) {
return assertNotDisplayed(resources().getString(textRes), useUnmergedTree, substring, ignoreCase)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.alorma.barista_compose.assertion.internal

import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.ComposeTestRule

internal fun ComposeTestRule.resources() = if (this is AndroidComposeTestRule<*, *>) {
activity.resources
} else {
throw RuntimeException("ComposeTestRule is not AndroidComposeTestRule")
}
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
buildscript {
ext {
compose_version = '1.0.2'
}
repositories {
maven { url "https://plugins.gradle.org/m2/" }
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.adevinta.android.barista.internal.viewaction.ViewPager2SwipeAction.Di
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anyOf
import java.util.Locale

object SwipeActions {

Expand Down Expand Up @@ -142,7 +143,7 @@ private class GeneralSwipeWithPartiallyVisibleViewAction(
}

override fun getDescription(): String {
return swiper.toString().toLowerCase() + " swipe"
return swiper.toString().lowercase(Locale.getDefault()) + " swipe"
}

companion object {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gradleEnterprise {
}
}
include ':sample', ':library'
include ':barista-compose'

0 comments on commit 3f0e105

Please sign in to comment.