@@ -15,20 +15,22 @@ import androidx.compose.ui.test.getBoundsInRoot
15
15
import androidx.compose.ui.test.junit4.ComposeContentTestRule
16
16
import androidx.compose.ui.test.junit4.createComposeRule
17
17
import androidx.compose.ui.test.onNodeWithTag
18
- import androidx.compose.ui.test.onRoot
19
18
import androidx.compose.ui.test.performMouseInput
20
- import androidx.compose.ui.test.printToLog
21
19
import androidx.compose.ui.unit.Dp
22
20
import androidx.compose.ui.unit.DpRect
23
21
import androidx.test.ext.junit.runners.AndroidJUnit4
22
+
24
23
import io.syslogic.colorpicker.compose.ColorPickerComponent
25
- import org.junit.Before
26
24
25
+ import org.junit.Before
27
26
import org.junit.Rule
28
27
import org.junit.Test
29
28
import org.junit.runner.RunWith
29
+
30
30
import java.util.Random
31
31
32
+ import kotlin.math.roundToInt
33
+
32
34
/* *
33
35
* Compose Content Test Case
34
36
* @author Martin Zeitler
@@ -39,10 +41,6 @@ class TestComposeContent : TestSuite() {
39
41
@get:Rule
40
42
var testRule: ComposeContentTestRule = createComposeRule()
41
43
42
- private val logTag: String = TestComposeContent ::class .java.simpleName
43
-
44
- private lateinit var interaction: SemanticsNodeInteraction
45
-
46
44
@Before
47
45
fun setUp () {
48
46
testRule.setContent {
@@ -62,124 +60,73 @@ class TestComposeContent : TestSuite() {
62
60
}
63
61
}
64
62
}
63
+ // testRule.onRoot().printToLog(logTag)
64
+ // testRule.onRoot().getBoundsInRoot()
65
65
}
66
66
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. */
74
68
@Test
75
69
@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" ))
80
72
}
81
73
82
- /* * Randomly tapping the satValPanel . */
74
+ /* * Randomly clicking the hue panel . */
83
75
@Test
84
76
@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" ))
89
79
}
90
80
91
- /* * Randomly tapping the alphaPanel . */
81
+ /* * Randomly clicking the alpha panel . */
92
82
@Test
93
83
@OptIn(ExperimentalTestApi ::class )
94
84
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" ))
116
86
}
117
87
88
+ /* * Testing if the elements are there. */
118
89
@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. */
160
110
private fun getNodeWithTag (testTag : String ): SemanticsNodeInteraction {
161
111
return testRule.onNodeWithTag(testTag = testTag)
162
112
}
163
113
164
- private val Dp .px: Int get() = (this .value / getSystem().displayMetrics.density).toInt()
165
-
166
- /* Randomly click. */
114
+ /* * Randomly click. */
167
115
@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) {
172
117
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 )
183
120
}
184
121
}
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
+ }
185
132
}
0 commit comments