|
| 1 | +package com.android.messaging.sms |
| 2 | + |
| 3 | +import android.app.Notification |
| 4 | +import android.app.NotificationManager |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.content.pm.ActivityInfo |
| 8 | +import android.content.pm.ApplicationInfo |
| 9 | +import android.content.pm.ResolveInfo |
| 10 | +import android.content.res.Resources |
| 11 | +import android.provider.Settings |
| 12 | +import com.android.messaging.Factory |
| 13 | +import com.android.messaging.FactoryTestAccess |
| 14 | +import com.android.messaging.R |
| 15 | +import com.android.messaging.testutil.installTestFactory |
| 16 | +import com.android.messaging.ui.UIIntentsImpl |
| 17 | +import com.android.messaging.util.PendingIntentConstants |
| 18 | +import com.android.messaging.util.PhoneUtils |
| 19 | +import io.mockk.every |
| 20 | +import io.mockk.mockk |
| 21 | +import org.junit.After |
| 22 | +import org.junit.Assert.assertEquals |
| 23 | +import org.junit.Assert.assertFalse |
| 24 | +import org.junit.Assert.assertNull |
| 25 | +import org.junit.Assert.assertTrue |
| 26 | +import org.junit.Before |
| 27 | +import org.junit.Test |
| 28 | +import org.junit.runner.RunWith |
| 29 | +import org.robolectric.RobolectricTestRunner |
| 30 | +import org.robolectric.RuntimeEnvironment |
| 31 | +import org.robolectric.Shadows.shadowOf |
| 32 | +import org.robolectric.annotation.Config |
| 33 | +import org.robolectric.annotation.Implementation |
| 34 | +import org.robolectric.annotation.Implements |
| 35 | +import org.robolectric.annotation.RealObject |
| 36 | +import org.robolectric.shadow.api.Shadow.directlyOn |
| 37 | +import org.robolectric.shadows.ShadowNotificationManager |
| 38 | +import org.robolectric.util.ReflectionHelpers.ClassParameter |
| 39 | + |
| 40 | +private const val STORAGE_WARNING_TITLE = "Storage space running out" |
| 41 | +private const val STORAGE_WARNING_TEXT = |
| 42 | + "Messaging might not send or receive messages until more space is available on your device." |
| 43 | +private const val STORAGE_WARNING_TICKER = "Low SMS storage. You may need to delete messages." |
| 44 | + |
| 45 | +@RunWith(RobolectricTestRunner::class) |
| 46 | +@Config(sdk = [36], shadows = [StorageWarningResourcesShadow::class]) |
| 47 | +internal class SmsStorageStatusManagerTest { |
| 48 | + |
| 49 | + private lateinit var context: Context |
| 50 | + private lateinit var phoneUtils: PhoneUtils |
| 51 | + |
| 52 | + @Before |
| 53 | + fun setUp() { |
| 54 | + ShadowNotificationManager.reset() |
| 55 | + context = RuntimeEnvironment.getApplication().applicationContext |
| 56 | + phoneUtils = mockk() |
| 57 | + every { phoneUtils.isSmsEnabled() } returns true |
| 58 | + installTestFactory( |
| 59 | + context = context, |
| 60 | + phoneUtils = phoneUtils, |
| 61 | + ) |
| 62 | + every { requireNotNull(Factory.get()).getUIIntents() } returns UIIntentsImpl() |
| 63 | + shadowOf(context.packageManager).setResolveInfosForIntent( |
| 64 | + Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS), |
| 65 | + listOf( |
| 66 | + ResolveInfo().apply { |
| 67 | + activityInfo = ActivityInfo().apply { |
| 68 | + applicationInfo = ApplicationInfo().apply { |
| 69 | + packageName = "com.android.settings" |
| 70 | + } |
| 71 | + packageName = "com.android.settings" |
| 72 | + name = "StorageSettingsActivity" |
| 73 | + } |
| 74 | + }, |
| 75 | + ), |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + @After |
| 80 | + fun tearDown() { |
| 81 | + ShadowNotificationManager.reset() |
| 82 | + FactoryTestAccess.reset() |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun handleStorageLow_postsDismissibleStorageSettingsNotification() { |
| 87 | + SmsStorageStatusManager.handleStorageLow() |
| 88 | + |
| 89 | + assertStorageWarningNotification() |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun handleStorageFull_postsDismissibleStorageSettingsNotification() { |
| 94 | + SmsStorageStatusManager.handleStorageFull() |
| 95 | + |
| 96 | + assertStorageWarningNotification() |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun handleStorageOk_cancelsStorageWarningNotification() { |
| 101 | + SmsStorageStatusManager.handleStorageLow() |
| 102 | + |
| 103 | + SmsStorageStatusManager.handleStorageOk() |
| 104 | + |
| 105 | + assertNull(storageNotification()) |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + fun storageWarnings_doNotPostWhenMessagingIsNotTheDefaultSmsApp() { |
| 110 | + every { phoneUtils.isSmsEnabled() } returns false |
| 111 | + |
| 112 | + SmsStorageStatusManager.handleStorageLow() |
| 113 | + SmsStorageStatusManager.handleStorageFull() |
| 114 | + |
| 115 | + assertEquals(0, shadowOf(notificationManager()).size()) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun lowStorageNotification_usesGenericSettingsWhenStorageSettingsCannotBeResolved() { |
| 120 | + shadowOf(context.packageManager).setResolveInfosForIntent( |
| 121 | + Intent(Settings.ACTION_INTERNAL_STORAGE_SETTINGS), |
| 122 | + emptyList(), |
| 123 | + ) |
| 124 | + |
| 125 | + val pendingIntent = UIIntentsImpl().getPendingIntentForLowStorageNotifications(context) |
| 126 | + |
| 127 | + assertEquals(Settings.ACTION_SETTINGS, shadowOf(pendingIntent).savedIntent.action) |
| 128 | + } |
| 129 | + |
| 130 | + private fun assertStorageWarningNotification() { |
| 131 | + val notification = requireNotNull(storageNotification()) |
| 132 | + |
| 133 | + assertEquals( |
| 134 | + STORAGE_WARNING_TITLE, |
| 135 | + notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString(), |
| 136 | + ) |
| 137 | + assertEquals( |
| 138 | + STORAGE_WARNING_TICKER, |
| 139 | + notification.tickerText.toString(), |
| 140 | + ) |
| 141 | + assertEquals( |
| 142 | + STORAGE_WARNING_TEXT, |
| 143 | + notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString(), |
| 144 | + ) |
| 145 | + assertEquals( |
| 146 | + STORAGE_WARNING_TEXT, |
| 147 | + notification.extras.getCharSequence(Notification.EXTRA_BIG_TEXT).toString(), |
| 148 | + ) |
| 149 | + assertEquals(R.drawable.ic_sms_light, notification.smallIcon.resId) |
| 150 | + assertFalse(notification.flags and Notification.FLAG_ONGOING_EVENT != 0) |
| 151 | + assertFalse(notification.flags and Notification.FLAG_AUTO_CANCEL != 0) |
| 152 | + |
| 153 | + val contentIntent = requireNotNull(notification.contentIntent) |
| 154 | + val shadowPendingIntent = shadowOf(contentIntent) |
| 155 | + assertTrue(shadowPendingIntent.isActivity) |
| 156 | + assertTrue(shadowPendingIntent.isImmutable) |
| 157 | + assertEquals( |
| 158 | + Settings.ACTION_INTERNAL_STORAGE_SETTINGS, |
| 159 | + shadowPendingIntent.savedIntent.action, |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + private fun storageNotification(): Notification? { |
| 164 | + return shadowOf(notificationManager()).getNotification( |
| 165 | + "${context.packageName}:smsstoragelow", |
| 166 | + PendingIntentConstants.SMS_STORAGE_LOW_NOTIFICATION_ID, |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + private fun notificationManager(): NotificationManager { |
| 171 | + return requireNotNull(context.getSystemService(NotificationManager::class.java)) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +@Implements(Resources::class) |
| 176 | +internal class StorageWarningResourcesShadow { |
| 177 | + |
| 178 | + @RealObject |
| 179 | + private lateinit var realResources: Resources |
| 180 | + |
| 181 | + @Implementation |
| 182 | + fun getString(resourceId: Int): String { |
| 183 | + return when (resourceId) { |
| 184 | + R.string.sms_storage_low_title -> STORAGE_WARNING_TITLE |
| 185 | + R.string.sms_storage_low_text -> STORAGE_WARNING_TEXT |
| 186 | + R.string.sms_storage_low_notification_ticker -> STORAGE_WARNING_TICKER |
| 187 | + else -> directlyOn( |
| 188 | + realResources, |
| 189 | + Resources::class.java, |
| 190 | + "getString", |
| 191 | + ClassParameter.from(Int::class.javaPrimitiveType!!, resourceId), |
| 192 | + ) |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments