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

View File

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

View File

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