Skip to content
Open

fix: ci #17015

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 90 additions & 69 deletions app/src/androidTest/java/com/nextcloud/test/FileDeletionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@ import kotlin.random.Random
@Suppress("TooManyFunctions", "MagicNumber")
class FileDeletionTests : AbstractIT() {

private lateinit var tempDir: File
private val createdFilePaths = mutableListOf<String>()

@Before
fun setup() {
val parent = System.getProperty("java.io.tmpdir")
val childPath = "file_deletion_test_${System.currentTimeMillis()}"
tempDir = File(parent, childPath)
tempDir.mkdirs()
createdFilePaths.clear()
}

@After
fun cleanup() {
tempDir.deleteRecursively()
createdFilePaths.forEach { File(it).delete() }
createdFilePaths.clear()
}

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

private fun createFileAtExpectedPath(ocFile: OCFile, content: String = "Temporary test content"): File {
val expectedPath = FileStorageUtils.getDefaultSavePathFor(user.accountName, ocFile)
val localFile = File(expectedPath).apply {
parentFile?.mkdirs()
createNewFile()
writeText(content)
}
createdFilePaths.add(localFile.absolutePath)
return localFile
}

private fun createAndSaveSingleFileWithLocalCopy(): OCFile {
val now = System.currentTimeMillis()

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

val localFile = File(tempDir, "TestFile_${file.fileId}.txt").apply {
parentFile?.mkdirs()
createNewFile()
writeText("Temporary test content")
}
val localFile = createFileAtExpectedPath(file)
file.storagePath = localFile.absolutePath

storageManager.saveFile(file)
Expand Down Expand Up @@ -117,11 +122,11 @@ class FileDeletionTests : AbstractIT() {

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

val file1Path = File(tempDir, "file1_${file1.fileId}.txt").apply { createNewFile() }
val file2Path = File(tempDir, "file2_${file2.fileId}.txt").apply { createNewFile() }
val localFile1 = createFileAtExpectedPath(file1)
val localFile2 = createFileAtExpectedPath(file2)

file1.storagePath = file1Path.absolutePath
file2.storagePath = file2Path.absolutePath
file1.storagePath = localFile1.absolutePath
file2.storagePath = localFile2.absolutePath

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

fun createFolder(id: Long, parentId: Long, path: String): OCFile = OCFile(path).apply {
fileId = id
fun saveFolder(parentId: Long, path: String): OCFile = OCFile(path).apply {
this.parentId = parentId
remoteId = getRandomRemoteId()
mimeType = MimeType.DIRECTORY
creationTimestamp = now
modificationTimestamp = now
permissions = "RWDNVCK"
}

fun createFile(id: Long, parentId: Long, path: String, size: Long, mime: String): OCFile = OCFile(path).apply {
fileId = id
this.parentId = parentId
remoteId = getRandomRemoteId()
fileLength = size
creationTimestamp = now
mimeType = mime
modificationTimestamp = now
permissions = "RWDNV"
}

val list = mutableListOf<OCFile>()

list.add(createFolder(1, 0, "/"))

list.add(createFolder(5, 2, "/Documents/Projects"))
list.add(createFile(9, 5, "/Documents/Projects/spec.txt", 12000, MimeType.TEXT_PLAIN))
list.add(createFolder(2, 1, "/Documents"))
list.add(createFile(11, 7, "/Photos/Vacation/img2.jpg", 300000, MimeType.JPEG))
list.add(createFolder(7, 3, "/Photos/Vacation"))
list.add(createFile(4, 2, "/Documents/example.pdf", 150000, MimeType.PDF))
list.add(createFolder(3, 1, "/Photos"))
list.add(createFile(12, 3, "/Photos/cover.png", 80000, MimeType.PNG))
list.add(createFile(6, 5, "/Documents/Projects/readme.txt", 2000, MimeType.TEXT_PLAIN))
list.add(createFolder(8, 5, "/Documents/Projects/Archive"))
list.add(createFile(13, 8, "/Documents/Projects/Archive/old.bmp", 900000, MimeType.BMP))
list.add(createFile(10, 7, "/Photos/Vacation/img1.jpg", 250000, MimeType.JPEG))
list.add(createFolder(14, 1, "/Temp"))
list.add(createFile(15, 14, "/Temp/tmp_file_1.txt", 400, MimeType.TEXT_PLAIN))
list.add(createFile(16, 14, "/Temp/tmp_file_2.txt", 800, MimeType.TEXT_PLAIN))
list.add(createFolder(17, 14, "/Temp/Nested"))
list.add(createFile(18, 17, "/Temp/Nested/deep.txt", 100, MimeType.TEXT_PLAIN))
list.add(createFile(19, 2, "/Documents/notes.txt", 1500, MimeType.TEXT_PLAIN))
list.add(createFolder(20, 3, "/Photos/EmptyFolder"))

list.forEach { ocFile ->
if (!ocFile.isFolder) {
val localFile = File(tempDir, ocFile.remoteId).apply {
parentFile?.mkdirs()
createNewFile()
writeText("test content")
}
ocFile.storagePath = localFile.absolutePath
storageManager.saveFile(ocFile)
} else {
// For folders, create the folder in tempDir
val localFolder = File(tempDir, ocFile.remoteId).apply { mkdirs() }
ocFile.storagePath = localFolder.absolutePath
storageManager.saveFile(ocFile)
}.also { storageManager.saveFile(it) }

fun saveFileWithLocalCopy(parentId: Long, path: String, size: Long, mime: String): OCFile {
val ocFile = OCFile(path).apply {
this.parentId = parentId
remoteId = getRandomRemoteId()
fileLength = size
creationTimestamp = now
mimeType = mime
modificationTimestamp = now
permissions = "RWDNV"
storagePath = FileStorageUtils.getDefaultSavePathFor(user.accountName, this)
}
val localFile = File(ocFile.storagePath).apply {
parentFile?.mkdirs()
createNewFile()
writeText("test content")
}
createdFilePaths.add(localFile.absolutePath)
storageManager.saveFile(ocFile)
return ocFile
}

return list
val root = saveFolder(0, "/")
val documents = saveFolder(root.fileId, "/Documents")
val photos = saveFolder(root.fileId, "/Photos")
val temp = saveFolder(root.fileId, "/Temp")
val projects = saveFolder(documents.fileId, "/Documents/Projects")
val vacation = saveFolder(photos.fileId, "/Photos/Vacation")
val archive = saveFolder(projects.fileId, "/Documents/Projects/Archive")
val nested = saveFolder(temp.fileId, "/Temp/Nested")
val emptyFolder = saveFolder(photos.fileId, "/Photos/EmptyFolder")

val allEntries = mutableListOf(root, documents, photos, temp, projects, vacation, archive, nested, emptyFolder)

allEntries.add(
saveFileWithLocalCopy(
projects.fileId,
"/Documents/Projects/spec.txt",
12000,
MimeType.TEXT_PLAIN
)
)
allEntries.add(saveFileWithLocalCopy(vacation.fileId, "/Photos/Vacation/img2.jpg", 300000, MimeType.JPEG))
allEntries.add(saveFileWithLocalCopy(documents.fileId, "/Documents/example.pdf", 150000, MimeType.PDF))
allEntries.add(saveFileWithLocalCopy(photos.fileId, "/Photos/cover.png", 80000, MimeType.PNG))
allEntries.add(
saveFileWithLocalCopy(
projects.fileId,
"/Documents/Projects/readme.txt",
2000,
MimeType.TEXT_PLAIN
)
)
allEntries.add(
saveFileWithLocalCopy(
archive.fileId,
"/Documents/Projects/Archive/old.bmp",
900000,
MimeType.BMP
)
)
allEntries.add(saveFileWithLocalCopy(vacation.fileId, "/Photos/Vacation/img1.jpg", 250000, MimeType.JPEG))
allEntries.add(saveFileWithLocalCopy(temp.fileId, "/Temp/tmp_file_1.txt", 400, MimeType.TEXT_PLAIN))
allEntries.add(saveFileWithLocalCopy(temp.fileId, "/Temp/tmp_file_2.txt", 800, MimeType.TEXT_PLAIN))
allEntries.add(saveFileWithLocalCopy(nested.fileId, "/Temp/Nested/deep.txt", 100, MimeType.TEXT_PLAIN))
allEntries.add(saveFileWithLocalCopy(documents.fileId, "/Documents/notes.txt", 1500, MimeType.TEXT_PLAIN))

return allEntries.sortedWith(
compareBy<OCFile> { it.isFolder }
.thenByDescending { it.remotePath.count { c -> c == '/' } }
)
}

@Test
Expand Down
68 changes: 55 additions & 13 deletions app/src/androidTest/java/com/nextcloud/utils/UploadDateTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import androidx.test.platform.app.InstrumentationRegistry
import com.nextcloud.client.database.entity.UploadEntity
import com.nextcloud.client.database.entity.toOCUpload
import com.nextcloud.client.database.entity.toUploadEntity
import com.nextcloud.utils.date.DateFormatPattern
import com.owncloud.android.R
import com.owncloud.android.utils.DisplayUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class UploadDateTests {

Expand Down Expand Up @@ -97,12 +94,12 @@ class UploadDateTests {

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

val result = DisplayUtils.getRelativeDateTimeString(
context,
System.currentTimeMillis() + ONE_MINUTE,
time,
DateUtils.SECOND_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0,
Expand Down Expand Up @@ -130,26 +127,59 @@ class UploadDateTests {
@Test
fun getRelativeDateTimeStringReturnsAbbreviatedStringForOneWeekAgo() {
val time = System.currentTimeMillis() - ONE_WEEK
val formatter = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US)
val expected = formatter.format(Date(time))
val expectedString = DateUtils.getRelativeDateTimeString(
context,
time,
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
).toString()
val parts = expectedString.split(",")
val expected = if (parts.size == 2) {
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
} else {
expectedString
}

assertRelativeDateTimeString(time, expected)
}

@Test
fun getRelativeDateTimeStringReturnsAbbreviatedStringForOneMonthAgo() {
val time = System.currentTimeMillis() - ONE_MONTH
val formatter = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US)
val expected = formatter.format(Date(time))
val expectedString = DateUtils.getRelativeDateTimeString(
context,
time,
DateUtils.SECOND_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
).toString()
val parts = expectedString.split(",")
val expected = if (parts.size == 2) {
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
} else {
expectedString
}

assertRelativeDateTimeString(time, expected, DateUtils.SECOND_IN_MILLIS)
}

@Test
fun getRelativeDateTimeStringReturnsAbsoluteStringForOneYearAgo() {
val time = System.currentTimeMillis() - ONE_YEAR
val formatter = SimpleDateFormat("M/d/YYYY", Locale.US)
val expected = formatter.format(Date(time))
val expectedString = DateUtils.getRelativeDateTimeString(
context,
time,
DateUtils.SECOND_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
).toString()
val parts = expectedString.split(",")
val expected = if (parts.size == 2) {
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
} else {
expectedString
}

assertRelativeDateTimeString(time, expected, DateUtils.SECOND_IN_MILLIS)
}
Expand Down Expand Up @@ -194,7 +224,19 @@ class UploadDateTests {
assertEquals(expected, result)

testTimestamp = System.currentTimeMillis() - 7 * DateUtils.DAY_IN_MILLIS
expected = SimpleDateFormat(DateFormatPattern.MonthWithDate.pattern, Locale.US).format(testTimestamp)
val expectedString = DateUtils.getRelativeDateTimeString(
context,
testTimestamp,
DateUtils.DAY_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0
).toString()
val parts = expectedString.split(",")
expected = if (parts.size == 2) {
if (parts[1].contains(":") && !parts[0].contains(":")) parts[0].trim() else parts[1].trim()
} else {
expectedString
}
result = DisplayUtils.getRelativeDateTimeString(
context,
testTimestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public void uploadOCUpload(OCUpload ocUpload, int localBehaviour) {
ConnectivityService connectivityServiceMock = new ConnectivityService() {
@Override
public void isNetworkAndServerAvailable(@NonNull GenericCallback<Boolean> callback) {

callback.onComplete(true);
}

@Override
public boolean isConnected() {
return false;
return true;
}

@Override
Expand Down Expand Up @@ -248,7 +248,7 @@ public boolean isPowerSavingEnabled() {
user,
null,
ocUpload,
NameCollisionPolicy.DEFAULT,
NameCollisionPolicy.OVERWRITE,
localBehaviour,
targetContext,
false,
Expand Down
Loading
Loading