-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add directories initialiser and tests (#132)
* Add directories initialiser and tests * Update documentation * Update api
- Loading branch information
Showing
23 changed files
with
565 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
<application> | ||
<provider | ||
android:name="androidx.startup.InitializationProvider" | ||
android:authorities="${applicationId}.androidx-startup" | ||
android:exported="false" | ||
tools:node="merge"> | ||
<meta-data | ||
android:name="io.github.xxfast.kstore.file.DirectoriesInitializer" | ||
android:value="androidx.startup" /> | ||
</provider> | ||
</application> | ||
</manifest> |
26 changes: 26 additions & 0 deletions
26
kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/AndroidDirectoryProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.xxfast.kstore.file | ||
|
||
import android.content.Context | ||
import io.github.xxfast.kstore.utils.ExperimentalKStoreApi | ||
import kotlinx.io.files.Path | ||
|
||
/** | ||
* When no context is available, this will point to root directory. | ||
*/ | ||
@ExperimentalKStoreApi | ||
internal var _directories: DirectoryProvider = RootDirectoryProvider | ||
|
||
@ExperimentalKStoreApi | ||
public actual val DefaultDirectories: DirectoryProvider get() = _directories | ||
|
||
@ExperimentalKStoreApi | ||
internal class AndroidDirectoryProvider(context: Context) : DirectoryProvider { | ||
override val files: Path = Path(context.filesDir.path) | ||
override val caches: Path = Path(context.cacheDir.path) | ||
} | ||
|
||
@ExperimentalKStoreApi | ||
internal object RootDirectoryProvider: DirectoryProvider { | ||
override val files: Path = Path("/") | ||
override val caches: Path = Path("/") | ||
} |
13 changes: 13 additions & 0 deletions
13
kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/DirectoriesInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.xxfast.kstore.file | ||
|
||
import android.content.Context | ||
import androidx.startup.Initializer | ||
import io.github.xxfast.kstore.utils.ExperimentalKStoreApi | ||
|
||
@ExperimentalKStoreApi | ||
public class DirectoriesInitializer: Initializer<DirectoryProvider> { | ||
override fun create(context: Context): DirectoryProvider = AndroidDirectoryProvider(context) | ||
.also { _directories = it } | ||
|
||
override fun dependencies(): List<Class<out Initializer<*>>> = emptyList() | ||
} |
46 changes: 46 additions & 0 deletions
46
kstore-file/src/androidUnitTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.xxfast.kstore.file | ||
|
||
import android.content.Context | ||
import androidx.startup.AppInitializer | ||
import io.github.xxfast.kstore.utils.ExperimentalKStoreApi | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import java.io.File | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
|
||
@OptIn(ExperimentalKStoreApi::class) | ||
class DirectoryProviders { | ||
|
||
@AfterTest | ||
fun cleanUp() { | ||
_directories = RootDirectoryProvider | ||
} | ||
|
||
@Test | ||
fun testRootDirectoryWhenNotInitialised() { | ||
val provider: DirectoryProvider = DefaultDirectories | ||
val files: String = provider.files.toString() | ||
val caches: String = provider.caches.toString() | ||
assert(files == "/") { "$files doesn't match expected" } | ||
assert(caches == "/") { "$caches doesn't match expected" } | ||
} | ||
|
||
@Test | ||
fun testRootDirectoryWhenInitialised() { | ||
val context: Context = mockk() | ||
|
||
every { context.applicationContext } returns context | ||
every { context.filesDir } returns File("files") | ||
every { context.cacheDir } returns File("caches") | ||
|
||
AppInitializer.getInstance(context) | ||
.initializeComponent(DirectoriesInitializer::class.java) | ||
|
||
val provider: DirectoryProvider = DefaultDirectories | ||
val files: String = provider.files.toString() | ||
val caches: String = provider.caches.toString() | ||
assert(files == "files") { "$files doesn't match expected" } | ||
assert(caches == "caches") { "$caches doesn't match expected" } | ||
} | ||
} |
Oops, something went wrong.