From 49d8183917efc950069a1dfccca85a56190913a0 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Mon, 17 May 2021 14:45:34 +0530 Subject: fix(tests): return null/default values from faked android sdk fixes behaviour of getFilesDir Signed-off-by: SphericalKat --- app/build.gradle.kts | 3 + .../msfjarvis/aps/util/settings/MigrationsTest.kt | 124 -------------------- .../msfjarvis/aps/util/settings/MigrationsTest.kt | 125 +++++++++++++++++++++ 3 files changed, 128 insertions(+), 124 deletions(-) delete mode 100644 app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt create mode 100644 app/src/test/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4b420846..c12cf566 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -47,6 +47,9 @@ android { create("free") {} create("nonFree") {} } + testOptions { + unitTests.isReturnDefaultValues = true + } } dependencies { diff --git a/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt b/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt deleted file mode 100644 index a5975be7..00000000 --- a/app/src/androidTest/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -@file:Suppress("DEPRECATION") - -package dev.msfjarvis.aps.util.settings - -import android.content.Context -import androidx.core.content.edit -import dev.msfjarvis.aps.Application -import dev.msfjarvis.aps.util.extensions.getString -import dev.msfjarvis.aps.util.extensions.sharedPrefs -import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse -import org.junit.Assert.assertNull -import org.junit.Test - -class MigrationsTest { - - private fun checkOldKeysAreRemoved(context: Context) = - with(context.sharedPrefs) { - assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_LOCATION)) - assertNull(getString(PreferenceKeys.GIT_REMOTE_PROTOCOL)) - } - - @Test - fun verifySshWithCustomPortMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_PORT, "2200") - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo" - ) - } - - @Test - fun verifySshWithDefaultPortMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo" - ) - } - - @Test - fun verifyHttpsWithGitHubMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") - putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test") - putString(PreferenceKeys.GIT_REMOTE_SERVER, "github.com") - putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref) - putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref) - } - runMigrations(context) - checkOldKeysAreRemoved(context) - assertEquals( - context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), - "https://github.com/Android-Password-Store/pass-test" - ) - } - - @Test - fun verifyHiddenFoldersMigrationIfDisabled() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { clear() } - runMigrations(context) - assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)) - assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) - } - - @Test - fun verifyHiddenFoldersMigrationIfEnabled() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true) - } - runMigrations(context) - assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)) - assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) - } - - @Test - fun verifyClearClipboardHistoryMigration() { - val context = Application.instance.applicationContext - context.sharedPrefs.edit { - clear() - putBoolean(PreferenceKeys.CLEAR_CLIPBOARD_20X, true) - } - runMigrations(context) - assertEquals( - true, - context.sharedPrefs.getBoolean(PreferenceKeys.CLEAR_CLIPBOARD_HISTORY, false) - ) - assertFalse(context.sharedPrefs.contains(PreferenceKeys.CLEAR_CLIPBOARD_20X)) - } -} diff --git a/app/src/test/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt b/app/src/test/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt new file mode 100644 index 00000000..d6c9e6b6 --- /dev/null +++ b/app/src/test/java/dev/msfjarvis/aps/util/settings/MigrationsTest.kt @@ -0,0 +1,125 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +@file:Suppress("DEPRECATION") + +package dev.msfjarvis.aps.util.settings + +import android.content.Context +import androidx.core.content.edit +import com.github.ivanshafran.sharedpreferencesmock.SPMockBuilder +import dev.msfjarvis.aps.Application +import dev.msfjarvis.aps.util.extensions.getString +import dev.msfjarvis.aps.util.extensions.sharedPrefs +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Test + +class MigrationsTest { + + private fun checkOldKeysAreRemoved(context: Context) = + with(context.sharedPrefs) { + assertNull(getString(PreferenceKeys.GIT_REMOTE_PORT)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_USERNAME)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_SERVER)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_LOCATION)) + assertNull(getString(PreferenceKeys.GIT_REMOTE_PROTOCOL)) + } + + @Test + fun verifySshWithCustomPortMigration() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_PORT, "2200") + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.Password.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "ssh://msfjarvis@192.168.0.102:2200/mnt/disk3/pass-repo" + ) + } + + @Test + fun verifySshWithDefaultPortMigration() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "/mnt/disk3/pass-repo") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "192.168.0.102") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Ssh.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.SshKey.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "msfjarvis@192.168.0.102:/mnt/disk3/pass-repo" + ) + } + + @Test + fun verifyHttpsWithGitHubMigration() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { + clear() + putString(PreferenceKeys.GIT_REMOTE_USERNAME, "msfjarvis") + putString(PreferenceKeys.GIT_REMOTE_LOCATION, "Android-Password-Store/pass-test") + putString(PreferenceKeys.GIT_REMOTE_SERVER, "github.com") + putString(PreferenceKeys.GIT_REMOTE_PROTOCOL, Protocol.Https.pref) + putString(PreferenceKeys.GIT_REMOTE_AUTH, AuthMode.None.pref) + } + runMigrations(context) + checkOldKeysAreRemoved(context) + assertEquals( + context.sharedPrefs.getString(PreferenceKeys.GIT_REMOTE_URL), + "https://github.com/Android-Password-Store/pass-test" + ) + } + + @Test + fun verifyHiddenFoldersMigrationIfDisabled() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { clear() } + runMigrations(context) + assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true)) + assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) + } + + @Test + fun verifyHiddenFoldersMigrationIfEnabled() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { + clear() + putBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, true) + } + runMigrations(context) + assertEquals(false, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_FOLDERS, false)) + assertEquals(true, context.sharedPrefs.getBoolean(PreferenceKeys.SHOW_HIDDEN_CONTENTS, false)) + } + + @Test + fun verifyClearClipboardHistoryMigration() { + val context = SPMockBuilder().createContext() + context.sharedPrefs.edit { + clear() + putBoolean(PreferenceKeys.CLEAR_CLIPBOARD_20X, true) + } + runMigrations(context) + assertEquals( + true, + context.sharedPrefs.getBoolean(PreferenceKeys.CLEAR_CLIPBOARD_HISTORY, false) + ) + assertFalse(context.sharedPrefs.contains(PreferenceKeys.CLEAR_CLIPBOARD_20X)) + } +} -- cgit v1.2.3