Browse Source

Merge pull request #7118 from jmue/fix-documentprovider-test

fix documentprovider test failure because of asynchronous upload
Tobias Kaminsky 4 years ago
parent
commit
984c93fe50

+ 38 - 28
src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt

@@ -6,6 +6,7 @@ import androidx.documentfile.provider.DocumentFile
 import com.owncloud.android.AbstractOnServerIT
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile.ROOT_PATH
+import com.owncloud.android.lib.common.network.WebdavUtils
 import com.owncloud.android.providers.DocumentsProviderUtils.assertExistsOnServer
 import com.owncloud.android.providers.DocumentsProviderUtils.assertListFilesEquals
 import com.owncloud.android.providers.DocumentsProviderUtils.assertReadEquals
@@ -18,6 +19,9 @@ import com.owncloud.android.providers.DocumentsProviderUtils.listFilesBlocking
 import com.owncloud.android.providers.DocumentsStorageProvider.DOCUMENTID_SEPARATOR
 import kotlinx.coroutines.runBlocking
 import net.bytebuddy.utility.RandomString
+import org.apache.commons.httpclient.HttpStatus
+import org.apache.commons.httpclient.methods.ByteArrayRequestEntity
+import org.apache.jackrabbit.webdav.client.methods.PutMethod
 import org.junit.After
 import org.junit.Assert.assertEquals
 import org.junit.Assert.assertFalse
@@ -173,32 +177,38 @@ class DocumentsStorageProviderIT : AbstractOnServerIT() {
         assertExistsOnServer(client, ocFile1.remotePath, false)
     }
 
-    // disabled as flaky test
-    // @Test
-    // fun testServerChangedFileContent() {
-    //     // create random file
-    //     val file1 = rootDir.createFile("text/plain", RandomString.make())!!
-    //     file1.assertRegularFile(size = 0L)
-    //
-    //     val content1 = "initial content".toByteArray()
-    //
-    //     // write content bytes to file
-    //     contentResolver.openOutputStream(file1.uri, "wt").use {
-    //         it!!.write(content1)
-    //     }
-    //
-    //     val remotePath = file1.getOCFile(storageManager)!!.remotePath
-    //
-    //     val content2 = "new content".toByteArray()
-    //
-    //     // modify content on server side
-    //     val putMethod = PutMethod(client.webdavUri.toString() + WebdavUtils.encodePath(remotePath))
-    //     putMethod.setRequestEntity(ByteArrayRequestEntity(content2))
-    //     assertEquals(HttpStatus.SC_NO_CONTENT, client.executeMethod(putMethod))
-    //     client.exhaustResponse(putMethod.responseBodyAsStream)
-    //     putMethod.releaseConnection() // let the connection available for other methods
-    //
-    //     // read back content bytes
-    //     assertReadEquals(content2, contentResolver.openInputStream(file1.uri))
-    // }
+    @Test
+    fun testServerChangedFileContent() {
+        // create random file
+        val file1 = rootDir.createFile("text/plain", RandomString.make())!!
+        file1.assertRegularFile(size = 0L)
+
+        val createdETag = file1.getOCFile(storageManager)!!.etag
+
+        val content1 = "initial content".toByteArray()
+
+        // write content bytes to file
+        contentResolver.openOutputStream(file1.uri, "wt").use {
+            it!!.write(content1)
+        }
+
+        while (file1.getOCFile(storageManager)!!.etag == createdETag) {
+            shortSleep()
+        }
+
+        val remotePath = file1.getOCFile(storageManager)!!.remotePath
+
+        val content2 = "new content".toByteArray()
+
+        // modify content on server side
+        val putMethod = PutMethod(client.webdavUri.toString() + WebdavUtils.encodePath(remotePath))
+        putMethod.requestEntity = ByteArrayRequestEntity(content2)
+        assertEquals(HttpStatus.SC_NO_CONTENT, client.executeMethod(putMethod))
+        client.exhaustResponse(putMethod.responseBodyAsStream)
+        putMethod.releaseConnection() // let the connection available for other methods
+
+        // read back content bytes
+        val bytes = contentResolver.openInputStream(file1.uri)?.readBytes() ?: ByteArray(0)
+        assertEquals(String(content2), String(bytes))
+    }
 }