DeckApiTest.kt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.integrations.deck
  21. import android.content.Context
  22. import android.content.Intent
  23. import android.content.pm.PackageManager
  24. import android.content.pm.ResolveInfo
  25. import androidx.test.platform.app.InstrumentationRegistry
  26. import com.nextcloud.client.account.User
  27. import com.owncloud.android.lib.resources.notifications.models.Notification
  28. import org.junit.Assert.assertFalse
  29. import org.junit.Assert.assertTrue
  30. import org.junit.Before
  31. import org.junit.Test
  32. import org.junit.runner.RunWith
  33. import org.junit.runners.Parameterized
  34. import org.junit.runners.Suite
  35. import org.mockito.Mock
  36. import org.mockito.MockitoAnnotations
  37. import org.mockito.kotlin.any
  38. import org.mockito.kotlin.anyOrNull
  39. import org.mockito.kotlin.never
  40. import org.mockito.kotlin.times
  41. import org.mockito.kotlin.verify
  42. import org.mockito.kotlin.whenever
  43. @RunWith(Suite::class)
  44. @Suite.SuiteClasses(
  45. DeckApiTest.DeckIsInstalled::class,
  46. DeckApiTest.DeckIsNotInstalled::class
  47. )
  48. class DeckApiTest {
  49. abstract class Fixture {
  50. @Mock
  51. lateinit var packageManager: PackageManager
  52. lateinit var context: Context
  53. @Mock
  54. lateinit var user: User
  55. lateinit var deck: DeckApiImpl
  56. @Before
  57. fun setUpFixture() {
  58. MockitoAnnotations.initMocks(this)
  59. context = InstrumentationRegistry.getInstrumentation().targetContext
  60. deck = DeckApiImpl(context, packageManager)
  61. }
  62. }
  63. @RunWith(Parameterized::class)
  64. class DeckIsInstalled : Fixture() {
  65. @Parameterized.Parameter(0)
  66. lateinit var installedDeckPackage: String
  67. companion object {
  68. @Parameterized.Parameters
  69. @JvmStatic
  70. fun initParametrs(): Array<String> {
  71. return DeckApiImpl.DECK_APP_PACKAGES
  72. }
  73. }
  74. @Before
  75. fun setUp() {
  76. whenever(packageManager.resolveActivity(any(), any())).thenAnswer {
  77. val intent = it.getArgument<Intent>(0)
  78. return@thenAnswer if (intent.component?.packageName == installedDeckPackage) {
  79. ResolveInfo()
  80. } else {
  81. null
  82. }
  83. }
  84. }
  85. @Test
  86. fun can_forward_deck_notification() {
  87. // GIVEN
  88. // notification to deck arrives
  89. val notification = Notification().apply { app = "deck" }
  90. // WHEN
  91. // deck action is created
  92. val forwardActionIntent = deck.createForwardToDeckActionIntent(notification, user)
  93. // THEN
  94. // open action is created
  95. assertTrue("Failed for $installedDeckPackage", forwardActionIntent.isPresent)
  96. }
  97. @Test
  98. fun notifications_from_other_apps_are_ignored() {
  99. // GIVEN
  100. // notification from other app arrives
  101. val deckNotification = Notification().apply {
  102. app = "some_other_app"
  103. }
  104. // WHEN
  105. // deck action is created
  106. val openDeckActionIntent = deck.createForwardToDeckActionIntent(deckNotification, user)
  107. // THEN
  108. // deck application is not being resolved
  109. // open action is not created
  110. verify(packageManager, never()).resolveActivity(anyOrNull(), anyOrNull())
  111. assertFalse(openDeckActionIntent.isPresent)
  112. }
  113. }
  114. class DeckIsNotInstalled : Fixture() {
  115. @Before
  116. fun setUp() {
  117. whenever(packageManager.resolveActivity(any(), any())).thenReturn(null)
  118. }
  119. @Test
  120. fun cannot_forward_deck_notification() {
  121. // GIVEN
  122. // notification is coming from deck app
  123. val notification = Notification().apply {
  124. app = DeckApiImpl.APP_NAME
  125. }
  126. // WHEN
  127. // creating open in deck action
  128. val openDeckActionIntent = deck.createForwardToDeckActionIntent(notification, user)
  129. // THEN
  130. // deck application is being resolved using all known packages
  131. // open action is not created
  132. verify(packageManager, times(DeckApiImpl.DECK_APP_PACKAGES.size)).resolveActivity(anyOrNull(), anyOrNull())
  133. assertFalse(openDeckActionIntent.isPresent)
  134. }
  135. @Test
  136. fun notifications_from_other_apps_are_ignored() {
  137. // GIVEN
  138. // notification is coming from other app
  139. val notification = Notification().apply {
  140. app = "some_other_app"
  141. }
  142. // WHEN
  143. // creating open in deck action
  144. val openDeckActionIntent = deck.createForwardToDeckActionIntent(notification, user)
  145. // THEN
  146. // deck application is not being resolved
  147. // open action is not created
  148. verify(packageManager, never()).resolveActivity(anyOrNull(), anyOrNull())
  149. assertFalse(openDeckActionIntent.isPresent)
  150. }
  151. }
  152. }