@@ -2,28 +2,198 @@ package org.qownnotes.mobile
22
33import androidx.compose.ui.test.assertIsDisplayed
44import androidx.compose.ui.test.junit4.createAndroidComposeRule
5+ import androidx.compose.ui.test.onAllNodesWithText
6+ import androidx.compose.ui.test.onNodeWithTag
57import androidx.compose.ui.test.onNodeWithText
8+ import androidx.compose.ui.test.performClick
69import androidx.test.ext.junit.runners.AndroidJUnit4
10+ import com.nextcloud.android.sso.model.SingleSignOnAccount
11+ import java.io.IOException
12+ import kotlinx.coroutines.CompletableDeferred
13+ import kotlinx.coroutines.runBlocking
14+ import org.junit.Assert.assertEquals
15+ import org.junit.Before
716import org.junit.Rule
817import org.junit.Test
918import org.junit.runner.RunWith
19+ import org.qownnotes.mobile.core.BackendException
20+ import org.qownnotes.mobile.core.PullResult
21+ import org.qownnotes.mobile.core.RemoteNote
1022
1123@RunWith(AndroidJUnit4 ::class )
1224class AppLaunchTest {
1325 @get:Rule val composeRule = createAndroidComposeRule<MainActivity >()
1426
27+ private val application
28+ get() = composeRule.activity.application as TestQOwnNotesApplication
29+
30+ @Before
31+ fun resetApplication () {
32+ runBlocking { application.reset() }
33+ composeRule.waitForIdle()
34+ }
35+
1536 @Test
16- fun launchesAccountEntryPoint () {
17- composeRule.onNodeWithText(" Add" , substring = true ).assertIsDisplayed()
37+ fun onboardingImportsAccountAndDisplaysInitialPull () {
38+ val account = testAccount(" alice" )
39+ application.fakeAccountImporter.enqueue(account)
40+ application.fakeBackend.enqueue(account, pull(" Alice note" , " etag-1" , 10 ))
41+
42+ composeRule.onNodeWithTag(" onboarding" ).assertIsDisplayed()
43+ composeRule.onNodeWithTag(" add-account" ).performClick()
44+
45+ composeRule.waitForText(" Alice note" )
46+ composeRule.onNodeWithText(" alice @ cloud.example" ).assertIsDisplayed()
47+ composeRule.onNodeWithText(" Alice note" ).assertIsDisplayed()
1848 }
1949
2050 @Test
2151 fun showsAccountImportFailureOnOnboarding () {
22- composeRule.runOnUiThread {
23- val application = composeRule.activity.application as QOwnNotesApplication
24- application.component.reportImportError(IllegalStateException (" Import unavailable" ))
25- }
52+ application.fakeAccountImporter.enqueueFailure(IllegalStateException (" Import unavailable" ))
2653
54+ composeRule.onNodeWithTag(" add-account" ).performClick()
55+
56+ composeRule.waitForText(" Import unavailable" )
2757 composeRule.onNodeWithText(" Import unavailable" ).assertIsDisplayed()
2858 }
59+
60+ @Test
61+ fun accountImportSurvivesActivityRecreationDuringValidation () {
62+ val account = testAccount(" alice" )
63+ val validationGate = CompletableDeferred <Unit >()
64+ application.fakeBackend.validationGate = validationGate
65+ application.fakeAccountImporter.enqueue(account)
66+ application.fakeBackend.enqueue(account, pull(" Alice note" , " etag-1" , 10 ))
67+ composeRule.onNodeWithTag(" add-account" ).performClick()
68+ composeRule.waitUntil {
69+ application.fakeBackend.validatedAccountIds.isNotEmpty()
70+ }
71+
72+ composeRule.activityRule.scenario.recreate()
73+ validationGate.complete(Unit )
74+
75+ composeRule.waitForText(" Alice note" )
76+ composeRule.onNodeWithText(" Alice note" ).assertIsDisplayed()
77+ }
78+
79+ @Test
80+ fun cachedNotesRemainVisibleAfterOfflineActivityRestart () {
81+ val account = importAccount(" alice" , " Cached note" , " etag-1" , 10 )
82+ application.fakeBackend.enqueueFailure(
83+ account,
84+ BackendException .Retryable (IOException (" offline" ))
85+ )
86+
87+ composeRule.activityRule.scenario.recreate()
88+
89+ composeRule.waitForText(" The server could not be reached" )
90+ composeRule.onNodeWithText(" Cached note" ).assertIsDisplayed()
91+ }
92+
93+ @Test
94+ fun reconnectPreservesCachedDataAndCheckpoint () {
95+ val account = importAccount(" alice" , " Cached note" , " etag-1" , 10 )
96+ application.fakeBackend.enqueueFailure(account, BackendException .Authentication ())
97+ composeRule.onNodeWithText(" Refresh" ).performClick()
98+ composeRule.waitForText(" Reconnect" )
99+
100+ application.fakeAccountImporter.enqueue(account)
101+ application.fakeBackend.enqueue(account, pull(" Updated note" , " etag-2" , 20 ))
102+ composeRule.onNodeWithText(" Reconnect" ).performClick()
103+
104+ composeRule.waitForText(" Updated note" )
105+ composeRule.onNodeWithText(" Cached note" ).assertDoesNotExist()
106+ val reconnectCheckpoint = application.fakeBackend.checkpoints.last().second
107+ assertEquals(" etag-1" , reconnectCheckpoint.collectionEtag)
108+ assertEquals(10L , reconnectCheckpoint.lastModifiedEpochSeconds)
109+ assertEquals(2 , application.fakeBackend.validatedAccountIds.size)
110+ }
111+
112+ @Test
113+ fun reconnectRejectsADifferentAccount () {
114+ val account = importAccount(" alice" , " Cached note" , " etag-1" , 10 )
115+ application.fakeBackend.enqueueFailure(account, BackendException .Authentication ())
116+ composeRule.onNodeWithText(" Refresh" ).performClick()
117+ composeRule.waitForText(" Reconnect" )
118+
119+ application.fakeAccountImporter.enqueue(testAccount(" bob" ))
120+ composeRule.onNodeWithText(" Reconnect" ).performClick()
121+
122+ composeRule.waitForText(" Select the same Nextcloud account to reconnect" )
123+ composeRule.onNodeWithText(" Cached note" ).assertIsDisplayed()
124+ }
125+
126+ @Test
127+ fun addRejectsAConflictingLocalIdentity () {
128+ importAccount(" alice" , " Alice note" , " etag-1" , 10 )
129+ application.fakeAccountImporter.enqueue(
130+ SingleSignOnAccount (
131+ " alice" ,
132+ " someone-else" ,
133+ " test-token" ,
134+ " https://other.example" ,
135+ " nextcloud"
136+ )
137+ )
138+
139+ composeRule.onNodeWithTag(" add-account" ).performClick()
140+
141+ composeRule.waitForText(" A different Nextcloud account already uses this local identity" )
142+ composeRule.onNodeWithText(" Alice note" ).assertIsDisplayed()
143+ }
144+
145+ @Test
146+ fun switchingAndRemovingAccountsKeepsDataAccountScoped () {
147+ importAccount(" alice" , " Alice note" , " etag-a" , 10 )
148+ val bob = testAccount(" bob" )
149+ application.fakeAccountImporter.enqueue(bob)
150+ application.fakeBackend.enqueue(bob, pull(" Bob note" , " etag-b" , 20 ))
151+ composeRule.onNodeWithTag(" add-account" ).performClick()
152+ composeRule.waitForText(" Switch" )
153+ composeRule.onNodeWithTag(" switch-account" ).performClick()
154+ composeRule.waitForText(" Bob note" )
155+ composeRule.onNodeWithText(" Alice note" ).assertDoesNotExist()
156+
157+ composeRule.onNodeWithTag(" remove-account" ).performClick()
158+ composeRule.onNodeWithText(" server notes will not be deleted" , substring = true )
159+ .assertIsDisplayed()
160+ composeRule.onNodeWithTag(" confirm-remove-account" ).performClick()
161+ composeRule.waitForText(" Alice note" )
162+ composeRule.onNodeWithText(" Bob note" ).assertDoesNotExist()
163+
164+ composeRule.onNodeWithTag(" remove-account" ).performClick()
165+ composeRule.onNodeWithTag(" confirm-remove-account" ).performClick()
166+ composeRule.waitForText(" Your Nextcloud notes, offline" )
167+ }
168+
169+ private fun importAccount (
170+ user : String ,
171+ title : String ,
172+ etag : String ,
173+ modified : Long
174+ ): SingleSignOnAccount {
175+ val account = testAccount(user)
176+ application.fakeAccountImporter.enqueue(account)
177+ application.fakeBackend.enqueue(account, pull(title, etag, modified))
178+ composeRule.onNodeWithTag(" add-account" ).performClick()
179+ composeRule.waitForText(title)
180+ return account
181+ }
182+
183+ private fun testAccount (user : String ) =
184+ SingleSignOnAccount (user, user, " test-token" , " https://cloud.example" , " nextcloud" )
185+
186+ private fun pull (title : String , etag : String , modified : Long ) = PullResult (
187+ notes = listOf (RemoteNote (42 , etag, title, " # $title " , " " , modified)),
188+ collectionEtag = etag,
189+ lastModifiedEpochSeconds = modified
190+ )
191+
192+ private fun androidx.compose.ui.test.junit4.AndroidComposeTestRule <* , * >.waitForText (
193+ text : String
194+ ) {
195+ waitUntil(timeoutMillis = 10_000 ) {
196+ onAllNodesWithText(text).fetchSemanticsNodes().isNotEmpty()
197+ }
198+ }
29199}
0 commit comments