From 1bea4c136f7b784a989d9667895d16e2e2b1ad12 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 17:44:29 +0200 Subject: [PATCH 01/85] Introduce JUnit5 --- app/build.gradle | 4 +++- .../ninetofive/assignment_tests/ExampleUnitTest.kt | 11 +++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c897068..14f5f56 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,6 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' - testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' @@ -58,4 +57,7 @@ dependencies { implementation "com.jakewharton.rxbinding2:rxbinding:$rxbindingVersion" implementation "com.jakewharton.rxbinding2:rxbinding-support-v4:$rxbindingVersion" implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:$rxbindingVersion" + + testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2" } \ No newline at end of file diff --git a/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt index 9487bf0..3be2eb9 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt @@ -1,15 +1,10 @@ package software.ninetofive.assignment_tests -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test -import org.junit.Assert.* - -/** - * Example local unit test, which will execute on the development machine (host). - * - * See [testing documentation](http://d.android.com/tools/testing). - */ class ExampleUnitTest { + @Test fun addition_isCorrect() { assertEquals(4, 2 + 2) From 4dacca436aaf4d15539fa07e70eca0b7ee442f04 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 17:55:09 +0200 Subject: [PATCH 02/85] Allow instantiations with invalid context (temporarly) --- .../assignment_tests/utils/AppPreferences.kt | 7 ++++--- .../assignment_tests/utils/AppPreferencesTest.kt | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index 8095ef8..443fe25 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -8,13 +8,13 @@ import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor import software.ninetofive.assignment_tests.main.ViewingOption import software.ninetofive.assignment_tests.main.SelectedScreen +import java.lang.IllegalStateException import javax.inject.Inject import javax.inject.Singleton @Singleton class AppPreferences @Inject constructor( - @ApplicationContext context: Context - + @ApplicationContext context: Context? ) { companion object { private const val PREFERENCES_NAME = "app_prefs" @@ -23,7 +23,8 @@ class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val preferences: SharedPreferences = context.getSharedPreferences(PREFERENCES_NAME, 0) + private val preferences: SharedPreferences = context?.getSharedPreferences(PREFERENCES_NAME, 0) + ?: throw IllegalStateException("Valid context is required!") private val viewingOptionChangedProcessor: FlowableProcessor = PublishProcessor.create() private val showValidDotProcessor: FlowableProcessor = BehaviorProcessor.create() diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt new file mode 100644 index 0000000..e695c83 --- /dev/null +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -0,0 +1,15 @@ +package software.ninetofive.assignment_tests.utils + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import software.ninetofive.assignment_tests.main.SelectedScreen + +class AppPreferencesTest { + + @Test + fun instantiate() { + val appPreferences = AppPreferences(null) + + assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) + } +} \ No newline at end of file From f83e74bf0e6c6390ea9f46956178458b1673d47a Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 17:57:15 +0200 Subject: [PATCH 03/85] Introduce a function to be leveraged as a seam --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index 443fe25..cb9f91d 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -23,8 +23,12 @@ class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val preferences: SharedPreferences = context?.getSharedPreferences(PREFERENCES_NAME, 0) + private val preferences: SharedPreferences = getPreferences(context) ?: throw IllegalStateException("Valid context is required!") + + private fun getPreferences(context: Context?) = + context?.getSharedPreferences(PREFERENCES_NAME, 0) + private val viewingOptionChangedProcessor: FlowableProcessor = PublishProcessor.create() private val showValidDotProcessor: FlowableProcessor = BehaviorProcessor.create() From 0f1c12e1ba3017949bf05086d472e79a69d6d863 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:00:12 +0200 Subject: [PATCH 04/85] Introduce a seam --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index cb9f91d..3477fb7 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -13,7 +13,7 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton -class AppPreferences @Inject constructor( +open class AppPreferences @Inject constructor( @ApplicationContext context: Context? ) { companion object { @@ -26,7 +26,7 @@ class AppPreferences @Inject constructor( private val preferences: SharedPreferences = getPreferences(context) ?: throw IllegalStateException("Valid context is required!") - private fun getPreferences(context: Context?) = + protected open fun getPreferences(context: Context?) = context?.getSharedPreferences(PREFERENCES_NAME, 0) private val viewingOptionChangedProcessor: FlowableProcessor = PublishProcessor.create() From 5b53aa34d1d069968d058f891f81a535af3d648f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:06:24 +0200 Subject: [PATCH 05/85] Leverage the seam to allow test-double substitution --- .../assignment_tests/utils/AppPreferencesTest.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index e695c83..c5f2034 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -1,5 +1,7 @@ package software.ninetofive.assignment_tests.utils +import android.content.Context +import android.content.SharedPreferences import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen @@ -8,8 +10,15 @@ class AppPreferencesTest { @Test fun instantiate() { - val appPreferences = AppPreferences(null) + val appPreferences = TestableAppPreferences() assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) } + + class TestableAppPreferences : AppPreferences(null) { + + override fun getPreferences(context: Context?): SharedPreferences? { + return super.getPreferences(context) + } + } } \ No newline at end of file From e3a5d45a9455263eecec846ecabba329db371b86 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:07:25 +0200 Subject: [PATCH 06/85] Introduce test-double to substitute SharedPreferences --- .../utils/AppPreferencesTest.kt | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index c5f2034..1551eba 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -18,7 +18,61 @@ class AppPreferencesTest { class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { - return super.getPreferences(context) + return InMemorySharedPreferences() + } + } + + private class InMemorySharedPreferences : SharedPreferences { + + override fun getAll(): MutableMap { + TODO("Not yet implemented") + } + + override fun getString(key: String?, defaultValue: String?): String? { + TODO("Not yet implemented") + } + + override fun getStringSet( + key: String?, + defaultValue: MutableSet? + ): MutableSet? { + TODO("Not yet implemented") + } + + override fun getInt(key: String?, defaultValue: Int): Int { + TODO("Not yet implemented") + } + + override fun getLong(key: String?, defaultValue: Long): Long { + TODO("Not yet implemented") + } + + override fun getFloat(key: String?, defaultValue: Float): Float { + TODO("Not yet implemented") + } + + override fun getBoolean(key: String?, defaultValue: Boolean): Boolean { + TODO("Not yet implemented") + } + + override fun contains(key: String?): Boolean { + TODO("Not yet implemented") + } + + override fun edit(): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun registerOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener? + ) { + TODO("Not yet implemented") + } + + override fun unregisterOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener? + ) { + TODO("Not yet implemented") } } } \ No newline at end of file From c02c5293302d477d51b00c7c3829dba83caaf89b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:08:07 +0200 Subject: [PATCH 07/85] Instantiation test passing --- .../ninetofive/assignment_tests/utils/AppPreferencesTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 1551eba..2a81c24 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -29,7 +29,7 @@ class AppPreferencesTest { } override fun getString(key: String?, defaultValue: String?): String? { - TODO("Not yet implemented") + return "" } override fun getStringSet( From 37542329823d05bbfc2fef1f8f211749750d622e Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:09:05 +0200 Subject: [PATCH 08/85] Rename the test to reflect the behavior it exercises --- .../ninetofive/assignment_tests/utils/AppPreferencesTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 2a81c24..33922bd 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -9,7 +9,7 @@ import software.ninetofive.assignment_tests.main.SelectedScreen class AppPreferencesTest { @Test - fun instantiate() { + fun defaultSelectedScreen() { val appPreferences = TestableAppPreferences() assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) From 88cddd10f47d2c7f50c95b95dd0abdab6054631f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:19:18 +0200 Subject: [PATCH 09/85] Editing not working due to missing implementation in test-double --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 33922bd..0f9f6dc 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -15,6 +15,15 @@ class AppPreferencesTest { assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) } + @Test + fun selectedScreenA() { + val appPreferences = TestableAppPreferences() + + appPreferences.setStartScreen(SelectedScreen.SCREEN_A) + + assertEquals(SelectedScreen.SCREEN_A, appPreferences.getStartScreen()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 83f5f73b89d6953753907d837bb8505c66e030aa Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:23:34 +0200 Subject: [PATCH 10/85] Allow preferences editing --- .../utils/AppPreferencesTest.kt | 56 +++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 0f9f6dc..eb4377c 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -33,12 +33,15 @@ class AppPreferencesTest { private class InMemorySharedPreferences : SharedPreferences { + private var selectedScreen: String? = "" + private val editor = Editor() + override fun getAll(): MutableMap { TODO("Not yet implemented") } override fun getString(key: String?, defaultValue: String?): String? { - return "" + return selectedScreen } override fun getStringSet( @@ -68,9 +71,7 @@ class AppPreferencesTest { TODO("Not yet implemented") } - override fun edit(): SharedPreferences.Editor { - TODO("Not yet implemented") - } + override fun edit(): SharedPreferences.Editor = editor override fun registerOnSharedPreferenceChangeListener( listener: SharedPreferences.OnSharedPreferenceChangeListener? @@ -83,5 +84,52 @@ class AppPreferencesTest { ) { TODO("Not yet implemented") } + + inner class Editor: SharedPreferences.Editor { + + override fun putString(key: String?, value: String?): SharedPreferences.Editor { + selectedScreen = value + return this + } + + override fun putStringSet( + key: String?, + value: MutableSet? + ): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putInt(key: String?, value: Int): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putLong(key: String?, value: Long): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putFloat(key: String?, value: Float): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putBoolean(key: String?, value: Boolean): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun remove(key: String?): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun clear(): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun commit(): Boolean { + TODO("Not yet implemented") + } + + override fun apply() { + + } + } } } \ No newline at end of file From 547759159d6d2fd1b55e686a88fc0109b810dd96 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:31:08 +0200 Subject: [PATCH 11/85] Introduce proper preferences editor and storing mechanism in memory --- .../utils/AppPreferencesTest.kt | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index eb4377c..cc62a1e 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -33,15 +33,16 @@ class AppPreferencesTest { private class InMemorySharedPreferences : SharedPreferences { - private var selectedScreen: String? = "" - private val editor = Editor() + private val valuesMap = HashMap() + private val uncommittedValuesMap = HashMap() + private val editor = Editor(valuesMap, uncommittedValuesMap) override fun getAll(): MutableMap { TODO("Not yet implemented") } override fun getString(key: String?, defaultValue: String?): String? { - return selectedScreen + return valuesMap.getOrDefault(key, defaultValue) as? String? } override fun getStringSet( @@ -85,15 +86,18 @@ class AppPreferencesTest { TODO("Not yet implemented") } - inner class Editor: SharedPreferences.Editor { + inner class Editor( + private val valuesMap: MutableMap, + private val uncommittedValuesMap: MutableMap + ) : SharedPreferences.Editor { - override fun putString(key: String?, value: String?): SharedPreferences.Editor { - selectedScreen = value + override fun putString(key: String, value: String?): SharedPreferences.Editor { + uncommittedValuesMap[key] = value return this } override fun putStringSet( - key: String?, + key: String, value: MutableSet? ): SharedPreferences.Editor { TODO("Not yet implemented") @@ -115,20 +119,27 @@ class AppPreferencesTest { TODO("Not yet implemented") } - override fun remove(key: String?): SharedPreferences.Editor { - TODO("Not yet implemented") + override fun remove(key: String): SharedPreferences.Editor { + uncommittedValuesMap.remove(key) + return this } override fun clear(): SharedPreferences.Editor { - TODO("Not yet implemented") + uncommittedValuesMap.clear() + valuesMap.clear() + return this } override fun commit(): Boolean { - TODO("Not yet implemented") + uncommittedValuesMap.forEach { + valuesMap[it.key] = it.value + } + uncommittedValuesMap.clear() + return true } override fun apply() { - + commit() } } } From 44a4e62129b98a7e5c0ee2fa14940284476737c0 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:35:10 +0200 Subject: [PATCH 12/85] Cover the case for the B screen --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index cc62a1e..94e12af 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -24,6 +24,15 @@ class AppPreferencesTest { assertEquals(SelectedScreen.SCREEN_A, appPreferences.getStartScreen()) } + @Test + fun selectedScreenB() { + val appPreferences = TestableAppPreferences() + + appPreferences.setStartScreen(SelectedScreen.SCREEN_B) + + assertEquals(SelectedScreen.SCREEN_B, appPreferences.getStartScreen()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 4615f33bf5845072f9db031bcc12a401dadf547e Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:37:15 +0200 Subject: [PATCH 13/85] Cover exlicit selection of C screen --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 94e12af..52c1116 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -33,6 +33,15 @@ class AppPreferencesTest { assertEquals(SelectedScreen.SCREEN_B, appPreferences.getStartScreen()) } + @Test + fun selectedScreenC() { + val appPreferences = TestableAppPreferences() + + appPreferences.setStartScreen(SelectedScreen.SCREEN_C) + + assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 87eca112e973f00087501fead7b76153ff99e908 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:51:13 +0200 Subject: [PATCH 14/85] Default viewing option --- .../assignment_tests/utils/AppPreferencesTest.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 52c1116..38a8e47 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -5,6 +5,7 @@ import android.content.SharedPreferences import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen +import software.ninetofive.assignment_tests.main.ViewingOption class AppPreferencesTest { @@ -42,6 +43,13 @@ class AppPreferencesTest { assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) } + @Test + fun defaultViewingOption() { + val appPreferences = TestableAppPreferences() + + assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 8080da2ad16ff8de73bfd7cfd3eb18c7be9b6650 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:51:48 +0200 Subject: [PATCH 15/85] Cover ShowName viewing option --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 38a8e47..67f85a9 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -50,6 +50,15 @@ class AppPreferencesTest { assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) } + @Test + fun selectedShowName() { + val appPreferences = TestableAppPreferences() + + appPreferences.setViewingOption(ViewingOption.SHOW_NAME) + + assertEquals(ViewingOption.SHOW_NAME, appPreferences.getViewingOption()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From f55593b51e9d31b19cb02e00baf03b595fe13598 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:55:40 +0200 Subject: [PATCH 16/85] Cover Date viewing option --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 67f85a9..9320eef 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -59,6 +59,15 @@ class AppPreferencesTest { assertEquals(ViewingOption.SHOW_NAME, appPreferences.getViewingOption()) } + @Test + fun selectedDate() { + val appPreferences = TestableAppPreferences() + + appPreferences.setViewingOption(ViewingOption.DATE) + + assertEquals(ViewingOption.DATE, appPreferences.getViewingOption()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 8440e0e3bdee3989318ce314f26472760ba67267 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:56:04 +0200 Subject: [PATCH 17/85] Cover expicit selection of nothing view option --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 9320eef..712abcd 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -68,6 +68,15 @@ class AppPreferencesTest { assertEquals(ViewingOption.DATE, appPreferences.getViewingOption()) } + @Test + fun selectedNothing() { + val appPreferences = TestableAppPreferences() + + appPreferences.setViewingOption(ViewingOption.NOTHING) + + assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { From 79cdd1d60516bd78365d1d36fadd13ed1421459a Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 18:58:19 +0200 Subject: [PATCH 18/85] Default show valid dot --- .../assignment_tests/utils/AppPreferencesTest.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 712abcd..41ff4d7 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -77,6 +77,13 @@ class AppPreferencesTest { assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) } + @Test + fun defaultShowValidDot() { + val appPreferences = TestableAppPreferences() + + assertEquals(false, appPreferences.shouldShowValidDot()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { @@ -118,7 +125,7 @@ class AppPreferencesTest { } override fun getBoolean(key: String?, defaultValue: Boolean): Boolean { - TODO("Not yet implemented") + return valuesMap.getOrDefault(key, defaultValue) as Boolean } override fun contains(key: String?): Boolean { From ed1c26fc72d04a1b39d8f4cfef82b8201aae9641 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:00:29 +0200 Subject: [PATCH 19/85] Cover setting show valid dot (functionallity covered) --- .../assignment_tests/utils/AppPreferencesTest.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 41ff4d7..aa33cf0 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -84,6 +84,15 @@ class AppPreferencesTest { assertEquals(false, appPreferences.shouldShowValidDot()) } + @Test + fun toggleShowValidDot() { + val appPreferences = TestableAppPreferences() + + appPreferences.setShowValidDot(true) + + assertEquals(true, appPreferences.shouldShowValidDot()) + } + class TestableAppPreferences : AppPreferences(null) { override fun getPreferences(context: Context?): SharedPreferences? { @@ -175,8 +184,9 @@ class AppPreferencesTest { TODO("Not yet implemented") } - override fun putBoolean(key: String?, value: Boolean): SharedPreferences.Editor { - TODO("Not yet implemented") + override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor { + uncommittedValuesMap[key] = value + return this } override fun remove(key: String): SharedPreferences.Editor { From 6aed8baf5ca2d88ea25aab4c53167c84a7018b2c Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:14:24 +0200 Subject: [PATCH 20/85] Push the SharedPreferences property in the constructor --- .../assignment_tests/utils/AppPreferences.kt | 5 ++-- .../utils/SharedPreferencesModule.kt | 21 +++++++++++++++ .../utils/AppPreferencesTest.kt | 26 ++++++++++--------- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/software/ninetofive/assignment_tests/utils/SharedPreferencesModule.kt diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index 3477fb7..df7e782 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -14,7 +14,8 @@ import javax.inject.Singleton @Singleton open class AppPreferences @Inject constructor( - @ApplicationContext context: Context? + @ApplicationContext context: Context?, + private val sharedPreferences: SharedPreferences? ) { companion object { private const val PREFERENCES_NAME = "app_prefs" @@ -23,7 +24,7 @@ open class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val preferences: SharedPreferences = getPreferences(context) + private val preferences: SharedPreferences = sharedPreferences ?: getPreferences(context) ?: throw IllegalStateException("Valid context is required!") protected open fun getPreferences(context: Context?) = diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/SharedPreferencesModule.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/SharedPreferencesModule.kt new file mode 100644 index 0000000..c0e610a --- /dev/null +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/SharedPreferencesModule.kt @@ -0,0 +1,21 @@ +package software.ninetofive.assignment_tests.utils + +import android.content.Context +import android.content.SharedPreferences +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent + +@Module +@InstallIn(SingletonComponent::class) +object SharedPreferencesModule { + + @Provides + fun provideSharedPreferences( + @ApplicationContext context: Context + ): SharedPreferences { + return context.getSharedPreferences("app_prefs", 0) + } +} \ No newline at end of file diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index aa33cf0..8c21609 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -11,14 +11,14 @@ class AppPreferencesTest { @Test fun defaultSelectedScreen() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) } @Test fun selectedScreenA() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_A) @@ -27,7 +27,7 @@ class AppPreferencesTest { @Test fun selectedScreenB() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_B) @@ -36,7 +36,7 @@ class AppPreferencesTest { @Test fun selectedScreenC() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_C) @@ -45,14 +45,14 @@ class AppPreferencesTest { @Test fun defaultViewingOption() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) } @Test fun selectedShowName() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.SHOW_NAME) @@ -61,7 +61,7 @@ class AppPreferencesTest { @Test fun selectedDate() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.DATE) @@ -70,7 +70,7 @@ class AppPreferencesTest { @Test fun selectedNothing() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.NOTHING) @@ -79,24 +79,26 @@ class AppPreferencesTest { @Test fun defaultShowValidDot() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) assertEquals(false, appPreferences.shouldShowValidDot()) } @Test fun toggleShowValidDot() { - val appPreferences = TestableAppPreferences() + val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) appPreferences.setShowValidDot(true) assertEquals(true, appPreferences.shouldShowValidDot()) } - class TestableAppPreferences : AppPreferences(null) { + private class TestableAppPreferences( + val sharedPreferences: InMemorySharedPreferences + ) : AppPreferences(null, sharedPreferences) { override fun getPreferences(context: Context?): SharedPreferences? { - return InMemorySharedPreferences() + return sharedPreferences } } From b7cc37f00a6f885f23aba2a374cdd10269f16df9 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:15:44 +0200 Subject: [PATCH 21/85] Make the property non-nullable --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index df7e782..f630c8e 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -15,7 +15,7 @@ import javax.inject.Singleton @Singleton open class AppPreferences @Inject constructor( @ApplicationContext context: Context?, - private val sharedPreferences: SharedPreferences? + private val sharedPreferences: SharedPreferences ) { companion object { private const val PREFERENCES_NAME = "app_prefs" From 3442e1f912ebdbfdf9fcc1be4b0c6e28f37e1cba Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:16:08 +0200 Subject: [PATCH 22/85] Remove death code --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index f630c8e..735805d 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -24,8 +24,7 @@ open class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val preferences: SharedPreferences = sharedPreferences ?: getPreferences(context) - ?: throw IllegalStateException("Valid context is required!") + private val preferences: SharedPreferences = sharedPreferences protected open fun getPreferences(context: Context?) = context?.getSharedPreferences(PREFERENCES_NAME, 0) From 7d66e1d74909b85349b54a8d813b90d6be97e0bc Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:16:54 +0200 Subject: [PATCH 23/85] Remove unused constructor property --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 3 --- .../ninetofive/assignment_tests/utils/AppPreferencesTest.kt | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index 735805d..bca9926 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -2,19 +2,16 @@ package software.ninetofive.assignment_tests.utils import android.content.Context import android.content.SharedPreferences -import dagger.hilt.android.qualifiers.ApplicationContext import io.reactivex.processors.BehaviorProcessor import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor import software.ninetofive.assignment_tests.main.ViewingOption import software.ninetofive.assignment_tests.main.SelectedScreen -import java.lang.IllegalStateException import javax.inject.Inject import javax.inject.Singleton @Singleton open class AppPreferences @Inject constructor( - @ApplicationContext context: Context?, private val sharedPreferences: SharedPreferences ) { companion object { diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 8c21609..16bde91 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -95,7 +95,7 @@ class AppPreferencesTest { private class TestableAppPreferences( val sharedPreferences: InMemorySharedPreferences - ) : AppPreferences(null, sharedPreferences) { + ) : AppPreferences(sharedPreferences) { override fun getPreferences(context: Context?): SharedPreferences? { return sharedPreferences From 53457c398d6851886594ac45f7df0cc60173cca4 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:17:59 +0200 Subject: [PATCH 24/85] Inline propery --- .../assignment_tests/utils/AppPreferences.kt | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index bca9926..e7062ef 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -21,8 +21,6 @@ open class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val preferences: SharedPreferences = sharedPreferences - protected open fun getPreferences(context: Context?) = context?.getSharedPreferences(PREFERENCES_NAME, 0) @@ -30,13 +28,13 @@ open class AppPreferences @Inject constructor( private val showValidDotProcessor: FlowableProcessor = BehaviorProcessor.create() fun setStartScreen(selectedScreen: SelectedScreen) { - preferences.edit() + sharedPreferences.edit() .putString(KEY_SELECTED_SCREEN, selectedScreen.toString()) .apply() } fun getStartScreen(): SelectedScreen = - when (preferences.getString(KEY_SELECTED_SCREEN, SelectedScreen.SCREEN_C.toString())) { + when (sharedPreferences.getString(KEY_SELECTED_SCREEN, SelectedScreen.SCREEN_C.toString())) { SelectedScreen.SCREEN_A.toString() -> SelectedScreen.SCREEN_A SelectedScreen.SCREEN_C.toString() -> SelectedScreen.SCREEN_C SelectedScreen.SCREEN_B.toString() -> SelectedScreen.SCREEN_B @@ -44,14 +42,14 @@ open class AppPreferences @Inject constructor( } fun setViewingOption(viewingOption: ViewingOption) { - preferences.edit() + sharedPreferences.edit() .putString(KEY_VIEWING_OPTION, viewingOption.toString()) .apply() viewingOptionChangedProcessor.onNext(Any()) } fun getViewingOption(): ViewingOption = - when (preferences.getString(KEY_VIEWING_OPTION, ViewingOption.NOTHING.toString())) { + when (sharedPreferences.getString(KEY_VIEWING_OPTION, ViewingOption.NOTHING.toString())) { ViewingOption.SHOW_NAME.toString() -> ViewingOption.SHOW_NAME ViewingOption.DATE.toString() -> ViewingOption.DATE ViewingOption.NOTHING.toString() -> ViewingOption.NOTHING @@ -60,12 +58,11 @@ open class AppPreferences @Inject constructor( fun setShowValidDot(shouldShowValidDot: Boolean) { - preferences.edit() + sharedPreferences.edit() .putBoolean(KEY_SHOW_VALID_DOT, shouldShowValidDot) .apply() showValidDotProcessor.onNext(shouldShowValidDot) } - fun shouldShowValidDot() = preferences.getBoolean(KEY_SHOW_VALID_DOT, false) - + fun shouldShowValidDot() = sharedPreferences.getBoolean(KEY_SHOW_VALID_DOT, false) } From 061d0643bb4994056b814f0a87654937ce89fb73 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:19:54 +0200 Subject: [PATCH 25/85] Use AppPreferences in the tests directly --- .../utils/AppPreferencesTest.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 16bde91..68cd692 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -11,14 +11,14 @@ class AppPreferencesTest { @Test fun defaultSelectedScreen() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) assertEquals(SelectedScreen.SCREEN_C, appPreferences.getStartScreen()) } @Test fun selectedScreenA() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_A) @@ -27,7 +27,7 @@ class AppPreferencesTest { @Test fun selectedScreenB() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_B) @@ -36,7 +36,7 @@ class AppPreferencesTest { @Test fun selectedScreenC() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setStartScreen(SelectedScreen.SCREEN_C) @@ -45,14 +45,14 @@ class AppPreferencesTest { @Test fun defaultViewingOption() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) assertEquals(ViewingOption.NOTHING, appPreferences.getViewingOption()) } @Test fun selectedShowName() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.SHOW_NAME) @@ -61,7 +61,7 @@ class AppPreferencesTest { @Test fun selectedDate() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.DATE) @@ -70,7 +70,7 @@ class AppPreferencesTest { @Test fun selectedNothing() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setViewingOption(ViewingOption.NOTHING) @@ -79,14 +79,14 @@ class AppPreferencesTest { @Test fun defaultShowValidDot() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) assertEquals(false, appPreferences.shouldShowValidDot()) } @Test fun toggleShowValidDot() { - val appPreferences = TestableAppPreferences(InMemorySharedPreferences()) + val appPreferences = AppPreferences(InMemorySharedPreferences()) appPreferences.setShowValidDot(true) From 0ae30ee9162d94cba86ec988f076238107892b82 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:22:30 +0200 Subject: [PATCH 26/85] Eliminate the seam --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 6 ++---- .../assignment_tests/utils/AppPreferencesTest.kt | 9 --------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index e7062ef..cf0bed1 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -11,9 +11,10 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton -open class AppPreferences @Inject constructor( +class AppPreferences @Inject constructor( private val sharedPreferences: SharedPreferences ) { + companion object { private const val PREFERENCES_NAME = "app_prefs" private const val KEY_SELECTED_SCREEN = "selected_screen" @@ -21,9 +22,6 @@ open class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - protected open fun getPreferences(context: Context?) = - context?.getSharedPreferences(PREFERENCES_NAME, 0) - private val viewingOptionChangedProcessor: FlowableProcessor = PublishProcessor.create() private val showValidDotProcessor: FlowableProcessor = BehaviorProcessor.create() diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 68cd692..8035ac4 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -93,15 +93,6 @@ class AppPreferencesTest { assertEquals(true, appPreferences.shouldShowValidDot()) } - private class TestableAppPreferences( - val sharedPreferences: InMemorySharedPreferences - ) : AppPreferences(sharedPreferences) { - - override fun getPreferences(context: Context?): SharedPreferences? { - return sharedPreferences - } - } - private class InMemorySharedPreferences : SharedPreferences { private val valuesMap = HashMap() From 1ec6ddea45ae120ae6525b776763612abfa76802 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 19:22:54 +0200 Subject: [PATCH 27/85] Clean-up --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index cf0bed1..c06a160 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -1,12 +1,11 @@ package software.ninetofive.assignment_tests.utils -import android.content.Context import android.content.SharedPreferences import io.reactivex.processors.BehaviorProcessor import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor -import software.ninetofive.assignment_tests.main.ViewingOption import software.ninetofive.assignment_tests.main.SelectedScreen +import software.ninetofive.assignment_tests.main.ViewingOption import javax.inject.Inject import javax.inject.Singleton @@ -16,7 +15,6 @@ class AppPreferences @Inject constructor( ) { companion object { - private const val PREFERENCES_NAME = "app_prefs" private const val KEY_SELECTED_SCREEN = "selected_screen" private const val KEY_VIEWING_OPTION = "viewing_option" private const val KEY_SHOW_VALID_DOT = "show_valid_dot" From dc17ff9ebf5fb49c530f5148c38e5c503045acd1 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:23:54 +0200 Subject: [PATCH 28/85] UI testing dependencies setup --- app/build.gradle | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 14f5f56..aca604e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,3 @@ - - plugins { id 'kotlin-android' id 'kotlin-android-extensions' @@ -28,10 +26,12 @@ android { proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + kotlinOptions { jvmTarget = '1.8' } @@ -45,8 +45,6 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // hilt implementation "com.google.dagger:hilt-android:$hilt_version" @@ -58,6 +56,12 @@ dependencies { implementation "com.jakewharton.rxbinding2:rxbinding-support-v4:$rxbindingVersion" implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:$rxbindingVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0" + androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3" + + androidTestImplementation "com.google.dagger:hilt-android-testing:$hilt_version" + kaptAndroidTest "com.google.dagger:hilt-android-compiler:$hilt_version" + testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2" } \ No newline at end of file From 74d9cc4b8a4d2bc25ffa1c2df4a3f2799cc77284 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:25:47 +0200 Subject: [PATCH 29/85] Initial UI test setup --- .../assignment_tests/main/MainScreenTest.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt new file mode 100644 index 0000000..d3a7566 --- /dev/null +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -0,0 +1,24 @@ +package software.ninetofive.assignment_tests.main + +import androidx.test.core.app.ActivityScenario +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withText +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.junit.Test +import org.junit.runner.RunWith +import software.ninetofive.assignment_tests.R + +@RunWith(AndroidJUnit4::class) +class MainScreenTest { + + @Test + fun defaultScreen() { + + ActivityScenario.launch(MainActivity::class.java) + + onView(withText(R.string.choose_start_screen_header)) + .check(matches(isDisplayed())) + } +} \ No newline at end of file From 6c773c3f20a07d87c824b0a279650e7c7d4ce45b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:32:58 +0200 Subject: [PATCH 30/85] Define sharedTest directory --- app/build.gradle | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/build.gradle b/app/build.gradle index aca604e..7577926 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,6 +35,16 @@ android { kotlinOptions { jvmTarget = '1.8' } + + sourceSets { + def sharedTestDirectory = "src/sharedTest/java" + test { + java.srcDir sharedTestDirectory + } + androidTest { + java.srcDir sharedTestDirectory + } + } } ext.rxbindingVersion = '2.1.1' From fec65eff0fcf7c34d4b9839f7e077821aa011ab7 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:37:07 +0200 Subject: [PATCH 31/85] Move test-double inside sharedTest folder for reusability --- .../utils/InMemorySharedPreferences.kt | 117 ++++++++++++++++++ .../utils/AppPreferencesTest.kt | 115 ----------------- 2 files changed, 117 insertions(+), 115 deletions(-) create mode 100644 app/src/sharedTest/java/software/ninetofive/assignment_tests/utils/InMemorySharedPreferences.kt diff --git a/app/src/sharedTest/java/software/ninetofive/assignment_tests/utils/InMemorySharedPreferences.kt b/app/src/sharedTest/java/software/ninetofive/assignment_tests/utils/InMemorySharedPreferences.kt new file mode 100644 index 0000000..d3e8fad --- /dev/null +++ b/app/src/sharedTest/java/software/ninetofive/assignment_tests/utils/InMemorySharedPreferences.kt @@ -0,0 +1,117 @@ +package software.ninetofive.assignment_tests.utils + +import android.content.SharedPreferences + +class InMemorySharedPreferences : SharedPreferences { + + private val valuesMap = HashMap() + private val uncommittedValuesMap = HashMap() + private val editor = Editor(valuesMap, uncommittedValuesMap) + + override fun getAll(): MutableMap { + TODO("Not yet implemented") + } + + override fun getString(key: String?, defaultValue: String?): String? { + return valuesMap.getOrDefault(key, defaultValue) as? String? + } + + override fun getStringSet( + key: String?, + defaultValue: MutableSet? + ): MutableSet? { + TODO("Not yet implemented") + } + + override fun getInt(key: String?, defaultValue: Int): Int { + TODO("Not yet implemented") + } + + override fun getLong(key: String?, defaultValue: Long): Long { + TODO("Not yet implemented") + } + + override fun getFloat(key: String?, defaultValue: Float): Float { + TODO("Not yet implemented") + } + + override fun getBoolean(key: String?, defaultValue: Boolean): Boolean { + return valuesMap.getOrDefault(key, defaultValue) as Boolean + } + + override fun contains(key: String?): Boolean { + TODO("Not yet implemented") + } + + override fun edit(): SharedPreferences.Editor = editor + + override fun registerOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener? + ) { + TODO("Not yet implemented") + } + + override fun unregisterOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener? + ) { + TODO("Not yet implemented") + } + + inner class Editor( + private val valuesMap: MutableMap, + private val uncommittedValuesMap: MutableMap + ) : SharedPreferences.Editor { + + override fun putString(key: String, value: String?): SharedPreferences.Editor { + uncommittedValuesMap[key] = value + return this + } + + override fun putStringSet( + key: String, + value: MutableSet? + ): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putInt(key: String?, value: Int): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putLong(key: String?, value: Long): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putFloat(key: String?, value: Float): SharedPreferences.Editor { + TODO("Not yet implemented") + } + + override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor { + uncommittedValuesMap[key] = value + return this + } + + override fun remove(key: String): SharedPreferences.Editor { + uncommittedValuesMap.remove(key) + return this + } + + override fun clear(): SharedPreferences.Editor { + uncommittedValuesMap.clear() + valuesMap.clear() + return this + } + + override fun commit(): Boolean { + uncommittedValuesMap.forEach { + valuesMap[it.key] = it.value + } + uncommittedValuesMap.clear() + return true + } + + override fun apply() { + commit() + } + } +} \ No newline at end of file diff --git a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt index 8035ac4..74c04fa 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/utils/AppPreferencesTest.kt @@ -1,7 +1,5 @@ package software.ninetofive.assignment_tests.utils -import android.content.Context -import android.content.SharedPreferences import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen @@ -93,117 +91,4 @@ class AppPreferencesTest { assertEquals(true, appPreferences.shouldShowValidDot()) } - private class InMemorySharedPreferences : SharedPreferences { - - private val valuesMap = HashMap() - private val uncommittedValuesMap = HashMap() - private val editor = Editor(valuesMap, uncommittedValuesMap) - - override fun getAll(): MutableMap { - TODO("Not yet implemented") - } - - override fun getString(key: String?, defaultValue: String?): String? { - return valuesMap.getOrDefault(key, defaultValue) as? String? - } - - override fun getStringSet( - key: String?, - defaultValue: MutableSet? - ): MutableSet? { - TODO("Not yet implemented") - } - - override fun getInt(key: String?, defaultValue: Int): Int { - TODO("Not yet implemented") - } - - override fun getLong(key: String?, defaultValue: Long): Long { - TODO("Not yet implemented") - } - - override fun getFloat(key: String?, defaultValue: Float): Float { - TODO("Not yet implemented") - } - - override fun getBoolean(key: String?, defaultValue: Boolean): Boolean { - return valuesMap.getOrDefault(key, defaultValue) as Boolean - } - - override fun contains(key: String?): Boolean { - TODO("Not yet implemented") - } - - override fun edit(): SharedPreferences.Editor = editor - - override fun registerOnSharedPreferenceChangeListener( - listener: SharedPreferences.OnSharedPreferenceChangeListener? - ) { - TODO("Not yet implemented") - } - - override fun unregisterOnSharedPreferenceChangeListener( - listener: SharedPreferences.OnSharedPreferenceChangeListener? - ) { - TODO("Not yet implemented") - } - - inner class Editor( - private val valuesMap: MutableMap, - private val uncommittedValuesMap: MutableMap - ) : SharedPreferences.Editor { - - override fun putString(key: String, value: String?): SharedPreferences.Editor { - uncommittedValuesMap[key] = value - return this - } - - override fun putStringSet( - key: String, - value: MutableSet? - ): SharedPreferences.Editor { - TODO("Not yet implemented") - } - - override fun putInt(key: String?, value: Int): SharedPreferences.Editor { - TODO("Not yet implemented") - } - - override fun putLong(key: String?, value: Long): SharedPreferences.Editor { - TODO("Not yet implemented") - } - - override fun putFloat(key: String?, value: Float): SharedPreferences.Editor { - TODO("Not yet implemented") - } - - override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor { - uncommittedValuesMap[key] = value - return this - } - - override fun remove(key: String): SharedPreferences.Editor { - uncommittedValuesMap.remove(key) - return this - } - - override fun clear(): SharedPreferences.Editor { - uncommittedValuesMap.clear() - valuesMap.clear() - return this - } - - override fun commit(): Boolean { - uncommittedValuesMap.forEach { - valuesMap[it.key] = it.value - } - uncommittedValuesMap.clear() - return true - } - - override fun apply() { - commit() - } - } - } } \ No newline at end of file From a25d30023203c0ab805b1a0fa6a56de9d1cf9583 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:41:26 +0200 Subject: [PATCH 32/85] Define custom test runner for UI tests --- app/build.gradle | 2 +- .../assignment_tests/CustomTestRunner.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 app/src/androidTest/java/software/ninetofive/assignment_tests/CustomTestRunner.kt diff --git a/app/build.gradle b/app/build.gradle index 7577926..3ea4db9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,7 @@ android { versionCode 1 versionName "1.0" - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "software.ninetofive.assignment_tests.CustomTestRunner" } buildTypes { diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/CustomTestRunner.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/CustomTestRunner.kt new file mode 100644 index 0000000..9ea3ba7 --- /dev/null +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/CustomTestRunner.kt @@ -0,0 +1,17 @@ +package software.ninetofive.assignment_tests + +import android.app.Application +import android.content.Context +import androidx.test.runner.AndroidJUnitRunner +import dagger.hilt.android.testing.HiltTestApplication + +class CustomTestRunner : AndroidJUnitRunner() { + + override fun newApplication( + classLoader: ClassLoader?, + className: String?, + context: Context? + ): Application { + return super.newApplication(classLoader, HiltTestApplication::class.java.name, context) + } +} \ No newline at end of file From e621feeafe69a11fcc07e2c25b18915747734750 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:41:48 +0200 Subject: [PATCH 33/85] Leverage the shared test-double in the UI tests --- .../assignment_tests/main/MainScreenTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index d3a7566..ebbfa11 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -1,18 +1,31 @@ package software.ninetofive.assignment_tests.main +import android.content.SharedPreferences import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 +import dagger.hilt.android.testing.* +import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import software.ninetofive.assignment_tests.R +import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences +import software.ninetofive.assignment_tests.utils.SharedPreferencesModule +@HiltAndroidTest +@UninstallModules(SharedPreferencesModule::class) @RunWith(AndroidJUnit4::class) class MainScreenTest { + @get:Rule + val hiltRule = HiltAndroidRule(this) + + @BindValue + val sharedPreferences: SharedPreferences = InMemorySharedPreferences() + @Test fun defaultScreen() { From f8081db68c0ee02ef31801a5f9e3a2a967e80d19 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:46:46 +0200 Subject: [PATCH 34/85] Cover default values for SelectedScreen --- .../ninetofive/assignment_tests/main/MainScreenTest.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index ebbfa11..f21ee8b 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -4,8 +4,7 @@ import android.content.SharedPreferences import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView import androidx.test.espresso.assertion.ViewAssertions.matches -import androidx.test.espresso.matcher.ViewMatchers.isDisplayed -import androidx.test.espresso.matcher.ViewMatchers.withText +import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.ext.junit.runners.AndroidJUnit4 import dagger.hilt.android.testing.* import org.junit.Rule @@ -33,5 +32,11 @@ class MainScreenTest { onView(withText(R.string.choose_start_screen_header)) .check(matches(isDisplayed())) + onView(withId(R.id.radio_screen_a)) + .check(matches(isNotChecked())) + onView(withId(R.id.radio_screen_b)) + .check(matches(isChecked())) //TODO Bug! C preference is recognised as B on screen + onView(withId(R.id.radio_screen_c)) + .check(matches(isNotChecked())) } } \ No newline at end of file From f1936de59aa6d6a03c4b35dabf9a8e11cde56648 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Fri, 8 Apr 2022 23:47:39 +0200 Subject: [PATCH 35/85] Improve test name --- .../ninetofive/assignment_tests/main/MainScreenTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index f21ee8b..3778fa7 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -26,8 +26,7 @@ class MainScreenTest { val sharedPreferences: SharedPreferences = InMemorySharedPreferences() @Test - fun defaultScreen() { - + fun defaultSelectScreenOptionValues() { ActivityScenario.launch(MainActivity::class.java) onView(withText(R.string.choose_start_screen_header)) From 4be3ba6914c42467e0f1b79ce8b943426bb5a123 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:00:40 +0200 Subject: [PATCH 36/85] Introduce screen robot to improve tests intention and clarity --- .../assignment_tests/main/MainScreenRobot.kt | 32 +++++++++++++++++++ .../assignment_tests/main/MainScreenTest.kt | 25 ++++++--------- 2 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt new file mode 100644 index 0000000..4ac3439 --- /dev/null +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -0,0 +1,32 @@ +package software.ninetofive.assignment_tests.main + +import androidx.test.core.app.ActivityScenario +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.assertion.ViewAssertions.matches +import androidx.test.espresso.matcher.ViewMatchers.* +import software.ninetofive.assignment_tests.R +import software.ninetofive.assignment_tests.main.SelectedScreen.* + +fun launchMainScreen( + block: MainScreenRobot.() -> Unit +): MainScreenRobot { + ActivityScenario.launch(MainActivity::class.java) + return MainScreenRobot().apply(block) +} + +class MainScreenRobot { + + fun checkSelectedScreenHeaderShown() { + onView(withText(R.string.choose_start_screen_header)).check(matches(isDisplayed())) + } + + fun checkSelectedScreenOptionIs(selectedScreen: SelectedScreen) { + val screenASelected = if (selectedScreen == SCREEN_A) isChecked() else isNotChecked() + val screenBSelected = if (selectedScreen == SCREEN_B) isChecked() else isNotChecked() + val screenCSelected = if (selectedScreen == SCREEN_C) isChecked() else isNotChecked() + + onView(withId(R.id.radio_screen_a)).check(matches(screenASelected)) + onView(withId(R.id.radio_screen_b)).check(matches(screenBSelected)) + onView(withId(R.id.radio_screen_c)).check(matches(screenCSelected)) + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index 3778fa7..94cf23e 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -1,16 +1,14 @@ package software.ninetofive.assignment_tests.main import android.content.SharedPreferences -import androidx.test.core.app.ActivityScenario -import androidx.test.espresso.Espresso.onView -import androidx.test.espresso.assertion.ViewAssertions.matches -import androidx.test.espresso.matcher.ViewMatchers.* import androidx.test.ext.junit.runners.AndroidJUnit4 -import dagger.hilt.android.testing.* +import dagger.hilt.android.testing.BindValue +import dagger.hilt.android.testing.HiltAndroidRule +import dagger.hilt.android.testing.HiltAndroidTest +import dagger.hilt.android.testing.UninstallModules import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith -import software.ninetofive.assignment_tests.R import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences import software.ninetofive.assignment_tests.utils.SharedPreferencesModule @@ -27,15 +25,10 @@ class MainScreenTest { @Test fun defaultSelectScreenOptionValues() { - ActivityScenario.launch(MainActivity::class.java) - - onView(withText(R.string.choose_start_screen_header)) - .check(matches(isDisplayed())) - onView(withId(R.id.radio_screen_a)) - .check(matches(isNotChecked())) - onView(withId(R.id.radio_screen_b)) - .check(matches(isChecked())) //TODO Bug! C preference is recognised as B on screen - onView(withId(R.id.radio_screen_c)) - .check(matches(isNotChecked())) + launchMainScreen { + checkSelectedScreenHeaderShown() + //TODO Bug! Default preference (SCREEN_C) is recognised as B on screen + checkSelectedScreenOptionIs(SelectedScreen.SCREEN_B) + } } } \ No newline at end of file From bc591ee486f1c3ae6f6f702c3be9bbb3d76f0179 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:13:12 +0200 Subject: [PATCH 37/85] Improve test name --- .../software/ninetofive/assignment_tests/main/MainScreenTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index 94cf23e..694c895 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -24,7 +24,7 @@ class MainScreenTest { val sharedPreferences: SharedPreferences = InMemorySharedPreferences() @Test - fun defaultSelectScreenOptionValues() { + fun defaultSelectScreenOptionValue() { launchMainScreen { checkSelectedScreenHeaderShown() //TODO Bug! Default preference (SCREEN_C) is recognised as B on screen From 8e7e41d182f322da70f479ed5b15d8cab98db42f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:20:45 +0200 Subject: [PATCH 38/85] Cover default values for ViewingOption --- .../assignment_tests/main/MainScreenRobot.kt | 15 +++++++++++++++ .../assignment_tests/main/MainScreenTest.kt | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt index 4ac3439..ae6dda9 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -6,6 +6,7 @@ import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.* import software.ninetofive.assignment_tests.R import software.ninetofive.assignment_tests.main.SelectedScreen.* +import software.ninetofive.assignment_tests.main.ViewingOption.* fun launchMainScreen( block: MainScreenRobot.() -> Unit @@ -29,4 +30,18 @@ class MainScreenRobot { onView(withId(R.id.radio_screen_b)).check(matches(screenBSelected)) onView(withId(R.id.radio_screen_c)).check(matches(screenCSelected)) } + + fun checkViewingOptionsHeader() { + onView(withText(R.string.check_view_options_label)).check(matches(isDisplayed())) + } + + fun checkSelectedViewingOptionIs(viewingOption: ViewingOption) { + val showNameSelected = if (viewingOption == SHOW_NAME) isChecked() else isNotChecked() + val showDateSelected = if (viewingOption == DATE) isChecked() else isNotChecked() + val showNothingSelected = if (viewingOption == NOTHING) isChecked() else isNotChecked() + + onView(withId(R.id.radio_show_name)).check(matches(showNameSelected)) + onView(withId(R.id.radio_show_date)).check(matches(showDateSelected)) + onView(withId(R.id.radio_show_nothing)).check(matches(showNothingSelected)) + } } \ No newline at end of file diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index 694c895..a3d881e 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -31,4 +31,12 @@ class MainScreenTest { checkSelectedScreenOptionIs(SelectedScreen.SCREEN_B) } } + + @Test + fun defaultViewingOptionValue() { + launchMainScreen { + checkViewingOptionsHeader() + checkSelectedViewingOptionIs(ViewingOption.NOTHING) + } + } } \ No newline at end of file From 31a160dd38fab01e2daebb8ee295ff0ec4c128fa Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:27:02 +0200 Subject: [PATCH 39/85] Cover default value for valid dot option --- .../ninetofive/assignment_tests/main/MainScreenRobot.kt | 5 +++++ .../ninetofive/assignment_tests/main/MainScreenTest.kt | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt index ae6dda9..55feda0 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -44,4 +44,9 @@ class MainScreenRobot { onView(withId(R.id.radio_show_date)).check(matches(showDateSelected)) onView(withId(R.id.radio_show_nothing)).check(matches(showNothingSelected)) } + + fun checkShowValidDotOptionSelected(isSelected: Boolean) { + val selected = if (isSelected) isChecked() else isNotChecked() + onView(withId(R.id.valid_dot_switch)).check(matches(selected)) + } } \ No newline at end of file diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index a3d881e..d24fee1 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -39,4 +39,11 @@ class MainScreenTest { checkSelectedViewingOptionIs(ViewingOption.NOTHING) } } + + @Test + fun defaultShowValidDotValue() { + launchMainScreen { + checkShowValidDotOptionSelected(false) + } + } } \ No newline at end of file From 15f4697101c5c5469554c4901ae6b16dc2967688 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:30:07 +0200 Subject: [PATCH 40/85] Update test setup to enable more robust verification --- .../ninetofive/assignment_tests/main/MainScreenTest.kt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index d24fee1..854ce04 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -1,19 +1,16 @@ package software.ninetofive.assignment_tests.main -import android.content.SharedPreferences import androidx.test.ext.junit.runners.AndroidJUnit4 import dagger.hilt.android.testing.BindValue import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest -import dagger.hilt.android.testing.UninstallModules import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import software.ninetofive.assignment_tests.utils.AppPreferences import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences -import software.ninetofive.assignment_tests.utils.SharedPreferencesModule @HiltAndroidTest -@UninstallModules(SharedPreferencesModule::class) @RunWith(AndroidJUnit4::class) class MainScreenTest { @@ -21,7 +18,7 @@ class MainScreenTest { val hiltRule = HiltAndroidRule(this) @BindValue - val sharedPreferences: SharedPreferences = InMemorySharedPreferences() + val appPreferences = AppPreferences(InMemorySharedPreferences()) @Test fun defaultSelectScreenOptionValue() { From 421c0953c0ede5624cf1feb01c277f273f675025 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:39:27 +0200 Subject: [PATCH 41/85] Cover select screen options interaction --- .../assignment_tests/main/MainScreenRobot.kt | 13 +++++++++++++ .../assignment_tests/main/MainScreenTest.kt | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt index 55feda0..9bcc34c 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -2,6 +2,7 @@ package software.ninetofive.assignment_tests.main import androidx.test.core.app.ActivityScenario import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.matcher.ViewMatchers.* import software.ninetofive.assignment_tests.R @@ -21,6 +22,18 @@ class MainScreenRobot { onView(withText(R.string.choose_start_screen_header)).check(matches(isDisplayed())) } + fun tapOnScreenAOption() { + onView(withText(R.string.check_start_screen_a)).perform(click()) + } + + fun tapOnScreenBOption() { + onView(withText(R.string.check_start_screen_b)).perform(click()) + } + + fun tapOnScreenCOption() { + onView(withText(R.string.check_start_screen_c)).perform(click()) + } + fun checkSelectedScreenOptionIs(selectedScreen: SelectedScreen) { val screenASelected = if (selectedScreen == SCREEN_A) isChecked() else isNotChecked() val screenBSelected = if (selectedScreen == SCREEN_B) isChecked() else isNotChecked() diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index 854ce04..ea5630a 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -29,6 +29,22 @@ class MainScreenTest { } } + @Test + fun selectScreenOptionInteractions() { + appPreferences.setStartScreen(SelectedScreen.SCREEN_A) + + launchMainScreen { + checkSelectedScreenHeaderShown() + checkSelectedScreenOptionIs(SelectedScreen.SCREEN_A) + tapOnScreenBOption() + checkSelectedScreenOptionIs(SelectedScreen.SCREEN_B) + tapOnScreenCOption() + checkSelectedScreenOptionIs(SelectedScreen.SCREEN_C) + tapOnScreenAOption() + checkSelectedScreenOptionIs(SelectedScreen.SCREEN_A) + } + } + @Test fun defaultViewingOptionValue() { launchMainScreen { From 9a75efaea2f4eace0c2b8cf2b798a858c6412f78 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:52:28 +0200 Subject: [PATCH 42/85] Cover select viewing options interactions --- .../assignment_tests/main/MainScreenRobot.kt | 12 ++++++++++++ .../assignment_tests/main/MainScreenTest.kt | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt index 9bcc34c..8fb5869 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -48,6 +48,18 @@ class MainScreenRobot { onView(withText(R.string.check_view_options_label)).check(matches(isDisplayed())) } + fun tapOnShowNameOption() { + onView(withText(R.string.check_view_options_name)).perform(click()) + } + + fun tapOnDateOption() { + onView(withText(R.string.check_view_options_date)).perform(click()) + } + + fun tapOnNothingOption() { + onView(withText(R.string.check_view_options_nothing)).perform(click()) + } + fun checkSelectedViewingOptionIs(viewingOption: ViewingOption) { val showNameSelected = if (viewingOption == SHOW_NAME) isChecked() else isNotChecked() val showDateSelected = if (viewingOption == DATE) isChecked() else isNotChecked() diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index ea5630a..64e0074 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -53,6 +53,22 @@ class MainScreenTest { } } + @Test + fun selectViewingOptionInteractions() { + appPreferences.setViewingOption(ViewingOption.DATE) + + launchMainScreen { + checkViewingOptionsHeader() + checkSelectedViewingOptionIs(ViewingOption.DATE) + tapOnNothingOption() + checkSelectedViewingOptionIs(ViewingOption.NOTHING) + tapOnShowNameOption() + checkSelectedViewingOptionIs(ViewingOption.SHOW_NAME) + tapOnDateOption() + checkSelectedViewingOptionIs(ViewingOption.DATE) + } + } + @Test fun defaultShowValidDotValue() { launchMainScreen { From 025a76e623c975a7a6a45b6555ca93d3250233a2 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:55:46 +0200 Subject: [PATCH 43/85] Cover show valid dot option interactions (functionallity covered) --- .../assignment_tests/main/MainScreenRobot.kt | 4 ++++ .../assignment_tests/main/MainScreenTest.kt | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt index 8fb5869..c8dca70 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenRobot.kt @@ -70,6 +70,10 @@ class MainScreenRobot { onView(withId(R.id.radio_show_nothing)).check(matches(showNothingSelected)) } + fun tapOnShowValidDot() { + onView(withText(R.string.show_valid_spot_settings_label)).perform(click()) + } + fun checkShowValidDotOptionSelected(isSelected: Boolean) { val selected = if (isSelected) isChecked() else isNotChecked() onView(withId(R.id.valid_dot_switch)).check(matches(selected)) diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt index 64e0074..f1211ac 100644 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt +++ b/app/src/androidTest/java/software/ninetofive/assignment_tests/main/MainScreenTest.kt @@ -75,4 +75,15 @@ class MainScreenTest { checkShowValidDotOptionSelected(false) } } + + @Test + fun showValidDotOptionInteractions() { + launchMainScreen { + checkShowValidDotOptionSelected(false) + tapOnShowValidDot() + checkShowValidDotOptionSelected(true) + tapOnShowValidDot() + checkShowValidDotOptionSelected(false) + } + } } \ No newline at end of file From 8a29b93519ef52c47fd2529b89d7cc689cf5cad2 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 00:58:27 +0200 Subject: [PATCH 44/85] Delete default project-generated sample test files --- .../ExampleInstrumentedTest.kt | 24 ------------------- .../assignment_tests/ExampleUnitTest.kt | 12 ---------- 2 files changed, 36 deletions(-) delete mode 100644 app/src/androidTest/java/software/ninetofive/assignment_tests/ExampleInstrumentedTest.kt delete mode 100644 app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt diff --git a/app/src/androidTest/java/software/ninetofive/assignment_tests/ExampleInstrumentedTest.kt b/app/src/androidTest/java/software/ninetofive/assignment_tests/ExampleInstrumentedTest.kt deleted file mode 100644 index 57c28e3..0000000 --- a/app/src/androidTest/java/software/ninetofive/assignment_tests/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package software.ninetofive.assignment_tests - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("software.ninetofive.assignment_tests", appContext.packageName) - } -} \ No newline at end of file diff --git a/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt deleted file mode 100644 index 3be2eb9..0000000 --- a/app/src/test/java/software/ninetofive/assignment_tests/ExampleUnitTest.kt +++ /dev/null @@ -1,12 +0,0 @@ -package software.ninetofive.assignment_tests - -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test - -class ExampleUnitTest { - - @Test - fun addition_isCorrect() { - assertEquals(4, 2 + 2) - } -} \ No newline at end of file From ac74444bb1b22a8b4db82daca8b933588be64a4d Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:25:40 +0200 Subject: [PATCH 45/85] Cover loading the selected screen state --- .../assignment_tests/main/LoadSettingsTest.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt new file mode 100644 index 0000000..a4e0e96 --- /dev/null +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -0,0 +1,18 @@ +package software.ninetofive.assignment_tests.main + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import software.ninetofive.assignment_tests.utils.AppPreferences +import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences + +class LoadSettingsTest { + + @Test + fun defaultSelectedScreen() { + val appPreferences = AppPreferences(InMemorySharedPreferences()) + val presenter = MainPresenter(appPreferences) + val subscriber = presenter.onScreenSelectedFlowable.test() + + assertEquals(SelectedScreen.SCREEN_C, subscriber.values().last()) + } +} \ No newline at end of file From cc089f0659e615562e0944f009e1f52985336c3d Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:29:33 +0200 Subject: [PATCH 46/85] Cover various selected screen options selection --- .../assignment_tests/main/LoadSettingsTest.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index a4e0e96..633e2aa 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -15,4 +15,23 @@ class LoadSettingsTest { assertEquals(SelectedScreen.SCREEN_C, subscriber.values().last()) } + + @Test + fun selectedScreenSelection() { + val appPreferences = AppPreferences(InMemorySharedPreferences()) + val presenter = MainPresenter(appPreferences) + val subscriber = presenter.onScreenSelectedFlowable.test() + + presenter.selectScreen(SelectedScreen.SCREEN_B) + presenter.selectScreen(SelectedScreen.SCREEN_A) + + assertEquals( + listOf( + SelectedScreen.SCREEN_C, + SelectedScreen.SCREEN_B, + SelectedScreen.SCREEN_A + ), + subscriber.values() + ) + } } \ No newline at end of file From ff27fde7551e2ece6972479ba64ecb2d03ccf540 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:30:06 +0200 Subject: [PATCH 47/85] Remove duplication --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 633e2aa..fe76d43 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -7,19 +7,17 @@ import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences class LoadSettingsTest { + private val appPreferences = AppPreferences(InMemorySharedPreferences()) + private val presenter = MainPresenter(appPreferences) + @Test fun defaultSelectedScreen() { - val appPreferences = AppPreferences(InMemorySharedPreferences()) - val presenter = MainPresenter(appPreferences) val subscriber = presenter.onScreenSelectedFlowable.test() assertEquals(SelectedScreen.SCREEN_C, subscriber.values().last()) } - @Test fun selectedScreenSelection() { - val appPreferences = AppPreferences(InMemorySharedPreferences()) - val presenter = MainPresenter(appPreferences) val subscriber = presenter.onScreenSelectedFlowable.test() presenter.selectScreen(SelectedScreen.SCREEN_B) From 717fe1c410ee2a42c9ac4c43b25db0b2246677f0 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:30:50 +0200 Subject: [PATCH 48/85] Improve formatting --- .../assignment_tests/main/LoadSettingsTest.kt | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index fe76d43..4342f55 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -2,6 +2,7 @@ package software.ninetofive.assignment_tests.main import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test +import software.ninetofive.assignment_tests.main.SelectedScreen.* import software.ninetofive.assignment_tests.utils.AppPreferences import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences @@ -14,22 +15,16 @@ class LoadSettingsTest { fun defaultSelectedScreen() { val subscriber = presenter.onScreenSelectedFlowable.test() - assertEquals(SelectedScreen.SCREEN_C, subscriber.values().last()) + assertEquals(SCREEN_C, subscriber.values().last()) } + @Test fun selectedScreenSelection() { val subscriber = presenter.onScreenSelectedFlowable.test() - presenter.selectScreen(SelectedScreen.SCREEN_B) - presenter.selectScreen(SelectedScreen.SCREEN_A) - - assertEquals( - listOf( - SelectedScreen.SCREEN_C, - SelectedScreen.SCREEN_B, - SelectedScreen.SCREEN_A - ), - subscriber.values() - ) + presenter.selectScreen(SCREEN_B) + presenter.selectScreen(SCREEN_A) + + assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), subscriber.values()) } } \ No newline at end of file From d34fb3f02c30f9226d190e36d34f21cb9306ccde Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:45:23 +0200 Subject: [PATCH 49/85] Cover storing selected start screen value --- .../assignment_tests/main/LoadSettingsTest.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 4342f55..1271007 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -1,6 +1,9 @@ package software.ninetofive.assignment_tests.main +import io.reactivex.disposables.CompositeDisposable +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen.* import software.ninetofive.assignment_tests.utils.AppPreferences @@ -10,6 +13,12 @@ class LoadSettingsTest { private val appPreferences = AppPreferences(InMemorySharedPreferences()) private val presenter = MainPresenter(appPreferences) + private val disposable = CompositeDisposable() + + @BeforeEach + fun setUp() { + disposable.add(presenter.dispose()) + } @Test fun defaultSelectedScreen() { @@ -27,4 +36,16 @@ class LoadSettingsTest { assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), subscriber.values()) } + + @Test + fun storingScreenSelection() { + presenter.selectScreen(SCREEN_B) + + assertEquals(SCREEN_B, appPreferences.getStartScreen()) + } + + @AfterEach + fun tearDown() { + disposable.dispose() + } } \ No newline at end of file From bf51ec8fd30c1f88b78722bdb3dcc1c869b45f5b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:50:12 +0200 Subject: [PATCH 50/85] Cover loading the viewing option --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 1271007..5e9a72b 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -44,6 +44,13 @@ class LoadSettingsTest { assertEquals(SCREEN_B, appPreferences.getStartScreen()) } + @Test + fun defaultViewingOption() { + val subscriber = presenter.viewingOptionFlowable.test() + + assertEquals(ViewingOption.NOTHING, subscriber.values().last()) + } + @AfterEach fun tearDown() { disposable.dispose() From 3146a169d0f0a240a189abb8c51b9558c5997a80 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:50:35 +0200 Subject: [PATCH 51/85] Cover various viewing option selection --- .../assignment_tests/main/LoadSettingsTest.kt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 5e9a72b..364cae3 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -51,6 +51,19 @@ class LoadSettingsTest { assertEquals(ViewingOption.NOTHING, subscriber.values().last()) } + @Test + fun selectedViewSelection() { + val subscriber = presenter.viewingOptionFlowable.test() + + presenter.selectViewingOption(ViewingOption.SHOW_NAME) + presenter.selectViewingOption(ViewingOption.DATE) + + assertEquals( + listOf(ViewingOption.NOTHING, ViewingOption.SHOW_NAME, ViewingOption.DATE), + subscriber.values() + ) + } + @AfterEach fun tearDown() { disposable.dispose() From b1ae00dfe4686c31fbf78cb3367ea82e04202f21 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:50:56 +0200 Subject: [PATCH 52/85] Improve formatting --- .../assignment_tests/main/LoadSettingsTest.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 364cae3..9048669 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -6,6 +6,7 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen.* +import software.ninetofive.assignment_tests.main.ViewingOption.* import software.ninetofive.assignment_tests.utils.AppPreferences import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences @@ -48,20 +49,17 @@ class LoadSettingsTest { fun defaultViewingOption() { val subscriber = presenter.viewingOptionFlowable.test() - assertEquals(ViewingOption.NOTHING, subscriber.values().last()) + assertEquals(NOTHING, subscriber.values().last()) } @Test fun selectedViewSelection() { val subscriber = presenter.viewingOptionFlowable.test() - presenter.selectViewingOption(ViewingOption.SHOW_NAME) - presenter.selectViewingOption(ViewingOption.DATE) + presenter.selectViewingOption(SHOW_NAME) + presenter.selectViewingOption(DATE) - assertEquals( - listOf(ViewingOption.NOTHING, ViewingOption.SHOW_NAME, ViewingOption.DATE), - subscriber.values() - ) + assertEquals(listOf(NOTHING, SHOW_NAME, DATE), subscriber.values()) } @AfterEach From bd17da3516bad1b5ae02f27ece8df0ebab02cdb3 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:53:25 +0200 Subject: [PATCH 53/85] Cover storing selected viewing option value --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 9048669..e8b8934 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -62,6 +62,13 @@ class LoadSettingsTest { assertEquals(listOf(NOTHING, SHOW_NAME, DATE), subscriber.values()) } + @Test + fun storingViewingOptionSelection() { + presenter.selectViewingOption(SHOW_NAME) + + assertEquals(SHOW_NAME, appPreferences.getViewingOption()) + } + @AfterEach fun tearDown() { disposable.dispose() From e84a0b64466b98fa20168502a34e912ea12aa4a0 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:55:45 +0200 Subject: [PATCH 54/85] Cover loading the valid dot option --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index e8b8934..13c9e58 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -3,6 +3,7 @@ package software.ninetofive.assignment_tests.main import io.reactivex.disposables.CompositeDisposable import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen.* @@ -69,6 +70,11 @@ class LoadSettingsTest { assertEquals(SHOW_NAME, appPreferences.getViewingOption()) } + @Test + fun defaultValidDotCheckedOption() { + assertFalse(presenter.isValidDotChecked()) + } + @AfterEach fun tearDown() { disposable.dispose() From ee1f50f220fe694354202436a8bbf9bd3ecaeabb Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 21:59:36 +0200 Subject: [PATCH 55/85] Cover storing the selected viewing dot option value --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 13c9e58..e52799d 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -2,8 +2,7 @@ package software.ninetofive.assignment_tests.main import io.reactivex.disposables.CompositeDisposable import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen.* @@ -75,6 +74,12 @@ class LoadSettingsTest { assertFalse(presenter.isValidDotChecked()) } + @Test + fun validDotToggling() { + presenter.setValidDotVisibility(true) + assertTrue(presenter.isValidDotChecked()) + } + @AfterEach fun tearDown() { disposable.dispose() From 1399a5bb50ea7bd93d28665552b0773001d3f7db Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:34:56 +0200 Subject: [PATCH 56/85] Cover storing valid dot option value to app preferences --- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index e52799d..6380340 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -78,6 +78,7 @@ class LoadSettingsTest { fun validDotToggling() { presenter.setValidDotVisibility(true) assertTrue(presenter.isValidDotChecked()) + assertEquals(true, appPreferences.shouldShowValidDot()) } @AfterEach From a88fbb87a72e6f19c8581f5aaa237f73a9de52d4 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:02:58 +0200 Subject: [PATCH 57/85] Move classes out of the presenter file --- .../assignment_tests/main/MainPresenter.kt | 27 +------------------ .../assignment_tests/main/SelectedScreen.kt | 13 +++++++++ .../assignment_tests/main/ViewingOption.kt | 13 +++++++++ 3 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/software/ninetofive/assignment_tests/main/SelectedScreen.kt create mode 100644 app/src/main/java/software/ninetofive/assignment_tests/main/ViewingOption.kt diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 77608ac..26e4a47 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -32,29 +32,4 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() fun setValidDotVisibility(isActive: Boolean) = appPreferences.setShowValidDot(isActive) -} - -enum class SelectedScreen { - SCREEN_A { - override fun toString() = "SCREEN_A" - }, - SCREEN_B { - override fun toString() = "SCREEN_B" - }, - SCREEN_C { - override fun toString() = "SCREEN_C" - } -} - -enum class ViewingOption { - SHOW_NAME { - override fun toString() = "SHOW_NAME" - }, - DATE { - override fun toString() = "DATE" - }, - NOTHING { - override fun toString() = "NOTHING" - } -} - +} \ No newline at end of file diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/SelectedScreen.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/SelectedScreen.kt new file mode 100644 index 0000000..ea490c6 --- /dev/null +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/SelectedScreen.kt @@ -0,0 +1,13 @@ +package software.ninetofive.assignment_tests.main + +enum class SelectedScreen { + SCREEN_A { + override fun toString() = "SCREEN_A" + }, + SCREEN_B { + override fun toString() = "SCREEN_B" + }, + SCREEN_C { + override fun toString() = "SCREEN_C" + } +} \ No newline at end of file diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/ViewingOption.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/ViewingOption.kt new file mode 100644 index 0000000..b2f0e80 --- /dev/null +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/ViewingOption.kt @@ -0,0 +1,13 @@ +package software.ninetofive.assignment_tests.main + +enum class ViewingOption { + SHOW_NAME { + override fun toString() = "SHOW_NAME" + }, + DATE { + override fun toString() = "DATE" + }, + NOTHING { + override fun toString() = "NOTHING" + } +} \ No newline at end of file From 5e3a7eccac304e35eb98431d938ef73204f00f98 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:11:42 +0200 Subject: [PATCH 58/85] Remove internal selecScreenSubsject subscription --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 26e4a47..378d38e 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -20,14 +20,15 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc val dispose = { CompositeDisposable( - selectScreenSubject - .subscribe { appPreferences.setStartScreen(it) }, viewingOptionsSubject .subscribe { appPreferences.setViewingOption(it) } ) } - fun selectScreen(selectedScreen: SelectedScreen) = selectScreenSubject.onNext(selectedScreen) + fun selectScreen(selectedScreen: SelectedScreen) { + selectScreenSubject.onNext(selectedScreen) + appPreferences.setStartScreen(selectedScreen) + } fun selectViewingOption(viewingOption: ViewingOption) = viewingOptionsSubject.onNext(viewingOption) fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() From d70e832584622623e224cd212b3b0d7488c59d5f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:14:11 +0200 Subject: [PATCH 59/85] Remove internal viewingOptionSubject subscription --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 378d38e..7dbaa1e 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -19,17 +19,18 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc .startWith(appPreferences.getViewingOption()) val dispose = { - CompositeDisposable( - viewingOptionsSubject - .subscribe { appPreferences.setViewingOption(it) } - ) + CompositeDisposable() } fun selectScreen(selectedScreen: SelectedScreen) { selectScreenSubject.onNext(selectedScreen) appPreferences.setStartScreen(selectedScreen) } - fun selectViewingOption(viewingOption: ViewingOption) = viewingOptionsSubject.onNext(viewingOption) + + fun selectViewingOption(viewingOption: ViewingOption) { + viewingOptionsSubject.onNext(viewingOption) + appPreferences.setViewingOption(viewingOption) + } fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() fun setValidDotVisibility(isActive: Boolean) = appPreferences.setShowValidDot(isActive) From eeb0823cdaba7b177740404f3085486a7825d411 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:18:09 +0200 Subject: [PATCH 60/85] Clean up empty disposable and the subjects --- .../assignment_tests/main/MainActivity.kt | 1 - .../assignment_tests/main/MainPresenter.kt | 5 ----- .../assignment_tests/main/LoadSettingsTest.kt | 14 -------------- 3 files changed, 20 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index 34db1d1..c4f5152 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -32,7 +32,6 @@ class MainActivity : AppCompatActivity() { valid_dot_switch.setOnCheckedChangeListener { buttonView, isChecked -> presenter.setValidDotVisibility(isChecked) } compositeDisposable.addAll( - presenter.dispose(), presenter.onScreenSelectedFlowable .subscribe(::onScreenSelected), presenter.viewingOptionFlowable diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 7dbaa1e..b63005d 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -1,7 +1,6 @@ package software.ninetofive.assignment_tests.main import io.reactivex.Flowable -import io.reactivex.disposables.CompositeDisposable import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor import software.ninetofive.assignment_tests.utils.AppPreferences @@ -18,10 +17,6 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc val viewingOptionFlowable: Flowable = viewingOptionsSubject .startWith(appPreferences.getViewingOption()) - val dispose = { - CompositeDisposable() - } - fun selectScreen(selectedScreen: SelectedScreen) { selectScreenSubject.onNext(selectedScreen) appPreferences.setStartScreen(selectedScreen) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 6380340..44f4010 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -1,9 +1,6 @@ package software.ninetofive.assignment_tests.main -import io.reactivex.disposables.CompositeDisposable -import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.* -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import software.ninetofive.assignment_tests.main.SelectedScreen.* import software.ninetofive.assignment_tests.main.ViewingOption.* @@ -14,12 +11,6 @@ class LoadSettingsTest { private val appPreferences = AppPreferences(InMemorySharedPreferences()) private val presenter = MainPresenter(appPreferences) - private val disposable = CompositeDisposable() - - @BeforeEach - fun setUp() { - disposable.add(presenter.dispose()) - } @Test fun defaultSelectedScreen() { @@ -80,9 +71,4 @@ class LoadSettingsTest { assertTrue(presenter.isValidDotChecked()) assertEquals(true, appPreferences.shouldShowValidDot()) } - - @AfterEach - fun tearDown() { - disposable.dispose() - } } \ No newline at end of file From 0adeec2ec2ccb2e53cf97b96c09b828e9bcf1e2f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:18:29 +0200 Subject: [PATCH 61/85] Clean up uselsee subsjects from the AppPreferences --- .../ninetofive/assignment_tests/utils/AppPreferences.kt | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt index c06a160..2848d97 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/utils/AppPreferences.kt @@ -1,9 +1,6 @@ package software.ninetofive.assignment_tests.utils import android.content.SharedPreferences -import io.reactivex.processors.BehaviorProcessor -import io.reactivex.processors.FlowableProcessor -import io.reactivex.processors.PublishProcessor import software.ninetofive.assignment_tests.main.SelectedScreen import software.ninetofive.assignment_tests.main.ViewingOption import javax.inject.Inject @@ -20,9 +17,6 @@ class AppPreferences @Inject constructor( private const val KEY_SHOW_VALID_DOT = "show_valid_dot" } - private val viewingOptionChangedProcessor: FlowableProcessor = PublishProcessor.create() - private val showValidDotProcessor: FlowableProcessor = BehaviorProcessor.create() - fun setStartScreen(selectedScreen: SelectedScreen) { sharedPreferences.edit() .putString(KEY_SELECTED_SCREEN, selectedScreen.toString()) @@ -41,7 +35,6 @@ class AppPreferences @Inject constructor( sharedPreferences.edit() .putString(KEY_VIEWING_OPTION, viewingOption.toString()) .apply() - viewingOptionChangedProcessor.onNext(Any()) } fun getViewingOption(): ViewingOption = @@ -52,12 +45,10 @@ class AppPreferences @Inject constructor( else -> ViewingOption.NOTHING } - fun setShowValidDot(shouldShowValidDot: Boolean) { sharedPreferences.edit() .putBoolean(KEY_SHOW_VALID_DOT, shouldShowValidDot) .apply() - showValidDotProcessor.onNext(shouldShowValidDot) } fun shouldShowValidDot() = sharedPreferences.getBoolean(KEY_SHOW_VALID_DOT, false) From a5cd690f6b8167095f57ac02fcc2e72494dccd36 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:21:31 +0200 Subject: [PATCH 62/85] Introduce LiveData for selected screen --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 5 +++++ .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 1 + 2 files changed, 6 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index b63005d..0270a94 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -1,5 +1,7 @@ package software.ninetofive.assignment_tests.main +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData import io.reactivex.Flowable import io.reactivex.processors.FlowableProcessor import io.reactivex.processors.PublishProcessor @@ -8,6 +10,9 @@ import javax.inject.Inject class MainPresenter @Inject constructor(private val appPreferences: AppPreferences) { + private val mutableSelectedScreenLiveData = MutableLiveData(SelectedScreen.SCREEN_C) + val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData + private val selectScreenSubject: FlowableProcessor = PublishProcessor.create() private val viewingOptionsSubject: FlowableProcessor = PublishProcessor.create() diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 44f4010..6254658 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -17,6 +17,7 @@ class LoadSettingsTest { val subscriber = presenter.onScreenSelectedFlowable.test() assertEquals(SCREEN_C, subscriber.values().last()) + assertEquals(SCREEN_C, presenter.selectedScreenLiveData.value) } @Test From 6d5560e44ef88f9a647103bc888dd2e518c66584 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:24:40 +0200 Subject: [PATCH 63/85] Introduce InstantTaskExecutionExtension (to be able to work with LiveData) --- .../InstantTaskExecutorExtension.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 app/src/test/java/software/ninetofive/assignment_tests/InstantTaskExecutorExtension.kt diff --git a/app/src/test/java/software/ninetofive/assignment_tests/InstantTaskExecutorExtension.kt b/app/src/test/java/software/ninetofive/assignment_tests/InstantTaskExecutorExtension.kt new file mode 100644 index 0000000..bf0a06c --- /dev/null +++ b/app/src/test/java/software/ninetofive/assignment_tests/InstantTaskExecutorExtension.kt @@ -0,0 +1,30 @@ +package software.ninetofive.assignment_tests + +import androidx.arch.core.executor.ArchTaskExecutor +import androidx.arch.core.executor.TaskExecutor +import org.junit.jupiter.api.extension.AfterAllCallback +import org.junit.jupiter.api.extension.BeforeAllCallback +import org.junit.jupiter.api.extension.ExtensionContext + +class InstantTaskExecutorExtension : BeforeAllCallback, AfterAllCallback { + + override fun beforeAll(context: ExtensionContext?) { + ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() { + override fun executeOnDiskIO(runnable: Runnable) { + runnable.run() + } + + override fun postToMainThread(runnable: Runnable) { + runnable.run() + } + + override fun isMainThread(): Boolean { + return true + } + }) + } + + override fun afterAll(context: ExtensionContext?) { + ArchTaskExecutor.getInstance().setDelegate(null) + } +} \ No newline at end of file From 3bd5b4fcceb45deb99b5eca86f64d8e75789f7e8 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:26:17 +0200 Subject: [PATCH 64/85] Deliver selected screen value to the live data observers --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 1 + .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 0270a94..1a0f286 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -25,6 +25,7 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc fun selectScreen(selectedScreen: SelectedScreen) { selectScreenSubject.onNext(selectedScreen) appPreferences.setStartScreen(selectedScreen) + mutableSelectedScreenLiveData.value = selectedScreen } fun selectViewingOption(viewingOption: ViewingOption) { diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 6254658..30b3ed7 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -2,11 +2,14 @@ package software.ninetofive.assignment_tests.main import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import software.ninetofive.assignment_tests.InstantTaskExecutorExtension import software.ninetofive.assignment_tests.main.SelectedScreen.* import software.ninetofive.assignment_tests.main.ViewingOption.* import software.ninetofive.assignment_tests.utils.AppPreferences import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences +@ExtendWith(InstantTaskExecutorExtension::class) class LoadSettingsTest { private val appPreferences = AppPreferences(InMemorySharedPreferences()) @@ -22,12 +25,15 @@ class LoadSettingsTest { @Test fun selectedScreenSelection() { + val selectedScreenObservedValues = mutableListOf() + presenter.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } val subscriber = presenter.onScreenSelectedFlowable.test() presenter.selectScreen(SCREEN_B) presenter.selectScreen(SCREEN_A) assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), subscriber.values()) + assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), selectedScreenObservedValues) } @Test From efa3e590e2454eec60322787ccaf89eb2cbf4c83 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:39:08 +0200 Subject: [PATCH 65/85] Set initial live data value from appPreferences --- .../software/ninetofive/assignment_tests/main/MainPresenter.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 1a0f286..93d6614 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -10,7 +10,7 @@ import javax.inject.Inject class MainPresenter @Inject constructor(private val appPreferences: AppPreferences) { - private val mutableSelectedScreenLiveData = MutableLiveData(SelectedScreen.SCREEN_C) + private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData private val selectScreenSubject: FlowableProcessor = PublishProcessor.create() From bd7e7f287b751741e3858a34e1595717753cf36f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 22:43:24 +0200 Subject: [PATCH 66/85] Migrate screen selection options to live data --- .../assignment_tests/main/MainActivity.kt | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index c4f5152..2acf4fa 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -24,6 +24,8 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_choose_start_screen) setupToolbar() + setupSelectStartScreenOptions() + setupSelectedScreenObserver() // Little hack to force radio buttons on the right. After enabled RTL everything is still ok. arrayOf(radio_screen_a, radio_screen_b, radio_screen_c, radio_show_name, radio_show_date, radio_show_nothing) .forEach { ViewCompat.setLayoutDirection(it, ViewCompat.LAYOUT_DIRECTION_RTL) } @@ -32,21 +34,8 @@ class MainActivity : AppCompatActivity() { valid_dot_switch.setOnCheckedChangeListener { buttonView, isChecked -> presenter.setValidDotVisibility(isChecked) } compositeDisposable.addAll( - presenter.onScreenSelectedFlowable - .subscribe(::onScreenSelected), presenter.viewingOptionFlowable .subscribe(::onViewingOptionSelected), - RxRadioGroup.checkedChanges(radio_group_select_screen) - .skipInitialValue() - .map { - when (it) { - R.id.radio_screen_a -> SelectedScreen.SCREEN_A - R.id.radio_screen_b -> SelectedScreen.SCREEN_C - R.id.radio_screen_c -> SelectedScreen.SCREEN_B - else -> throw RuntimeException("Unknown select screen for id: $it") - } - } - .subscribe { presenter.selectScreen(it) }, RxRadioGroup.checkedChanges(radio_group_viewing_options) .skipInitialValue() .map { @@ -61,6 +50,22 @@ class MainActivity : AppCompatActivity() { ) } + private fun setupSelectedScreenObserver() { + presenter.selectedScreenLiveData.observe(this) { + onScreenSelected(it) + } + } + + private fun setupSelectStartScreenOptions() { + radio_group_select_screen.setOnCheckedChangeListener { _, id -> + when (id) { + R.id.radio_screen_a -> SelectedScreen.SCREEN_A + R.id.radio_screen_b -> SelectedScreen.SCREEN_C + R.id.radio_screen_c -> SelectedScreen.SCREEN_B + } + } + } + private fun onScreenSelected(selectedScreen: SelectedScreen) = when (selectedScreen) { SelectedScreen.SCREEN_A -> radio_screen_a.isChecked = true SelectedScreen.SCREEN_C -> radio_screen_b.isChecked = true From 3428f35c17ca444a1b9401084adb46c6ce2afa6b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:01:19 +0200 Subject: [PATCH 67/85] Remove screen selection flowable from presenter --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 5 ----- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 5 ----- 2 files changed, 10 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 93d6614..ff1fafc 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -13,17 +13,12 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData - private val selectScreenSubject: FlowableProcessor = PublishProcessor.create() private val viewingOptionsSubject: FlowableProcessor = PublishProcessor.create() - val onScreenSelectedFlowable: Flowable = selectScreenSubject - .startWith(appPreferences.getStartScreen()) - val viewingOptionFlowable: Flowable = viewingOptionsSubject .startWith(appPreferences.getViewingOption()) fun selectScreen(selectedScreen: SelectedScreen) { - selectScreenSubject.onNext(selectedScreen) appPreferences.setStartScreen(selectedScreen) mutableSelectedScreenLiveData.value = selectedScreen } diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 30b3ed7..4c5ffa5 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -17,9 +17,6 @@ class LoadSettingsTest { @Test fun defaultSelectedScreen() { - val subscriber = presenter.onScreenSelectedFlowable.test() - - assertEquals(SCREEN_C, subscriber.values().last()) assertEquals(SCREEN_C, presenter.selectedScreenLiveData.value) } @@ -27,12 +24,10 @@ class LoadSettingsTest { fun selectedScreenSelection() { val selectedScreenObservedValues = mutableListOf() presenter.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } - val subscriber = presenter.onScreenSelectedFlowable.test() presenter.selectScreen(SCREEN_B) presenter.selectScreen(SCREEN_A) - assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), subscriber.values()) assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), selectedScreenObservedValues) } From 2ecd897f348bd8caee4b3b76c31f973289ada8a8 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:02:00 +0200 Subject: [PATCH 68/85] Introduce LiveData for viewing option --- .../software/ninetofive/assignment_tests/main/MainPresenter.kt | 2 ++ .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 1 + 2 files changed, 3 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index ff1fafc..34af422 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -13,6 +13,8 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData + private val mutableViewingOptionLiveData = MutableLiveData(appPreferences.getViewingOption()) + val viewingOptionLiveData: LiveData = mutableViewingOptionLiveData private val viewingOptionsSubject: FlowableProcessor = PublishProcessor.create() val viewingOptionFlowable: Flowable = viewingOptionsSubject diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 4c5ffa5..b242c11 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -43,6 +43,7 @@ class LoadSettingsTest { val subscriber = presenter.viewingOptionFlowable.test() assertEquals(NOTHING, subscriber.values().last()) + assertEquals(NOTHING, presenter.viewingOptionLiveData.value) } @Test From 573aa351742122a8338df42f24065c34a8ce2a0b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:02:30 +0200 Subject: [PATCH 69/85] Deliver viewing option value to the live data observers --- .../software/ninetofive/assignment_tests/main/MainPresenter.kt | 2 ++ .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 3 +++ 2 files changed, 5 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 34af422..68c86ee 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -15,6 +15,7 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc private val mutableViewingOptionLiveData = MutableLiveData(appPreferences.getViewingOption()) val viewingOptionLiveData: LiveData = mutableViewingOptionLiveData + private val viewingOptionsSubject: FlowableProcessor = PublishProcessor.create() val viewingOptionFlowable: Flowable = viewingOptionsSubject @@ -28,6 +29,7 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc fun selectViewingOption(viewingOption: ViewingOption) { viewingOptionsSubject.onNext(viewingOption) appPreferences.setViewingOption(viewingOption) + mutableViewingOptionLiveData.value = viewingOption } fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index b242c11..abb0f3b 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -48,12 +48,15 @@ class LoadSettingsTest { @Test fun selectedViewSelection() { + val viewingOptionObservedValues = mutableListOf() + presenter.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } val subscriber = presenter.viewingOptionFlowable.test() presenter.selectViewingOption(SHOW_NAME) presenter.selectViewingOption(DATE) assertEquals(listOf(NOTHING, SHOW_NAME, DATE), subscriber.values()) + assertEquals(listOf(NOTHING, SHOW_NAME, DATE), viewingOptionObservedValues) } @Test From 681f446427665132f5f0e0a540401bb1a022a3e8 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:06:59 +0200 Subject: [PATCH 70/85] Migrate viewing options to live data --- .../assignment_tests/main/MainActivity.kt | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index 2acf4fa..256e220 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -25,7 +25,8 @@ class MainActivity : AppCompatActivity() { setContentView(R.layout.activity_choose_start_screen) setupToolbar() setupSelectStartScreenOptions() - setupSelectedScreenObserver() + setupViewingOptions() + setupLiveDataObservers() // Little hack to force radio buttons on the right. After enabled RTL everything is still ok. arrayOf(radio_screen_a, radio_screen_b, radio_screen_c, radio_show_name, radio_show_date, radio_show_nothing) .forEach { ViewCompat.setLayoutDirection(it, ViewCompat.LAYOUT_DIRECTION_RTL) } @@ -33,27 +34,16 @@ class MainActivity : AppCompatActivity() { valid_dot_switch.isChecked = presenter.isValidDotChecked() valid_dot_switch.setOnCheckedChangeListener { buttonView, isChecked -> presenter.setValidDotVisibility(isChecked) } - compositeDisposable.addAll( - presenter.viewingOptionFlowable - .subscribe(::onViewingOptionSelected), - RxRadioGroup.checkedChanges(radio_group_viewing_options) - .skipInitialValue() - .map { - when (it) { - R.id.radio_show_name -> ViewingOption.SHOW_NAME - R.id.radio_show_date -> ViewingOption.DATE - R.id.radio_show_nothing -> ViewingOption.NOTHING - else -> throw RuntimeException("Unknown viewing option for id: $it") - } - } - .subscribe { presenter.selectViewingOption(it) } - ) + compositeDisposable.addAll() } - private fun setupSelectedScreenObserver() { + private fun setupLiveDataObservers() { presenter.selectedScreenLiveData.observe(this) { onScreenSelected(it) } + presenter.viewingOptionLiveData.observe(this) { + onViewingOptionSelected(it) + } } private fun setupSelectStartScreenOptions() { @@ -66,6 +56,16 @@ class MainActivity : AppCompatActivity() { } } + private fun setupViewingOptions() { + radio_group_viewing_options.setOnCheckedChangeListener { _, id -> + when (id) { + R.id.radio_show_name -> ViewingOption.SHOW_NAME + R.id.radio_show_date -> ViewingOption.DATE + R.id.radio_show_nothing -> ViewingOption.NOTHING + } + } + } + private fun onScreenSelected(selectedScreen: SelectedScreen) = when (selectedScreen) { SelectedScreen.SCREEN_A -> radio_screen_a.isChecked = true SelectedScreen.SCREEN_C -> radio_screen_b.isChecked = true From 4f43ac88494cc756cb98ff5c7ff3d3d4a15ece3c Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:08:39 +0200 Subject: [PATCH 71/85] Remove obsolete code --- .../ninetofive/assignment_tests/main/MainActivity.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index 256e220..a94814d 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -3,9 +3,7 @@ package software.ninetofive.assignment_tests.main import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat -import com.jakewharton.rxbinding2.widget.RxRadioGroup import dagger.hilt.android.AndroidEntryPoint -import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.activity_choose_start_screen.* import kotlinx.android.synthetic.main.choose_viewing_options.* import kotlinx.android.synthetic.main.choose_start_screen.* @@ -18,8 +16,6 @@ class MainActivity : AppCompatActivity() { @Inject lateinit var presenter: MainPresenter - protected val compositeDisposable = CompositeDisposable() - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_choose_start_screen) @@ -33,8 +29,6 @@ class MainActivity : AppCompatActivity() { valid_dot_switch.isChecked = presenter.isValidDotChecked() valid_dot_switch.setOnCheckedChangeListener { buttonView, isChecked -> presenter.setValidDotVisibility(isChecked) } - - compositeDisposable.addAll() } private fun setupLiveDataObservers() { @@ -84,5 +78,4 @@ class MainActivity : AppCompatActivity() { setTitle(R.string.choose_start_screen_title) } } - } From d48e5c2d19e78c74fb5c775c0bdc3019eaf9df26 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:09:39 +0200 Subject: [PATCH 72/85] Remove ViewingOptionFlowable from presenter --- .../ninetofive/assignment_tests/main/MainPresenter.kt | 9 --------- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 5 ----- 2 files changed, 14 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt index 68c86ee..1e08425 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt @@ -2,9 +2,6 @@ package software.ninetofive.assignment_tests.main import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData -import io.reactivex.Flowable -import io.reactivex.processors.FlowableProcessor -import io.reactivex.processors.PublishProcessor import software.ninetofive.assignment_tests.utils.AppPreferences import javax.inject.Inject @@ -16,18 +13,12 @@ class MainPresenter @Inject constructor(private val appPreferences: AppPreferenc private val mutableViewingOptionLiveData = MutableLiveData(appPreferences.getViewingOption()) val viewingOptionLiveData: LiveData = mutableViewingOptionLiveData - private val viewingOptionsSubject: FlowableProcessor = PublishProcessor.create() - - val viewingOptionFlowable: Flowable = viewingOptionsSubject - .startWith(appPreferences.getViewingOption()) - fun selectScreen(selectedScreen: SelectedScreen) { appPreferences.setStartScreen(selectedScreen) mutableSelectedScreenLiveData.value = selectedScreen } fun selectViewingOption(viewingOption: ViewingOption) { - viewingOptionsSubject.onNext(viewingOption) appPreferences.setViewingOption(viewingOption) mutableViewingOptionLiveData.value = viewingOption } diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index abb0f3b..af39014 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -40,9 +40,6 @@ class LoadSettingsTest { @Test fun defaultViewingOption() { - val subscriber = presenter.viewingOptionFlowable.test() - - assertEquals(NOTHING, subscriber.values().last()) assertEquals(NOTHING, presenter.viewingOptionLiveData.value) } @@ -50,12 +47,10 @@ class LoadSettingsTest { fun selectedViewSelection() { val viewingOptionObservedValues = mutableListOf() presenter.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } - val subscriber = presenter.viewingOptionFlowable.test() presenter.selectViewingOption(SHOW_NAME) presenter.selectViewingOption(DATE) - assertEquals(listOf(NOTHING, SHOW_NAME, DATE), subscriber.values()) assertEquals(listOf(NOTHING, SHOW_NAME, DATE), viewingOptionObservedValues) } From 3981676cf59168eefc76f2cc5b6ba42023f3e58d Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:14:44 +0200 Subject: [PATCH 73/85] Remove RxJava completely --- app/build.gradle | 8 -------- 1 file changed, 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3ea4db9..fd31747 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,8 +47,6 @@ android { } } -ext.rxbindingVersion = '2.1.1' - dependencies { implementation 'androidx.core:core-ktx:1.7.0' @@ -60,12 +58,6 @@ dependencies { implementation "com.google.dagger:hilt-android:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version" - implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0' - implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' - implementation "com.jakewharton.rxbinding2:rxbinding:$rxbindingVersion" - implementation "com.jakewharton.rxbinding2:rxbinding-support-v4:$rxbindingVersion" - implementation "com.jakewharton.rxbinding2:rxbinding-appcompat-v7:$rxbindingVersion" - androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0" androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3" From b5914232ec25a4de2e13bed9346797cc72079cae Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:21:43 +0200 Subject: [PATCH 74/85] Improve code organisation in MainActivity --- .../assignment_tests/main/MainActivity.kt | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index a94814d..f511348 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -5,8 +5,8 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import dagger.hilt.android.AndroidEntryPoint import kotlinx.android.synthetic.main.activity_choose_start_screen.* -import kotlinx.android.synthetic.main.choose_viewing_options.* import kotlinx.android.synthetic.main.choose_start_screen.* +import kotlinx.android.synthetic.main.choose_viewing_options.* import software.ninetofive.assignment_tests.R import javax.inject.Inject @@ -19,27 +19,36 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_choose_start_screen) + setupUI() + setupLiveDataObservers() + } + + private fun setupUI() { setupToolbar() + forceRadioButtonsToCorrectPosition() setupSelectStartScreenOptions() setupViewingOptions() - setupLiveDataObservers() - // Little hack to force radio buttons on the right. After enabled RTL everything is still ok. - arrayOf(radio_screen_a, radio_screen_b, radio_screen_c, radio_show_name, radio_show_date, radio_show_nothing) - .forEach { ViewCompat.setLayoutDirection(it, ViewCompat.LAYOUT_DIRECTION_RTL) } - - valid_dot_switch.isChecked = presenter.isValidDotChecked() - valid_dot_switch.setOnCheckedChangeListener { buttonView, isChecked -> presenter.setValidDotVisibility(isChecked) } + setupValidDotSwitch() } - private fun setupLiveDataObservers() { - presenter.selectedScreenLiveData.observe(this) { - onScreenSelected(it) - } - presenter.viewingOptionLiveData.observe(this) { - onViewingOptionSelected(it) + private fun setupToolbar() { + setSupportActionBar(location_choose_start_screen) + supportActionBar?.apply { + setTitle(R.string.choose_start_screen_title) } } + private fun forceRadioButtonsToCorrectPosition() { + arrayOf( + radio_screen_a, + radio_screen_b, + radio_screen_c, + radio_show_name, + radio_show_date, + radio_show_nothing + ).forEach { ViewCompat.setLayoutDirection(it, ViewCompat.LAYOUT_DIRECTION_RTL) } + } + private fun setupSelectStartScreenOptions() { radio_group_select_screen.setOnCheckedChangeListener { _, id -> when (id) { @@ -60,6 +69,22 @@ class MainActivity : AppCompatActivity() { } } + private fun setupValidDotSwitch() { + valid_dot_switch.isChecked = presenter.isValidDotChecked() + valid_dot_switch.setOnCheckedChangeListener { _, isChecked -> + presenter.setValidDotVisibility(isChecked) + } + } + + private fun setupLiveDataObservers() { + presenter.selectedScreenLiveData.observe(this) { + onScreenSelected(it) + } + presenter.viewingOptionLiveData.observe(this) { + onViewingOptionSelected(it) + } + } + private fun onScreenSelected(selectedScreen: SelectedScreen) = when (selectedScreen) { SelectedScreen.SCREEN_A -> radio_screen_a.isChecked = true SelectedScreen.SCREEN_C -> radio_screen_b.isChecked = true @@ -71,11 +96,4 @@ class MainActivity : AppCompatActivity() { ViewingOption.DATE -> radio_show_date.isChecked = true ViewingOption.NOTHING -> radio_show_nothing.isChecked = true } - - private fun setupToolbar() { - setSupportActionBar(location_choose_start_screen) - supportActionBar?.apply { - setTitle(R.string.choose_start_screen_title) - } - } } From e81476155e9d86cf1c3a1cb55b7739b63a17f471 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:29:16 +0200 Subject: [PATCH 75/85] Rename presenter --- .../assignment_tests/main/MainActivity.kt | 10 +++---- .../{MainPresenter.kt => MainViewModel.kt} | 2 +- .../assignment_tests/main/LoadSettingsTest.kt | 28 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) rename app/src/main/java/software/ninetofive/assignment_tests/main/{MainPresenter.kt => MainViewModel.kt} (94%) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index f511348..3a9e23f 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -14,7 +14,7 @@ import javax.inject.Inject class MainActivity : AppCompatActivity() { @Inject - lateinit var presenter: MainPresenter + lateinit var viewModel: MainViewModel override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -70,17 +70,17 @@ class MainActivity : AppCompatActivity() { } private fun setupValidDotSwitch() { - valid_dot_switch.isChecked = presenter.isValidDotChecked() + valid_dot_switch.isChecked = viewModel.isValidDotChecked() valid_dot_switch.setOnCheckedChangeListener { _, isChecked -> - presenter.setValidDotVisibility(isChecked) + viewModel.setValidDotVisibility(isChecked) } } private fun setupLiveDataObservers() { - presenter.selectedScreenLiveData.observe(this) { + viewModel.selectedScreenLiveData.observe(this) { onScreenSelected(it) } - presenter.viewingOptionLiveData.observe(this) { + viewModel.viewingOptionLiveData.observe(this) { onViewingOptionSelected(it) } } diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt similarity index 94% rename from app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt rename to app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index 1e08425..3df1aca 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainPresenter.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -5,7 +5,7 @@ import androidx.lifecycle.MutableLiveData import software.ninetofive.assignment_tests.utils.AppPreferences import javax.inject.Inject -class MainPresenter @Inject constructor(private val appPreferences: AppPreferences) { +class MainViewModel @Inject constructor(private val appPreferences: AppPreferences) { private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index af39014..539d0a6 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -13,63 +13,63 @@ import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences class LoadSettingsTest { private val appPreferences = AppPreferences(InMemorySharedPreferences()) - private val presenter = MainPresenter(appPreferences) + private val viewModel = MainViewModel(appPreferences) @Test fun defaultSelectedScreen() { - assertEquals(SCREEN_C, presenter.selectedScreenLiveData.value) + assertEquals(SCREEN_C, viewModel.selectedScreenLiveData.value) } @Test fun selectedScreenSelection() { val selectedScreenObservedValues = mutableListOf() - presenter.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } + viewModel.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } - presenter.selectScreen(SCREEN_B) - presenter.selectScreen(SCREEN_A) + viewModel.selectScreen(SCREEN_B) + viewModel.selectScreen(SCREEN_A) assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), selectedScreenObservedValues) } @Test fun storingScreenSelection() { - presenter.selectScreen(SCREEN_B) + viewModel.selectScreen(SCREEN_B) assertEquals(SCREEN_B, appPreferences.getStartScreen()) } @Test fun defaultViewingOption() { - assertEquals(NOTHING, presenter.viewingOptionLiveData.value) + assertEquals(NOTHING, viewModel.viewingOptionLiveData.value) } @Test fun selectedViewSelection() { val viewingOptionObservedValues = mutableListOf() - presenter.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } + viewModel.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } - presenter.selectViewingOption(SHOW_NAME) - presenter.selectViewingOption(DATE) + viewModel.selectViewingOption(SHOW_NAME) + viewModel.selectViewingOption(DATE) assertEquals(listOf(NOTHING, SHOW_NAME, DATE), viewingOptionObservedValues) } @Test fun storingViewingOptionSelection() { - presenter.selectViewingOption(SHOW_NAME) + viewModel.selectViewingOption(SHOW_NAME) assertEquals(SHOW_NAME, appPreferences.getViewingOption()) } @Test fun defaultValidDotCheckedOption() { - assertFalse(presenter.isValidDotChecked()) + assertFalse(viewModel.isValidDotChecked()) } @Test fun validDotToggling() { - presenter.setValidDotVisibility(true) - assertTrue(presenter.isValidDotChecked()) + viewModel.setValidDotVisibility(true) + assertTrue(viewModel.isValidDotChecked()) assertEquals(true, appPreferences.shouldShowValidDot()) } } \ No newline at end of file From ef060fd254cf3281f026ad4b5c6d79042c405830 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:36:30 +0200 Subject: [PATCH 76/85] Migrate to proper ViewModel --- app/build.gradle | 1 + .../ninetofive/assignment_tests/main/MainActivity.kt | 5 ++--- .../ninetofive/assignment_tests/main/MainViewModel.kt | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index fd31747..c7f0c43 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -50,6 +50,7 @@ android { dependencies { implementation 'androidx.core:core-ktx:1.7.0' + implementation "androidx.fragment:fragment-ktx:1.4.1" implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index 3a9e23f..ea62a26 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -1,6 +1,7 @@ package software.ninetofive.assignment_tests.main import android.os.Bundle +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import dagger.hilt.android.AndroidEntryPoint @@ -8,13 +9,11 @@ import kotlinx.android.synthetic.main.activity_choose_start_screen.* import kotlinx.android.synthetic.main.choose_start_screen.* import kotlinx.android.synthetic.main.choose_viewing_options.* import software.ninetofive.assignment_tests.R -import javax.inject.Inject @AndroidEntryPoint class MainActivity : AppCompatActivity() { - @Inject - lateinit var viewModel: MainViewModel + private val viewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index 3df1aca..a1844ae 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -2,10 +2,15 @@ package software.ninetofive.assignment_tests.main import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import dagger.hilt.android.lifecycle.HiltViewModel import software.ninetofive.assignment_tests.utils.AppPreferences import javax.inject.Inject -class MainViewModel @Inject constructor(private val appPreferences: AppPreferences) { +@HiltViewModel +class MainViewModel @Inject constructor( + private val appPreferences: AppPreferences +) : ViewModel() { private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData From 16c45cde4d03ffb2e34ee2a3fb7f057dc6795b5b Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:42:34 +0200 Subject: [PATCH 77/85] Introduce ScreenState (to be used to consolidate the properties) --- .../ninetofive/assignment_tests/main/MainViewModel.kt | 4 ++++ .../assignment_tests/main/state/ScreenState.kt | 10 ++++++++++ .../assignment_tests/main/LoadSettingsTest.kt | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 app/src/main/java/software/ninetofive/assignment_tests/main/state/ScreenState.kt diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index a1844ae..9699574 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import dagger.hilt.android.lifecycle.HiltViewModel +import software.ninetofive.assignment_tests.main.state.ScreenState import software.ninetofive.assignment_tests.utils.AppPreferences import javax.inject.Inject @@ -12,6 +13,9 @@ class MainViewModel @Inject constructor( private val appPreferences: AppPreferences ) : ViewModel() { + private val mutableScreenState = MutableLiveData(ScreenState()) + val screenState: LiveData = mutableScreenState + private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/state/ScreenState.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/state/ScreenState.kt new file mode 100644 index 0000000..0fb9dab --- /dev/null +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/state/ScreenState.kt @@ -0,0 +1,10 @@ +package software.ninetofive.assignment_tests.main.state + +import software.ninetofive.assignment_tests.main.SelectedScreen +import software.ninetofive.assignment_tests.main.ViewingOption + +data class ScreenState( + val selectedScreen: SelectedScreen = SelectedScreen.SCREEN_C, + val viewingOption: ViewingOption = ViewingOption.NOTHING, + val isValidDotChecked: Boolean = false +) diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 539d0a6..3759f54 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -6,6 +6,7 @@ import org.junit.jupiter.api.extension.ExtendWith import software.ninetofive.assignment_tests.InstantTaskExecutorExtension import software.ninetofive.assignment_tests.main.SelectedScreen.* import software.ninetofive.assignment_tests.main.ViewingOption.* +import software.ninetofive.assignment_tests.main.state.ScreenState import software.ninetofive.assignment_tests.utils.AppPreferences import software.ninetofive.assignment_tests.utils.InMemorySharedPreferences @@ -18,6 +19,7 @@ class LoadSettingsTest { @Test fun defaultSelectedScreen() { assertEquals(SCREEN_C, viewModel.selectedScreenLiveData.value) + assertEquals(ScreenState(selectedScreen = SCREEN_C), viewModel.screenState.value) } @Test From eb87b327d94f4d03d7f38c21582016192c117d2e Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:47:16 +0200 Subject: [PATCH 78/85] Deliver screen states to live data observers --- .../ninetofive/assignment_tests/main/MainViewModel.kt | 9 ++++++++- .../assignment_tests/main/LoadSettingsTest.kt | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index 9699574..ab12207 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -25,6 +25,7 @@ class MainViewModel @Inject constructor( fun selectScreen(selectedScreen: SelectedScreen) { appPreferences.setStartScreen(selectedScreen) mutableSelectedScreenLiveData.value = selectedScreen + mutableScreenState.update { it.copy(selectedScreen = selectedScreen) } } fun selectViewingOption(viewingOption: ViewingOption) { @@ -34,4 +35,10 @@ class MainViewModel @Inject constructor( fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() fun setValidDotVisibility(isActive: Boolean) = appPreferences.setShowValidDot(isActive) -} \ No newline at end of file + + private fun MutableLiveData.update(block: (T) -> T) { + val current = value + val updated = current?.let { block(current) } + value = updated + } +} diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 3759f54..4064021 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -25,12 +25,22 @@ class LoadSettingsTest { @Test fun selectedScreenSelection() { val selectedScreenObservedValues = mutableListOf() + val observedStateValues = mutableListOf() viewModel.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } + viewModel.screenState.observeForever { observedStateValues.add(it) } viewModel.selectScreen(SCREEN_B) viewModel.selectScreen(SCREEN_A) assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), selectedScreenObservedValues) + assertEquals( + listOf( + ScreenState(selectedScreen = SCREEN_C), + ScreenState(selectedScreen = SCREEN_B), + ScreenState(selectedScreen = SCREEN_A) + ), + observedStateValues + ) } @Test From a0f85dccff106feed91ce4e80adf100a8d895f8f Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:51:08 +0200 Subject: [PATCH 79/85] Deliver ViewingOption with the ScreenState --- .../ninetofive/assignment_tests/main/MainViewModel.kt | 1 + .../assignment_tests/main/LoadSettingsTest.kt | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index ab12207..4aaea2f 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -31,6 +31,7 @@ class MainViewModel @Inject constructor( fun selectViewingOption(viewingOption: ViewingOption) { appPreferences.setViewingOption(viewingOption) mutableViewingOptionLiveData.value = viewingOption + mutableScreenState.update { it.copy(viewingOption = viewingOption) } } fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 4064021..e0b776e 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -53,17 +53,28 @@ class LoadSettingsTest { @Test fun defaultViewingOption() { assertEquals(NOTHING, viewModel.viewingOptionLiveData.value) + assertEquals(ScreenState(viewingOption = NOTHING), viewModel.screenState.value) } @Test fun selectedViewSelection() { + val observedValues = mutableListOf() val viewingOptionObservedValues = mutableListOf() viewModel.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } + viewModel.screenState.observeForever { observedValues.add(it) } viewModel.selectViewingOption(SHOW_NAME) viewModel.selectViewingOption(DATE) assertEquals(listOf(NOTHING, SHOW_NAME, DATE), viewingOptionObservedValues) + assertEquals( + listOf( + ScreenState(viewingOption = NOTHING), + ScreenState(viewingOption = SHOW_NAME), + ScreenState(viewingOption = DATE) + ), + observedValues + ) } @Test From 1b819172fe2e069160aa6d45c715b622d445a2ee Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sat, 9 Apr 2022 23:53:07 +0200 Subject: [PATCH 80/85] Deliver validity dot option with screen state --- .../ninetofive/assignment_tests/main/MainViewModel.kt | 6 +++++- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index 4aaea2f..aed93e4 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -35,7 +35,11 @@ class MainViewModel @Inject constructor( } fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() - fun setValidDotVisibility(isActive: Boolean) = appPreferences.setShowValidDot(isActive) + + fun setValidDotVisibility(isActive: Boolean) { + appPreferences.setShowValidDot(isActive) + mutableScreenState.update { it.copy(isValidDotChecked = isActive) } + } private fun MutableLiveData.update(block: (T) -> T) { val current = value diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index e0b776e..6831116 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -87,6 +87,7 @@ class LoadSettingsTest { @Test fun defaultValidDotCheckedOption() { assertFalse(viewModel.isValidDotChecked()) + assertEquals(ScreenState(isValidDotChecked = false), viewModel.screenState.value) } @Test @@ -94,5 +95,6 @@ class LoadSettingsTest { viewModel.setValidDotVisibility(true) assertTrue(viewModel.isValidDotChecked()) assertEquals(true, appPreferences.shouldShowValidDot()) + assertEquals(ScreenState(isValidDotChecked = true), viewModel.screenState.value) } } \ No newline at end of file From 4a7384507923d4ad1fa17ed5a7d0f1bd83a1873d Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:01:00 +0200 Subject: [PATCH 81/85] Expose initial screen state loading --- .../assignment_tests/main/MainViewModel.kt | 7 +++++++ .../assignment_tests/main/LoadSettingsTest.kt | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index aed93e4..cd55e01 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -22,6 +22,13 @@ class MainViewModel @Inject constructor( private val mutableViewingOptionLiveData = MutableLiveData(appPreferences.getViewingOption()) val viewingOptionLiveData: LiveData = mutableViewingOptionLiveData + fun loadSettings() { + val selectedScreen = appPreferences.getStartScreen() + val viewingOption = appPreferences.getViewingOption() + val validDotVisible = appPreferences.shouldShowValidDot() + mutableScreenState.update { ScreenState(selectedScreen, viewingOption, validDotVisible) } + } + fun selectScreen(selectedScreen: SelectedScreen) { appPreferences.setStartScreen(selectedScreen) mutableSelectedScreenLiveData.value = selectedScreen diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 6831116..5de35a6 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -97,4 +97,20 @@ class LoadSettingsTest { assertEquals(true, appPreferences.shouldShowValidDot()) assertEquals(ScreenState(isValidDotChecked = true), viewModel.screenState.value) } + + @Test + fun initialSettingsLoad() { + appPreferences.run { + setStartScreen(SCREEN_B) + setViewingOption(DATE) + setShowValidDot(true) + } + + viewModel.loadSettings() + + assertEquals( + ScreenState(selectedScreen = SCREEN_B, viewingOption = DATE, isValidDotChecked = true), + viewModel.screenState.value + ) + } } \ No newline at end of file From 1e4f53da4ae4a98c6877430255eef8cf0e57e465 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:04:17 +0200 Subject: [PATCH 82/85] Migrate the activity to use the screen state live data --- .../assignment_tests/main/MainActivity.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index ea62a26..0cbed1d 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -19,7 +19,8 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_choose_start_screen) setupUI() - setupLiveDataObservers() + setupLiveDataObserver() + viewModel.loadSettings() } private fun setupUI() { @@ -69,18 +70,16 @@ class MainActivity : AppCompatActivity() { } private fun setupValidDotSwitch() { - valid_dot_switch.isChecked = viewModel.isValidDotChecked() valid_dot_switch.setOnCheckedChangeListener { _, isChecked -> viewModel.setValidDotVisibility(isChecked) } } - private fun setupLiveDataObservers() { - viewModel.selectedScreenLiveData.observe(this) { - onScreenSelected(it) - } - viewModel.viewingOptionLiveData.observe(this) { - onViewingOptionSelected(it) + private fun setupLiveDataObserver() { + viewModel.screenState.observe(this) { + onScreenSelected(it.selectedScreen) + onViewingOptionSelected(it.viewingOption) + onValidDotToggled(it.isValidDotChecked) } } @@ -95,4 +94,8 @@ class MainActivity : AppCompatActivity() { ViewingOption.DATE -> radio_show_date.isChecked = true ViewingOption.NOTHING -> radio_show_nothing.isChecked = true } + + private fun onValidDotToggled(validDotChecked: Boolean) { + valid_dot_switch.isChecked = validDotChecked + } } From cec646d3298c23e069d9bdcdf28ac25ed7425a8e Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:08:05 +0200 Subject: [PATCH 83/85] Remove unused code --- .../software/ninetofive/assignment_tests/main/MainViewModel.kt | 2 -- .../ninetofive/assignment_tests/main/LoadSettingsTest.kt | 2 -- 2 files changed, 4 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index cd55e01..19fb9c2 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -41,8 +41,6 @@ class MainViewModel @Inject constructor( mutableScreenState.update { it.copy(viewingOption = viewingOption) } } - fun isValidDotChecked(): Boolean = appPreferences.shouldShowValidDot() - fun setValidDotVisibility(isActive: Boolean) { appPreferences.setShowValidDot(isActive) mutableScreenState.update { it.copy(isValidDotChecked = isActive) } diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 5de35a6..51572b9 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -86,14 +86,12 @@ class LoadSettingsTest { @Test fun defaultValidDotCheckedOption() { - assertFalse(viewModel.isValidDotChecked()) assertEquals(ScreenState(isValidDotChecked = false), viewModel.screenState.value) } @Test fun validDotToggling() { viewModel.setValidDotVisibility(true) - assertTrue(viewModel.isValidDotChecked()) assertEquals(true, appPreferences.shouldShowValidDot()) assertEquals(ScreenState(isValidDotChecked = true), viewModel.screenState.value) } From fa37fd341b7497867c6f6929c2bcbec2390353be Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:09:53 +0200 Subject: [PATCH 84/85] Remove obsolete code --- .../ninetofive/assignment_tests/main/MainViewModel.kt | 8 -------- .../assignment_tests/main/LoadSettingsTest.kt | 10 +--------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt index 19fb9c2..3988ea0 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainViewModel.kt @@ -16,12 +16,6 @@ class MainViewModel @Inject constructor( private val mutableScreenState = MutableLiveData(ScreenState()) val screenState: LiveData = mutableScreenState - private val mutableSelectedScreenLiveData = MutableLiveData(appPreferences.getStartScreen()) - val selectedScreenLiveData: LiveData = mutableSelectedScreenLiveData - - private val mutableViewingOptionLiveData = MutableLiveData(appPreferences.getViewingOption()) - val viewingOptionLiveData: LiveData = mutableViewingOptionLiveData - fun loadSettings() { val selectedScreen = appPreferences.getStartScreen() val viewingOption = appPreferences.getViewingOption() @@ -31,13 +25,11 @@ class MainViewModel @Inject constructor( fun selectScreen(selectedScreen: SelectedScreen) { appPreferences.setStartScreen(selectedScreen) - mutableSelectedScreenLiveData.value = selectedScreen mutableScreenState.update { it.copy(selectedScreen = selectedScreen) } } fun selectViewingOption(viewingOption: ViewingOption) { appPreferences.setViewingOption(viewingOption) - mutableViewingOptionLiveData.value = viewingOption mutableScreenState.update { it.copy(viewingOption = viewingOption) } } diff --git a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt index 51572b9..7ad4924 100644 --- a/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt +++ b/app/src/test/java/software/ninetofive/assignment_tests/main/LoadSettingsTest.kt @@ -1,6 +1,6 @@ package software.ninetofive.assignment_tests.main -import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import software.ninetofive.assignment_tests.InstantTaskExecutorExtension @@ -18,21 +18,17 @@ class LoadSettingsTest { @Test fun defaultSelectedScreen() { - assertEquals(SCREEN_C, viewModel.selectedScreenLiveData.value) assertEquals(ScreenState(selectedScreen = SCREEN_C), viewModel.screenState.value) } @Test fun selectedScreenSelection() { - val selectedScreenObservedValues = mutableListOf() val observedStateValues = mutableListOf() - viewModel.selectedScreenLiveData.observeForever { selectedScreenObservedValues.add(it) } viewModel.screenState.observeForever { observedStateValues.add(it) } viewModel.selectScreen(SCREEN_B) viewModel.selectScreen(SCREEN_A) - assertEquals(listOf(SCREEN_C, SCREEN_B, SCREEN_A), selectedScreenObservedValues) assertEquals( listOf( ScreenState(selectedScreen = SCREEN_C), @@ -52,21 +48,17 @@ class LoadSettingsTest { @Test fun defaultViewingOption() { - assertEquals(NOTHING, viewModel.viewingOptionLiveData.value) assertEquals(ScreenState(viewingOption = NOTHING), viewModel.screenState.value) } @Test fun selectedViewSelection() { val observedValues = mutableListOf() - val viewingOptionObservedValues = mutableListOf() - viewModel.viewingOptionLiveData.observeForever { viewingOptionObservedValues.add(it) } viewModel.screenState.observeForever { observedValues.add(it) } viewModel.selectViewingOption(SHOW_NAME) viewModel.selectViewingOption(DATE) - assertEquals(listOf(NOTHING, SHOW_NAME, DATE), viewingOptionObservedValues) assertEquals( listOf( ScreenState(viewingOption = NOTHING), From 93e265c71d787a82a8b92e5316eeeee48eb919c4 Mon Sep 17 00:00:00 2001 From: Jov Mit Date: Sun, 10 Apr 2022 00:18:43 +0200 Subject: [PATCH 85/85] Migrate to ViewBinding --- app/build.gradle | 4 ++ .../assignment_tests/main/MainActivity.kt | 41 +++++++++---------- .../layout/activity_choose_start_screen.xml | 8 +++- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c7f0c43..75de0b3 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,6 +27,10 @@ android { } } + buildFeatures { + viewBinding true + } + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 diff --git a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt index 0cbed1d..fff4295 100644 --- a/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt +++ b/app/src/main/java/software/ninetofive/assignment_tests/main/MainActivity.kt @@ -5,19 +5,18 @@ import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import dagger.hilt.android.AndroidEntryPoint -import kotlinx.android.synthetic.main.activity_choose_start_screen.* -import kotlinx.android.synthetic.main.choose_start_screen.* -import kotlinx.android.synthetic.main.choose_viewing_options.* import software.ninetofive.assignment_tests.R +import software.ninetofive.assignment_tests.databinding.ActivityChooseStartScreenBinding @AndroidEntryPoint class MainActivity : AppCompatActivity() { private val viewModel by viewModels() + private val binding by lazy { ActivityChooseStartScreenBinding.inflate(layoutInflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_choose_start_screen) + setContentView(binding.root) setupUI() setupLiveDataObserver() viewModel.loadSettings() @@ -32,7 +31,7 @@ class MainActivity : AppCompatActivity() { } private fun setupToolbar() { - setSupportActionBar(location_choose_start_screen) + setSupportActionBar(binding.locationChooseStartScreen) supportActionBar?.apply { setTitle(R.string.choose_start_screen_title) } @@ -40,17 +39,17 @@ class MainActivity : AppCompatActivity() { private fun forceRadioButtonsToCorrectPosition() { arrayOf( - radio_screen_a, - radio_screen_b, - radio_screen_c, - radio_show_name, - radio_show_date, - radio_show_nothing + binding.chooseStartScreen.radioScreenA, + binding.chooseStartScreen.radioScreenB, + binding.chooseStartScreen.radioScreenC, + binding.chooseViewingOptions.radioShowName, + binding.chooseViewingOptions.radioShowDate, + binding.chooseViewingOptions.radioShowNothing ).forEach { ViewCompat.setLayoutDirection(it, ViewCompat.LAYOUT_DIRECTION_RTL) } } private fun setupSelectStartScreenOptions() { - radio_group_select_screen.setOnCheckedChangeListener { _, id -> + binding.chooseStartScreen.radioGroupSelectScreen.setOnCheckedChangeListener { _, id -> when (id) { R.id.radio_screen_a -> SelectedScreen.SCREEN_A R.id.radio_screen_b -> SelectedScreen.SCREEN_C @@ -60,7 +59,7 @@ class MainActivity : AppCompatActivity() { } private fun setupViewingOptions() { - radio_group_viewing_options.setOnCheckedChangeListener { _, id -> + binding.chooseViewingOptions.radioGroupViewingOptions.setOnCheckedChangeListener { _, id -> when (id) { R.id.radio_show_name -> ViewingOption.SHOW_NAME R.id.radio_show_date -> ViewingOption.DATE @@ -70,7 +69,7 @@ class MainActivity : AppCompatActivity() { } private fun setupValidDotSwitch() { - valid_dot_switch.setOnCheckedChangeListener { _, isChecked -> + binding.validDotSwitch.setOnCheckedChangeListener { _, isChecked -> viewModel.setValidDotVisibility(isChecked) } } @@ -84,18 +83,18 @@ class MainActivity : AppCompatActivity() { } private fun onScreenSelected(selectedScreen: SelectedScreen) = when (selectedScreen) { - SelectedScreen.SCREEN_A -> radio_screen_a.isChecked = true - SelectedScreen.SCREEN_C -> radio_screen_b.isChecked = true - SelectedScreen.SCREEN_B -> radio_screen_c.isChecked = true + SelectedScreen.SCREEN_A -> binding.chooseStartScreen.radioScreenA.isChecked = true + SelectedScreen.SCREEN_C -> binding.chooseStartScreen.radioScreenB.isChecked = true + SelectedScreen.SCREEN_B -> binding.chooseStartScreen.radioScreenC.isChecked = true } private fun onViewingOptionSelected(viewingOption: ViewingOption) = when (viewingOption) { - ViewingOption.SHOW_NAME -> radio_show_name.isChecked = true - ViewingOption.DATE -> radio_show_date.isChecked = true - ViewingOption.NOTHING -> radio_show_nothing.isChecked = true + ViewingOption.SHOW_NAME -> binding.chooseViewingOptions.radioShowName.isChecked = true + ViewingOption.DATE -> binding.chooseViewingOptions.radioShowDate.isChecked = true + ViewingOption.NOTHING -> binding.chooseViewingOptions.radioShowNothing.isChecked = true } private fun onValidDotToggled(validDotChecked: Boolean) { - valid_dot_switch.isChecked = validDotChecked + binding.validDotSwitch.isChecked = validDotChecked } } diff --git a/app/src/main/res/layout/activity_choose_start_screen.xml b/app/src/main/res/layout/activity_choose_start_screen.xml index 529daf7..4ac5c94 100644 --- a/app/src/main/res/layout/activity_choose_start_screen.xml +++ b/app/src/main/res/layout/activity_choose_start_screen.xml @@ -28,7 +28,9 @@ android:gravity="center" android:text="@string/choose_start_screen_header" /> - + - +