-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathAutoUploadHelperTest.kt
More file actions
330 lines (265 loc) · 10.6 KB
/
AutoUploadHelperTest.kt
File metadata and controls
330 lines (265 loc) · 10.6 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package com.owncloud.android.utils
import android.content.Context
import com.nextcloud.client.database.dao.FileSystemDao
import com.nextcloud.client.jobs.autoUpload.AutoUploadHelper
import com.nextcloud.client.jobs.autoUpload.FileSystemRepository
import com.nextcloud.client.preferences.SubFolderRule
import com.nextcloud.utils.extensions.shouldSkipFile
import com.owncloud.android.datamodel.MediaFolderType
import com.owncloud.android.datamodel.SyncedFolder
import io.mockk.clearAllMocks
import io.mockk.mockk
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import java.io.File
import java.nio.file.Files
@Suppress("MagicNumber")
class AutoUploadHelperTest {
private lateinit var tempDir: File
private val helper = AutoUploadHelper()
private val accountName = "testAccount"
private val mockDao: FileSystemDao = mockk(relaxed = true)
private val mockContext: Context = mockk(relaxed = true)
private lateinit var repo: FileSystemRepository
@Before
fun setup() {
tempDir = Files.createTempDirectory("auto_upload_test_").toFile()
tempDir.mkdirs()
assertTrue("Failed to create temp directory", tempDir.exists())
repo = FileSystemRepository(mockDao, mockContext)
}
@After
fun cleanup() {
tempDir.deleteRecursively()
clearAllMocks()
}
private fun createTestFolder(
localPath: String = tempDir.absolutePath,
excludeHidden: Boolean = false,
lastScan: Long = -1L,
enabledTimestamp: Long = 0L,
alsoUploadExistingFiles: Boolean = true,
type: MediaFolderType = MediaFolderType.CUSTOM
): SyncedFolder = SyncedFolder(
localPath,
"",
true,
false,
alsoUploadExistingFiles,
false,
accountName,
1,
1,
true,
enabledTimestamp,
type,
false,
SubFolderRule.YEAR_MONTH,
excludeHidden,
lastScan
)
@Test
fun testInsertCustomFolderProcessedCount() {
File(tempDir, "file1.txt").apply {
writeText("Hello")
assertTrue("File1 should exist", exists())
}
File(tempDir, "file2.txt").apply {
writeText("World")
assertTrue("File2 should exist", exists())
}
val folder = createTestFolder(
localPath = tempDir.absolutePath,
type = MediaFolderType.CUSTOM
)
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
assertEquals("Should process 2 files", 2, processedCount)
}
@Test
fun testInsertCustomFolderWithHiddenFiles() {
File(tempDir, "visible.txt").apply { writeText("Visible") }
File(tempDir, ".hidden.txt").apply {
writeText("Hidden")
}
val folder = createTestFolder(
excludeHidden = true,
type = MediaFolderType.CUSTOM
)
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
assertTrue("Should process at least 1 file", processedCount >= 1)
}
@Test
fun testInsertCustomFolderWithLastScanFilter() {
val currentTime = System.currentTimeMillis()
// Create an old file
val oldFile = File(tempDir, "old.txt").apply { writeText("Old") }
val oldFileLastModified = currentTime - 10000 // 10 seconds ago
// Create a new file
val newFile = File(tempDir, "new.txt").apply { writeText("New") }
val folder = createTestFolder(
lastScan = currentTime - 5000, // Last scan was 5 seconds ago
type = MediaFolderType.CUSTOM
)
val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null, true)
assertTrue(shouldSkipOldFile)
val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null, false)
assertTrue(!shouldSkipNewFile)
}
@Test
fun testInsertCustomFolderNotExisting() {
val currentTime = System.currentTimeMillis()
// old file should not be scanned
val oldFile = File(tempDir, "old.txt").apply { writeText("Old") }
val oldFileLastModified = currentTime - 10000 // 10 seconds ago
val newFile = File(tempDir, "new.txt").apply { writeText("New") }
// Enabled 5 seconds ago
val folder = createTestFolder(
enabledTimestamp = currentTime - 5000,
type = MediaFolderType.CUSTOM
).apply {
lastScanTimestampMs = currentTime
}
val shouldSkipOldFile = folder.shouldSkipFile(oldFile, oldFileLastModified, null, true)
assertTrue(shouldSkipOldFile)
val shouldSkipNewFile = folder.shouldSkipFile(newFile, currentTime, null, false)
assertTrue(!shouldSkipNewFile)
}
@Test
fun testInsertCustomFolderEmpty() {
val folder = createTestFolder(type = MediaFolderType.CUSTOM)
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
assertEquals("Empty folder should process 0 files", 0, processedCount)
}
@Test
fun testInsertCustomFolderNonExistentPath() {
val nonExistentPath = File(tempDir, "does_not_exist").absolutePath
val folder = createTestFolder(
localPath = nonExistentPath,
type = MediaFolderType.CUSTOM
)
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
assertEquals("Non-existent folder should return 0", 0, processedCount)
}
@Test
fun testInsertCustomFolderWithSubdirectories() {
val subDir = File(tempDir, "subdir")
subDir.mkdirs()
File(tempDir, "root.txt").writeText("Root file")
File(subDir, "nested.txt").writeText("Nested file")
val folder = createTestFolder(type = MediaFolderType.CUSTOM)
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
assertEquals("Should process files in root and subdirectories", 2, processedCount)
}
@Test
fun testInsertCustomFolderWithHiddenDirectory() {
val currentTime = System.currentTimeMillis()
val hiddenDir = File(tempDir, ".hidden_dir")
hiddenDir.mkdirs()
try {
Files.setAttribute(hiddenDir.toPath(), "dos:hidden", true)
} catch (_: Exception) {
}
File(hiddenDir, "file.txt").writeText("Hidden dir file")
// Create regular file
File(tempDir, "regular.txt").writeText("Regular file")
val folder = createTestFolder(
excludeHidden = true,
type = MediaFolderType.CUSTOM
).apply {
lastScanTimestampMs = currentTime
}
val processedCount = helper.insertCustomFolderIntoDB(folder, repo)
// Should skip hidden directory and its contents
assertEquals("Should only process regular file", 1, processedCount)
}
@Test
fun testInsertCustomFolderComplexNestedStructure() {
// Root folder: FOLDER_A
val folderA = File(tempDir, "FOLDER_A")
folderA.mkdirs()
// Subfolders of FOLDER_A
val folderB = File(folderA, "FOLDER_B")
folderB.mkdirs()
val folderC = File(folderA, "FOLDER_C")
folderC.mkdirs()
// Files in FOLDER_A
File(folderA, "FILE_A.txt").writeText("File in A")
// Subfolders of FOLDER_B
val folderD = File(folderB, "FOLDER_D")
folderD.mkdirs()
// Files in FOLDER_B
File(folderB, "FILE_B.txt").writeText("File in B")
// Files in FOLDER_C
File(folderC, "FILE_A.txt").writeText("File in C")
File(folderC, "FILE_B.txt").writeText("Another file in C")
// Subfolders of FOLDER_D
val folderE = File(folderD, "FOLDER_E")
folderE.mkdirs()
// Files in FOLDER_E
File(folderE, "FILE_A.txt").writeText("File in E")
val syncedFolder = createTestFolder(
localPath = folderA.absolutePath,
type = MediaFolderType.CUSTOM
)
/*
* Expected file count with full paths:
* ${tempDir.absolutePath}/FOLDER_A/FILE_A.txt -> 1
* ${tempDir.absolutePath}/FOLDER_A/FOLDER_B/FILE_B.txt -> 1
* ${tempDir.absolutePath}/FOLDER_A/FOLDER_C/FILE_A.txt -> 1
* ${tempDir.absolutePath}/FOLDER_A/FOLDER_C/FILE_B.txt -> 1
* ${tempDir.absolutePath}/FOLDER_A/FOLDER_B/FOLDER_D/FOLDER_E/FILE_A.txt -> 1
* Total = 5 files
*/
val processedCount = helper.insertCustomFolderIntoDB(syncedFolder, repo)
assertEquals("Should process all files in complex nested structure", 5, processedCount)
}
@Test
fun testAlsoUploadExistingFiles() {
val currentTime = System.currentTimeMillis()
// Old file (created before enabling auto-upload)
val oldFile = File(tempDir, "old_file.txt").apply {
writeText("Old file")
}
val oldFileCreationTime = currentTime - 10_000
val oldFileLastModified = currentTime - 5_000
// New file (created after enabling auto-upload)
val newFile = File(tempDir, "new_file.txt").apply {
writeText("New file")
}
val newFileCreationTime = currentTime + 10_000
val newFileLastModified = currentTime + 5_000
val folderSkipOld = createTestFolder(
localPath = tempDir.absolutePath,
type = MediaFolderType.CUSTOM,
alsoUploadExistingFiles = false
).apply {
setEnabled(true, currentTime)
}
val shouldSkipOldFile = folderSkipOld.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime, true)
assertTrue(shouldSkipOldFile)
val shouldSkipNewFile = folderSkipOld.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime, false)
assertTrue(!shouldSkipNewFile)
val folderUploadAll = createTestFolder(
localPath = tempDir.absolutePath,
type = MediaFolderType.CUSTOM,
alsoUploadExistingFiles = true
).apply {
setEnabled(true, currentTime)
}
val shouldSkipOldFileIfAlsoUploadExistingFile =
folderUploadAll.shouldSkipFile(oldFile, oldFileLastModified, oldFileCreationTime, true)
assertTrue(!shouldSkipOldFileIfAlsoUploadExistingFile)
val shouldSkipNewFileIfAlsoUploadExistingFile =
folderUploadAll.shouldSkipFile(newFile, newFileLastModified, newFileCreationTime, false)
assertTrue(!shouldSkipNewFileIfAlsoUploadExistingFile)
}
}