|
1 | 1 | package io.syslogic.demo.colorpicker
|
2 | 2 |
|
| 3 | +import androidx.compose.ui.geometry.Offset |
| 4 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 5 | +import androidx.compose.ui.test.SemanticsNodeInteraction |
3 | 6 | import androidx.compose.ui.test.assertIsDisplayed
|
| 7 | +import androidx.compose.ui.test.click |
| 8 | +import androidx.compose.ui.test.getBoundsInRoot |
| 9 | +import androidx.compose.ui.test.junit4.AndroidComposeTestRule |
4 | 10 | import androidx.compose.ui.test.junit4.createAndroidComposeRule
|
5 | 11 | import androidx.compose.ui.test.onNodeWithTag
|
6 | 12 | import androidx.compose.ui.test.onRoot
|
| 13 | +import androidx.compose.ui.test.performMouseInput |
7 | 14 | import androidx.compose.ui.test.printToLog
|
| 15 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 16 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
8 | 17 |
|
9 | 18 | import io.syslogic.demo.colorpicker.activity.ComposeActivity
|
10 | 19 |
|
11 | 20 | import org.junit.Rule
|
12 | 21 | import org.junit.Test
|
| 22 | +import org.junit.runner.RunWith |
| 23 | +import java.util.Random |
13 | 24 |
|
14 | 25 | /**
|
15 |
| - * Color-Picker Test Case |
| 26 | + * Compose Activity Test Case |
16 | 27 | *
|
17 | 28 | * @author Martin Zeitler
|
18 | 29 | */
|
19 |
| -class TestComposeActivity { |
| 30 | +@RunWith(AndroidJUnit4::class) |
| 31 | +class TestComposeActivity : TestSuite() { |
20 | 32 |
|
21 | 33 | @get:Rule
|
22 |
| - val composeTestRule = createAndroidComposeRule(ComposeActivity::class.java) |
| 34 | + val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComposeActivity>, ComposeActivity> = |
| 35 | + createAndroidComposeRule(ComposeActivity::class.java) |
| 36 | + |
| 37 | + lateinit var interaction: SemanticsNodeInteraction |
23 | 38 |
|
24 | 39 | @Test
|
25 | 40 | fun logTest() {
|
26 |
| - composeTestRule.onRoot().printToLog("TAG") |
| 41 | + interaction = composeTestRule.onRoot() |
| 42 | + interaction.printToLog("TAG") |
27 | 43 | }
|
28 | 44 |
|
29 | 45 | @Test
|
| 46 | + @OptIn(ExperimentalTestApi::class) |
30 | 47 | fun huePanelTest() {
|
31 |
| - composeTestRule.onNodeWithTag(testTag = "hue").assertIsDisplayed() |
| 48 | + |
| 49 | + interaction = composeTestRule.onNodeWithTag(testTag = "hue") |
| 50 | + interaction.assertExists().assertIsDisplayed() |
| 51 | + |
| 52 | + /* Randomly tapping the huePanel for 5 seconds. */ |
| 53 | + for (i in 0..99) { |
| 54 | + randomlyClick(interaction) |
| 55 | + Thread.sleep(50) |
| 56 | + } |
32 | 57 | }
|
33 | 58 |
|
34 | 59 | @Test
|
35 |
| - fun satPanelTest() { |
36 |
| - composeTestRule.onNodeWithTag(testTag = "sat_val").assertIsDisplayed() |
| 60 | + @OptIn(ExperimentalTestApi::class) |
| 61 | + fun satValPanelTest() { |
| 62 | + |
| 63 | + interaction = composeTestRule.onNodeWithTag(testTag = "sat_val") |
| 64 | + interaction.assertExists().assertIsDisplayed() |
| 65 | + |
| 66 | + /* Randomly tapping the satValPanel for 5 seconds. */ |
| 67 | + for (i in 0..99) { |
| 68 | + randomlyClick(interaction) |
| 69 | + Thread.sleep(50) |
| 70 | + } |
37 | 71 | }
|
38 | 72 |
|
39 | 73 | @Test
|
| 74 | + @OptIn(ExperimentalTestApi::class) |
40 | 75 | fun alphaPanelTest() {
|
41 |
| - composeTestRule.onNodeWithTag(testTag = "alpha").assertIsDisplayed() |
| 76 | + |
| 77 | + interaction = composeTestRule.onNodeWithTag(testTag = "alpha") |
| 78 | + interaction.assertExists().assertIsDisplayed() |
| 79 | + |
| 80 | + /* Randomly tapping the alphaPanel for 5 seconds. */ |
| 81 | + for (i in 0..99) { |
| 82 | + randomlyClick(interaction) |
| 83 | + Thread.sleep(50) |
| 84 | + } |
42 | 85 | }
|
43 | 86 |
|
44 | 87 | @Test
|
45 | 88 | fun hexColorTest() {
|
46 |
| - composeTestRule.onNodeWithTag(testTag = "hex").assertIsDisplayed() |
| 89 | + interaction = composeTestRule.onNodeWithTag(testTag = "hex") |
| 90 | + interaction.assertExists().assertIsDisplayed() |
47 | 91 | }
|
48 | 92 |
|
49 | 93 | @Test
|
50 | 94 | fun oldColorTest() {
|
51 |
| - composeTestRule.onNodeWithTag(testTag = "old_color").assertIsDisplayed() |
| 95 | + interaction = composeTestRule.onNodeWithTag(testTag = "old_color") |
| 96 | + interaction.assertExists().assertIsDisplayed() |
52 | 97 | }
|
53 | 98 |
|
54 | 99 | @Test
|
55 | 100 | fun newColorTest() {
|
56 |
| - composeTestRule.onNodeWithTag(testTag = "new_color").assertIsDisplayed() |
| 101 | + interaction = composeTestRule.onNodeWithTag(testTag = "new_color") |
| 102 | + interaction.assertExists().assertIsDisplayed() |
57 | 103 | }
|
58 | 104 |
|
59 | 105 | @Test
|
60 | 106 | fun valueHueTest() {
|
61 |
| - composeTestRule.onNodeWithTag(testTag = "value_hue").assertIsDisplayed() |
| 107 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_hue") |
| 108 | + interaction.assertExists().assertIsDisplayed() |
62 | 109 | }
|
63 | 110 |
|
64 | 111 | @Test
|
65 | 112 | fun valueSatTest() {
|
66 |
| - composeTestRule.onNodeWithTag(testTag = "value_sat").assertIsDisplayed() |
| 113 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_sat") |
| 114 | + interaction.assertExists().assertIsDisplayed() |
67 | 115 | }
|
68 | 116 |
|
69 | 117 | @Test
|
70 | 118 | fun valueValTest() {
|
71 |
| - composeTestRule.onNodeWithTag(testTag = "value_val").assertIsDisplayed() |
| 119 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_val") |
| 120 | + interaction.assertExists().assertIsDisplayed() |
72 | 121 | }
|
73 | 122 |
|
74 | 123 | @Test
|
75 | 124 | fun valueAlphaTest() {
|
76 |
| - composeTestRule.onNodeWithTag(testTag = "value_alpha").assertIsDisplayed() |
| 125 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_alpha") |
| 126 | + interaction.assertExists().assertIsDisplayed() |
77 | 127 | }
|
78 | 128 |
|
79 | 129 | @Test
|
80 | 130 | fun valueBlueTest() {
|
81 |
| - composeTestRule.onNodeWithTag(testTag = "value_blue").assertIsDisplayed() |
| 131 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_blue") |
| 132 | + interaction.assertExists().assertIsDisplayed() |
82 | 133 | }
|
83 | 134 |
|
84 | 135 | @Test
|
85 | 136 | fun valueRedTest() {
|
86 |
| - composeTestRule.onNodeWithTag(testTag = "value_red").assertIsDisplayed() |
| 137 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_red") |
| 138 | + interaction.assertExists().assertIsDisplayed() |
87 | 139 | }
|
88 | 140 |
|
89 | 141 | @Test
|
90 | 142 | fun valueGreenTest() {
|
91 |
| - composeTestRule.onNodeWithTag(testTag = "value_green").assertIsDisplayed() |
| 143 | + interaction = composeTestRule.onNodeWithTag(testTag = "value_green") |
| 144 | + interaction.assertExists().assertIsDisplayed() |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + @ExperimentalTestApi |
| 149 | + fun randomlyClick(interaction: SemanticsNodeInteraction) { |
| 150 | + val rect = interaction.getBoundsInRoot() |
| 151 | + val rnd = Random() |
| 152 | + val coordinate = longArrayOf( |
| 153 | + ((rect.left.value + rnd.nextInt((rect.right.value - rect.left.value + 1).toInt())).toLong()), |
| 154 | + ((rect.top.value + rnd.nextInt((rect.bottom.value - rect.top.value + 1).toInt())).toLong()) |
| 155 | + ) |
| 156 | + val position = Offset(coordinate[0].toFloat(), coordinate[1].toFloat()) |
| 157 | + interaction.performMouseInput { |
| 158 | + click(position) |
| 159 | + } |
92 | 160 | }
|
93 | 161 | }
|
0 commit comments