Skip to content

Commit

Permalink
TestDispatcherProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
massivemadness committed Oct 22, 2023
1 parent d5da943 commit 9e4a00b
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* limitations under the License.
*/

package com.blacksquircle.ui.feature.changelog
package com.blacksquircle.ui.core.tests

import org.junit.Assert.*
import org.junit.Test
import com.blacksquircle.ui.core.provider.coroutine.DispatcherProvider
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

class ExampleUnitTest {

@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
class TestDispatcherProvider : DispatcherProvider {
override fun io(): CoroutineDispatcher = Dispatchers.Unconfined
override fun computation(): CoroutineDispatcher = Dispatchers.Unconfined
override fun mainThread(): CoroutineDispatcher = Dispatchers.Unconfined
}
7 changes: 7 additions & 0 deletions common-ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ android {
defaultConfig {
minSdk = BuildConst.MIN_SDK
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
viewBinding = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.changelog

import android.content.Context
import android.content.res.Resources
import com.blacksquircle.ui.core.tests.TestDispatcherProvider
import com.blacksquircle.ui.feature.changelog.data.repository.ChangelogRepositoryImpl
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.junit.Test
import java.io.InputStream

class ChangelogRepositoryTests {

@Test
fun `When loading changelog Then read data from resource file`() = runTest {
// Given
val resources = mockk<Resources>()
val context = mockk<Context>()
every { context.resources } returns resources
every { resources.openRawResource(any()) } returns InputStream.nullInputStream()

val repository = ChangelogRepositoryImpl(
dispatcherProvider = TestDispatcherProvider(),
context = context
)

// When
repository.loadChangelog()

// Then
verify(exactly = 1) { resources.openRawResource(R.raw.changelog) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.changelog

import com.blacksquircle.ui.feature.changelog.data.converter.ReleaseConverter
import junit.framework.TestCase.assertEquals
import org.junit.Test

class ReleaseConverterTest {

@Test
fun `When converting changelog Then check list size`() {
// Given
val testData = """
<b>v2018.1.2, 11 Feb. 2018</b><br>
• <b>Added:</b> Line №1<br>
• <b>Fixed:</b> Line №2<br>
• <b>Fixed:</b> Line №3<br>
• Line №4<br>
<br>
<b>v2018.1.1, 28 Jan. 2018</b><br>
• <b>Added:</b> Line №1<br>
• <b>Added:</b> Line №2<br>
• Line №3<br>
<br>
<b>v2018.1.0, 23 Jan. 2018</b><br>
• Line №1<br>
<br>
""".trimIndent()

// When
val releaseList = ReleaseConverter.toReleaseModels(testData)

// Then
assertEquals(3, releaseList.size)
}

@Test
fun `When converting changelog Then verify release info`() {
// Given
val testData = """
<b>v2018.1.2, 11 Feb. 2018</b><br>
• <b>Added:</b> Line №1<br>
• <b>Fixed:</b> Line №2<br>
• <b>Fixed:</b> Line №3<br>
• Line №4<br>
<br>
""".trimIndent()

// When
val releaseModel = ReleaseConverter.toReleaseModels(testData).first()

// Then
assertEquals("v2018.1.2", releaseModel.versionName)
assertEquals("11 Feb. 2018", releaseModel.releaseDate)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.settings

import android.content.Context
import com.blacksquircle.ui.core.storage.keyvalue.SettingsManager
import com.blacksquircle.ui.feature.settings.data.repository.SettingsRepositoryImpl
import com.blacksquircle.ui.feature.settings.domain.model.KeyModel
import io.mockk.every
import io.mockk.mockk
import junit.framework.TestCase.assertEquals
import org.junit.Test

class SettingsRepositoryTests {

@Test
fun `When loading keyboard preset Then return keys list containing a tab`() {
// Given
val repository = SettingsRepositoryImpl(
settingsManager = mockk<SettingsManager>().apply {
every { keyboardPreset } returns "ABC"
},
context = mockk<Context>().apply {
every { getString(any()) } returns "Tab"
},
)

// When
val actual = repository.keyboardPreset()

// Then
val expected = listOf(
KeyModel(display = "Tab", value = '\t'),
KeyModel(display = "A", value = 'A'),
KeyModel(display = "B", value = 'B'),
KeyModel(display = "C", value = 'C'),
)
assertEquals(expected, actual)
}
}

0 comments on commit 9e4a00b

Please sign in to comment.