mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 12:53:55 +01:00
Add More Unit Tests for NotificationActivity (#4314)
This commit is contained in:
parent
a479dd5bb1
commit
5ddbc9e27a
2 changed files with 270 additions and 6 deletions
21
app/src/test/kotlin/fr/free/nrw/commons/ShadowActionBar.java
Normal file
21
app/src/test/kotlin/fr/free/nrw/commons/ShadowActionBar.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package fr.free.nrw.commons;
|
||||
|
||||
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
|
||||
@Implements(ActionBar.class)
|
||||
public class ShadowActionBar {
|
||||
|
||||
private boolean showHomeAsUp;
|
||||
|
||||
public boolean getShowHomeAsUp() {
|
||||
return showHomeAsUp;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
void setDisplayHomeAsUpEnabled(final boolean showHomeAsUp) {
|
||||
this.showHomeAsUp = showHomeAsUp;
|
||||
}
|
||||
}
|
||||
|
|
@ -4,13 +4,18 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import fr.free.nrw.commons.R
|
||||
import fr.free.nrw.commons.ShadowActionBar
|
||||
import fr.free.nrw.commons.TestAppAdapter
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.utils.NetworkUtils
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.`when`
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.MockitoAnnotations
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
|
@ -19,9 +24,11 @@ import org.robolectric.annotation.Config
|
|||
import org.robolectric.fakes.RoboMenu
|
||||
import org.robolectric.fakes.RoboMenuItem
|
||||
import org.wikipedia.AppAdapter
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.Method
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@Config(sdk = [21], application = TestCommonsApplication::class, shadows = [ShadowActionBar::class])
|
||||
class NotificationActivityUnitTests {
|
||||
|
||||
@Mock
|
||||
|
|
@ -30,17 +37,25 @@ class NotificationActivityUnitTests {
|
|||
@Mock
|
||||
private lateinit var notification: Notification
|
||||
|
||||
private lateinit var context: Context
|
||||
|
||||
@Mock
|
||||
private lateinit var menuItem: MenuItem
|
||||
|
||||
private lateinit var networkUtils: NetworkUtils
|
||||
private lateinit var context: Context
|
||||
private lateinit var menuItemWithId: MenuItem
|
||||
private lateinit var menuItemWithoutId: MenuItem
|
||||
private lateinit var menu: Menu
|
||||
private var notificationWorkerFragment = NotificationWorkerFragment()
|
||||
private var notificationList =
|
||||
listOf(Notification(NotificationType.UNKNOWN, "", "", "", "", ""))
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
|
||||
MockitoAnnotations.initMocks(this)
|
||||
|
||||
networkUtils = mock(NetworkUtils::class.java)
|
||||
|
||||
AppAdapter.set(TestAppAdapter())
|
||||
|
||||
val intent = Intent().putExtra("title", "read")
|
||||
|
|
@ -50,10 +65,17 @@ class NotificationActivityUnitTests {
|
|||
|
||||
context = RuntimeEnvironment.application.applicationContext
|
||||
|
||||
menuItem = RoboMenuItem(null)
|
||||
menuItemWithId = RoboMenuItem(R.id.archived)
|
||||
|
||||
menuItemWithoutId = RoboMenuItem(null)
|
||||
|
||||
menu = RoboMenu(context)
|
||||
|
||||
val notificationMenuItem: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("notificationMenuItem")
|
||||
notificationMenuItem.isAccessible = true
|
||||
notificationMenuItem.set(activity, menuItemWithId)
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -70,7 +92,21 @@ class NotificationActivityUnitTests {
|
|||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkRemoveNotification() {
|
||||
fun checkRemoveNotificationIsReadTrue() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, true)
|
||||
activity.removeNotification(notification)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkRemoveNotificationIsReadFalse() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, false)
|
||||
activity.removeNotification(notification)
|
||||
}
|
||||
|
||||
|
|
@ -82,8 +118,215 @@ class NotificationActivityUnitTests {
|
|||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnOptionsItemSelected() {
|
||||
fun testOnOptionsItemSelectedDefault() {
|
||||
activity.onOptionsItemSelected(menuItemWithoutId)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnOptionsItemSelectedTitleCaseRead() {
|
||||
`when`(menuItem.itemId).thenReturn(R.id.archived)
|
||||
`when`(menuItem.title).thenReturn(context.getString(R.string.menu_option_read))
|
||||
activity.onOptionsItemSelected(menuItem)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnOptionsItemSelectedTitleCaseUnread() {
|
||||
`when`(menuItem.itemId).thenReturn(R.id.archived)
|
||||
`when`(menuItem.title).thenReturn(context.getString(R.string.menu_option_unread))
|
||||
activity.onOptionsItemSelected(menuItem)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetEmptyViewIsReadTrue() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, true)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setEmptyView"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetEmptyViewIsReadFalse() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, false)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setEmptyView"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetMenuItemTitleIsReadTrue() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, true)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setMenuItemTitle"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetMenuItemTitleIsReadFalse() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, false)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setMenuItemTitle"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetPageTitleIsReadTrue() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, true)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setPageTitle"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetPageTitleIsReadFalse() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, false)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setPageTitle"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetItemsListNull() {
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setItems", List::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testSetItemsListNonNull() {
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"setItems", List::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, notificationList)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testHandleUrlNull() {
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"handleUrl", String::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testHandleUrlNonNull() {
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"handleUrl", String::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, "string")
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkAddNotificationsNull() {
|
||||
val mNotificationWorkerFragment: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("mNotificationWorkerFragment")
|
||||
mNotificationWorkerFragment.isAccessible = true
|
||||
mNotificationWorkerFragment.set(activity, null)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"addNotifications", Boolean::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun checkAddNotificationsNonNull() {
|
||||
val mNotificationWorkerFragment: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("mNotificationWorkerFragment")
|
||||
mNotificationWorkerFragment.isAccessible = true
|
||||
mNotificationWorkerFragment.set(activity, notificationWorkerFragment)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"addNotifications", Boolean::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testInitListViewIsReadFalse() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, false)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"initListView"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testInitListViewIsReadTrue() {
|
||||
val isRead: Field =
|
||||
NotificationActivity::class.java.getDeclaredField("isRead")
|
||||
isRead.isAccessible = true
|
||||
isRead.set(activity, true)
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"initListView"
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testRefreshNull() {
|
||||
val method: Method = NotificationActivity::class.java.getDeclaredMethod(
|
||||
"refresh", Boolean::class.java
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, true)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue