Skip to content

Commit b57e92f

Browse files
committed
tests updated.
1 parent 0135043 commit b57e92f

File tree

6 files changed

+194
-151
lines changed

6 files changed

+194
-151
lines changed

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ task javadoc(type: Javadoc) {
110110
// options.verbose()
111111
// javadoc: warning - The code being documented uses modules but the packages
112112
// defined in https://developer.android.com/reference/ are in the unnamed module.
113-
options.links "https://docs.oracle.com/en/java/javase/11/docs/api/"
113+
options.links "https://docs.oracle.com/en/java/javase/17/docs/api/"
114114
options.linksOffline "https://developer.android.com/reference", "${android.sdkDirectory}/docs/reference"
115115
options.linkSource true
116116
options.author true

mobile/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ android {
1212
defaultConfig {
1313
namespace "io.syslogic.demo.colorpicker"
1414
applicationId "io.syslogic.demo.colorpicker"
15+
testBuildType "debug"
1516
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1617
versionCode version_code
1718
versionName version_name
@@ -91,8 +92,9 @@ dependencies {
9192
// https://mvnrepository.com/artifact/androidx.test/monitor
9293
debugImplementation "androidx.test:monitor:$test_monitor_version"
9394

95+
// Required for connected tests (ActivityInvoker).
9496
// https://mvnrepository.com/artifact/androidx.test.ext
95-
androidTestImplementation "androidx.test.ext:junit:$test_junit_version"
97+
debugImplementation "androidx.test.ext:junit:$test_junit_version"
9698

9799
// https://mvnrepository.com/artifact/androidx.test
98100
// https://developer.android.com/jetpack/androidx/releases/test
@@ -119,7 +121,7 @@ dependencies {
119121
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$compose_version")
120122

121123
// Needed for createComposeRule, but not createAndroidComposeRule:
122-
// debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
124+
debugImplementation("androidx.compose.ui:ui-test-manifest:$compose_version")
123125
}
124126

125127
// disable caching

mobile/src/androidTest/java/io/syslogic/demo/colorpicker/TestColorPicker.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
/**
1313
* Color-Picker Test Case
14-
*
1514
* @author Martin Zeitler
1615
*/
1716
@RunWith(AndroidJUnit4.class)
Lines changed: 2 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,24 @@
11
package io.syslogic.demo.colorpicker
22

3-
import androidx.compose.ui.geometry.Offset
4-
import androidx.compose.ui.test.ExperimentalTestApi
5-
import androidx.compose.ui.test.SemanticsNodeInteraction
6-
import androidx.compose.ui.test.assertIsDisplayed
7-
import androidx.compose.ui.test.click
8-
import androidx.compose.ui.test.getBoundsInRoot
93
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
104
import androidx.compose.ui.test.junit4.createAndroidComposeRule
11-
import androidx.compose.ui.test.onNodeWithTag
12-
import androidx.compose.ui.test.onRoot
13-
import androidx.compose.ui.test.performMouseInput
14-
import androidx.compose.ui.test.printToLog
15-
import androidx.compose.ui.unit.DpRect
165
import androidx.test.ext.junit.rules.ActivityScenarioRule
176
import androidx.test.ext.junit.runners.AndroidJUnit4
187

198
import io.syslogic.demo.colorpicker.activity.ComposeActivity
209

2110
import org.junit.Rule
22-
import org.junit.Test
2311
import org.junit.runner.RunWith
24-
import java.util.Random
2512

2613
/**
2714
* Compose Activity Test Case
28-
*
2915
* @author Martin Zeitler
3016
*/
17+
@Deprecated("kept for reference")
3118
@RunWith(AndroidJUnit4::class)
3219
class TestComposeActivity : TestSuite() {
3320

3421
@get:Rule
35-
val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComposeActivity>, ComposeActivity> =
22+
val testRule: AndroidComposeTestRule<ActivityScenarioRule<ComposeActivity>, ComposeActivity> =
3623
createAndroidComposeRule(ComposeActivity::class.java)
37-
38-
lateinit var interaction: SemanticsNodeInteraction
39-
40-
@Test
41-
fun logTest() {
42-
interaction = composeTestRule.onRoot()
43-
interaction.printToLog("TAG")
44-
}
45-
46-
@Test
47-
@OptIn(ExperimentalTestApi::class)
48-
fun huePanelTest() {
49-
50-
interaction = getNodeWithTag("hue")
51-
interaction.assertExists().assertIsDisplayed()
52-
53-
/* Randomly tapping the huePanel for 5 seconds. */
54-
for (i in 0..99) {
55-
randomlyClick(interaction)
56-
sleep(50)
57-
}
58-
}
59-
60-
@Test
61-
@OptIn(ExperimentalTestApi::class)
62-
fun satValPanelTest() {
63-
64-
interaction = getNodeWithTag("sat_val")
65-
interaction.assertExists().assertIsDisplayed()
66-
67-
/* Randomly tapping the satValPanel for 5 seconds. */
68-
for (i in 0..99) {
69-
randomlyClick(interaction)
70-
sleep(50)
71-
}
72-
}
73-
74-
@Test
75-
@OptIn(ExperimentalTestApi::class)
76-
fun alphaPanelTest() {
77-
78-
interaction = getNodeWithTag("alpha")
79-
interaction.assertExists().assertIsDisplayed()
80-
81-
/* Randomly tapping the alphaPanel for 5 seconds. */
82-
for (i in 0..99) {
83-
randomlyClick(interaction)
84-
sleep(50)
85-
}
86-
}
87-
88-
@Test
89-
fun hexColorTest() {
90-
interaction = getNodeWithTag("hex")
91-
interaction.assertExists().assertIsDisplayed()
92-
}
93-
94-
@Test
95-
fun oldColorTest() {
96-
interaction = getNodeWithTag("old_color")
97-
interaction.assertExists().assertIsDisplayed()
98-
}
99-
100-
@Test
101-
fun newColorTest() {
102-
interaction = getNodeWithTag("new_color")
103-
interaction.assertExists().assertIsDisplayed()
104-
}
105-
106-
@Test
107-
fun valueHueTest() {
108-
interaction = getNodeWithTag("value_hue")
109-
interaction.assertExists().assertIsDisplayed()
110-
}
111-
112-
@Test
113-
fun valueSatTest() {
114-
interaction = getNodeWithTag("value_sat")
115-
interaction.assertExists().assertIsDisplayed()
116-
}
117-
118-
@Test
119-
fun valueValTest() {
120-
interaction = getNodeWithTag("value_val")
121-
interaction.assertExists().assertIsDisplayed()
122-
}
123-
124-
@Test
125-
fun valueAlphaTest() {
126-
interaction = getNodeWithTag("value_alpha")
127-
interaction.assertExists().assertIsDisplayed()
128-
}
129-
130-
@Test
131-
fun valueBlueTest() {
132-
interaction = getNodeWithTag("value_blue")
133-
interaction.assertExists().assertIsDisplayed()
134-
}
135-
136-
@Test
137-
fun valueRedTest() {
138-
interaction = getNodeWithTag("value_red")
139-
interaction.assertExists().assertIsDisplayed()
140-
}
141-
142-
@Test
143-
fun valueGreenTest() {
144-
interaction = getNodeWithTag("value_green")
145-
interaction.assertExists().assertIsDisplayed()
146-
}
147-
148-
@Override
149-
@ExperimentalTestApi
150-
private fun randomlyClick(interaction: SemanticsNodeInteraction) {
151-
val rect: DpRect = interaction.getBoundsInRoot()
152-
val rnd = Random()
153-
val coordinate = floatArrayOf(
154-
((rect.left.value + rnd.nextInt((rect.right.value - rect.left.value + 1).toInt()))),
155-
((rect.top.value + rnd.nextInt((rect.bottom.value - rect.top.value + 1).toInt())))
156-
)
157-
val position = Offset(coordinate[0], coordinate[1])
158-
interaction.performMouseInput {
159-
click(position)
160-
}
161-
}
162-
163-
private fun getNodeWithTag(testTag: String): SemanticsNodeInteraction {
164-
return composeTestRule.onNodeWithTag(testTag = testTag)
165-
}
16624
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package io.syslogic.demo.colorpicker
2+
3+
import android.content.res.Resources.getSystem
4+
import androidx.compose.foundation.layout.fillMaxSize
5+
import androidx.compose.material.MaterialTheme
6+
import androidx.compose.material.Surface
7+
import androidx.compose.ui.Modifier
8+
import androidx.compose.ui.geometry.Offset
9+
import androidx.compose.ui.graphics.Color
10+
import androidx.compose.ui.test.ExperimentalTestApi
11+
import androidx.compose.ui.test.SemanticsNodeInteraction
12+
import androidx.compose.ui.test.assertIsDisplayed
13+
import androidx.compose.ui.test.click
14+
import androidx.compose.ui.test.getBoundsInRoot
15+
import androidx.compose.ui.test.junit4.ComposeContentTestRule
16+
import androidx.compose.ui.test.junit4.createComposeRule
17+
import androidx.compose.ui.test.onNodeWithTag
18+
import androidx.compose.ui.test.onRoot
19+
import androidx.compose.ui.test.performMouseInput
20+
import androidx.compose.ui.test.printToLog
21+
import androidx.compose.ui.unit.Dp
22+
import androidx.compose.ui.unit.DpRect
23+
import androidx.test.ext.junit.runners.AndroidJUnit4
24+
import io.syslogic.colorpicker.compose.ColorPickerComponent
25+
import org.junit.Before
26+
27+
import org.junit.Rule
28+
import org.junit.Test
29+
import org.junit.runner.RunWith
30+
import java.util.Random
31+
32+
/**
33+
* Compose Content Test Case
34+
* @author Martin Zeitler
35+
*/
36+
@RunWith(AndroidJUnit4::class)
37+
class TestComposeContent : TestSuite() {
38+
39+
@get:Rule
40+
var testRule: ComposeContentTestRule = createComposeRule()
41+
42+
private lateinit var interaction: SemanticsNodeInteraction
43+
44+
@Before
45+
fun setUp() {
46+
testRule.setContent {
47+
MaterialTheme {
48+
Surface(
49+
modifier = Modifier.fillMaxSize(),
50+
color = MaterialTheme.colors.background
51+
) {
52+
ColorPickerComponent(
53+
initialColor = Color(0xFF444444.toInt()),
54+
onColorChanged = { _ ->
55+
// println("onColorChanged: $color")
56+
},
57+
showAlpha = true,
58+
showHSV = true,
59+
showARGB = true,
60+
showHex = true
61+
)
62+
}
63+
}
64+
}
65+
}
66+
67+
@Test
68+
fun logTest() {
69+
interaction = testRule.onRoot()
70+
interaction.printToLog("TAG")
71+
}
72+
73+
/** Randomly tapping the huePanel. */
74+
@Test
75+
@OptIn(ExperimentalTestApi::class)
76+
fun huePanelTest() {
77+
interaction = getNodeWithTag("hue")
78+
interaction.assertExists().assertIsDisplayed()
79+
randomlyClick(interaction)
80+
}
81+
82+
/** Randomly tapping the satValPanel . */
83+
@Test
84+
@OptIn(ExperimentalTestApi::class)
85+
fun satValPanelTest() {
86+
interaction = getNodeWithTag("sat_val")
87+
interaction.assertExists().assertIsDisplayed()
88+
randomlyClick(interaction)
89+
}
90+
91+
/** Randomly tapping the alphaPanel. */
92+
@Test
93+
@OptIn(ExperimentalTestApi::class)
94+
fun alphaPanelTest() {
95+
interaction = getNodeWithTag("alpha")
96+
interaction.assertExists().assertIsDisplayed()
97+
randomlyClick(interaction)
98+
}
99+
100+
@Test
101+
fun hexColorTest() {
102+
interaction = getNodeWithTag("hex")
103+
interaction.assertExists().assertIsDisplayed()
104+
}
105+
106+
@Test
107+
fun oldColorTest() {
108+
interaction = getNodeWithTag("old_color")
109+
interaction.assertExists().assertIsDisplayed()
110+
}
111+
112+
@Test
113+
fun newColorTest() {
114+
interaction = getNodeWithTag("new_color")
115+
interaction.assertExists().assertIsDisplayed()
116+
}
117+
118+
@Test
119+
fun valueHueTest() {
120+
interaction = getNodeWithTag("value_hue")
121+
interaction.assertExists().assertIsDisplayed()
122+
}
123+
124+
@Test
125+
fun valueSatTest() {
126+
interaction = getNodeWithTag("value_sat")
127+
interaction.assertExists().assertIsDisplayed()
128+
}
129+
130+
@Test
131+
fun valueValTest() {
132+
interaction = getNodeWithTag("value_val")
133+
interaction.assertExists().assertIsDisplayed()
134+
}
135+
136+
@Test
137+
fun valueAlphaTest() {
138+
interaction = getNodeWithTag("value_alpha")
139+
interaction.assertExists().assertIsDisplayed()
140+
}
141+
142+
@Test
143+
fun valueBlueTest() {
144+
interaction = getNodeWithTag("value_blue")
145+
interaction.assertExists().assertIsDisplayed()
146+
}
147+
148+
@Test
149+
fun valueRedTest() {
150+
interaction = getNodeWithTag("value_red")
151+
interaction.assertExists().assertIsDisplayed()
152+
}
153+
154+
@Test
155+
fun valueGreenTest() {
156+
interaction = getNodeWithTag("value_green")
157+
interaction.assertExists().assertIsDisplayed()
158+
}
159+
160+
private fun getNodeWithTag(testTag: String): SemanticsNodeInteraction {
161+
return testRule.onNodeWithTag(testTag = testTag)
162+
}
163+
164+
private val Dp.px: Int get() = (this.value / getSystem().displayMetrics.density).toInt()
165+
166+
/* Randomly tapping. */
167+
@ExperimentalTestApi
168+
private fun randomlyClick(interaction: SemanticsNodeInteraction, count: Int = 100, ms: Int = 50) {
169+
val rnd = Random()
170+
val rect: DpRect = interaction.getBoundsInRoot()
171+
println("DpRect >> x: " + rect.left.px + "px, y: " + rect.top.px + "px")
172+
for (i in 0 until count) {
173+
val coordinate = floatArrayOf(
174+
(rect.left.px + rnd.nextInt(rect.right.px - rect.left.px + 1)).toFloat(),
175+
(rect.top.px + rnd.nextInt(rect.bottom.px - rect.top.px + 1)).toFloat()
176+
)
177+
val position = Offset(coordinate[0], coordinate[1])
178+
println("Offset >> x: " + position.x.toInt() + ", y: " + position.y.toInt())
179+
interaction.performMouseInput {
180+
click(position)
181+
}
182+
sleep(ms)
183+
}
184+
}
185+
}

0 commit comments

Comments
 (0)