Fix SyncAnalyzer tests

This commit is contained in:
Shinokuni 2024-07-28 22:36:41 +02:00
parent cafb46c727
commit f143e282c8
3 changed files with 55 additions and 53 deletions

View File

@ -19,6 +19,7 @@ import org.junit.Assert.assertTrue
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import kotlin.test.assertNull
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class SyncAnalyzerTest { class SyncAnalyzerTest {
@ -27,6 +28,9 @@ class SyncAnalyzerTest {
private lateinit var syncAnalyzer: SyncAnalyzer private lateinit var syncAnalyzer: SyncAnalyzer
private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
private val nullContentException =
NullPointerException("Notification content shouldn't be null")
private val account1 = Account( private val account1 = Account(
accountName = "test account 1", accountName = "test account 1",
accountType = AccountType.FRESHRSS, accountType = AccountType.FRESHRSS,
@ -87,12 +91,13 @@ class SyncAnalyzerTest {
database.itemDao().insert(item) database.itemDao().insert(item)
val syncResult = SyncResult(items = listOf(item)) val syncResult = SyncResult(items = listOf(item))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))
assertEquals("caseOneElementEveryWhere", notificationContent.content) syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))?.let { content ->
assertEquals("feed 1", notificationContent.title) assertEquals("caseOneElementEveryWhere", content.text)
assertTrue(notificationContent.largeIcon != null) assertEquals("feed 1", content.title)
assertTrue(notificationContent.accountId > 0) assertTrue(content.largeIcon != null)
assertTrue(content.accountId > 0)
} ?: throw nullContentException
database.itemDao().delete(item) database.itemDao().delete(item)
} }
@ -100,14 +105,14 @@ class SyncAnalyzerTest {
@Test @Test
fun testTwoItemsOneFeed() = runTest { fun testTwoItemsOneFeed() = runTest {
val item = Item(title = "caseTwoItemsOneFeed", feedId = 1) val item = Item(title = "caseTwoItemsOneFeed", feedId = 1)
val syncResult = SyncResult(items = listOf(item, item, item)) val syncResult = SyncResult(items = listOf(item, item, item))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))
assertEquals(context.getString(R.string.new_items, 3), notificationContent.content) syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))?.let { content ->
assertEquals("feed 1", notificationContent.title) assertEquals(context.getString(R.string.new_items, 3), content.text)
assertTrue(notificationContent.largeIcon != null) assertEquals("feed 1", content.title)
assertTrue(notificationContent.accountId > 0) assertTrue(content.largeIcon != null)
assertTrue(content.accountId > 0)
} ?: throw nullContentException
} }
@Test @Test
@ -116,12 +121,13 @@ class SyncAnalyzerTest {
val item2 = Item(feedId = 3) val item2 = Item(feedId = 3)
val syncResult = SyncResult(items = listOf(item, item2)) val syncResult = SyncResult(items = listOf(item, item2))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))
assertEquals(context.getString(R.string.new_items, 2), notificationContent.content) syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))?.let { content ->
assertEquals(account1.accountName, notificationContent.title) assertEquals(context.getString(R.string.new_items, 2), content.text)
assertTrue(notificationContent.largeIcon != null) assertEquals(account1.accountName, content.title)
assertTrue(notificationContent.accountId > 0) assertTrue(content.largeIcon != null)
assertTrue(content.accountId > 0)
} ?: throw nullContentException
} }
@Test @Test
@ -133,9 +139,9 @@ class SyncAnalyzerTest {
val syncResult2 = SyncResult(items = listOf(item, item2)) val syncResult2 = SyncResult(items = listOf(item, item2))
val syncResults = mapOf(account1 to syncResult, account3 to syncResult2) val syncResults = mapOf(account1 to syncResult, account3 to syncResult2)
val notificationContent = syncAnalyzer.getNotificationContent(syncResults) syncAnalyzer.getNotificationContent(syncResults)?.let { content ->
assertEquals(context.getString(R.string.new_items, 4), content.title)
assertEquals(context.getString(R.string.new_items, 4), notificationContent.title) } ?: throw nullContentException
} }
@Test @Test
@ -144,11 +150,9 @@ class SyncAnalyzerTest {
val item2 = Item(title = "testAccountNotificationsDisabled2", feedId = 1) val item2 = Item(title = "testAccountNotificationsDisabled2", feedId = 1)
val syncResult = SyncResult(items = listOf(item1, item2)) val syncResult = SyncResult(items = listOf(item1, item2))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account2 to syncResult))
assert(notificationContent.title == null) val content = syncAnalyzer.getNotificationContent(mapOf(account2 to syncResult))
assert(notificationContent.content == null) assertNull(content)
assert(notificationContent.largeIcon == null)
} }
@Test @Test
@ -157,11 +161,8 @@ class SyncAnalyzerTest {
val item2 = Item(title = "testAccountNotificationsDisabled2", feedId = 2) val item2 = Item(title = "testAccountNotificationsDisabled2", feedId = 2)
val syncResult = SyncResult(items = listOf(item1, item2)) val syncResult = SyncResult(items = listOf(item1, item2))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult)) val content = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))
assertNull(content)
assert(notificationContent.title == null)
assert(notificationContent.content == null)
assert(notificationContent.largeIcon == null)
} }
@Test @Test
@ -190,18 +191,18 @@ class SyncAnalyzerTest {
val syncResults = mapOf(account1 to syncResult1, account2 to syncResult2) val syncResults = mapOf(account1 to syncResult1, account2 to syncResult2)
val notificationContent = syncAnalyzer.getNotificationContent(syncResults) syncAnalyzer.getNotificationContent(syncResults)?.let { content ->
assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", content.text)
assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notificationContent.content) assertEquals("feed 1", content.title)
assertEquals("feed 1", notificationContent.title) assertTrue(content.largeIcon != null)
assertTrue(notificationContent.largeIcon != null) assertTrue(content.item != null)
assertTrue(notificationContent.item != null) } ?: throw nullContentException
database.itemDao().delete(item1) database.itemDao().delete(item1)
} }
@Test @Test
fun testTwoAccountsWithOneFeedNotificationEnabled() = runTest{ fun testTwoAccountsWithOneFeedNotificationEnabled() = runTest {
val item1 = Item( val item1 = Item(
title = "testTwoAccountsWithOneAccountNotificationsEnabled", title = "testTwoAccountsWithOneAccountNotificationsEnabled",
feedId = 1, feedId = 1,
@ -225,12 +226,13 @@ class SyncAnalyzerTest {
val syncResult2 = SyncResult(items = listOf(item2, item3)) val syncResult2 = SyncResult(items = listOf(item2, item3))
val syncResults = mapOf(account1 to syncResult1, account2 to syncResult2) val syncResults = mapOf(account1 to syncResult1, account2 to syncResult2)
val notificationContent = syncAnalyzer.getNotificationContent(syncResults)
assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notificationContent.content) syncAnalyzer.getNotificationContent(syncResults)?.let { content ->
assertEquals("feed 1", notificationContent.title) assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", content.text)
assertTrue(notificationContent.largeIcon != null) assertEquals("feed 1", content.title)
assertTrue(notificationContent.item != null) assertTrue(content.largeIcon != null)
assertTrue(content.item != null)
} ?: throw nullContentException
database.itemDao().delete(item1) database.itemDao().delete(item1)
} }
@ -258,13 +260,13 @@ class SyncAnalyzerTest {
database.itemDao().insert(item1) database.itemDao().insert(item1)
val syncResult = SyncResult(items = listOf(item1, item2, item3)) val syncResult = SyncResult(items = listOf(item1, item2, item3))
val notificationContent = syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult)) syncAnalyzer.getNotificationContent(mapOf(account1 to syncResult))?.let { content ->
assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", content.text)
assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notificationContent.content) assertEquals("feed 1", content.title)
assertEquals("feed 1", notificationContent.title) assertTrue(content.largeIcon != null)
assertTrue(notificationContent.largeIcon != null) assertTrue(content.item != null)
assertTrue(notificationContent.item != null) assertTrue(content.accountId > 0)
assertTrue(notificationContent.accountId > 0) } ?: throw nullContentException
database.itemDao().delete(item1) database.itemDao().delete(item1)
} }

