diff options
Diffstat (limited to 'sample')
9 files changed, 164 insertions, 47 deletions
diff --git a/sample/build.gradle b/sample/build.gradle index 0ed7818..77165e8 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -26,7 +26,7 @@ android { versionName androidGitVersion.name() versionCode androidGitVersion.code() multiDexEnabled true - testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "ca.allanwang.kau.sample.SampleTestRunner" } viewBinding { enabled = true @@ -137,9 +137,11 @@ dependencies { implementation project(':mediapicker') implementation Dependencies.materialDialog("input") + implementation Dependencies.koin testImplementation Dependencies.kotlinTest testImplementation Dependencies.junit + testImplementation Dependencies.koinTest androidTestImplementation Dependencies.kotlinTest androidTestImplementation Dependencies.espresso @@ -147,4 +149,5 @@ dependencies { androidTestImplementation Dependencies.espresso("contrib") androidTestImplementation Dependencies.testRules androidTestImplementation Dependencies.testRunner + androidTestImplementation Dependencies.koinTest } diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt index 72199cf..156b66f 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/KPrefViewTest.kt @@ -34,7 +34,14 @@ import org.hamcrest.Matchers.allOf import org.hamcrest.Matchers.instanceOf import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain +import org.junit.rules.TestRule import org.junit.runner.RunWith +import org.koin.test.KoinTest +import org.koin.test.inject +import kotlin.test.BeforeTest +import kotlin.test.assertFalse +import kotlin.test.assertTrue /** * Created by Allan Wang on 21/12/2018. @@ -43,11 +50,20 @@ import org.junit.runner.RunWith */ @RunWith(AndroidJUnit4::class) @MediumTest -class KPrefViewTest { +class KPrefViewTest : KoinTest { - @get:Rule val activity: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java) + @get:Rule + val rule: TestRule = RuleChain.outerRule(SampleTestRule()).around(activity) + + private val pref: KPrefSample by inject() + + @BeforeTest + fun before() { + pref.reset() + } + fun verifyCheck(checked: Boolean): Matcher<View> { return object : BoundedMatcher<View, View>(View::class.java) { @@ -93,11 +109,11 @@ class KPrefViewTest { fun basicCheckboxToggle() { val checkbox1 = onCheckboxView(withChild(withText(R.string.checkbox_1))) - val initiallyChecked = KPrefSample.check1 + assertTrue(pref.check1, "check1 not normalized") - checkbox1.verifyCheck("checkbox1 init", initiallyChecked) + checkbox1.verifyCheck("checkbox1 init", true) checkbox1.perform(click()) - checkbox1.verifyCheck("checkbox1 after click", !initiallyChecked) + checkbox1.verifyCheck("checkbox1 after click", false) } /** @@ -107,23 +123,24 @@ class KPrefViewTest { fun dependentCheckboxToggle() { val checkbox2 = onCheckboxView(withChild(withText(R.string.checkbox_2))) val checkbox3 = - onCheckboxView(withChild(withText(R.string.checkbox_3)), withChild(withText(R.string.desc_dependent))) + onCheckboxView( + withChild(withText(R.string.checkbox_3)), + withChild(withText(R.string.desc_dependent)) + ) - // normalize so that both are checked - if (!KPrefSample.check2) - checkbox2.perform(click()) - if (!KPrefSample.check3) - checkbox3.perform(click()) + assertFalse(pref.check2, "check2 not normalized") + assertFalse(pref.check3, "check3 not normalized") - checkbox3.verifyCheck("checkbox3 init", true, true) + checkbox2.verifyCheck("checkbox2 init", checked = false, enabled = true) + checkbox3.verifyCheck("checkbox3 init", checked = false, enabled = false) checkbox3.perform(click()) - checkbox3.verifyCheck("checkbox3 after click", false, true) + checkbox3.verifyCheck("checkbox3 after disabled click", checked = false, enabled = false) checkbox2.perform(click()) - checkbox2.verifyCheck("checkbox2 after click", false, true) - checkbox3.verifyCheck("checkbox3 after checkbox2 click", false, false) + checkbox2.verifyCheck("checkbox2 after click", checked = true, enabled = true) + checkbox3.verifyCheck("checkbox3 after checkbox2 click", checked = false, enabled = true) checkbox3.perform(click()) - checkbox3.verifyCheck("checkbox3 after disabled click", false, false) + checkbox3.verifyCheck("checkbox3 after enabled click", checked = true, enabled = true) } } diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt new file mode 100644 index 0000000..8705fbc --- /dev/null +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/SampleTestApp.kt @@ -0,0 +1,60 @@ +package ca.allanwang.kau.sample + +import android.app.Application +import android.content.Context +import androidx.test.runner.AndroidJUnitRunner +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryInMemory +import ca.allanwang.kau.logging.KL +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement +import org.koin.android.ext.koin.androidContext +import org.koin.android.ext.koin.androidLogger +import org.koin.core.KoinComponent +import org.koin.core.context.startKoin +import org.koin.core.get +import org.koin.dsl.module + +class SampleTestRunner : AndroidJUnitRunner() { + override fun newApplication( + cl: ClassLoader?, + className: String?, + context: Context? + ): Application { + return super.newApplication(cl, SampleTestApp::class.java.name, context) + } +} + +class SampleTestRule : TestRule { + override fun apply(base: Statement, description: Description): Statement = + object : Statement(), KoinComponent { + override fun evaluate() { + + // Reset prefs + val pref: KPrefSample = get() + pref.reset() + + base.evaluate() + } + } +} + +class SampleTestApp : Application() { + override fun onCreate() { + super.onCreate() + startKoin { + if (BuildConfig.DEBUG) { + androidLogger() + } + androidContext(this@SampleTestApp) + modules(listOf(prefFactoryModule(), SampleApp.prefModule())) + } + } + + fun prefFactoryModule() = module { + single<KPrefFactory> { + KPrefFactoryInMemory + } + } +}
\ No newline at end of file diff --git a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt index 09ad00a..2a6f0a9 100644 --- a/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt +++ b/sample/src/androidTest/kotlin/ca/allanwang/kau/sample/utils/EspressoUtils.kt @@ -15,6 +15,8 @@ */ package ca.allanwang.kau.sample.utils +import android.content.Context +import androidx.test.platform.app.InstrumentationRegistry import org.hamcrest.BaseMatcher import org.hamcrest.Description import org.hamcrest.Matcher diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt index a472f09..bd1039b 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/AnimActivity.kt @@ -28,6 +28,7 @@ import ca.allanwang.kau.utils.fullLinearRecycler import ca.allanwang.kau.utils.startActivity import ca.allanwang.kau.utils.withAlpha import ca.allanwang.kau.utils.withSlideOut +import org.koin.android.ext.android.inject /** * Created by Allan Wang on 2017-06-12. @@ -37,12 +38,14 @@ import ca.allanwang.kau.utils.withSlideOut */ class AnimActivity : KauBaseActivity() { + private val pref: KPrefSample by inject() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val adapter = SingleFastAdapter() setContentView(fullLinearRecycler(adapter).apply { setBackgroundColor( - KPrefSample.bgColor.withAlpha(255) + pref.bgColor.withAlpha(255) ) }) diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt index 80a75bf..97e8075 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/KPrefSample.kt @@ -17,11 +17,12 @@ package ca.allanwang.kau.sample import android.graphics.Color import ca.allanwang.kau.kpref.KPref +import ca.allanwang.kau.kpref.KPrefFactory /** * Created by Allan Wang on 2017-06-07. */ -object KPrefSample : KPref() { +class KPrefSample(factory: KPrefFactory) : KPref("pref_sample", factory = factory) { var version: Int by kpref("version", -1) var textColor: Int by kpref("TEXT_COLOR", Color.WHITE) var accentColor: Int by kpref("ACCENT_COLOR", 0xffff8900.toInt()) diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt index 4fe941b..4cf632f 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/MainActivity.kt @@ -40,6 +40,7 @@ import ca.allanwang.kau.utils.withSceneTransitionAnimation import ca.allanwang.kau.xml.showChangelog import com.afollestad.materialdialogs.input.input import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial +import org.koin.android.ext.android.inject class MainActivity : KPrefActivity() { @@ -101,9 +102,11 @@ class MainActivity : KPrefActivity() { const val REQUEST_MEDIA = 27 } + private val pref: KPrefSample by inject() + override fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit = { - textColor = KPrefSample::textColor - accentColor = KPrefSample::accentColor + textColor = pref::textColor + accentColor = pref::accentColor } override fun onCreateKPrefs(savedInstanceState: Bundle?): KPrefAdapterBuilder.() -> Unit = { @@ -113,7 +116,7 @@ class MainActivity : KPrefActivity() { /** * This is how the setup looks like with all the proper tags */ - checkbox(title = R.string.checkbox_1, getter = KPrefSample::check1, setter = { KPrefSample.check1 = it }, + checkbox(title = R.string.checkbox_1, getter = pref::check1, setter = { pref.check1 = it }, builder = { descRes = R.string.desc }) @@ -123,25 +126,25 @@ class MainActivity : KPrefActivity() { */ checkbox( R.string.checkbox_2, - KPrefSample::check2, - { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }) + pref::check2, + { pref.check2 = it; reloadByTitle(R.string.checkbox_3) }) /** * Since the builder is the last argument and is a lambda, we may write the setup cleanly like so: */ - checkbox(R.string.checkbox_3, KPrefSample::check3, { KPrefSample.check3 = it }) { + checkbox(R.string.checkbox_3, pref::check3, { pref.check3 = it }) { descRes = R.string.desc_dependent - enabler = { KPrefSample.check2 } + enabler = { pref.check2 } onDisabledClick = { itemView.context.toast("I am still disabled") } } - colorPicker(R.string.text_color, KPrefSample::textColor, { KPrefSample.textColor = it; reload() }) { + colorPicker(R.string.text_color, pref::textColor, { pref.textColor = it; reload() }) { descRes = R.string.color_custom allowCustom = true } - colorPicker(R.string.accent_color, KPrefSample::accentColor, { - KPrefSample.accentColor = it + colorPicker(R.string.accent_color, pref::accentColor, { + pref.accentColor = it reload() this@MainActivity.navigationBarColor = it toolbarCanvas.ripple(it, RippleCanvas.MIDDLE, RippleCanvas.END, duration = 500L) @@ -150,8 +153,8 @@ class MainActivity : KPrefActivity() { allowCustom = false } - colorPicker(R.string.background_color, KPrefSample::bgColor, { - KPrefSample.bgColor = it; bgCanvas.ripple(it, duration = 500L) + colorPicker(R.string.background_color, pref::bgColor, { + pref.bgColor = it; bgCanvas.ripple(it, duration = 500L) }) { iicon = GoogleMaterial.Icon.gmd_colorize descRes = R.string.color_custom_alpha @@ -159,7 +162,7 @@ class MainActivity : KPrefActivity() { allowCustom = true } - text(R.string.text, KPrefSample::text, { KPrefSample.text = it }) { + text(R.string.text, pref::text, { pref.text = it }) { descRes = R.string.text_desc onClick = { itemView.context.materialDialog { @@ -171,7 +174,7 @@ class MainActivity : KPrefActivity() { } } - seekbar(R.string.seekbar, KPrefSample::seekbar, { KPrefSample.seekbar = it }) { + seekbar(R.string.seekbar, pref::seekbar, { pref.seekbar = it }) { descRes = R.string.kau_lorem_ipsum increments = 5 textViewConfigs = { @@ -215,30 +218,30 @@ class MainActivity : KPrefActivity() { */ checkbox( R.string.checkbox_2, - KPrefSample::check2, - { KPrefSample.check2 = it; reloadByTitle(R.string.checkbox_3) }) { + pref::check2, + { pref.check2 = it; reloadByTitle(R.string.checkbox_3) }) { titleFun = { R.string.checkbox_3 } descRes = R.string.kau_lorem_ipsum } - text(R.string.text, KPrefSample::text, { KPrefSample.text = it }) { + text(R.string.text, pref::text, { pref.text = it }) { descRes = R.string.kau_lorem_ipsum textGetter = { string(R.string.kau_lorem_ipsum) } } - timePicker(R.string.time, KPrefSample::time12, { KPrefSample.time12 = it }) { + timePicker(R.string.time, pref::time12, { pref.time12 = it }) { descRes = R.string.time_desc_12 use24HourFormat = false } - timePicker(R.string.time, KPrefSample::time24, { KPrefSample.time24 = it }) { + timePicker(R.string.time, pref::time24, { pref.time24 = it }) { descRes = R.string.time_desc_24 use24HourFormat = true } } fun subPrefs(): KPrefAdapterBuilder.() -> Unit = { - text(R.string.text, { KPrefSample.text }, { KPrefSample.text = it }) { + text(R.string.text, { pref.text }, { pref.text = it }) { descRes = R.string.text_desc onClick = { itemView.context.materialDialog { @@ -254,11 +257,11 @@ class MainActivity : KPrefActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - bgCanvas.set(KPrefSample.bgColor) - toolbarCanvas.set(KPrefSample.accentColor) - this.navigationBarColor = KPrefSample.accentColor - if (KPrefSample.version < BuildConfig.VERSION_CODE) { - KPrefSample.version = BuildConfig.VERSION_CODE + bgCanvas.set(pref.bgColor) + toolbarCanvas.set(pref.accentColor) + this.navigationBarColor = pref.accentColor + if (pref.version < BuildConfig.VERSION_CODE) { + pref.version = BuildConfig.VERSION_CODE if (!BuildConfig.DEBUG) showChangelog(R.xml.kau_changelog) } diff --git a/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt b/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt index 6e6d718..2537090 100644 --- a/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt +++ b/sample/src/main/kotlin/ca/allanwang/kau/sample/SampleApp.kt @@ -16,6 +16,13 @@ package ca.allanwang.kau.sample import android.app.Application +import ca.allanwang.kau.kpref.KPrefFactory +import ca.allanwang.kau.kpref.KPrefFactoryAndroid +import org.koin.android.ext.koin.androidContext +import org.koin.android.ext.koin.androidLogger +import org.koin.core.context.startKoin +import org.koin.core.module.Module +import org.koin.dsl.module /** * Created by Allan Wang on 2017-06-08. @@ -23,6 +30,27 @@ import android.app.Application class SampleApp : Application() { override fun onCreate() { super.onCreate() - KPrefSample.initialize(this, "pref_sample") + + startKoin { + if (BuildConfig.DEBUG) { + androidLogger() + } + androidContext(this@SampleApp) + modules(listOf(prefFactoryModule(), prefModule())) + } + } + + companion object { + fun prefFactoryModule(): Module = module { + single<KPrefFactory> { + KPrefFactoryAndroid(get()) + } + } + + fun prefModule(): Module = module { + single { + KPrefSample(get()) + } + } } } diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index d61fe63..55adabc 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -6,9 +6,9 @@ <item text="" /> --> - <version title="v5.2.1" /> + <version title="v5.3.0" /> <item text=":color: Allow option to disable selected ring" /> - <item text="" /> + <item text=":core: Breaking KPref changes; see migration" /> <item text="" /> <item text="" /> |