|
| 1 | +/* |
| 2 | + Copyright 2024 Adobe. All rights reserved. |
| 3 | + This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + Unless required by applicable law or agreed to in writing, software distributed under |
| 7 | + the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 8 | + OF ANY KIND, either express or implied. See the License for the specific language |
| 9 | + governing permissions and limitations under the License. |
| 10 | +*/ |
| 11 | + |
| 12 | +package com.adobe.marketing.mobile |
| 13 | + |
| 14 | +import android.app.Application |
| 15 | +import androidx.core.os.UserManagerCompat |
| 16 | +import com.adobe.marketing.mobile.internal.eventhub.EventHub |
| 17 | +import junit.framework.TestCase.assertTrue |
| 18 | +import org.junit.Before |
| 19 | +import org.junit.Test |
| 20 | +import org.junit.runner.RunWith |
| 21 | +import org.mockito.Mockito |
| 22 | +import org.mockito.kotlin.any |
| 23 | +import org.mockito.kotlin.never |
| 24 | +import org.mockito.kotlin.times |
| 25 | +import org.mockito.kotlin.verify |
| 26 | +import org.robolectric.RobolectricTestRunner |
| 27 | +import org.robolectric.RuntimeEnvironment |
| 28 | +import org.robolectric.annotation.Config |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean |
| 30 | +import kotlin.test.assertFalse |
| 31 | + |
| 32 | +@RunWith(RobolectricTestRunner::class) |
| 33 | +class MobileCoreRobolectricTests { |
| 34 | + |
| 35 | + @Before |
| 36 | + fun setup() { |
| 37 | + MobileCore.sdkInitializedWithContext = AtomicBoolean(false) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @Config(sdk = [23]) |
| 42 | + fun `test setApplication when the device doesn't support direct boot mode`() { |
| 43 | + // Android supports direct boot mode on API level 24 and above |
| 44 | + val app = RuntimeEnvironment.application as Application |
| 45 | + val mockedEventHub = Mockito.mock(EventHub::class.java) |
| 46 | + Mockito.mockStatic(UserManagerCompat::class.java).use { mockedStaticUserManagerCompat -> |
| 47 | + mockedStaticUserManagerCompat.`when`<Any> { UserManagerCompat.isUserUnlocked(Mockito.any()) } |
| 48 | + .thenReturn(false) |
| 49 | + MobileCore.setApplication(app) |
| 50 | + mockedStaticUserManagerCompat.verify({ UserManagerCompat.isUserUnlocked(Mockito.any()) }, never()) |
| 51 | + } |
| 52 | + verify(mockedEventHub, never()).executeInEventHubExecutor(any()) |
| 53 | + assertTrue(MobileCore.sdkInitializedWithContext.get()) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + @Config(sdk = [24]) |
| 58 | + fun `test setApplication when the app is not configured to run in direct boot mode`() { |
| 59 | + val app = RuntimeEnvironment.application as Application |
| 60 | + val mockedEventHub = Mockito.mock(EventHub::class.java) |
| 61 | + EventHub.shared = mockedEventHub |
| 62 | + Mockito.mockStatic(UserManagerCompat::class.java).use { mockedStaticUserManagerCompat -> |
| 63 | + // when initializing SDK, the app is not in direct boot mode (device is unlocked) |
| 64 | + mockedStaticUserManagerCompat.`when`<Any> { UserManagerCompat.isUserUnlocked(Mockito.any()) }.thenReturn(true) |
| 65 | + MobileCore.setApplication(app) |
| 66 | + mockedStaticUserManagerCompat.verify({ UserManagerCompat.isUserUnlocked(Mockito.any()) }, times(1)) |
| 67 | + } |
| 68 | + verify(mockedEventHub, times(1)).executeInEventHubExecutor(any()) |
| 69 | + assertTrue(MobileCore.sdkInitializedWithContext.get()) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @Config(sdk = [24]) |
| 74 | + fun `test setApplication when the app is launched in direct boot mode`() { |
| 75 | + val app = RuntimeEnvironment.application as Application |
| 76 | + val mockedEventHub = Mockito.mock(EventHub::class.java) |
| 77 | + Mockito.mockStatic(UserManagerCompat::class.java).use { mockedStaticUserManagerCompat -> |
| 78 | + // when initializing SDK, the app is in direct boot mode (device is still locked) |
| 79 | + mockedStaticUserManagerCompat.`when`<Any> { UserManagerCompat.isUserUnlocked(Mockito.any()) }.thenReturn(false) |
| 80 | + MobileCore.setApplication(app) |
| 81 | + mockedStaticUserManagerCompat.verify({ UserManagerCompat.isUserUnlocked(Mockito.any()) }, times(1)) |
| 82 | + } |
| 83 | + verify(mockedEventHub, never()).executeInEventHubExecutor(any()) |
| 84 | + assertFalse(MobileCore.sdkInitializedWithContext.get()) |
| 85 | + } |
| 86 | +} |
0 commit comments