forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiveExternalFilesActivityIT.kt
More file actions
246 lines (216 loc) · 11.1 KB
/
ReceiveExternalFilesActivityIT.kt
File metadata and controls
246 lines (216 loc) · 11.1 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2025 Philipp Hasper <vcs@hasper.info>
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2022 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
package com.owncloud.android.ui.activity
import android.content.Intent
import android.net.Uri
import android.view.KeyEvent
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import com.facebook.testing.screenshot.internal.TestNameDetector
import com.nextcloud.client.preferences.AppPreferencesImpl
import com.nextcloud.test.GrantStoragePermissionRule
import com.nextcloud.test.withSelectedText
import com.nextcloud.utils.extensions.removeFileExtension
import com.owncloud.android.AbstractIT
import com.owncloud.android.R
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.utils.ScreenshotTest
import org.hamcrest.Matchers.not
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import java.io.File
class ReceiveExternalFilesActivityIT : AbstractIT() {
@get:Rule
var storagePermissionRule: TestRule = GrantStoragePermissionRule.grant()
lateinit var mainFolder: OCFile
lateinit var subFolder: OCFile
lateinit var existingImageFile: OCFile
@Before
fun setupFolderAndFileStructure() {
// Create folders with the necessary permissions and another test file
mainFolder = OCFile("/folder/").apply {
permissions = OCFile.PERMISSION_CAN_CREATE_FILE_AND_FOLDER
setFolder()
fileDataStorageManager.saveNewFile(this)
}
subFolder = OCFile("${mainFolder.remotePath}sub folder/").apply {
permissions = OCFile.PERMISSION_CAN_CREATE_FILE_AND_FOLDER
setFolder()
fileDataStorageManager.saveNewFile(this)
}
existingImageFile = OCFile("${mainFolder.remotePath}Existing Image File.jpg").apply {
fileDataStorageManager.saveNewFile(this)
}
}
@Test
@ScreenshotTest
fun open() {
// Screenshot name must be constructed outside of the scenario, otherwise it will not be reliably detected
val screenShotName = TestNameDetector.getTestClass() + "_" + TestNameDetector.getTestName()
launchActivity<ReceiveExternalFilesActivity>().use { scenario ->
onView(isRoot()).check(matches(isDisplayed()))
scenario.onActivity { sut ->
screenshotViaName(sut, screenShotName)
}
}
}
@Test
@ScreenshotTest
fun openMultiAccount() {
val secondAccount = createAccount("secondtest@https://nextcloud.localhost")
open()
removeAccount(secondAccount)
}
fun createSendIntent(file: File): Intent = Intent(targetContext, ReceiveExternalFilesActivity::class.java).apply {
action = Intent.ACTION_SEND
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file))
}
fun createSendIntent(files: Iterable<File>): Intent =
Intent(targetContext, ReceiveExternalFilesActivity::class.java).apply {
action = Intent.ACTION_SEND_MULTIPLE
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(files.map { Uri.fromFile(it) }))
}
@Test
fun renameSingleFileUpload() {
val imageFile = getDummyFile("image.jpg")
val intent = createSendIntent(imageFile)
// Store the folder in preferences, so the activity starts from there.
@Suppress("DEPRECATION")
val preferences = AppPreferencesImpl.fromContext(targetContext)
preferences.setLastUploadPath(mainFolder.remotePath)
launchActivity<ReceiveExternalFilesActivity>(intent).use {
val expectedMainFolderTitle = (getCurrentActivity() as ToolbarActivity).getActionBarTitle(mainFolder, false)
// Verify that the test starts in the expected folder. If this fails, change the setup calls above
onView(withId(R.id.toolbar))
.check(matches(hasDescendant(withText(expectedMainFolderTitle))))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
// Test the pre-selection behavior (filename, but without extension, shall be selected)
onView(withId(R.id.user_input))
.check(matches(withText(imageFile.name)))
.perform(ViewActions.click())
.check(matches(withSelectedText(imageFile.name.removeFileExtension())))
// Set a new file name
val secondFileName = "New filename.jpg"
onView(withId(R.id.user_input))
.perform(ViewActions.typeTextIntoFocusedView(secondFileName.removeFileExtension()))
.check(matches(withText(secondFileName)))
// Leave the field and come back to verify the pre-selection behavior correctly handles the new name
.perform(ViewActions.pressKey(KeyEvent.KEYCODE_TAB))
.perform(ViewActions.click())
.check(matches(withSelectedText(secondFileName.removeFileExtension())))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
// Set a file name without file extension
val thirdFileName = "No extension"
onView(withId(R.id.user_input))
.perform(ViewActions.clearText())
.perform(ViewActions.typeTextIntoFocusedView(thirdFileName))
.check(matches(withText(thirdFileName)))
// Leave the field and come back to verify the pre-selection behavior correctly handles the new name
.perform(ViewActions.pressKey(KeyEvent.KEYCODE_TAB))
.perform(ViewActions.click())
.check(matches(withSelectedText(thirdFileName)))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
// Test an invalid filename. Note: as the user is null, the capabilities are also null, so the name checker
// will not reject any special characters like '/'. So we only test empty and an existing file name
onView(withId(R.id.user_input))
.perform(ViewActions.clearText())
.check(matches(withText("")))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(not(isEnabled())))
onView(withId(R.id.user_input))
.perform(ViewActions.click())
.perform(ViewActions.typeTextIntoFocusedView(existingImageFile.fileName))
.check(matches(withText(existingImageFile.fileName)))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(not(isEnabled())))
val fourthFileName = "New file name.jpg"
onView(withId(R.id.user_input))
.perform(ViewActions.click())
.perform(ViewActions.clearText())
.perform(ViewActions.typeTextIntoFocusedView(fourthFileName))
.check(matches(withText(fourthFileName)))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
// Enter the subfolder and verify that the text stays intact
val expectedSubFolderTitle = (getCurrentActivity() as ToolbarActivity).getActionBarTitle(subFolder, false)
onView(withText(expectedSubFolderTitle))
.perform(ViewActions.click())
onView(withId(R.id.toolbar))
.check(matches(hasDescendant(withText(expectedSubFolderTitle))))
onView(withId(R.id.user_input))
.check(matches(withText(fourthFileName)))
.perform(ViewActions.click())
.check(matches(withSelectedText(fourthFileName.removeFileExtension())))
// Set a new, shorter file name
val fifthFileName = "short.jpg"
onView(withId(R.id.user_input))
.perform(ViewActions.typeTextIntoFocusedView(fifthFileName.removeFileExtension()))
.check(matches(withText(fifthFileName)))
// Start the upload, so the folder is stored in the preferences.
// Even though the upload is expected to fail because the backend is not mocked (yet?)
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
.perform(ViewActions.click())
}
// Start a new file receive flow. Should now start in the sub folder, but with the original filename again
launchActivity<ReceiveExternalFilesActivity>(intent).use {
val expectedMainFolderTitle = (getCurrentActivity() as ToolbarActivity).getActionBarTitle(subFolder, false)
onView(withId(R.id.toolbar))
.check(matches(hasDescendant(withText(expectedMainFolderTitle))))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
onView(withId(R.id.user_input))
.check(matches(withText(imageFile.name)))
}
}
@Test
fun noRenameForMultiUpload() {
val testFiles = createDummyFiles()
val intent = createSendIntent(testFiles)
// Store the folder in preferences, so the activity starts from there.
@Suppress("DEPRECATION")
val preferences = AppPreferencesImpl.fromContext(targetContext)
preferences.setLastUploadPath(mainFolder.remotePath)
launchActivity<ReceiveExternalFilesActivity>(intent).use {
val expectedMainFolderTitle = (getCurrentActivity() as ToolbarActivity).getActionBarTitle(mainFolder, false)
// Verify that the test starts in the expected folder. If this fails, change the setup calls above
onView(withId(R.id.toolbar))
.check(matches(hasDescendant(withText(expectedMainFolderTitle))))
onView(withText(R.string.uploader_btn_upload_text))
.check(matches(isDisplayed()))
.check(matches(isEnabled()))
onView(withId(R.id.user_input))
.check(matches(not(isDisplayed())))
}
}
}