BackgroundJobFactoryTest.kt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.jobs
  21. import android.app.NotificationManager
  22. import android.content.ContentResolver
  23. import android.content.Context
  24. import android.content.res.Resources
  25. import android.os.Build
  26. import androidx.work.WorkerParameters
  27. import com.nextcloud.client.account.UserAccountManager
  28. import com.nextcloud.client.core.Clock
  29. import com.nextcloud.client.device.DeviceInfo
  30. import com.nextcloud.client.device.PowerManagementService
  31. import com.nextcloud.client.logger.Logger
  32. import com.nextcloud.client.network.ConnectivityService
  33. import com.nextcloud.client.preferences.AppPreferences
  34. import com.nhaarman.mockitokotlin2.whenever
  35. import com.owncloud.android.datamodel.ArbitraryDataProvider
  36. import com.owncloud.android.datamodel.UploadsStorageManager
  37. import org.greenrobot.eventbus.EventBus
  38. import org.junit.Assert.assertNotNull
  39. import org.junit.Assert.assertNull
  40. import org.junit.Before
  41. import org.junit.Test
  42. import org.mockito.Mock
  43. import org.mockito.MockitoAnnotations
  44. import javax.inject.Provider
  45. class BackgroundJobFactoryTest {
  46. @Mock
  47. private lateinit var context: Context
  48. @Mock
  49. private lateinit var params: WorkerParameters
  50. @Mock
  51. private lateinit var contentResolver: ContentResolver
  52. @Mock
  53. private lateinit var preferences: AppPreferences
  54. @Mock
  55. private lateinit var powerManagementService: PowerManagementService
  56. @Mock
  57. private lateinit var backgroundJobManager: BackgroundJobManager
  58. @Mock
  59. private lateinit var deviceInfo: DeviceInfo
  60. @Mock
  61. private lateinit var clock: Clock
  62. @Mock
  63. private lateinit var accountManager: UserAccountManager
  64. @Mock
  65. private lateinit var resources: Resources
  66. @Mock
  67. private lateinit var dataProvider: ArbitraryDataProvider
  68. @Mock
  69. private lateinit var logger: Logger
  70. @Mock
  71. private lateinit var uploadsStorageManager: UploadsStorageManager
  72. @Mock
  73. private lateinit var connectivityService: ConnectivityService
  74. @Mock
  75. private lateinit var notificationManager: NotificationManager
  76. @Mock
  77. private lateinit var eventBus: EventBus
  78. private lateinit var factory: BackgroundJobFactory
  79. @Before
  80. fun setUp() {
  81. MockitoAnnotations.initMocks(this)
  82. factory = BackgroundJobFactory(
  83. logger,
  84. preferences,
  85. contentResolver,
  86. clock,
  87. powerManagementService,
  88. Provider { backgroundJobManager },
  89. deviceInfo,
  90. accountManager,
  91. resources,
  92. dataProvider,
  93. uploadsStorageManager,
  94. connectivityService,
  95. notificationManager,
  96. eventBus
  97. )
  98. }
  99. @Test
  100. fun content_observer_worker_is_created_on_api_level_24() {
  101. // GIVEN
  102. // api level is > 24
  103. // content URI trigger is supported
  104. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.N)
  105. // WHEN
  106. // factory is called to create content observer worker
  107. val worker = factory.createWorker(context, ContentObserverWork::class.java.name, params)
  108. // THEN
  109. // factory creates a worker compatible with API level
  110. assertNotNull(worker)
  111. }
  112. @Test
  113. fun content_observer_worker_is_not_created_below_api_level_24() {
  114. // GIVEN
  115. // api level is < 24
  116. // content URI trigger is not supported
  117. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.M)
  118. // WHEN
  119. // factory is called to create content observer worker
  120. val worker = factory.createWorker(context, ContentObserverWork::class.java.name, params)
  121. // THEN
  122. // factory does not create a worker incompatible with API level
  123. assertNull(worker)
  124. }
  125. }