Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose/basic/codelab #14

Merged
merged 33 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6b7cd3d
docs(Compose-Introduction): what is, why was introduced
sh1mj1 Dec 31, 2024
062f4d9
docs(Compose-Introduction): Problems with the traditional one and wha…
sh1mj1 Dec 31, 2024
a7a0f6f
docs(Compose-Introduction): advantage of compose, key concept, reference
sh1mj1 Dec 31, 2024
24afeb9
chore: packaging in compose/basic/introduction
sh1mj1 Dec 31, 2024
a083a0c
feat: compose basic codelab init setting
sh1mj1 Dec 31, 2024
3105cf9
build: compose test dependency
sh1mj1 Jan 5, 2025
1746697
feat(Greeting): Composable Text function
sh1mj1 Jan 5, 2025
bbc0ef6
feat(GreetingWithSurface): Theme, Surface
sh1mj1 Jan 5, 2025
263396d
chore: Greeting functions naming V#
sh1mj1 Jan 5, 2025
e344fbb
feat(GreetingV3): modifier.padding
sh1mj1 Jan 5, 2025
056893b
feat(Reusing composables): BasicComposeActivity and GreetingPreview r…
sh1mj1 Jan 5, 2025
609773b
feat(GreetingV4): use Column for vertically placed views
sh1mj1 Jan 5, 2025
eb0e854
feat(GreetingV5): use Row and ElevatedButton, multiple Modifier
sh1mj1 Jan 10, 2025
6953d14
test(GreetingV4): children text of column contain Hello and Android
sh1mj1 Jan 10, 2025
6dd7873
test(GreetingV5): children test, merged for Row and Column so use tes…
sh1mj1 Jan 11, 2025
a901673
feat(GreetingV6): state in compose. use remember and MutableState int…
sh1mj1 Jan 11, 2025
5d0e958
chore(BasicComposeActivity): move to compose/basic/codelab
sh1mj1 Jan 11, 2025
4ee2b60
chore(BasicComposeActivity): move to compose/basic/codelab
sh1mj1 Jan 11, 2025
35b81ba
feat(OnboardingScreen): State hoisting
sh1mj1 Jan 11, 2025
346d508
test(GreetingV6): Show more and Show less button is displayed or not
sh1mj1 Jan 11, 2025
7810669
test(OnboardingScreenV2KtTest): Continue buttons is not shown after c…
sh1mj1 Jan 11, 2025
7aa3958
test(GreetingsV1): normal Column cannot be scrolled
sh1mj1 Jan 11, 2025
6878911
test(GreetingsV2): LazyColumn can be scrolled
sh1mj1 Jan 11, 2025
e150626
test(GreetingsV2): item in LazyColumn doesn't save its state
sh1mj1 Jan 11, 2025
c135d23
test(BasicComposeActivity): not save the state after configuration ch…
sh1mj1 Jan 11, 2025
bd108a5
feat(GreetingV7, MyAppV6): use rememberSaveable
sh1mj1 Jan 11, 2025
4f11d9a
feat(GreetingV7): add the animation
sh1mj1 Jan 11, 2025
af9c726
feat: Stying
sh1mj1 Jan 11, 2025
137f877
build: compose material icon extension dependency
sh1mj1 Jan 11, 2025
4133d9c
feat(Greeting): final version using Card
sh1mj1 Jan 11, 2025
61f65ed
feat(Greetings): final version
sh1mj1 Jan 11, 2025
3acd0d2
feat(OnboardingScreen): final version
sh1mj1 Jan 11, 2025
ec5c604
feat(MyApp): final version, state hoisting to function parameter
sh1mj1 Jan 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ dependencies {
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)

debugImplementation(libs.compose.ui.test.manifest)
androidTestImplementation(libs.compose.ui.test.junit4)
implementation(libs.androidx.material.icons.extended)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.learningtest.compose.basic.codelab

