Skip to content

Commit ec9aa03

Browse files
committed
tests updated.
1 parent 0849d1d commit ec9aa03

File tree

2 files changed

+53
-106
lines changed

2 files changed

+53
-106
lines changed

.run/TestComposeActivity.run.xml renamed to .run/TestComposeContent.run.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<component name="ProjectRunConfigurationManager">
2-
<configuration default="false" name="TestComposeActivity" type="AndroidTestRunConfigurationType" factoryName="Android Instrumented Tests" singleton="true">
2+
<configuration default="false" name="TestComposeContent" type="AndroidTestRunConfigurationType" factoryName="Android Instrumented Tests" nameIsGenerated="true">
33
<module name="ColorPicker.mobile.androidTest" />
44
<option name="TESTING_TYPE" value="2" />
5-
<option name="METHOD_NAME" value="colorPickerTest" />
6-
<option name="CLASS_NAME" value="io.syslogic.demo.colorpicker.TestComposeActivity" />
5+
<option name="METHOD_NAME" value="" />
6+
<option name="CLASS_NAME" value="io.syslogic.demo.colorpicker.TestComposeContent" />
77
<option name="PACKAGE_NAME" value="" />
88
<option name="TEST_NAME_REGEX" value="" />
99
<option name="INSTRUMENTATION_RUNNER_CLASS" value="" />

mobile/src/androidTest/java/io/syslogic/demo/colorpicker/TestComposeContent.kt

Lines changed: 50 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ import androidx.compose.ui.test.getBoundsInRoot
1515
import androidx.compose.ui.test.junit4.ComposeContentTestRule
1616
import androidx.compose.ui.test.junit4.createComposeRule
1717
import androidx.compose.ui.test.onNodeWithTag
18-
import androidx.compose.ui.test.onRoot
1918
import androidx.compose.ui.test.performMouseInput
20-
import androidx.compose.ui.test.printToLog
2119
import androidx.compose.ui.unit.Dp
2220
import androidx.compose.ui.unit.DpRect
2321
import androidx.test.ext.junit.runners.AndroidJUnit4
22+
2423
import io.syslogic.colorpicker.compose.ColorPickerComponent
25-
import org.junit.Before
2624

25+
import org.junit.Before
2726
import org.junit.Rule
2827
import org.junit.Test
2928
import org.junit.runner.RunWith
29+
3030
import java.util.Random
3131

32+
import kotlin.math.roundToInt
33+
3234
/**
3335
* Compose Content Test Case
3436
* @author Martin Zeitler
@@ -39,10 +41,6 @@ class TestComposeContent : TestSuite() {
3941
@get:Rule
4042
var testRule: ComposeContentTestRule = createComposeRule()
4143

42-
private val logTag: String = TestComposeContent::class.java.simpleName
43-
44-
private lateinit var interaction: SemanticsNodeInteraction
45-
4644
@Before
4745
fun setUp() {
4846
testRule.setContent {
@@ -62,124 +60,73 @@ class TestComposeContent : TestSuite() {
6260
}
6361
}
6462
}
63+
// testRule.onRoot().printToLog(logTag)
64+
// testRule.onRoot().getBoundsInRoot()
6565
}
6666

67-
@Test
68-
fun printToLogTest() {
69-
interaction = testRule.onRoot()
70-
interaction.printToLog(logTag)
71-
}
72-
73-
/** Randomly tapping the huePanel. */
67+
/** Randomly clicking the sat/val panel. */
7468
@Test
7569
@OptIn(ExperimentalTestApi::class)
76-
fun huePanelTest() {
77-
interaction = getNodeWithTag("hue")
78-
interaction.assertExists().assertIsDisplayed()
79-
randomlyClick(interaction)
70+
fun satValPanelTest() {
71+
randomlyClick(getNodeWithTag("sat_val"))
8072
}
8173

82-
/** Randomly tapping the satValPanel . */
74+
/** Randomly clicking the hue panel. */
8375
@Test
8476
@OptIn(ExperimentalTestApi::class)
85-
fun satValPanelTest() {
86-
interaction = getNodeWithTag("sat_val")
87-
interaction.assertExists().assertIsDisplayed()
88-
randomlyClick(interaction)
77+
fun huePanelTest() {
78+
randomlyClick(getNodeWithTag("hue"))
8979
}
9080

91-
/** Randomly tapping the alphaPanel. */
81+
/** Randomly clicking the alpha panel. */
9282
@Test
9383
@OptIn(ExperimentalTestApi::class)
9484
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()
85+
randomlyClick(getNodeWithTag("alpha"))
11686
}
11787

88+
/** Testing if the elements are there. */
11889
@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-
90+
fun basicTest() {
91+
getNodeWithTag("sat_val").assertExists().assertIsDisplayed()
92+
getNodeWithTag("hue").assertExists().assertIsDisplayed()
93+
getNodeWithTag("alpha").assertExists().assertIsDisplayed()
94+
getNodeWithTag("hex").assertExists().assertIsDisplayed()
95+
getNodeWithTag("old_color").assertExists().assertIsDisplayed()
96+
getNodeWithTag("new_color").assertExists().assertIsDisplayed()
97+
getNodeWithTag("value_hue").assertExists().assertIsDisplayed()
98+
getNodeWithTag("value_sat").assertExists().assertIsDisplayed()
99+
getNodeWithTag("value_val").assertExists().assertIsDisplayed()
100+
getNodeWithTag("value_alpha").assertExists().assertIsDisplayed()
101+
getNodeWithTag("value_blue").assertExists().assertIsDisplayed()
102+
getNodeWithTag("value_red").assertExists().assertIsDisplayed()
103+
getNodeWithTag("value_green").assertExists().assertIsDisplayed()
104+
}
105+
106+
/** Convert dp to px. */
107+
private val Dp.px: Int get() = (this.value * getSystem().displayMetrics.density).roundToInt()
108+
109+
/** Getter for interaction nodes. */
160110
private fun getNodeWithTag(testTag: String): SemanticsNodeInteraction {
161111
return testRule.onNodeWithTag(testTag = testTag)
162112
}
163113

164-
private val Dp.px: Int get() = (this.value / getSystem().displayMetrics.density).toInt()
165-
166-
/* Randomly click. */
114+
/** Randomly click. */
167115
@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")
116+
private fun randomlyClick(interaction: SemanticsNodeInteraction, count: Int = 250) {
172117
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)
118+
interaction.performMouseInput { click(getOffset(interaction)) }
119+
sleep(20)
183120
}
184121
}
122+
123+
/** Random offset. */
124+
private fun getOffset(interaction: SemanticsNodeInteraction): Offset {
125+
val rect: DpRect = interaction.getBoundsInRoot()
126+
val boundX = rect.right.px - rect.left.px
127+
val boundY = rect.bottom.px - rect.top.px
128+
val x: Float = (Random().nextInt(boundX + 1)).toFloat() /* left */
129+
val y: Float = (Random().nextInt(boundY + 1)).toFloat() /* top */
130+
return Offset(x, y)
131+
}
185132
}

0 commit comments

Comments
 (0)