View File

@ -16,7 +16,7 @@ import org.koin.core.component.KoinComponent
data class NotificationContent( data class NotificationContent(
val title: String? = null, val title: String? = null,
val content: String? = null, val text: String? = null,
val largeIcon: Bitmap? = null, val largeIcon: Bitmap? = null,
val item: Item? = null, val item: Item? = null,
val color: Int = 0, val color: Int = 0,
@ -65,7 +65,7 @@ class SyncAnalyzer(
feedsIdsForNewItems.size > 1 && itemCount > 1 -> { feedsIdsForNewItems.size > 1 && itemCount > 1 -> {
NotificationContent( NotificationContent(
title = account.accountName!!, title = account.accountName!!,
content = context.getString(R.string.new_items, itemCount.toString()), text = context.getString(R.string.new_items, itemCount.toString()),
largeIcon = ContextCompat.getDrawable( largeIcon = ContextCompat.getDrawable(
context, context,
account.accountType!!.iconRes account.accountType!!.iconRes
@ -105,7 +105,7 @@ class SyncAnalyzer(
target.drawable?.toBitmap() target.drawable?.toBitmap()
} }
val (item, content) = if (items.size == 1) { val (item, text) = if (items.size == 1) {
val item = items.first() val item = items.first()
item to item.title item to item.title
} else { } else {
@ -114,8 +114,8 @@ class SyncAnalyzer(
return NotificationContent( return NotificationContent(
title = feed.name, title = feed.name,
text = text,
largeIcon = icon, largeIcon = icon,
content = content,
item = item, item = item,
color = feed.backgroundColor, color = feed.backgroundColor,
accountId = account.id accountId = account.id

View File

@ -252,8 +252,8 @@ class SyncWorker(
val notificationBuilder = Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID) val notificationBuilder = Builder(applicationContext, ReadropsApp.SYNC_CHANNEL_ID)
.setContentTitle(notificationContent.title) .setContentTitle(notificationContent.title)
.setContentText(notificationContent.content) .setContentText(notificationContent.text)
.setStyle(NotificationCompat.BigTextStyle().bigText(notificationContent.content)) .setStyle(NotificationCompat.BigTextStyle().bigText(notificationContent.text))
.setSmallIcon(R.drawable.ic_notifications) .setSmallIcon(R.drawable.ic_notifications)
.setColor(notificationContent.color) .setColor(notificationContent.color)
.setContentIntent( .setContentIntent(