|
| 1 | +package com.getcapacitor.community.admob.consent; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.ArgumentMatchers.eq; |
| 5 | +import static org.mockito.Mockito.doAnswer; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | + |
| 8 | +import android.app.Activity; |
| 9 | +import android.content.Context; |
| 10 | +import com.getcapacitor.JSObject; |
| 11 | +import com.getcapacitor.PluginCall; |
| 12 | +import com.google.android.gms.common.util.BiConsumer; |
| 13 | +import com.google.android.ump.ConsentForm; |
| 14 | +import com.google.android.ump.ConsentInformation; |
| 15 | +import com.google.android.ump.FormError; |
| 16 | +import com.google.android.ump.UserMessagingPlatform; |
| 17 | +import org.junit.jupiter.api.AfterEach; |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Nested; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.ArgumentCaptor; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.MockedStatic; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | + |
| 29 | +@ExtendWith(MockitoExtension.class) |
| 30 | +public class AdConsentExecutorTest { |
| 31 | + |
| 32 | + private static final String LOG_TAG = "AdConsentExecutorTest Log Tag"; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private Context contextMock; |
| 36 | + |
| 37 | + @Mock(strictness = Mock.Strictness.LENIENT) |
| 38 | + private Activity activityMock; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private BiConsumer<String, JSObject> notifierMock; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private ConsentInformation mockedConsentInformation; |
| 45 | + |
| 46 | + private MockedStatic<UserMessagingPlatform> mockedUserMessagingPlatform; |
| 47 | + |
| 48 | + private ArgumentCaptor<ConsentForm.OnConsentFormDismissedListener> listenerCaptor; |
| 49 | + |
| 50 | + @Mock |
| 51 | + private PluginCall pluginCallMock; |
| 52 | + |
| 53 | + private AdConsentExecutor adConsentExecutor; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void beforeEach() { |
| 57 | + mockedUserMessagingPlatform = Mockito.mockStatic(UserMessagingPlatform.class); |
| 58 | + mockedUserMessagingPlatform |
| 59 | + .when(() -> UserMessagingPlatform.getConsentInformation(any(Context.class))) |
| 60 | + .thenReturn(mockedConsentInformation); |
| 61 | + listenerCaptor = ArgumentCaptor.forClass(ConsentForm.OnConsentFormDismissedListener.class); |
| 62 | + |
| 63 | + adConsentExecutor = new AdConsentExecutor(() -> contextMock, () -> activityMock, notifierMock, LOG_TAG); |
| 64 | + |
| 65 | + doAnswer(invocation -> { |
| 66 | + Runnable runnable = invocation.getArgument(0); |
| 67 | + runnable.run(); |
| 68 | + return null; |
| 69 | + }) |
| 70 | + .when(activityMock) |
| 71 | + .runOnUiThread(any(Runnable.class)); |
| 72 | + } |
| 73 | + |
| 74 | + @AfterEach |
| 75 | + void afterEach() { |
| 76 | + mockedUserMessagingPlatform.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Nested |
| 80 | + @DisplayName("Show Privacy Options Form Tests") |
| 81 | + class ShowPrivacyOptionsFormTests { |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("should resolve the call on success") |
| 85 | + void showPrivacyOptionsForm_Success() { |
| 86 | + adConsentExecutor.showPrivacyOptionsForm(pluginCallMock, null); |
| 87 | + mockedUserMessagingPlatform.verify(() -> |
| 88 | + UserMessagingPlatform.showPrivacyOptionsForm(eq(activityMock), listenerCaptor.capture()) |
| 89 | + ); |
| 90 | + listenerCaptor.getValue().onConsentFormDismissed(null); |
| 91 | + verify(pluginCallMock).resolve(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @DisplayName("should reject the call on failure") |
| 96 | + void showPrivacyOptionsForm_Failure() { |
| 97 | + FormError testError = new FormError(123, "Test privacy form error"); |
| 98 | + adConsentExecutor.showPrivacyOptionsForm(pluginCallMock, null); |
| 99 | + mockedUserMessagingPlatform.verify(() -> |
| 100 | + UserMessagingPlatform.showPrivacyOptionsForm(eq(activityMock), listenerCaptor.capture()) |
| 101 | + ); |
| 102 | + listenerCaptor.getValue().onConsentFormDismissed(testError); |
| 103 | + verify(pluginCallMock).reject("Error when show privacy form", testError.getMessage()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("should reject the call if activity is null") |
| 108 | + void showPrivacyOptionsForm_NullActivity() { |
| 109 | + adConsentExecutor = new AdConsentExecutor(() -> contextMock, () -> null, notifierMock, LOG_TAG); |
| 110 | + adConsentExecutor.showPrivacyOptionsForm(pluginCallMock, null); |
| 111 | + verify(pluginCallMock).reject("Trying to show the privacy options form but the Activity is null"); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments