|
| 1 | +package to.bitkit.ui.screens.widgets.weather |
| 2 | + |
| 3 | +import androidx.compose.ui.test.assertIsDisplayed |
| 4 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 5 | +import androidx.compose.ui.test.onNodeWithTag |
| 6 | +import androidx.compose.ui.test.performClick |
| 7 | +import org.junit.Rule |
| 8 | +import org.junit.Test |
| 9 | +import to.bitkit.R |
| 10 | +import to.bitkit.data.dto.FeeCondition |
| 11 | +import to.bitkit.models.widget.WeatherPreferences |
| 12 | +import to.bitkit.ui.screens.widgets.blocks.WeatherModel |
| 13 | +import to.bitkit.ui.theme.AppThemeSurface |
| 14 | + |
| 15 | +class WeatherPreviewContentTest { |
| 16 | + |
| 17 | + @get:Rule |
| 18 | + val composeTestRule = createComposeRule() |
| 19 | + |
| 20 | + private val testWeatherModel = WeatherModel( |
| 21 | + title = R.string.widgets__weather__condition__good__title, |
| 22 | + description = R.string.widgets__weather__condition__good__description, |
| 23 | + currentFee = "15 sat/vB", |
| 24 | + nextBlockFee = "12 sat/vB", |
| 25 | + icon = FeeCondition.GOOD.icon |
| 26 | + ) |
| 27 | + |
| 28 | + private val defaultPreferences = WeatherPreferences() |
| 29 | + |
| 30 | + @Test |
| 31 | + fun testWeatherPreviewWithEnabledWidget() { |
| 32 | + // Arrange |
| 33 | + var closeClicked = false |
| 34 | + var backClicked = false |
| 35 | + var editClicked = false |
| 36 | + var deleteClicked = false |
| 37 | + var saveClicked = false |
| 38 | + |
| 39 | + // Act |
| 40 | + composeTestRule.setContent { |
| 41 | + AppThemeSurface { |
| 42 | + WeatherPreviewContent( |
| 43 | + onClose = { closeClicked = true }, |
| 44 | + onBack = { backClicked = true }, |
| 45 | + onClickEdit = { editClicked = true }, |
| 46 | + onClickDelete = { deleteClicked = true }, |
| 47 | + onClickSave = { saveClicked = true }, |
| 48 | + showWidgetTitles = true, |
| 49 | + isWeatherWidgetEnabled = true, |
| 50 | + weatherPreferences = defaultPreferences, |
| 51 | + weatherModel = testWeatherModel |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Assert main elements exist |
| 57 | + composeTestRule.onNodeWithTag("weather_preview_screen").assertExists() |
| 58 | + composeTestRule.onNodeWithTag("main_content").assertExists() |
| 59 | + |
| 60 | + // Verify header elements |
| 61 | + composeTestRule.onNodeWithTag("header_row").assertExists() |
| 62 | + composeTestRule.onNodeWithTag("widget_title").assertExists() |
| 63 | + composeTestRule.onNodeWithTag("widget_icon").assertExists() |
| 64 | + composeTestRule.onNodeWithTag("widget_description").assertExists() |
| 65 | + |
| 66 | + // Verify settings and preview section |
| 67 | + composeTestRule.onNodeWithTag("edit_settings_button").assertExists() |
| 68 | + composeTestRule.onNodeWithTag("preview_label").assertExists() |
| 69 | + composeTestRule.onNodeWithTag("weather_card").assertExists() |
| 70 | + |
| 71 | + // Verify buttons |
| 72 | + composeTestRule.onNodeWithTag("buttons_row").assertExists() |
| 73 | + composeTestRule.onNodeWithTag("delete_button").assertExists() |
| 74 | + composeTestRule.onNodeWithTag("save_button").assertExists() |
| 75 | + |
| 76 | + // Test button clicks |
| 77 | + composeTestRule.onNodeWithTag("edit_settings_button").performClick() |
| 78 | + assert(editClicked) |
| 79 | + |
| 80 | + composeTestRule.onNodeWithTag("delete_button").performClick() |
| 81 | + assert(deleteClicked) |
| 82 | + |
| 83 | + composeTestRule.onNodeWithTag("save_button").performClick() |
| 84 | + assert(saveClicked) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun testWeatherPreviewWithDisabledWidget() { |
| 89 | + // Arrange |
| 90 | + var closeClicked = false |
| 91 | + var backClicked = false |
| 92 | + var editClicked = false |
| 93 | + var deleteClicked = false |
| 94 | + var saveClicked = false |
| 95 | + |
| 96 | + // Act |
| 97 | + composeTestRule.setContent { |
| 98 | + AppThemeSurface { |
| 99 | + WeatherPreviewContent( |
| 100 | + onClose = { closeClicked = true }, |
| 101 | + onBack = { backClicked = true }, |
| 102 | + onClickEdit = { editClicked = true }, |
| 103 | + onClickDelete = { deleteClicked = true }, |
| 104 | + onClickSave = { saveClicked = true }, |
| 105 | + showWidgetTitles = false, |
| 106 | + isWeatherWidgetEnabled = false, |
| 107 | + weatherPreferences = defaultPreferences, |
| 108 | + weatherModel = testWeatherModel |
| 109 | + ) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Assert main elements exist |
| 114 | + composeTestRule.onNodeWithTag("weather_preview_screen").assertExists() |
| 115 | + composeTestRule.onNodeWithTag("buttons_row").assertExists() |
| 116 | + |
| 117 | + // Delete button should not exist when widget is disabled |
| 118 | + composeTestRule.onNodeWithTag("delete_button").assertDoesNotExist() |
| 119 | + composeTestRule.onNodeWithTag("save_button").assertExists() |
| 120 | + |
| 121 | + // Test save button click |
| 122 | + composeTestRule.onNodeWithTag("save_button").performClick() |
| 123 | + assert(saveClicked) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun testCustomWeatherPreferences() { |
| 128 | + // Arrange |
| 129 | + val customPreferences = WeatherPreferences( |
| 130 | + showTitle = true, |
| 131 | + showDescription = false, |
| 132 | + showCurrentFee = true, |
| 133 | + showNextBlockFee = false |
| 134 | + ) |
| 135 | + |
| 136 | + // Act |
| 137 | + composeTestRule.setContent { |
| 138 | + AppThemeSurface { |
| 139 | + WeatherPreviewContent( |
| 140 | + onClose = {}, |
| 141 | + onBack = {}, |
| 142 | + onClickEdit = {}, |
| 143 | + onClickDelete = {}, |
| 144 | + onClickSave = {}, |
| 145 | + showWidgetTitles = true, |
| 146 | + isWeatherWidgetEnabled = true, |
| 147 | + weatherPreferences = customPreferences, |
| 148 | + weatherModel = testWeatherModel |
| 149 | + ) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + // Assert that all elements still exist with custom preferences |
| 154 | + composeTestRule.onNodeWithTag("weather_preview_screen").assertExists() |
| 155 | + composeTestRule.onNodeWithTag("edit_settings_button").assertExists() |
| 156 | + composeTestRule.onNodeWithTag("weather_card").assertExists() |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + fun testAllElementsExist() { |
| 161 | + // Arrange |
| 162 | + composeTestRule.setContent { |
| 163 | + AppThemeSurface { |
| 164 | + WeatherPreviewContent( |
| 165 | + onClose = {}, |
| 166 | + onBack = {}, |
| 167 | + onClickEdit = {}, |
| 168 | + onClickDelete = {}, |
| 169 | + onClickSave = {}, |
| 170 | + showWidgetTitles = true, |
| 171 | + isWeatherWidgetEnabled = true, |
| 172 | + weatherPreferences = defaultPreferences, |
| 173 | + weatherModel = testWeatherModel |
| 174 | + ) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // Assert all tagged elements exist |
| 179 | + composeTestRule.onNodeWithTag("weather_preview_screen").assertExists() |
| 180 | + composeTestRule.onNodeWithTag("main_content").assertExists() |
| 181 | + composeTestRule.onNodeWithTag("header_row").assertExists() |
| 182 | + composeTestRule.onNodeWithTag("widget_title").assertExists() |
| 183 | + composeTestRule.onNodeWithTag("widget_icon").assertExists() |
| 184 | + composeTestRule.onNodeWithTag("widget_description").assertExists() |
| 185 | + composeTestRule.onNodeWithTag("divider").assertExists() |
| 186 | + composeTestRule.onNodeWithTag("edit_settings_button").assertExists() |
| 187 | + composeTestRule.onNodeWithTag("preview_label").assertExists() |
| 188 | + composeTestRule.onNodeWithTag("weather_card").assertExists() |
| 189 | + composeTestRule.onNodeWithTag("buttons_row").assertExists() |
| 190 | + composeTestRule.onNodeWithTag("delete_button").assertExists() |
| 191 | + composeTestRule.onNodeWithTag("save_button").assertExists() |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + fun testNavigationCallbacks() { |
| 196 | + // Arrange |
| 197 | + var closeClicked = false |
| 198 | + var backClicked = false |
| 199 | + |
| 200 | + composeTestRule.setContent { |
| 201 | + AppThemeSurface { |
| 202 | + WeatherPreviewContent( |
| 203 | + onClose = { closeClicked = true }, |
| 204 | + onBack = { backClicked = true }, |
| 205 | + onClickEdit = {}, |
| 206 | + onClickDelete = {}, |
| 207 | + onClickSave = {}, |
| 208 | + showWidgetTitles = true, |
| 209 | + isWeatherWidgetEnabled = true, |
| 210 | + weatherPreferences = defaultPreferences, |
| 211 | + weatherModel = testWeatherModel |
| 212 | + ) |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + // Note: Navigation callbacks are tested through the actual navigation components |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + fun testWithMinimalWeatherPreferences() { |
| 221 | + // Arrange |
| 222 | + val minimalPreferences = WeatherPreferences( |
| 223 | + showTitle = true, |
| 224 | + showDescription = false, |
| 225 | + showCurrentFee = false, |
| 226 | + showNextBlockFee = false |
| 227 | + ) |
| 228 | + |
| 229 | + // Act |
| 230 | + composeTestRule.setContent { |
| 231 | + AppThemeSurface { |
| 232 | + WeatherPreviewContent( |
| 233 | + onClose = {}, |
| 234 | + onBack = {}, |
| 235 | + onClickEdit = {}, |
| 236 | + onClickDelete = {}, |
| 237 | + onClickSave = {}, |
| 238 | + showWidgetTitles = false, |
| 239 | + isWeatherWidgetEnabled = false, |
| 240 | + weatherPreferences = minimalPreferences, |
| 241 | + weatherModel = testWeatherModel |
| 242 | + ) |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + // Assert core elements still exist |
| 247 | + composeTestRule.onNodeWithTag("weather_preview_screen").assertExists() |
| 248 | + composeTestRule.onNodeWithTag("weather_card").assertExists() |
| 249 | + composeTestRule.onNodeWithTag("save_button").assertExists() |
| 250 | + composeTestRule.onNodeWithTag("delete_button").assertDoesNotExist() |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + fun testWeatherCardVisibility() { |
| 255 | + // Act |
| 256 | + composeTestRule.setContent { |
| 257 | + AppThemeSurface { |
| 258 | + WeatherPreviewContent( |
| 259 | + onClose = {}, |
| 260 | + onBack = {}, |
| 261 | + onClickEdit = {}, |
| 262 | + onClickDelete = {}, |
| 263 | + onClickSave = {}, |
| 264 | + showWidgetTitles = true, |
| 265 | + isWeatherWidgetEnabled = true, |
| 266 | + weatherPreferences = defaultPreferences, |
| 267 | + weatherModel = testWeatherModel |
| 268 | + ) |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + // Assert weather card is displayed with correct content |
| 273 | + composeTestRule.onNodeWithTag("weather_card").assertIsDisplayed() |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + fun testEditButtonShowsCustomState() { |
| 278 | + // Arrange with custom preferences |
| 279 | + val customPreferences = WeatherPreferences( |
| 280 | + showTitle = true, |
| 281 | + showDescription = false, |
| 282 | + showCurrentFee = true, |
| 283 | + showNextBlockFee = false |
| 284 | + ) |
| 285 | + |
| 286 | + // Act |
| 287 | + composeTestRule.setContent { |
| 288 | + AppThemeSurface { |
| 289 | + WeatherPreviewContent( |
| 290 | + onClose = {}, |
| 291 | + onBack = {}, |
| 292 | + onClickEdit = {}, |
| 293 | + onClickDelete = {}, |
| 294 | + onClickSave = {}, |
| 295 | + showWidgetTitles = true, |
| 296 | + isWeatherWidgetEnabled = true, |
| 297 | + weatherPreferences = customPreferences, |
| 298 | + weatherModel = testWeatherModel |
| 299 | + ) |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + // Assert edit button shows custom state |
| 304 | + composeTestRule.onNodeWithTag("edit_settings_button").assertExists() |
| 305 | + } |
| 306 | + |
| 307 | + @Test |
| 308 | + fun testNullWeatherModelCase() { |
| 309 | + // Act |
| 310 | + composeTestRule.setContent { |
| 311 | + AppThemeSurface { |
| 312 | + WeatherPreviewContent( |
| 313 | + onClose = {}, |
| 314 | + onBack = {}, |
| 315 | + onClickEdit = {}, |
| 316 | + onClickDelete = {}, |
| 317 | + onClickSave = {}, |
| 318 | + showWidgetTitles = true, |
| 319 | + isWeatherWidgetEnabled = true, |
| 320 | + weatherPreferences = defaultPreferences, |
| 321 | + weatherModel = null |
| 322 | + ) |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + // Assert weather card doesn't exist when weather model is null |
| 327 | + composeTestRule.onNodeWithTag("weather_card").assertDoesNotExist() |
| 328 | + } |
| 329 | + |
| 330 | + @Test |
| 331 | + fun testEditButtonShowsDefaultState() { |
| 332 | + // Act |
| 333 | + composeTestRule.setContent { |
| 334 | + AppThemeSurface { |
| 335 | + WeatherPreviewContent( |
| 336 | + onClose = {}, |
| 337 | + onBack = {}, |
| 338 | + onClickEdit = {}, |
| 339 | + onClickDelete = {}, |
| 340 | + onClickSave = {}, |
| 341 | + showWidgetTitles = true, |
| 342 | + isWeatherWidgetEnabled = true, |
| 343 | + weatherPreferences = defaultPreferences, |
| 344 | + weatherModel = testWeatherModel |
| 345 | + ) |
| 346 | + } |
| 347 | + } |
| 348 | + |
| 349 | + // Assert edit button shows default state |
| 350 | + composeTestRule.onNodeWithTag("edit_settings_button").assertExists() |
| 351 | + } |
| 352 | +} |
0 commit comments