Repository navigation
Expand file tree
/
Copy pathComposeDeeplinkTests.kt
More file actions
131 lines (111 loc) · 4.66 KB
/
Copy pathComposeDeeplinkTests.kt
File metadata and controls
131 lines (111 loc) · 4.66 KB
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
125
126
127
128
129
130
131
package com.inspiredandroid.linuxcommandbibliotheca
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.activity.compose.setContent
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.platform.WindowInfo
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onAllNodesWithContentDescription
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.linuxcommandlibrary.app.App
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* Test if deeplinks from/to website open correct screens. If test started the first time
* "Always open urls with app" dialog has to be accepted. Or the app has to be signed with the
* release key for autoVerify to take effect.
*/
@RunWith(AndroidJUnit4::class)
class ComposeDeeplinkTests {
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Before
fun setUp() {
val context: Context = ApplicationProvider.getApplicationContext()
// Clear bookmarks
val prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(context)
prefs.edit().putString("KEY_BOOKMARKS", "").apply()
}
private fun openUrl(url: String) {
val context = ApplicationProvider.getApplicationContext<Context>()
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
.setPackage(context.packageName)
composeTestRule.activityRule.scenario.onActivity { activity ->
activity.startActivity(intent)
}
composeTestRule.waitForIdle()
}
@Test
fun testBasicCategories() {
openUrl("https://linuxcommandlibrary.com/basics")
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals("Basics")
}
@Test
fun testBasicCategory() {
openUrl("https://linuxcommandlibrary.com/basic/usersgroups")
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals("Users & Groups")
}
@Test
fun testTips() {
openUrl("https://linuxcommandlibrary.com/tips")
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals("Tips")
}
@Test
fun testCommandList() {
openUrl("https://linuxcommandlibrary.com/")
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals("Commands")
}
@Test
fun testCommandListDoesNotSelectFirstCommandOnExpandedLayout() {
// Pretend the window is tablet sized so the two-pane list/detail layout is used.
composeTestRule.runOnUiThread {
composeTestRule.activity.setContent {
val windowInfo = LocalWindowInfo.current
val density = LocalDensity.current
val expandedWindowInfo = remember(windowInfo, density) {
object : WindowInfo by windowInfo {
override val containerSize: IntSize
get() = with(density) { IntSize(1280.dp.roundToPx(), 800.dp.roundToPx()) }
}
}
CompositionLocalProvider(LocalWindowInfo provides expandedWindowInfo) {
App(initialDeeplink = "https://linuxcommandlibrary.com/")
}
}
}
// Wait for the command list to load before checking that nothing got selected.
composeTestRule.waitUntil(timeoutMillis = 10_000) {
composeTestRule.onAllNodesWithText("2048").fetchSemanticsNodes().isNotEmpty()
}
composeTestRule.waitForIdle()
composeTestRule.onNodeWithText("Select a command").assertIsDisplayed()
composeTestRule.onAllNodesWithContentDescription("Search in page")
.assertCountEquals(0)
}
@Test
fun testCommandDetail() {
openUrl("https://linuxcommandlibrary.com/man/2048")
composeTestRule.onNodeWithContentDescription("TopAppBarTitle")
.assertTextEquals("2048")
}
}