-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathComposeTests.kt
124 lines (101 loc) · 4.95 KB
/
ComposeTests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.inspiredandroid.linuxcommandbibliotheca
import android.content.Context
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.linuxcommandlibrary.shared.copyDatabase
import com.linuxcommandlibrary.shared.databaseHelper
import com.linuxcommandlibrary.shared.initDatabase
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.java.KoinJavaComponent.inject
/**
* Test for navigation, search and booksmarks. More tests to come
*/
@RunWith(AndroidJUnit4::class)
class ComposeTests {
@get:Rule
val composeTestRule = createComposeRule()
@Before
fun setUp() {
val context: Context = ApplicationProvider.getApplicationContext()
copyDatabase(context)
initDatabase(context)
val dataManager: com.inspiredandroid.linuxcommandbibliotheca.DataManager by inject(com.inspiredandroid.linuxcommandbibliotheca.DataManager::class.java)
dataManager.updateDatabaseVersion()
// Clear bookmarks
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
prefs.edit().putString("KEY_BOOKMARKS", "").apply()
composeTestRule.setContent { LinuxApp() }
}
/**
* Click though BottomNavigationBar and assert that TopAppBar titles are correct
*/
@Test
fun testBottomNavigation() {
composeTestRule.onNodeWithText("Tips").performClick()
composeTestRule.onNodeWithContentDescription("TopAppBarTitle").assertTextEquals("Tips")
composeTestRule.onNodeWithText("Commands").performClick()
composeTestRule.onNodeWithContentDescription("Back").performClick()
composeTestRule.onNodeWithContentDescription("TopAppBarTitle").assertTextEquals("Commands")
composeTestRule.onNodeWithText("Basics").performClick()
composeTestRule.onNodeWithContentDescription("TopAppBarTitle").assertTextEquals("Basics")
}
/**
* Test if info is shown when search for a command that doesn't exist and if command description
* is shown when search for an existing command
*/
@Test
fun testSearch() {
composeTestRule.onNodeWithText("Commands").performClick()
composeTestRule.onNodeWithContentDescription("Search").performClick()
// Search for a command that doesn't exist
composeTestRule.onNodeWithContentDescription("SearchField")
.performTextInput("CommandThatDoesn'tExist")
composeTestRule.onNodeWithText("404 command not found").assertIsDisplayed()
composeTestRule.onNodeWithContentDescription("SearchField").performTextClearance()
// Search for an existing command
val firstCommand = databaseHelper.getCommands().last()
composeTestRule.onNodeWithContentDescription("SearchField")
.performTextInput(firstCommand.name)
composeTestRule.onNodeWithText(firstCommand.description).assertIsDisplayed()
}
/**
* Test if bookmarks in command detail and command list are shown correctly
*/
@Test
fun testBookmarks() {
val firstCommand = databaseHelper.getCommands().first()
composeTestRule.onNodeWithText("Commands").performClick()
composeTestRule.onNodeWithContentDescription("Search").performClick()
// Search for first command and go to command detail screen
composeTestRule.onNodeWithContentDescription("SearchField")
.performTextInput(firstCommand.name)
composeTestRule.onNodeWithText(firstCommand.description).performClick()
// Click bookmark icon and check if icon/contentDescription changed
composeTestRule.onNodeWithContentDescription("Add bookmark").performClick()
composeTestRule.mainClock.advanceTimeBy(1000)
composeTestRule.waitForIdle()
composeTestRule.onNodeWithContentDescription("Remove bookmark").assertIsDisplayed()
// Go back to search/list and check if bookmark icon is visible
composeTestRule.onNodeWithContentDescription("Back").performClick()
composeTestRule.onNodeWithContentDescription("Bookmarked").assertIsDisplayed()
}
@Test
fun testBasicsScreen() {
val firstBasicCategory = databaseHelper.getBasics().first()
// Click on first category
composeTestRule.onNodeWithText(firstBasicCategory.title).performClick()
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals(firstBasicCategory.title)
val basicGroup = databaseHelper.getBasicGroupsByQuery(firstBasicCategory.id).first()
// Click on first group
composeTestRule.onNodeWithText(basicGroup.description).performClick()
// Check if commands of group expanded and therefore share icon(s) are visible
composeTestRule.onAllNodesWithContentDescription("Share").assertAny(isEnabled())
}
}