TestPowerManagementService.kt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. *
  6. * Copyright (C) 2019 Chris Narkiewicz <hello@ezaquarii.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.client.device
  22. import android.content.Context
  23. import android.content.Intent
  24. import android.os.BatteryManager
  25. import android.os.Build
  26. import android.os.PowerManager
  27. import com.nextcloud.client.preferences.AppPreferences
  28. import com.nhaarman.mockitokotlin2.any
  29. import com.nhaarman.mockitokotlin2.anyOrNull
  30. import com.nhaarman.mockitokotlin2.eq
  31. import com.nhaarman.mockitokotlin2.mock
  32. import com.nhaarman.mockitokotlin2.never
  33. import com.nhaarman.mockitokotlin2.verify
  34. import com.nhaarman.mockitokotlin2.whenever
  35. import org.junit.Assert.assertFalse
  36. import org.junit.Assert.assertTrue
  37. import org.junit.Before
  38. import org.junit.Test
  39. import org.junit.runner.RunWith
  40. import org.junit.runners.Suite
  41. import org.mockito.Mock
  42. import org.mockito.MockitoAnnotations
  43. @RunWith(Suite::class)
  44. @Suite.SuiteClasses(
  45. TestPowerManagementService.PowerSaveMode::class,
  46. TestPowerManagementService.BatteryCharging::class
  47. )
  48. class TestPowerManagementService {
  49. abstract class Base {
  50. @Mock
  51. lateinit var context: Context
  52. @Mock
  53. lateinit var platformPowerManager: PowerManager
  54. @Mock
  55. lateinit var deviceInfo: DeviceInfo
  56. internal lateinit var powerManagementService: PowerManagementServiceImpl
  57. @Mock
  58. lateinit var preferences: AppPreferences
  59. @Before
  60. fun setUpBase() {
  61. MockitoAnnotations.initMocks(this)
  62. powerManagementService = PowerManagementServiceImpl(
  63. context,
  64. platformPowerManager,
  65. preferences,
  66. deviceInfo
  67. )
  68. }
  69. }
  70. class PowerSaveMode : Base() {
  71. @Test
  72. fun `power saving queries power manager on API 21+`() {
  73. // GIVEN
  74. // API level >= 21
  75. // power save mode is on
  76. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.LOLLIPOP)
  77. whenever(platformPowerManager.isPowerSaveMode).thenReturn(true)
  78. // WHEN
  79. // power save mode is checked
  80. // THEN
  81. // power save mode is enabled
  82. // state is obtained from platform power manager
  83. assertTrue(powerManagementService.isPowerSavingEnabled)
  84. verify(platformPowerManager).isPowerSaveMode
  85. }
  86. @Test
  87. fun `power saving is not available below API 21`() {
  88. // GIVEN
  89. // API level <21
  90. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.KITKAT)
  91. // WHEN
  92. // power save mode is checked
  93. // THEN
  94. // power save mode is disabled
  95. // power manager is not queried
  96. assertFalse(powerManagementService.isPowerSavingEnabled)
  97. verify(platformPowerManager, never()).isPowerSaveMode
  98. }
  99. @Test
  100. fun `power save exclusion is available for flagged vendors`() {
  101. for (vendor in PowerManagementServiceImpl.OVERLY_AGGRESSIVE_POWER_SAVING_VENDORS) {
  102. whenever(deviceInfo.vendor).thenReturn(vendor)
  103. assertTrue("Vendor $vendor check failed", powerManagementService.isPowerSavingExclusionAvailable)
  104. }
  105. }
  106. @Test
  107. fun `power save exclusion is not available for other vendors`() {
  108. whenever(deviceInfo.vendor).thenReturn("some_other_nice_vendor")
  109. assertFalse(powerManagementService.isPowerSavingExclusionAvailable)
  110. }
  111. @Test
  112. fun `power saving check is disabled`() {
  113. // GIVEN
  114. // a device which falsely returns power save mode enabled
  115. // power check is overridden by user
  116. whenever(preferences.isPowerCheckDisabled).thenReturn(true)
  117. whenever(platformPowerManager.isPowerSaveMode).thenReturn(true)
  118. // WHEN
  119. // power save mode is checked
  120. // THEN
  121. // power saving is disabled
  122. assertFalse(powerManagementService.isPowerSavingEnabled)
  123. }
  124. }
  125. class BatteryCharging : Base() {
  126. val mockStickyBatteryStatusIntent: Intent = mock()
  127. @Before
  128. fun setUp() {
  129. whenever(context.registerReceiver(anyOrNull(), anyOrNull())).thenReturn(mockStickyBatteryStatusIntent)
  130. }
  131. @Test
  132. fun `battery charging status on API 17+`() {
  133. // GIVEN
  134. // device has API level 17+
  135. // battery status sticky intent is available
  136. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.JELLY_BEAN_MR1)
  137. val powerSources = setOf(
  138. BatteryManager.BATTERY_PLUGGED_AC,
  139. BatteryManager.BATTERY_PLUGGED_USB,
  140. BatteryManager.BATTERY_PLUGGED_WIRELESS
  141. )
  142. for (row in powerSources) {
  143. // WHEN
  144. // device is charging using supported power source
  145. whenever(mockStickyBatteryStatusIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
  146. .thenReturn(row)
  147. // THEN
  148. // charging flag is true
  149. assertTrue(powerManagementService.isBatteryCharging)
  150. }
  151. }
  152. @Test
  153. fun `battery charging status on API 16`() {
  154. // GIVEN
  155. // device has API level 16
  156. // battery status sticky intent is available
  157. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.JELLY_BEAN)
  158. val powerSources = setOf(
  159. BatteryManager.BATTERY_PLUGGED_AC,
  160. BatteryManager.BATTERY_PLUGGED_USB
  161. )
  162. for (row in powerSources) {
  163. // WHEN
  164. // device is charging using AC or USB
  165. whenever(mockStickyBatteryStatusIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
  166. .thenReturn(row)
  167. // THEN
  168. // charging flag is true
  169. assertTrue(powerManagementService.isBatteryCharging)
  170. }
  171. }
  172. @Test
  173. fun `wireless charging is not supported in API 16`() {
  174. // GIVEN
  175. // device has API level 16
  176. // battery status sticky intent is available
  177. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.JELLY_BEAN)
  178. // WHEN
  179. // spurious wireless power source is returned
  180. whenever(mockStickyBatteryStatusIntent.getIntExtra(eq(BatteryManager.EXTRA_PLUGGED), any()))
  181. .thenReturn(BatteryManager.BATTERY_PLUGGED_WIRELESS)
  182. // THEN
  183. // power source value is ignored on this API level
  184. // charging flag is false
  185. assertFalse(powerManagementService.isBatteryCharging)
  186. }
  187. @Test
  188. fun `battery status sticky intent is not available`() {
  189. // GIVEN
  190. // device has API level P or below
  191. // battery status sticky intent is NOT available
  192. whenever(deviceInfo.apiLevel).thenReturn(Build.VERSION_CODES.P)
  193. whenever(context.registerReceiver(anyOrNull(), anyOrNull())).thenReturn(null)
  194. // THEN
  195. // charging flag is false
  196. assertFalse(powerManagementService.isBatteryCharging)
  197. verify(context).registerReceiver(anyOrNull(), any())
  198. }
  199. }
  200. }