Add tests for sync results notification
This commit is contained in:
parent
2453f35179
commit
071e57ac89
@ -61,6 +61,7 @@ dependencies {
|
|||||||
implementation "androidx.work:work-runtime-ktx:2.3.0"
|
implementation "androidx.work:work-runtime-ktx:2.3.0"
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.12'
|
testImplementation 'junit:junit:4.12'
|
||||||
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
androidTestImplementation 'androidx.test:runner:1.2.0'
|
androidTestImplementation 'androidx.test:runner:1.2.0'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||||
|
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
package com.readrops.app;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import androidx.test.InstrumentationRegistry;
|
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Instrumented test, which will execute on an Android device.
|
|
||||||
*
|
|
||||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
|
||||||
*/
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
|
||||||
public class ExampleInstrumentedTest {
|
|
||||||
@Test
|
|
||||||
public void useAppContext() {
|
|
||||||
// Context of the app under test.
|
|
||||||
Context appContext = InstrumentationRegistry.getTargetContext();
|
|
||||||
|
|
||||||
assertEquals("com.readrops.app", appContext.getPackageName());
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,149 @@
|
|||||||
|
package com.readrops.app
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import com.readrops.app.utils.SyncResultAnalyser
|
||||||
|
import com.readrops.readropsdb.Database
|
||||||
|
import com.readrops.readropsdb.entities.Feed
|
||||||
|
import com.readrops.readropsdb.entities.Item
|
||||||
|
import com.readrops.readropsdb.entities.account.Account
|
||||||
|
import com.readrops.readropsdb.entities.account.AccountType
|
||||||
|
import com.readrops.readropslibrary.services.SyncResult
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4::class)
|
||||||
|
class SyncResultAnalyserTest {
|
||||||
|
|
||||||
|
private lateinit var database: Database
|
||||||
|
|
||||||
|
private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
|
||||||
|
private val account1 = Account().apply {
|
||||||
|
accountName = "test account 1"
|
||||||
|
accountType = AccountType.FRESHRSS
|
||||||
|
}
|
||||||
|
|
||||||
|
private val account2 = Account().apply {
|
||||||
|
accountName = "test account 2"
|
||||||
|
accountType = AccountType.NEXTCLOUD_NEWS
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setupDb() {
|
||||||
|
database = Room.inMemoryDatabaseBuilder(context, Database::class.java)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
var account1Id = 0
|
||||||
|
database.accountDao().insert(account1).subscribe { id -> account1Id = id.toInt() }
|
||||||
|
|
||||||
|
var account2Id = 0
|
||||||
|
database.accountDao().insert(account2).subscribe { id -> account2Id = id.toInt() }
|
||||||
|
|
||||||
|
for (i in 0..3) {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
database.feedDao().insert(feed).subscribe()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
fun closeDb() {
|
||||||
|
database.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun caseOneElementEveryWhere() {
|
||||||
|
val item = Item().apply {
|
||||||
|
title = "caseOneElementEveryWhere"
|
||||||
|
feedId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResult = SyncResult().apply {
|
||||||
|
items = mutableListOf(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent()
|
||||||
|
|
||||||
|
assertEquals("caseOneElementEveryWhere", notifContent.content)
|
||||||
|
assertEquals("feed 1", notifContent.title)
|
||||||
|
assertTrue(notifContent.largeIcon != null)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun caseTwoItemsOneFeed() {
|
||||||
|
val item = Item().apply {
|
||||||
|
title = "caseOneElementEveryWhere"
|
||||||
|
feedId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResult = SyncResult().apply {
|
||||||
|
items = mutableListOf(item, item, item)
|
||||||
|
}
|
||||||
|
|
||||||
|
val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent()
|
||||||
|
|
||||||
|
assertEquals("3 new articles", notifContent.content)
|
||||||
|
assertEquals("feed 1", notifContent.title)
|
||||||
|
assertTrue(notifContent.largeIcon != null)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun caseMultipleFeeds() {
|
||||||
|
val item = Item().apply {
|
||||||
|
feedId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val item2 = Item().apply {
|
||||||
|
feedId = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResult = SyncResult().apply {
|
||||||
|
items = mutableListOf(item, item2)
|
||||||
|
}
|
||||||
|
|
||||||
|
val notifContent = SyncResultAnalyser(context, mapOf(Pair(account1, syncResult)), database).getSyncNotifContent()
|
||||||
|
|
||||||
|
assertEquals("2 new articles", notifContent.content)
|
||||||
|
assertEquals(account1.accountName, notifContent.title)
|
||||||
|
//assertTrue(notifContent.largeIcon != null) doesn't work currently
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun multipleAccounts() {
|
||||||
|
val item = Item().apply {
|
||||||
|
feedId = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
val item2 = Item().apply {
|
||||||
|
feedId = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResult = SyncResult().apply {
|
||||||
|
items = mutableListOf(item, item2)
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResult2 = SyncResult().apply {
|
||||||
|
items = mutableListOf(item, item2)
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncResults = mutableMapOf<Account, SyncResult>().apply {
|
||||||
|
put(account1, syncResult)
|
||||||
|
put(account2, syncResult2)
|
||||||
|
}
|
||||||
|
|
||||||
|
val notifContent = SyncResultAnalyser(context, syncResults, database).getSyncNotifContent()
|
||||||
|
|
||||||
|
assertEquals("Notifications", notifContent.title)
|
||||||
|
assertEquals("4 new articles", notifContent.content)
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ import com.readrops.readropslibrary.services.SyncResult
|
|||||||
/**
|
/**
|
||||||
* Simple class to get synchro notification content (title, content and largeIcon) according to some rules
|
* Simple class to get synchro notification content (title, content and largeIcon) according to some rules
|
||||||
*/
|
*/
|
||||||
class SyncResultAnalyser(val context: Context, private val syncResults: Map<Account, SyncResult>) {
|
class SyncResultAnalyser(val context: Context, private val syncResults: Map<Account, SyncResult>, val database: Database) {
|
||||||
|
|
||||||
fun getSyncNotifContent(): SyncResultNotifContent {
|
fun getSyncNotifContent(): SyncResultNotifContent {
|
||||||
var title: String? = null
|
var title: String? = null
|
||||||
@ -40,7 +40,7 @@ class SyncResultAnalyser(val context: Context, private val syncResults: Map<Acco
|
|||||||
contentText = context.getString(R.string.new_items, syncResult.items.size.toString())
|
contentText = context.getString(R.string.new_items, syncResult.items.size.toString())
|
||||||
largeIcon = BitmapFactory.decodeResource(context.resources, account.accountType.iconRes)
|
largeIcon = BitmapFactory.decodeResource(context.resources, account.accountType.iconRes)
|
||||||
} else if (feedsIdsForNewItems.size == 1) { // new items from only one feed from one account
|
} else if (feedsIdsForNewItems.size == 1) { // new items from only one feed from one account
|
||||||
val feed = Database.getInstance(context).feedDao().getFeedById(feedsIdsForNewItems.first())
|
val feed = database.feedDao().getFeedById(feedsIdsForNewItems.first())
|
||||||
title = feed?.name
|
title = feed?.name
|
||||||
|
|
||||||
val target = GlideApp.with(context)
|
val target = GlideApp.with(context)
|
||||||
|
@ -19,10 +19,12 @@ import io.reactivex.disposables.Disposable
|
|||||||
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
|
class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(context, parameters) {
|
||||||
|
|
||||||
private lateinit var disposable: Disposable
|
private lateinit var disposable: Disposable
|
||||||
|
|
||||||
private val notificationManager = NotificationManagerCompat.from(applicationContext)
|
private val notificationManager = NotificationManagerCompat.from(applicationContext)
|
||||||
|
private val database = Database.getInstance(applicationContext)
|
||||||
|
|
||||||
override fun doWork(): Result {
|
override fun doWork(): Result {
|
||||||
val database = Database.getInstance(applicationContext)
|
|
||||||
val accounts = database.accountDao().selectAll()
|
val accounts = database.accountDao().selectAll()
|
||||||
var result = Result.success()
|
var result = Result.success()
|
||||||
|
|
||||||
@ -60,7 +62,7 @@ class SyncWorker(context: Context, parameters: WorkerParameters) : Worker(contex
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun displaySyncResultNotif(syncResults: Map<Account, SyncResult>) {
|
private fun displaySyncResultNotif(syncResults: Map<Account, SyncResult>) {
|
||||||
val notifContent = SyncResultAnalyser(applicationContext, syncResults).getSyncNotifContent()
|
val notifContent = SyncResultAnalyser(applicationContext, syncResults, database).getSyncNotifContent()
|
||||||
|
|
||||||
if (notifContent.title != null && notifContent.content != null) {
|
if (notifContent.title != null && notifContent.content != null) {
|
||||||
val intent = Intent(applicationContext, MainActivity::class.java)
|
val intent = Intent(applicationContext, MainActivity::class.java)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user