From 131c5b6b292392a093751fa9e56f43481b00cfb7 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 6 Aug 2020 16:43:15 -0300 Subject: [PATCH] Add test to reproduce the loading cursor phenomena with Nextcloud See: https://commonsware.com/blog/2019/12/14/scoped-storage-stories-listfiles-woe.html --- .../com/stevesoltys/seedvault/PluginTest.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/src/androidTest/java/com/stevesoltys/seedvault/PluginTest.kt b/app/src/androidTest/java/com/stevesoltys/seedvault/PluginTest.kt index 7f9954df..d2251636 100644 --- a/app/src/androidTest/java/com/stevesoltys/seedvault/PluginTest.kt +++ b/app/src/androidTest/java/com/stevesoltys/seedvault/PluginTest.kt @@ -62,6 +62,47 @@ class PluginTest : KoinComponent { assertNotNull(backupPlugin.providerPackageName) } + /** + * This test initializes the storage three times while creating two new restore sets. + * + * If this is run against a Nextcloud storage backend, + * it has a high chance of getting a loading cursor in the underlying queries + * that needs to get re-queried to get real results. + */ + @Test + fun testInitializationAndRestoreSets() { + // no backups available initially + assertEquals(0, restorePlugin.getAvailableBackups()?.toList()?.size) + val uri = settingsManager.getStorage()?.getDocumentFile(context)?.uri ?: error("no storage") + assertFalse(restorePlugin.hasBackup(uri)) + + // define storage changing state for later + every { + mockedSettingsManager.getAndResetIsStorageChanging() + } returns true andThen true andThen false + + // device needs initialization, because new and storage is changing + assertTrue(backupPlugin.initializeDevice(newToken = token)) + + // write metadata (needed for backup to be recognized) + backupPlugin.getMetadataOutputStream().writeAndClose(getRandomByteArray()) + + // one backup available now + assertEquals(1, restorePlugin.getAvailableBackups()?.toList()?.size) + assertTrue(restorePlugin.hasBackup(uri)) + + // initializing again (while changing storage) does add a restore set + assertTrue(backupPlugin.initializeDevice(newToken = token + 1)) + backupPlugin.getMetadataOutputStream().writeAndClose(getRandomByteArray()) + assertEquals(2, restorePlugin.getAvailableBackups()?.toList()?.size) + assertTrue(restorePlugin.hasBackup(uri)) + + // initializing again (without changing storage) doesn't change number of restore sets + assertFalse(backupPlugin.initializeDevice(newToken = token + 2)) + backupPlugin.getMetadataOutputStream().writeAndClose(getRandomByteArray()) + assertEquals(2, restorePlugin.getAvailableBackups()?.toList()?.size) + } + @Test fun testMetadataWriteRead() { every { mockedSettingsManager.getAndResetIsStorageChanging() } returns true andThen false