Skip to content

Commit 4c5a0bf

Browse files
Merge pull request #17015 from nextcloud/fix/ci-gh
fix: ci
2 parents 6de01ec + 7159715 commit 4c5a0bf

7 files changed

Lines changed: 273 additions & 220 deletions

File tree

app/src/androidTest/java/com/nextcloud/test/FileDeletionTests.kt

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,35 @@ import kotlin.random.Random
2525
@Suppress("TooManyFunctions", "MagicNumber")
2626
class FileDeletionTests : AbstractIT() {
2727

28-
private lateinit var tempDir: File
28+
private val createdFilePaths = mutableListOf<String>()
2929

3030
@Before
3131
fun setup() {
32-
val parent = System.getProperty("java.io.tmpdir")
33-
val childPath = "file_deletion_test_${System.currentTimeMillis()}"
34-
tempDir = File(parent, childPath)
35-
tempDir.mkdirs()
32+
createdFilePaths.clear()
3633
}
3734

3835
@After
3936
fun cleanup() {
40-
tempDir.deleteRecursively()
37+
createdFilePaths.forEach { File(it).delete() }
38+
createdFilePaths.clear()
4139
}
4240

4341
private fun getRandomRemoteId(): String = Random
4442
.nextLong(10_000_000L, 99_999_999L)
4543
.toString()
4644
.padEnd(32, '0')
4745

46+
private fun createFileAtExpectedPath(ocFile: OCFile, content: String = "Temporary test content"): File {
47+
val expectedPath = FileStorageUtils.getDefaultSavePathFor(user.accountName, ocFile)
48+
val localFile = File(expectedPath).apply {
49+
parentFile?.mkdirs()
50+
createNewFile()
51+
writeText(content)
52+
}
53+
createdFilePaths.add(localFile.absolutePath)
54+
return localFile
55+
}
56+
4857
private fun createAndSaveSingleFileWithLocalCopy(): OCFile {
4958
val now = System.currentTimeMillis()
5059

@@ -59,11 +68,7 @@ class FileDeletionTests : AbstractIT() {
5968
permissions = "RWDNV"
6069
}
6170

62-
val localFile = File(tempDir, "TestFile_${file.fileId}.txt").apply {
63-
parentFile?.mkdirs()
64-
createNewFile()
65-
writeText("Temporary test content")
66-
}
71+
val localFile = createFileAtExpectedPath(file)
6772
file.storagePath = localFile.absolutePath
6873

6974
storageManager.saveFile(file)
@@ -117,11 +122,11 @@ class FileDeletionTests : AbstractIT() {
117122

118123
listOf(rootFolder, subFolder, file1, file2).forEach { storageManager.saveFile(it) }
119124

120-
val file1Path = File(tempDir, "file1_${file1.fileId}.txt").apply { createNewFile() }
121-
val file2Path = File(tempDir, "file2_${file2.fileId}.txt").apply { createNewFile() }
125+
val localFile1 = createFileAtExpectedPath(file1)
126+
val localFile2 = createFileAtExpectedPath(file2)
122127

123-
file1.storagePath = file1Path.absolutePath
124-
file2.storagePath = file2Path.absolutePath
128+
file1.storagePath = localFile1.absolutePath
129+
file2.storagePath = localFile2.absolutePath
125130

126131
storageManager.saveFile(file1)
127132
storageManager.saveFile(file2)
@@ -132,69 +137,85 @@ class FileDeletionTests : AbstractIT() {
132137
private fun getMixedOcFiles(): List<OCFile> {
133138
val now = System.currentTimeMillis()
134139

135-
fun createFolder(id: Long, parentId: Long, path: String): OCFile = OCFile(path).apply {
136-
fileId = id
140+
fun saveFolder(parentId: Long, path: String): OCFile = OCFile(path).apply {
137141
this.parentId = parentId
138142
remoteId = getRandomRemoteId()
139143
mimeType = MimeType.DIRECTORY
140144
creationTimestamp = now
141145
modificationTimestamp = now
142146
permissions = "RWDNVCK"
143-
}
144-
145-
fun createFile(id: Long, parentId: Long, path: String, size: Long, mime: String): OCFile = OCFile(path).apply {
146-
fileId = id
147-
this.parentId = parentId
148-
remoteId = getRandomRemoteId()
149-
fileLength = size
150-
creationTimestamp = now
151-
mimeType = mime
152-
modificationTimestamp = now
153-
permissions = "RWDNV"
154-
}
155-
156-
val list = mutableListOf<OCFile>()
157-
158-
list.add(createFolder(1, 0, "/"))
159-
160-
list.add(createFolder(5, 2, "/Documents/Projects"))
161-
list.add(createFile(9, 5, "/Documents/Projects/spec.txt", 12000, MimeType.TEXT_PLAIN))
162-
list.add(createFolder(2, 1, "/Documents"))
163-
list.add(createFile(11, 7, "/Photos/Vacation/img2.jpg", 300000, MimeType.JPEG))
164-
list.add(createFolder(7, 3, "/Photos/Vacation"))
165-
list.add(createFile(4, 2, "/Documents/example.pdf", 150000, MimeType.PDF))
166-
list.add(createFolder(3, 1, "/Photos"))
167-
list.add(createFile(12, 3, "/Photos/cover.png", 80000, MimeType.PNG))
168-
list.add(createFile(6, 5, "/Documents/Projects/readme.txt", 2000, MimeType.TEXT_PLAIN))
169-
list.add(createFolder(8, 5, "/Documents/Projects/Archive"))
170-
list.add(createFile(13, 8, "/Documents/Projects/Archive/old.bmp", 900000, MimeType.BMP))
171-
list.add(createFile(10, 7, "/Photos/Vacation/img1.jpg", 250000, MimeType.JPEG))
172-
list.add(createFolder(14, 1, "/Temp"))
173-
list.add(createFile(15, 14, "/Temp/tmp_file_1.txt", 400, MimeType.TEXT_PLAIN))
174-
list.add(createFile(16, 14, "/Temp/tmp_file_2.txt", 800, MimeType.TEXT_PLAIN))
175-
list.add(createFolder(17, 14, "/Temp/Nested"))
176-
list.add(createFile(18, 17, "/Temp/Nested/deep.txt", 100, MimeType.TEXT_PLAIN))
177-
list.add(createFile(19, 2, "/Documents/notes.txt", 1500, MimeType.TEXT_PLAIN))
178-
list.add(createFolder(20, 3, "/Photos/EmptyFolder"))
179-
180-
list.forEach { ocFile ->
181-
if (!ocFile.isFolder) {
182-
val localFile = File(tempDir, ocFile.remoteId).apply {
183-
parentFile?.mkdirs()
184-
createNewFile()
185-
writeText("test content")
186-
}
187-
ocFile.storagePath = localFile.absolutePath
188-
storageManager.saveFile(ocFile)
189-
} else {
190-
// For folders, create the folder in tempDir
191-
val localFolder = File(tempDir, ocFile.remoteId).apply { mkdirs() }
192-
ocFile.storagePath = localFolder.absolutePath
193-
storageManager.saveFile(ocFile)
147+
}.also { storageManager.saveFile(it) }
148+
149+
fun saveFileWithLocalCopy(parentId: Long, path: String, size: Long, mime: String): OCFile {
150+
val ocFile = OCFile(path).apply {
151+
this.parentId = parentId
152+
remoteId = getRandomRemoteId()
153+
fileLength = size
154+
creationTimestamp = now
155+
mimeType = mime
156+
modificationTimestamp = now
157+
permissions = "RWDNV"
158+
storagePath = FileStorageUtils.getDefaultSavePathFor(user.accountName, this)
159+
}
160+
val localFile = File(ocFile.storagePath).apply {
161+
parentFile?.mkdirs()
162+
createNewFile()
163+
writeText("test content")
194164
}
165+
createdFilePaths.add(localFile.absolutePath)
166+
storageManager.saveFile(ocFile)
167+
return ocFile
195168
}
196169

197-
return list
170+
val root = saveFolder(0, "/")
171+
val documents = saveFolder(root.fileId, "/Documents")
172+
val photos = saveFolder(root.fileId, "/Photos")
173+
val temp = saveFolder(root.fileId, "/Temp")
174+
val projects = saveFolder(documents.fileId, "/Documents/Projects")
175+
val vacation = saveFolder(photos.fileId, "/Photos/Vacation")
176+
val archive = saveFolder(projects.fileId, "/Documents/Projects/Archive")
177+
val nested = saveFolder(temp.fileId, "/Temp/Nested")
178+
val emptyFolder = saveFolder(photos.fileId, "/Photos/EmptyFolder")
179+
180+
val allEntries = mutableListOf(root, documents, photos, temp, projects, vacation, archive, nested, emptyFolder)
181+
182+
allEntries.add(
183+
saveFileWithLocalCopy(
184+
projects.fileId,
185+
"/Documents/Projects/spec.txt",
186+
12000,
187+
MimeType.TEXT_PLAIN
188+
)
189+
)
190+
allEntries.add(saveFileWithLocalCopy(vacation.fileId, "/Photos/Vacation/img2.jpg", 300000, MimeType.JPEG))
191+
allEntries.add(saveFileWithLocalCopy(documents.fileId, "/Documents/example.pdf", 150000, MimeType.PDF))
192+
allEntries.add(saveFileWithLocalCopy(photos.fileId, "/Photos/cover.png", 80000, MimeType.PNG))
193+
allEntries.add(
194+
saveFileWithLocalCopy(
195+
projects.fileId,
196+
"/Documents/Projects/readme.txt",
197+
2000,
198+
MimeType.TEXT_PLAIN
199+
)
200+
)
201+
allEntries.add(
202+
saveFileWithLocalCopy(
203+
archive.fileId,
204+
"/Documents/Projects/Archive/old.bmp",
205+
900000,
206+
MimeType.BMP
207+
)
208+
)
209+
allEntries.add(saveFileWithLocalCopy(vacation.fileId, "/Photos/Vacation/img1.jpg", 250000, MimeType.JPEG))
210+
allEntries.add(saveFileWithLocalCopy(temp.fileId, "/Temp/tmp_file_1.txt", 400, MimeType.TEXT_PLAIN))
211+
allEntries.add(saveFileWithLocalCopy(temp.fileId, "/Temp/tmp_file_2.txt", 800, MimeType.TEXT_PLAIN))
212+
allEntries.add(saveFileWithLocalCopy(nested.fileId, "/Temp/Nested/deep.txt", 100, MimeType.TEXT_PLAIN))
213+
allEntries.add(saveFileWithLocalCopy(documents.fileId, "/Documents/notes.txt", 1500, MimeType.TEXT_PLAIN))
214+
215+
return allEntries.sortedWith(
216+
compareBy<OCFile> { it.isFolder }
217+
.thenByDescending { it.remotePath.count { c -> c == '/' } }
218+
)
198219
}
199220

200221
@Test

app/src/androidTest/java/com/nextcloud/utils/UploadDateTests.kt

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,13 @@ import androidx.test.platform.app.InstrumentationRegistry
1414
import com.nextcloud.client.database.entity.UploadEntity
1515
import com.nextcloud.client.database.entity.toOCUpload
1616
import com.nextcloud.client.database.entity.toUploadEntity
17-
import com.nextcloud.utils.date.DateFormatPattern
1817
import com.owncloud.android.R
1918
import com.owncloud.android.utils.DisplayUtils
2019
import org.junit.Assert.assertEquals
2120
import org.junit.Assert.assertNotNull
2221
import org.junit.Before
2322
import org.junit.Test
24-
import java.text.SimpleDateFormat
2523
import java.util.Date
26-
import java.util.Locale
2724

2825
class UploadDateTests {
2926

@@ -97,12 +94,12 @@ class UploadDateTests {
9794

9895
@Test
9996
fun getRelativeDateTimeStringReturnsFutureAsAbsoluteWhenShowFutureIsFalse() {
100-
val formatter = SimpleDateFormat("MMM d, yyyy h:mm:ss a", Locale.US)
101-
val expected = formatter.format(Date(System.currentTimeMillis() + ONE_MINUTE))
97+
val time = System.currentTimeMillis() + ONE_MINUTE
98+
val expected = java.text.DateFormat.getDateTimeInstance().format(Date(time))
10299

103100
val result = DisplayUtils.getRelativeDateTimeString(
104101
context,
105-
System.currentTimeMillis() + ONE_MINUTE,
102+
time,
106103
DateUtils.SECOND_IN_MILLIS,
107104
DateUtils.WEEK_IN_MILLIS,
108105
0,
@@ -130,26 +127,59 @@ class UploadDateTests {
130127
@Test
131128
fun getRelativeDateTimeStringReturnsAbbreviatedStringForOneWeekAgo() {
132129
val time = System.currentTimeMillis() - ONE_WEEK
133-
val formatter = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US)
134-
val expected = formatter.format(Date(time))
130+
val expectedString = DateUtils.getRelativeDateTimeString(
131+
context,
132+
time,
133+
DateUtils.MINUTE_IN_MILLIS,
134+
DateUtils.WEEK_IN_MILLIS,
135+
0
136+
).toString()
137+
val parts = expectedString.split(",")
138+
val expected = if (parts.size == 2) {
139+
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
140+
} else {
141+
expectedString
142+
}
135143

136144
assertRelativeDateTimeString(time, expected)
137145
}
138146

139147
@Test
140148
fun getRelativeDateTimeStringReturnsAbbreviatedStringForOneMonthAgo() {
141149
val time = System.currentTimeMillis() - ONE_MONTH
142-
val formatter = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US)
143-
val expected = formatter.format(Date(time))
150+
val expectedString = DateUtils.getRelativeDateTimeString(
151+
context,
152+
time,
153+
DateUtils.SECOND_IN_MILLIS,
154+
DateUtils.WEEK_IN_MILLIS,
155+
0
156+
).toString()
157+
val parts = expectedString.split(",")
158+
val expected = if (parts.size == 2) {
159+
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
160+
} else {
161+
expectedString
162+
}
144163

145164
assertRelativeDateTimeString(time, expected, DateUtils.SECOND_IN_MILLIS)
146165
}
147166

148167
@Test
149168
fun getRelativeDateTimeStringReturnsAbsoluteStringForOneYearAgo() {
150169
val time = System.currentTimeMillis() - ONE_YEAR
151-
val formatter = SimpleDateFormat("M/d/YYYY", Locale.US)
152-
val expected = formatter.format(Date(time))
170+
val expectedString = DateUtils.getRelativeDateTimeString(
171+
context,
172+
time,
173+
DateUtils.SECOND_IN_MILLIS,
174+
DateUtils.WEEK_IN_MILLIS,
175+
0
176+
).toString()
177+
val parts = expectedString.split(",")
178+
val expected = if (parts.size == 2) {
179+
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
180+
} else {
181+
expectedString
182+
}
153183

154184
assertRelativeDateTimeString(time, expected, DateUtils.SECOND_IN_MILLIS)
155185
}
@@ -194,7 +224,19 @@ class UploadDateTests {
194224
assertEquals(expected, result)
195225

196226
testTimestamp = System.currentTimeMillis() - 7 * DateUtils.DAY_IN_MILLIS
197-
expected = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US).format(testTimestamp)
227+
val expectedString = DateUtils.getRelativeDateTimeString(
228+
context,
229+
testTimestamp,
230+
DateUtils.DAY_IN_MILLIS,
231+
DateUtils.WEEK_IN_MILLIS,
232+
0
233+
).toString()
234+
val parts = expectedString.split(",")
235+
expected = if (parts.size == 2) {
236+
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
237+
} else {
238+
expectedString
239+
}
198240
result = DisplayUtils.getRelativeDateTimeString(
199241
context,
200242
testTimestamp,

app/src/androidTest/java/com/owncloud/android/AbstractOnServerIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ public void uploadOCUpload(OCUpload ocUpload, int localBehaviour) {
205205
ConnectivityService connectivityServiceMock = new ConnectivityService() {
206206
@Override
207207
public void isNetworkAndServerAvailable(@NonNull GenericCallback<Boolean> callback) {
208-
208+
callback.onComplete(true);
209209
}
210210

211211
@Override
212212
public boolean isConnected() {
213-
return false;
213+
return true;
214214
}
215215

216216
@Override
@@ -248,7 +248,7 @@ public boolean isPowerSavingEnabled() {
248248
user,
249249
null,
250250
ocUpload,
251-
NameCollisionPolicy.DEFAULT,
251+
NameCollisionPolicy.OVERWRITE,
252252
localBehaviour,
253253
targetContext,
254254
false,

0 commit comments

Comments
 (0)