From 896a626eb6f2848a8161f8488219c882b3257874 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Fri, 20 Mar 2020 19:51:58 +0100 Subject: [PATCH] Add more tests for sync result notification content --- .../readrops/app/SyncResultAnalyserTest.kt | 173 +++++++++++++++++- .../readrops/app/utils/SyncResultAnalyser.kt | 25 +-- 2 files changed, 181 insertions(+), 17 deletions(-) diff --git a/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt b/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt index 3feb3528..4b12d71f 100644 --- a/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt +++ b/app/src/androidTest/java/com/readrops/app/SyncResultAnalyserTest.kt @@ -34,6 +34,12 @@ class SyncResultAnalyserTest { private val account2 = Account().apply { accountName = "test account 2" accountType = AccountType.NEXTCLOUD_NEWS + isNotificationsEnabled = false + } + + private val account3 = Account().apply { + accountName = "test account 3" + accountType = AccountType.LOCAL isNotificationsEnabled = true } @@ -48,12 +54,16 @@ class SyncResultAnalyserTest { var account2Id = 0 database.accountDao().insert(account2).subscribe { id -> account2Id = id.toInt() } - for (i in 0..3) { + var account3Id = 0 + database.accountDao().insert(account3).subscribe { id -> account3Id = id.toInt() } + + val accountIds = listOf(account1Id, account2Id, account3Id) + for (i in 0..2) { val feed = Feed().apply { name = "feed ${i + 1}" iconUrl = "https://i0.wp.com/mrmondialisation.org/wp-content/uploads/2017/05/ico_final.gif" - this.accountId = if (i % 2 == 0) account1Id else account2Id - isNotificationEnabled = true + this.accountId = accountIds.find { it == (i + 1) }!! + isNotificationEnabled = i % 2 == 0 } database.feedDao().insert(feed).subscribe() @@ -108,7 +118,7 @@ class SyncResultAnalyserTest { @Test fun testMultipleFeeds() { val item = Item().apply { feedId = 1 } - val item2 = Item().apply { feedId = 2 } + val item2 = Item().apply { feedId = 3 } val syncResult = SyncResult().apply { items = listOf(item, item2) } val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent() @@ -121,14 +131,14 @@ class SyncResultAnalyserTest { @Test fun testMultipleAccounts() { val item = Item().apply { feedId = 1 } - val item2 = Item().apply { feedId = 2 } + val item2 = Item().apply { feedId = 3 } val syncResult = SyncResult().apply { items = listOf(item, item2) } val syncResult2 = SyncResult().apply { items = listOf(item, item2) } val syncResults = mutableMapOf().apply { put(account1, syncResult) - put(account2, syncResult2) + put(account3, syncResult2) } val notifContent = SyncResultAnalyser(context, syncResults, database).getSyncNotifContent() @@ -136,4 +146,155 @@ class SyncResultAnalyserTest { assertEquals("Notifications", notifContent.title) assertEquals(context.getString(R.string.new_items, 4), notifContent.content) } + + @Test + fun testAccountNotificationsDisabled() { + val item1 = Item().apply { + title = "testAccountNotificationsDisabled" + feedId = 1 + } + + val item2 = Item().apply { + title = "testAccountNotificationsDisabled2" + feedId = 1 + } + + val syncResult = SyncResult().apply { items = listOf(item1, item2) } + val notifContent = SyncResultAnalyser(context, mapOf(Pair(account2, syncResult)), database).getSyncNotifContent() + + assert(notifContent.title == null) + assert(notifContent.content == null) + assert(notifContent.largeIcon == null) + } + + @Test + fun testFeedNotificationsDisabled() { + val item1 = Item().apply { + title = "testAccountNotificationsDisabled" + feedId = 2 + } + + val item2 = Item().apply { + title = "testAccountNotificationsDisabled2" + feedId = 2 + } + + val syncResult = SyncResult().apply { items = listOf(item1, item2) } + val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent() + + assert(notifContent.title == null) + assert(notifContent.content == null) + assert(notifContent.largeIcon == null) + } + + @Test + fun testTwoAccountsWithOneAccountNotificationsEnabled() { + val item1 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled" + feedId = 1 + remoteId = "remoteId 1" + pubDate = LocalDateTime.now() + } + + val item2 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled2" + feedId = 3 + } + + val item3 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled3" + feedId = 3 + } + + database.itemDao().insert(item1).subscribe() + + val syncResult1 = SyncResult().apply { items = listOf(item1) } + val syncResult2 = SyncResult().apply { items = listOf(item2, item3) } + + val syncResults = mutableMapOf().apply { + put(account1, syncResult1) + put(account2, syncResult2) + } + + val notifContent = SyncResultAnalyser(context, syncResults, database).getSyncNotifContent() + + assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notifContent.content) + assertEquals("feed 1", notifContent.title) + assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.item != null) + + database.itemDao().delete(item1).subscribe() + } + + @Test + fun testTwoAccountsWithOneFeedNotificationEnabled() { + val item1 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled" + feedId = 1 + remoteId = "remoteId 1" + pubDate = LocalDateTime.now() + } + + val item2 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled2" + feedId = 2 + } + + val item3 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled3" + feedId = 2 + } + + database.itemDao().insert(item1).subscribe() + + val syncResult1 = SyncResult().apply { items = listOf(item1) } + val syncResult2 = SyncResult().apply { items = listOf(item2, item3) } + + val syncResults = mutableMapOf().apply { + put(account1, syncResult1) + put(account2, syncResult2) + } + + val notifContent = SyncResultAnalyser(context, syncResults, database).getSyncNotifContent() + + assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notifContent.content) + assertEquals("feed 1", notifContent.title) + assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.item != null) + + database.itemDao().delete(item1).subscribe() + } + + + @Test + fun testOneAccountTwoFeedsWithOneFeedNotificationEnabled() { + val item1 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled" + feedId = 1 + remoteId = "remoteId 1" + pubDate = LocalDateTime.now() + } + + val item2 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled2" + feedId = 2 + } + + val item3 = Item().apply { + title = "testTwoAccountsWithOneAccountNotificationsEnabled3" + feedId = 2 + } + + database.itemDao().insert(item1).subscribe() + + val syncResult = SyncResult().apply { items = listOf(item1, item2, item3) } + val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent() + + assertEquals("testTwoAccountsWithOneAccountNotificationsEnabled", notifContent.content) + assertEquals("feed 1", notifContent.title) + assertTrue(notifContent.largeIcon != null) + assertTrue(notifContent.item != null) + + database.itemDao().delete(item1).subscribe() + } } \ No newline at end of file diff --git a/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt b/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt index dcd14edf..b3eb3eed 100644 --- a/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt +++ b/app/src/main/java/com/readrops/app/utils/SyncResultAnalyser.kt @@ -37,17 +37,20 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map 1) { - val feeds = database.feedDao().selectFromIdList(feedsIdsForNewItems) - - val itemCount = syncResult.items.filter { isFeedNotificationEnabledForItem(feeds, it) }.size - + if (feedsIdsForNewItems.size > 1 && itemCount > 1) { notifContent.title = account.accountName notifContent.content = context.getString(R.string.new_items, itemCount.toString()) notifContent.largeIcon = Utils.getBitmapFromDrawable(ContextCompat.getDrawable(context, account.accountType.iconRes)) } else if (feedsIdsForNewItems.size == 1) // new items from only one feed from one account - oneFeedCase(feedsIdsForNewItems.first(), syncResult) + oneFeedCase(feedsIdsForNewItems.first(), syncResult.items) + else if (itemCount == 1) + oneFeedCase(items.first().feedId.toLong(), items) } } } @@ -55,7 +58,7 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map) { val feed = database.feedDao().getFeedById(feedId.toInt()) if (feed.isNotificationEnabled) { @@ -71,12 +74,12 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map