|
| 1 | +package com.onesignal.notifications.internal.badges |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.pm.ApplicationInfo |
| 5 | +import android.content.pm.PackageManager |
| 6 | +import android.os.Build |
| 7 | +import com.onesignal.core.internal.application.IApplicationService |
| 8 | +import com.onesignal.core.internal.database.ICursor |
| 9 | +import com.onesignal.core.internal.database.IDatabase |
| 10 | +import com.onesignal.core.internal.database.IDatabaseProvider |
| 11 | +import com.onesignal.notifications.internal.badges.impl.BadgeCountUpdater |
| 12 | +import com.onesignal.notifications.internal.badges.impl.shortcutbadger.ShortcutBadger |
| 13 | +import com.onesignal.notifications.internal.common.NotificationHelper |
| 14 | +import com.onesignal.notifications.internal.data.INotificationQueryHelper |
| 15 | +import io.kotest.core.spec.style.FunSpec |
| 16 | +import io.mockk.Runs |
| 17 | +import io.mockk.every |
| 18 | +import io.mockk.just |
| 19 | +import io.mockk.mockk |
| 20 | +import io.mockk.mockkObject |
| 21 | +import io.mockk.mockkStatic |
| 22 | +import io.mockk.unmockkObject |
| 23 | +import io.mockk.unmockkStatic |
| 24 | +import io.mockk.verify |
| 25 | + |
| 26 | +private class Mocks { |
| 27 | + val applicationService = mockk<IApplicationService>() |
| 28 | + val queryHelper = mockk<INotificationQueryHelper>(relaxed = true) |
| 29 | + val database = mockk<IDatabase>(relaxed = true) |
| 30 | + val databaseProvider = |
| 31 | + mockk<IDatabaseProvider> { |
| 32 | + every { os } returns database |
| 33 | + } |
| 34 | + |
| 35 | + init { |
| 36 | + val context = mockk<Context>() |
| 37 | + val packageManager = mockk<PackageManager>() |
| 38 | + val applicationInfo = ApplicationInfo() |
| 39 | + |
| 40 | + every { applicationService.appContext } returns context |
| 41 | + every { context.packageManager } returns packageManager |
| 42 | + every { context.packageName } returns "com.onesignal.example" |
| 43 | + every { queryHelper.recentUninteractedWithNotificationsWhere() } returns StringBuilder("1=1") |
| 44 | + every { |
| 45 | + packageManager.getApplicationInfo("com.onesignal.example", PackageManager.GET_META_DATA) |
| 46 | + } returns applicationInfo |
| 47 | + } |
| 48 | + |
| 49 | + fun queryReturnsCount(count: Int) { |
| 50 | + val cursor = mockk<ICursor>() |
| 51 | + every { cursor.count } returns count |
| 52 | + every { |
| 53 | + database.query(any(), any(), any(), any(), any(), any(), any(), any(), any()) |
| 54 | + } answers { |
| 55 | + arg<(ICursor) -> Unit>(8).invoke(cursor) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + fun badgeCountUpdater(sdkInt: Int) = |
| 60 | + BadgeCountUpdater.createForTesting( |
| 61 | + applicationService, |
| 62 | + queryHelper, |
| 63 | + databaseProvider, |
| 64 | + sdkInt, |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +class BadgeCountUpdaterTests : FunSpec({ |
| 69 | + beforeEach { |
| 70 | + mockkObject(NotificationHelper) |
| 71 | + every { NotificationHelper.areNotificationsEnabled(any()) } returns true |
| 72 | + mockkStatic(ShortcutBadger::class) |
| 73 | + every { ShortcutBadger.applyCountOrThrow(any(), any()) } just Runs |
| 74 | + } |
| 75 | + |
| 76 | + afterEach { |
| 77 | + unmockkStatic(ShortcutBadger::class) |
| 78 | + unmockkObject(NotificationHelper) |
| 79 | + } |
| 80 | + |
| 81 | + test("update should not use ShortcutBadger on Android O") { |
| 82 | + Mocks().badgeCountUpdater(Build.VERSION_CODES.O).update() |
| 83 | + |
| 84 | + verify(exactly = 0) { ShortcutBadger.applyCountOrThrow(any(), any()) } |
| 85 | + } |
| 86 | + |
| 87 | + test("updateCount should not use ShortcutBadger on Android O") { |
| 88 | + Mocks().badgeCountUpdater(Build.VERSION_CODES.O).updateCount(3) |
| 89 | + |
| 90 | + verify(exactly = 0) { ShortcutBadger.applyCountOrThrow(any(), any()) } |
| 91 | + } |
| 92 | + |
| 93 | + test("update should use ShortcutBadger on Android N MR1") { |
| 94 | + every { NotificationHelper.getActiveNotifications(any()) } returns emptyArray() |
| 95 | + |
| 96 | + Mocks().badgeCountUpdater(Build.VERSION_CODES.N_MR1).update() |
| 97 | + |
| 98 | + verify(exactly = 1) { ShortcutBadger.applyCountOrThrow(any(), 0) } |
| 99 | + } |
| 100 | + |
| 101 | + test("update should use ShortcutBadger before Android M") { |
| 102 | + val mocks = Mocks() |
| 103 | + mocks.queryReturnsCount(3) |
| 104 | + |
| 105 | + mocks.badgeCountUpdater(Build.VERSION_CODES.LOLLIPOP_MR1).update() |
| 106 | + |
| 107 | + verify(exactly = 1) { ShortcutBadger.applyCountOrThrow(any(), 3) } |
| 108 | + } |
| 109 | + |
| 110 | + test("updateCount should use ShortcutBadger before Android O") { |
| 111 | + Mocks().badgeCountUpdater(Build.VERSION_CODES.O - 1).updateCount(3) |
| 112 | + |
| 113 | + verify(exactly = 1) { ShortcutBadger.applyCountOrThrow(any(), 3) } |
| 114 | + } |
| 115 | +}) |
0 commit comments