From 119f2aeb3c61cd0005503e126efd83700a70772c Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Thu, 25 Jun 2020 18:17:33 -0400 Subject: [PATCH 1/3] Adjust Editor theme store to return null when fetching an empty set of values --- .../MockedStack_EditorThemeStoreTest.kt | 35 +++++++++++++++++-- .../fluxc/persistence/EditorThemeSqlUtils.kt | 8 ++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt b/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt index 2a2809b709..2ce7e58d89 100644 --- a/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt +++ b/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt @@ -1,5 +1,6 @@ package org.wordpress.android.fluxc.mocked +import android.os.Bundle import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode import org.junit.Assert @@ -46,9 +47,17 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { // See onEditorThemeChanged for the latch's countdown to fire. Assert.assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS)) + + // Validate Callback assertNotEmpty(editorTheme) + + // Validate Cache val cachedTheme = editorThemeStore.getEditorThemeForSite(site) assertNotEmpty(cachedTheme) + + // Validate Bundle + val themeBundle = editorTheme!!.themeSupport.toBundle() + assertNotEmpty(themeBundle) } @Test @@ -58,9 +67,17 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { // See onEditorThemeChanged for the latch's countdown to fire. Assert.assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS)) + + // Validate Callback assertEmpty(editorTheme) + + // Validate Cache val cachedTheme = editorThemeStore.getEditorThemeForSite(site) assertEmpty(cachedTheme) + + // Validate Bundle + val themeBundle = editorTheme!!.themeSupport.toBundle() + assertEmpty(themeBundle) } private fun assertNotEmpty(theme: EditorTheme?) { @@ -69,8 +86,22 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { } private fun assertEmpty(theme: EditorTheme?) { - Assert.assertTrue(theme?.themeSupport?.colors.isNullOrEmpty()) - Assert.assertTrue(theme?.themeSupport?.gradients.isNullOrEmpty()) + Assert.assertTrue(theme?.themeSupport?.colors == null) + Assert.assertTrue(theme?.themeSupport?.gradients == null) + } + + private fun assertEmpty(theme: Bundle) { + val colors = theme.getSerializable("colors") + val gradients = theme.getSerializable("gradients") + Assert.assertTrue(colors == null) + Assert.assertTrue(gradients == null) + } + + private fun assertNotEmpty(theme: Bundle) { + val colors = theme.getSerializable("colors") as ArrayList<*> + val gradients = theme.getSerializable("gradients") as ArrayList<*> + Assert.assertFalse(colors.isNullOrEmpty()) + Assert.assertFalse(gradients.isNullOrEmpty()) } @Suppress("unused") diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt index cbaebca9d7..f910ae842f 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt @@ -92,11 +92,11 @@ class EditorThemeSqlUtils { override fun getId() = mId fun toEditorTheme( - storedColors: List?, - storedGradients: List? + storedColors: List, + storedGradients: List ): EditorTheme { - val colors = storedColors?.mapNotNull { it.toEditorThemeElement() } - val gradients = storedGradients?.mapNotNull { it.toEditorThemeElement() } + val colors = if (storedColors.count() > 0) storedColors.mapNotNull { it.toEditorThemeElement() } else null + val gradients = if (storedColors.count() > 0) storedGradients.mapNotNull { it.toEditorThemeElement() } else null val editorThemeSupport = EditorThemeSupport(colors, gradients) return EditorTheme(editorThemeSupport, stylesheet, version) From 20782cf123eda3e81d4812cd4dbcc360a4b6551a Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Thu, 25 Jun 2020 18:29:10 -0400 Subject: [PATCH 2/3] Fix lint issue --- .../android/fluxc/persistence/EditorThemeSqlUtils.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt index f910ae842f..f0ad4b3891 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt @@ -96,7 +96,13 @@ class EditorThemeSqlUtils { storedGradients: List ): EditorTheme { val colors = if (storedColors.count() > 0) storedColors.mapNotNull { it.toEditorThemeElement() } else null - val gradients = if (storedColors.count() > 0) storedGradients.mapNotNull { it.toEditorThemeElement() } else null + val gradients: List? + if (storedColors.count() > 0) { + gradients = storedGradients.mapNotNull { it.toEditorThemeElement() } + } else { + gradients = null + } + val editorThemeSupport = EditorThemeSupport(colors, gradients) return EditorTheme(editorThemeSupport, stylesheet, version) From 3d30383a186ea234061e64b704113a78fe0d34d4 Mon Sep 17 00:00:00 2001 From: Chip Snyder Date: Fri, 26 Jun 2020 11:29:31 -0400 Subject: [PATCH 3/3] Allow null on stored elements and fix typo when checking gradients --- .../fluxc/persistence/EditorThemeSqlUtils.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt index f0ad4b3891..c9e628a1e2 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt @@ -92,15 +92,17 @@ class EditorThemeSqlUtils { override fun getId() = mId fun toEditorTheme( - storedColors: List, - storedGradients: List + storedColors: List?, + storedGradients: List? ): EditorTheme { - val colors = if (storedColors.count() > 0) storedColors.mapNotNull { it.toEditorThemeElement() } else null - val gradients: List? - if (storedColors.count() > 0) { + var colors: List? = null + if (storedColors != null && storedColors.count() > 0) { + colors = storedColors.mapNotNull { it.toEditorThemeElement() } + } + + var gradients: List? = null + if (storedGradients != null && storedGradients.count() > 0) { gradients = storedGradients.mapNotNull { it.toEditorThemeElement() } - } else { - gradients = null } val editorThemeSupport = EditorThemeSupport(colors, gradients)