import android.content.pm.ActivityInfo
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class BasicComposeActivityTest {
@get:Rule
val composeTestRule = createAndroidComposeRule(BasicComposeActivity::class.java)

@Test
fun save_the_state_after_configuration_change() {
// given
composeTestRule.onNodeWithText("Continue")
.assertIsDisplayed()

// when
composeTestRule.onNodeWithText("Continue")
.performClick()
composeTestRule.onNodeWithText("Continue")
.assertDoesNotExist()

composeTestRule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

// then
composeTestRule.onNodeWithText("Continue")
.assertDoesNotExist()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.learningtest.compose.basic.codelab

import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import org.junit.Rule
import org.junit.Test

class GreetingKtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun when_expanded_initially_then_ipsum_is_shown() {
composeTestRule.setContent {
Greeting(name = "Android", expandedInitially = true)
}

composeTestRule.onNode(hasText("ipsum", substring = true))
.assertIsDisplayed()
}

@Test
fun when_not_expanded_initially_then_ipsum_is_now_shown() {
composeTestRule.setContent {
Greeting(name = "Android", expandedInitially = false)
}

composeTestRule.onNode(hasText("ipsum", substring = true))
.assertIsNotDisplayed()
}

@Test
fun give_not_expanded_click_when_click_iconButton_then_ipsum_is_shown() {
composeTestRule.setContent {
Greeting(name = "Android", expandedInitially = false)
}
val buttonMatcher =
SemanticsMatcher.expectValue(
SemanticsProperties.Role,
Role.Button,
)
composeTestRule.onNode(buttonMatcher).performClick()

composeTestRule.onNode(hasText("ipsum", substring = true))
.assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.learningtest.compose.basic.codelab

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import org.junit.Rule
import org.junit.Test

class GreetingV1KtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun greeting_displaysCorrectName() {
// given
val name = "sh1mj1"

// when
composeTestRule.setContent {
GreetingV1(name = name)
}

// then
composeTestRule
.onNodeWithText("Hello sh1mj1!")
.assertExists()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.learningtest.compose.basic.codelab

import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.onParent
import org.junit.Rule
import org.junit.Test

class GreetingV4KtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun greetingV4_displaysTwoTextElementsCorrectly() {
// given && when
composeTestRule.setContent {
GreetingV4(name = "Android")
}

// then
composeTestRule.onNodeWithText("Hello")
.assertIsDisplayed()

val columnChildren =
composeTestRule.onNodeWithText("Hello")
.onParent()
.onChildren()

columnChildren.assertCountEquals(2)
columnChildren[0].assert(hasText("Hello"))
columnChildren[1].assert(hasText("Android"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.example.learningtest.compose.basic.codelab

import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChild
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onRoot
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.assertThrows

class GreetingV5KtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun greetingV5_display_button_named_Show_more() {
// given && when
composeTestRule.setContent {
GreetingV5(name = "Android")
}

val buttonMatcher =
SemanticsMatcher.expectValue(
SemanticsProperties.Role,
Role.Button,
)

// then
composeTestRule.onNode(matcher = buttonMatcher)
.assertTextContains("Show more")
}

@Test
fun greetingV5_root_has_one_node_Surface() {
// given && when
composeTestRule.setContent {
GreetingV5(name = "Android")
}

composeTestRule.onRoot()
.onChildren()
.assertCountEquals(1)
}

@Test
fun greetingV5_Surface_has_three_nodes() {
// given && when
composeTestRule.setContent {
GreetingV5(name = "Android")
}

assertThrows<AssertionError> {
composeTestRule.onRoot()
.onChild() // Surface
.onChildren() // Row
.assertCountEquals(1)
}

composeTestRule.onRoot()
.onChild() // Surface
.onChildren() // Row
.assertCountEquals(3) // Text("Hello"), Text("Android"), Button("Show more")
}

@Test
fun greetingV5WithTestTag_Surface_has_one_node_Row() {
// given && when
composeTestRule.setContent {
GreetingV5WithTestTag(name = "Android")
}

composeTestRule.onRoot()
.onChild() // Surface
.onChildren() // Row
.assertCountEquals(1) // Row
}

@Test
fun greetingV5WithTestTag_Row_has_two_nodes_Column_and_ElevatedButton() {
// given && when
composeTestRule.setContent {
GreetingV5WithTestTag(name = "Android")
}

composeTestRule.onRoot()
.onChild() // Surface
.onChild() // Row
.onChildren() // Column, ElevatedButton("Show more")
.assertCountEquals(2)
}

@Test
fun greetingV5WithTestTag_Column_has_two_nodes_Column_and_ElevatedButton() {
// given && when
composeTestRule.setContent {
GreetingV5WithTestTag(name = "Android")
}

composeTestRule.onRoot()
.onChild() // Surface
.onChild() // Row
.onChildren()[0] // Column
.onChildren() // Text("Hello"), Text("Android")
.assertCountEquals(2)
}

@Test
fun greetingV5WithSemanticsTest() {
composeTestRule.setContent {
GreetingV5WithSemantic(name = "Android")
}
composeTestRule.onRoot()
.onChild() // Surface
.onChildren() // Row
.assertCountEquals(1)

composeTestRule.onRoot()
.onChild() // Surface
.onChild() // Row
.onChildren() // Column, ElevatedButton("Show more")
.assertCountEquals(2)

composeTestRule.onRoot()
.onChild() // Surface
.onChild() // Row
.onChildren()[0] // Column
.onChildren() // Text("Hello"), Text("Android")
.assertCountEquals(2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.learningtest.compose.basic.codelab

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import org.junit.Rule
import org.junit.Test

class GreetingV6KtTest {
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun show_more_button_is_shown_at_first() {
// given && when
composeTestRule.setContent {
GreetingV6(name = "Android")
}

// then
composeTestRule.onNodeWithText("Show more").assertIsDisplayed()
composeTestRule.onNodeWithText("Show less").assertIsNotDisplayed()
}

@Test
fun show_less_button_is_shown_after_click_the_button() {
// given
composeTestRule.setContent {
GreetingV6(name = "Android")
}

// when
composeTestRule.onNodeWithText("Show more").performClick()

// then
composeTestRule.onNodeWithText("Show less").assertIsDisplayed()
composeTestRule.onNodeWithText("Show more").assertIsNotDisplayed()
}
}
Loading
Loading