Skip to content

Commit

Permalink
Fix status object (#1529)
Browse files Browse the repository at this point in the history
* Fix status object

* Fix test
  • Loading branch information
rlepinski authored Sep 30, 2024
1 parent e021a14 commit ecee46e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ public void enableUserNotifications(PermissionPromptFallback promptFallback, Con
preferenceDataStore.put(USER_NOTIFICATIONS_ENABLED_KEY, true);
permissionsManager.requestPermission(Permission.DISPLAY_NOTIFICATIONS, false, promptFallback, (result) -> {
consumer.accept(result.getPermissionStatus() == PermissionStatus.GRANTED);
updateStatusObserver();
});
}

Expand Down Expand Up @@ -1248,7 +1249,7 @@ public PushNotificationStatus getPushNotificationStatus() {
);
}

private void updateStatusObserver() {
void updateStatusObserver() {
this.statusObserver.update(getPushNotificationStatus());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ public suspend fun PushManager.enableUserNotifications(promptFallback: Permissio
permission = Permission.DISPLAY_NOTIFICATIONS,
fallback = promptFallback
)
updateStatusObserver()

return result.permissionStatus == PermissionStatus.GRANTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import com.urbanairship.permission.PermissionsManager;
import com.urbanairship.push.notifications.NotificationActionButtonGroup;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -723,6 +726,35 @@ public void testPushStatusNoToken() {
);
}

public void testPushStatusChangedListenerEnableNotifications() {
PushNotificationStatusListener listener = mock(PushNotificationStatusListener.class);

pushManager.setUserNotificationsEnabled(false);
pushManager.addNotificationStatusListener(listener);

TestConsumer<Boolean> consumer = new TestConsumer<>();

doAnswer(invocation -> {
Consumer<PermissionRequestResult> callback = invocation.getArgument(3);
callback.accept(PermissionRequestResult.granted());
return null;
}).when(mockPermissionManager).requestPermission(
eq(Permission.DISPLAY_NOTIFICATIONS),
eq(false),
eq(PermissionPromptFallback.None.INSTANCE),
any()
);

pushManager.enableUserNotifications(consumer);

assertTrue(consumer.lastResult);
assertTrue(pushManager.getUserNotificationsEnabled());

pushManager.enableUserNotifications(TestCase::assertTrue);

verify(listener, times(1)).onChange(any());
}

@Test
public void testPushStatusChanges() {
pushManager.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public class PagerControllerTest {
public fun testCreateView() {
val context: Context = mockk(relaxed = true)
val viewEnv: ViewEnvironment = mockk(relaxed = true)
pagerController.createView(context, viewEnv)
pagerController.createView(context, viewEnv, null)

verify { mockView.createView(eq(context), eq(viewEnv)) }
verify { mockView.createView(eq(context), eq(viewEnv), null) }
}

private companion object {
Expand Down

0 comments on commit ecee46e

Please sign in to comment.