mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-06-18 04:39:45 -04:00
Auto-format all files with ktfmt
This commit is contained in:
@@ -6,106 +6,97 @@ import android.content.Context.MODE_PRIVATE
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import org.fdroid.settings.SettingsManager
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.FileOutputStream
|
||||
import kotlin.random.Random
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import org.fdroid.settings.SettingsManager
|
||||
import org.junit.Rule
|
||||
import org.junit.rules.TemporaryFolder
|
||||
|
||||
private const val MAX_EVENTS = 10
|
||||
|
||||
internal class HistoryManagerTest {
|
||||
|
||||
@get:Rule
|
||||
val tempFolder = TemporaryFolder()
|
||||
@get:Rule val tempFolder = TemporaryFolder()
|
||||
|
||||
private val context: Context = mockk()
|
||||
private val settingsManager: SettingsManager = mockk()
|
||||
private val manager = HistoryManager(context, settingsManager, MAX_EVENTS)
|
||||
private val context: Context = mockk()
|
||||
private val settingsManager: SettingsManager = mockk()
|
||||
private val manager = HistoryManager(context, settingsManager, MAX_EVENTS)
|
||||
|
||||
@Test
|
||||
fun testAppendGetAndClear() {
|
||||
val file = tempFolder.newFile()
|
||||
every { context.openFileOutput(any(), MODE_APPEND) } answers {
|
||||
FileOutputStream(file, true)
|
||||
}
|
||||
every { context.openFileInput(any()) } answers {
|
||||
file.inputStream()
|
||||
}
|
||||
every { settingsManager.useInstallHistory } returns true
|
||||
@Test
|
||||
fun testAppendGetAndClear() {
|
||||
val file = tempFolder.newFile()
|
||||
every { context.openFileOutput(any(), MODE_APPEND) } answers { FileOutputStream(file, true) }
|
||||
every { context.openFileInput(any()) } answers { file.inputStream() }
|
||||
every { settingsManager.useInstallHistory } returns true
|
||||
|
||||
val installEvent = InstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "foo.bar",
|
||||
name = "Foo Bar",
|
||||
versionName = "1.0.3",
|
||||
oldVersionName = if (Random.nextBoolean()) null else "1.0.1",
|
||||
)
|
||||
val uninstallEvent = UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
manager.append(installEvent)
|
||||
manager.append(installEvent)
|
||||
manager.append(uninstallEvent)
|
||||
assertEquals(
|
||||
listOf(installEvent, installEvent, uninstallEvent),
|
||||
manager.getEvents(),
|
||||
)
|
||||
val installEvent =
|
||||
InstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "foo.bar",
|
||||
name = "Foo Bar",
|
||||
versionName = "1.0.3",
|
||||
oldVersionName = if (Random.nextBoolean()) null else "1.0.1",
|
||||
)
|
||||
val uninstallEvent =
|
||||
UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
manager.append(installEvent)
|
||||
manager.append(installEvent)
|
||||
manager.append(uninstallEvent)
|
||||
assertEquals(listOf(installEvent, installEvent, uninstallEvent), manager.getEvents())
|
||||
|
||||
// delete file
|
||||
every { context.deleteFile(any()) } returns true
|
||||
manager.clearAll()
|
||||
verify { context.deleteFile(any()) }
|
||||
// delete file
|
||||
every { context.deleteFile(any()) } returns true
|
||||
manager.clearAll()
|
||||
verify { context.deleteFile(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoAppendWhenDisabled() {
|
||||
val uninstallEvent =
|
||||
UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
every { settingsManager.useInstallHistory } returns false
|
||||
manager.append(uninstallEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPrune() {
|
||||
val file = tempFolder.newFile()
|
||||
every { context.openFileOutput(any(), MODE_APPEND) } answers { FileOutputStream(file, true) }
|
||||
every { context.openFileInput(any()) } answers { file.inputStream() }
|
||||
every { settingsManager.useInstallHistory } returns true
|
||||
|
||||
val installEvent =
|
||||
InstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "foo.bar",
|
||||
name = "Foo Bar",
|
||||
versionName = "1.0.3",
|
||||
oldVersionName = if (Random.nextBoolean()) null else "1.0.1",
|
||||
)
|
||||
val uninstallEvent =
|
||||
UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
repeat((0 until MAX_EVENTS).count()) {
|
||||
manager.append(installEvent)
|
||||
manager.append(uninstallEvent)
|
||||
}
|
||||
assertEquals(MAX_EVENTS * 2, manager.getEvents().size)
|
||||
|
||||
@Test
|
||||
fun testNoAppendWhenDisabled() {
|
||||
val uninstallEvent = UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
every { settingsManager.useInstallHistory } returns false
|
||||
manager.append(uninstallEvent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPrune() {
|
||||
val file = tempFolder.newFile()
|
||||
every { context.openFileOutput(any(), MODE_APPEND) } answers {
|
||||
FileOutputStream(file, true)
|
||||
}
|
||||
every { context.openFileInput(any()) } answers {
|
||||
file.inputStream()
|
||||
}
|
||||
every { settingsManager.useInstallHistory } returns true
|
||||
|
||||
val installEvent = InstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "foo.bar",
|
||||
name = "Foo Bar",
|
||||
versionName = "1.0.3",
|
||||
oldVersionName = if (Random.nextBoolean()) null else "1.0.1",
|
||||
)
|
||||
val uninstallEvent = UninstallEvent(
|
||||
time = Random.nextLong(),
|
||||
packageName = "org.example",
|
||||
name = if (Random.nextBoolean()) null else "2.0.3",
|
||||
)
|
||||
repeat((0 until MAX_EVENTS).count()) {
|
||||
manager.append(installEvent)
|
||||
manager.append(uninstallEvent)
|
||||
}
|
||||
assertEquals(MAX_EVENTS * 2, manager.getEvents().size)
|
||||
|
||||
every { context.openFileOutput(any(), MODE_PRIVATE) } answers {
|
||||
FileOutputStream(file, false)
|
||||
}
|
||||
manager.pruneEvents()
|
||||
assertEquals(MAX_EVENTS, manager.getEvents().size)
|
||||
}
|
||||
every { context.openFileOutput(any(), MODE_PRIVATE) } answers { FileOutputStream(file, false) }
|
||||
manager.pruneEvents()
|
||||
assertEquals(MAX_EVENTS, manager.getEvents().size)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +1,73 @@
|
||||
package org.fdroid.ui.details
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Tests modifications to app details descriptions done in [getHtmlDescription].
|
||||
*/
|
||||
/** Tests modifications to app details descriptions done in [getHtmlDescription]. */
|
||||
class HtmlDescriptionTest {
|
||||
@Test
|
||||
fun testLinks() {
|
||||
val description = """
|
||||
@Test
|
||||
fun testLinks() {
|
||||
val description =
|
||||
"""
|
||||
2. If you have experience with Java and the Android SDK, then we look forward to your contributions! More info: https://mediawiki.org/wiki/Wikimedia_Apps/Team/Android/App_hacking
|
||||
|
||||
3. Explanation of permissions needed by the app: https://mediawiki.org/wiki/Wikimedia_Apps/Android_FAQ#Security_and_Permissions
|
||||
"""
|
||||
val expectedDescription = """<br>
|
||||
val expectedDescription =
|
||||
"""<br>
|
||||
2. If you have experience with Java and the Android SDK, then we look forward to your contributions! More info: <a href="https://mediawiki.org/wiki/Wikimedia_Apps/Team/Android/App_hacking">https://mediawiki.org/wiki/Wikimedia_Apps/Team/Android/App_hacking</a><br>
|
||||
<br>
|
||||
3. Explanation of permissions needed by the app: <a href="https://mediawiki.org/wiki/Wikimedia_Apps/Android_FAQ#Security_and_Permissions">https://mediawiki.org/wiki/Wikimedia_Apps/Android_FAQ#Security_and_Permissions</a><br>
|
||||
"""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLinkAtTheVeryEnd() {
|
||||
val description = """
|
||||
@Test
|
||||
fun testLinkAtTheVeryEnd() {
|
||||
val description =
|
||||
"""
|
||||
Project page: https://github.com/lukaspieper/Gcam-Services-Provider"""
|
||||
val expectedDescription = """<br>
|
||||
val expectedDescription =
|
||||
"""<br>
|
||||
Project page: <a href="https://github.com/lukaspieper/Gcam-Services-Provider">https://github.com/lukaspieper/Gcam-Services-Provider</a>"""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLinkWithDotAtTheEnd() {
|
||||
val description = """please visit our website: https://wikimediafoundation.org/."""
|
||||
@Test
|
||||
fun testLinkWithDotAtTheEnd() {
|
||||
val description = """please visit our website: https://wikimediafoundation.org/."""
|
||||
|
||||
@Suppress("ktlint:standard:max-line-length")
|
||||
val expectedDescription = """please visit our website: <a href="https://wikimediafoundation.org/">https://wikimediafoundation.org/</a>."""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
val expectedDescription =
|
||||
"""please visit our website: <a href="https://wikimediafoundation.org/">https://wikimediafoundation.org/</a>."""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLinkInRoundBrackets() {
|
||||
val description = """our link (https://wikimediafoundation.org/)."""
|
||||
@Test
|
||||
fun testLinkInRoundBrackets() {
|
||||
val description = """our link (https://wikimediafoundation.org/)."""
|
||||
|
||||
@Suppress("ktlint:standard:max-line-length")
|
||||
val expectedDescription = """our link (<a href="https://wikimediafoundation.org/">https://wikimediafoundation.org/</a>)."""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
val expectedDescription =
|
||||
"""our link (<a href="https://wikimediafoundation.org/">https://wikimediafoundation.org/</a>)."""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testHeadlineRemoval() {
|
||||
val description = """<h1>SimpleX - the first messaging platform that has no user identifiers, not even random numbers!</h1>
|
||||
@Test
|
||||
fun testHeadlineRemoval() {
|
||||
val description =
|
||||
"""<h1>SimpleX - the first messaging platform that has no user identifiers, not even random numbers!</h1>
|
||||
<p><a href="https://simplex.chat/blog/20221108-simplex-chat-v4.2-security-audit-new-website.html" target="_blank">Security assessment</a> was done by Trail of Bits in November 2022.</p>
|
||||
<p>SimpleX Chat features:</p>
|
||||
<ul>
|
||||
<li>end-to-end encrypted messages, with editing, replies and deletion of messages.</li>
|
||||
<li>sending end-to-end encrypted images and files.</li>"""
|
||||
|
||||
val expectedDescription = """SimpleX - the first messaging platform that has no user identifiers, not even random numbers!<br>
|
||||
val expectedDescription =
|
||||
"""SimpleX - the first messaging platform that has no user identifiers, not even random numbers!<br>
|
||||
<p><a href="https://simplex.chat/blog/20221108-simplex-chat-v4.2-security-audit-new-website.html" target="_blank">Security assessment</a> was done by Trail of Bits in November 2022.</p>
|
||||
<p>SimpleX Chat features:</p>
|
||||
<ul>
|
||||
<li>end-to-end encrypted messages, with editing, replies and deletion of messages.</li>
|
||||
<li>sending end-to-end encrypted images and files.</li>"""
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
assertEquals(expectedDescription, getHtmlDescription(description))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,102 +8,105 @@ import android.content.Intent.EXTRA_PACKAGE_NAME
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.core.net.toUri
|
||||
import androidx.navigation3.runtime.NavBackStack
|
||||
import kotlin.test.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
@Config(sdk = [24]) // needed for ACTION_SHOW_APP_INFO
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class IntentRouterTest {
|
||||
|
||||
val navigationState = NavigationState(
|
||||
startRoute = NavigationKey.Discover,
|
||||
topLevelRoute = mutableStateOf(NavigationKey.Discover),
|
||||
backStacks = topLevelRoutes.associateWith { key ->
|
||||
NavBackStack(key)
|
||||
}
|
||||
val navigationState =
|
||||
NavigationState(
|
||||
startRoute = NavigationKey.Discover,
|
||||
topLevelRoute = mutableStateOf(NavigationKey.Discover),
|
||||
backStacks = topLevelRoutes.associateWith { key -> NavBackStack(key) },
|
||||
)
|
||||
val navigator = Navigator(navigationState)
|
||||
private val intentRouter = IntentRouter(navigator)
|
||||
val navigator = Navigator(navigationState)
|
||||
private val intentRouter = IntentRouter(navigator)
|
||||
|
||||
@Test
|
||||
fun testBrowserUris() {
|
||||
val packageName = "org.fdroid.fdroid"
|
||||
listOf(
|
||||
"https://f-droid.org/packages/$packageName/",
|
||||
"https://f-droid.org/de/packages/$packageName/",
|
||||
"https://f-droid.org/en-US/packages/$packageName",
|
||||
"https://cloudflare.f-droid.org/zh_Hans/packages/$packageName",
|
||||
"market://details?id=$packageName",
|
||||
).forEach { url ->
|
||||
val i = Intent().apply {
|
||||
action = ACTION_VIEW
|
||||
addCategory(CATEGORY_BROWSABLE)
|
||||
data = url.toUri()
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.AppDetails(packageName), navigator.last)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMalformedBrowserUris() {
|
||||
val packageName = "fdroid') DROP TABLE apps; --"
|
||||
listOf(
|
||||
"https://f-droid.org/packages/$packageName/",
|
||||
"https://f-droid.org/de/packages/$packageName/",
|
||||
"https://f-droid.org/en-US/packages/$packageName",
|
||||
"https://cloudflare.f-droid.org/zh_Hans/packages/$packageName",
|
||||
"market://details?id=$packageName",
|
||||
).forEach { url ->
|
||||
val i = Intent().apply {
|
||||
action = ACTION_VIEW
|
||||
addCategory(CATEGORY_BROWSABLE)
|
||||
data = url.toUri()
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.Discover, navigator.last)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShowAppInfo() {
|
||||
val packageName = "org.fdroid.fdroid"
|
||||
val i = Intent().apply {
|
||||
action = ACTION_SHOW_APP_INFO
|
||||
putExtra(EXTRA_PACKAGE_NAME, packageName)
|
||||
}
|
||||
@Test
|
||||
fun testBrowserUris() {
|
||||
val packageName = "org.fdroid.fdroid"
|
||||
listOf(
|
||||
"https://f-droid.org/packages/$packageName/",
|
||||
"https://f-droid.org/de/packages/$packageName/",
|
||||
"https://f-droid.org/en-US/packages/$packageName",
|
||||
"https://cloudflare.f-droid.org/zh_Hans/packages/$packageName",
|
||||
"market://details?id=$packageName",
|
||||
)
|
||||
.forEach { url ->
|
||||
val i =
|
||||
Intent().apply {
|
||||
action = ACTION_VIEW
|
||||
addCategory(CATEGORY_BROWSABLE)
|
||||
data = url.toUri()
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.AppDetails(packageName), navigator.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShowAppInfoMalformed() {
|
||||
val packageName = "fdroid') DROP TABLE apps; --"
|
||||
val i = Intent().apply {
|
||||
action = ACTION_SHOW_APP_INFO
|
||||
putExtra(EXTRA_PACKAGE_NAME, packageName)
|
||||
}
|
||||
@Test
|
||||
fun testMalformedBrowserUris() {
|
||||
val packageName = "fdroid') DROP TABLE apps; --"
|
||||
listOf(
|
||||
"https://f-droid.org/packages/$packageName/",
|
||||
"https://f-droid.org/de/packages/$packageName/",
|
||||
"https://f-droid.org/en-US/packages/$packageName",
|
||||
"https://cloudflare.f-droid.org/zh_Hans/packages/$packageName",
|
||||
"market://details?id=$packageName",
|
||||
)
|
||||
.forEach { url ->
|
||||
val i =
|
||||
Intent().apply {
|
||||
action = ACTION_VIEW
|
||||
addCategory(CATEGORY_BROWSABLE)
|
||||
data = url.toUri()
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.Discover, navigator.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRepoUris() {
|
||||
listOf(
|
||||
"fdroidrepos://example.org/repo",
|
||||
"FDROIDREPOS://example.org/repo",
|
||||
"https://fdroid.link/#repo=https://f-droid.org/repo",
|
||||
"https://fdroid.link/#foo/bar",
|
||||
).forEach { uri ->
|
||||
val i = Intent(ACTION_VIEW).apply {
|
||||
data = uri.toUri()
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.AddRepo(uri), navigator.last)
|
||||
}
|
||||
}
|
||||
@Test
|
||||
fun testShowAppInfo() {
|
||||
val packageName = "org.fdroid.fdroid"
|
||||
val i =
|
||||
Intent().apply {
|
||||
action = ACTION_SHOW_APP_INFO
|
||||
putExtra(EXTRA_PACKAGE_NAME, packageName)
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.AppDetails(packageName), navigator.last)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testShowAppInfoMalformed() {
|
||||
val packageName = "fdroid') DROP TABLE apps; --"
|
||||
val i =
|
||||
Intent().apply {
|
||||
action = ACTION_SHOW_APP_INFO
|
||||
putExtra(EXTRA_PACKAGE_NAME, packageName)
|
||||
}
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.Discover, navigator.last)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRepoUris() {
|
||||
listOf(
|
||||
"fdroidrepos://example.org/repo",
|
||||
"FDROIDREPOS://example.org/repo",
|
||||
"https://fdroid.link/#repo=https://f-droid.org/repo",
|
||||
"https://fdroid.link/#foo/bar",
|
||||
)
|
||||
.forEach { uri ->
|
||||
val i = Intent(ACTION_VIEW).apply { data = uri.toUri() }
|
||||
intentRouter.accept(i)
|
||||
assertEquals(NavigationKey.AddRepo(uri), navigator.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user