Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c8268dbe authored by cketti's avatar cketti
Browse files

Convert notification tests to Kotlin

parent 140da149
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -28,8 +28,10 @@ dependencies {
    testImplementation project(":mail:protocols:smtp")
    testImplementation project(":app:storage")
    testImplementation project(":app:testing")
    testImplementation "org.jetbrains.kotlin:kotlin-test:${versions.kotlin}"
    testImplementation "org.jetbrains.kotlin:kotlin-reflect:${versions.kotlin}"
    testImplementation "org.robolectric:robolectric:${versions.robolectric}"
    testImplementation "androidx.test:core:${versions.androidxTestCore}"
    testImplementation "junit:junit:${versions.junit}"
    testImplementation "com.google.truth:truth:${versions.truth}"
    testImplementation "org.mockito:mockito-core:${versions.mockito}"
+36 −42
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import com.fsck.k9.controller.MessageReference;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;


public class AddNotificationResultTest {
    private static final int NOTIFICATION_ID = 23;


    private NotificationHolder notificationHolder;


    @Before
    public void setUp() throws Exception {
        MessageReference messageReference = new MessageReference("irrelevant", 1, "irrelevant", null);
        NotificationContent notificationContent = new NotificationContent(messageReference, "irrelevant", "irrelevant",
                "irrelevant", "irrelevant", false);
        notificationHolder = new NotificationHolder(NOTIFICATION_ID, notificationContent);
    }
package com.fsck.k9.notification

import com.fsck.k9.controller.MessageReference
import com.google.common.truth.Truth.assertThat
import org.junit.Test

private const val NOTIFICATION_ID = 23

class AddNotificationResultTest {
    private val notificationHolder = NotificationHolder(
        notificationId = NOTIFICATION_ID,
        content = NotificationContent(
            messageReference = MessageReference("irrelevant", 1, "irrelevant", null),
            sender = "irrelevant",
            subject = "irrelevant",
            preview = "irrelevant",
            summary = "irrelevant",
            isStarred = false
        )
    )

    @Test
    public void newNotification_shouldCancelNotification_shouldReturnFalse() throws Exception {
        AddNotificationResult result = AddNotificationResult.newNotification(notificationHolder);
    fun newNotification_shouldCancelNotification_shouldReturnFalse() {
        val result = AddNotificationResult.newNotification(notificationHolder)

        assertFalse(result.shouldCancelNotification());
        assertThat(result.shouldCancelNotification).isFalse()
    }

    @Test(expected = IllegalStateException.class)
    public void newNotification_getNotificationId_shouldReturnNotificationId() throws Exception {
        AddNotificationResult result = AddNotificationResult.newNotification(notificationHolder);
    @Test(expected = IllegalStateException::class)
    fun newNotification_getNotificationId_shouldReturnNotificationId() {
        val result = AddNotificationResult.newNotification(notificationHolder)

        result.getNotificationId();
        result.notificationId
    }

    @Test
    public void replaceNotification_shouldCancelNotification_shouldReturnTrue() throws Exception {
        AddNotificationResult result = AddNotificationResult.replaceNotification(notificationHolder);
    fun replaceNotification_shouldCancelNotification_shouldReturnTrue() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertTrue(result.shouldCancelNotification());
        assertThat(result.shouldCancelNotification).isTrue()
    }

    @Test
    public void replaceNotification_getNotificationId_shouldReturnNotificationId() throws Exception {
        AddNotificationResult result = AddNotificationResult.replaceNotification(notificationHolder);
    fun replaceNotification_getNotificationId_shouldReturnNotificationId() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertEquals(NOTIFICATION_ID, result.getNotificationId());
        assertThat(result.notificationId).isEqualTo(NOTIFICATION_ID)
    }

    @Test
    public void getNotificationHolder_shouldReturnNotificationHolder() throws Exception {
        AddNotificationResult result = AddNotificationResult.replaceNotification(notificationHolder);
    fun getNotificationHolder_shouldReturnNotificationHolder() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertEquals(notificationHolder, result.getNotificationHolder());
        assertThat(result.notificationHolder).isEqualTo(notificationHolder)
    }
}
+77 −115
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import android.app.Notification;
import android.app.PendingIntent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.Builder;
import androidx.core.app.NotificationManagerCompat;

import com.fsck.k9.Account;
import com.fsck.k9.testing.MockHelper;
import com.fsck.k9.RobolectricTest;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.RuntimeEnvironment;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class AuthenticationErrorNotificationsTest extends RobolectricTest {
    private static final boolean INCOMING = true;
    private static final boolean OUTGOING = false;
    private static final int ACCOUNT_NUMBER = 1;
    private static final String ACCOUNT_NAME = "TestAccount";


    private NotificationResourceProvider resourceProvider = new TestNotificationResourceProvider();
    private Notification notification;
    private NotificationManagerCompat notificationManager;
    private NotificationCompat.Builder builder;
    private NotificationHelper notificationHelper;
    private Account account;
    private AuthenticationErrorNotifications authenticationErrorNotifications;
    private PendingIntent contentIntent;


    @Before
    public void setUp() throws Exception {
        notification = createFakeNotification();
        notificationManager = createFakeNotificationManager();
        builder = createFakeNotificationBuilder(notification);
        notificationHelper = createFakeNotificationHelper(notificationManager, builder);
        account = createFakeAccount();
        contentIntent = createFakeContentIntent();

        authenticationErrorNotifications = new TestAuthenticationErrorNotifications();
    }
package com.fsck.k9.notification

import android.app.Notification
import android.app.PendingIntent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.test.core.app.ApplicationProvider
import com.fsck.k9.Account
import com.fsck.k9.RobolectricTest
import com.fsck.k9.testing.MockHelper.mockBuilder
import org.junit.Test
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock

private const val INCOMING = true
private const val OUTGOING = false
private const val ACCOUNT_NUMBER = 1
private const val ACCOUNT_NAME = "TestAccount"

class AuthenticationErrorNotificationsTest : RobolectricTest() {
    private val resourceProvider = TestNotificationResourceProvider()
    private val notification = mock<Notification>()
    private val notificationManager = mock<NotificationManagerCompat>()
    private val builder = createFakeNotificationBuilder(notification)
    private val notificationHelper = createFakeNotificationHelper(notificationManager, builder)
    private val account = createFakeAccount()
    private val authenticationErrorNotifications = TestAuthenticationErrorNotifications()
    private val contentIntent = mock<PendingIntent>()

    @Test
    public void showAuthenticationErrorNotification_withIncomingServer_shouldCreateNotification() throws Exception {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING);
    fun showAuthenticationErrorNotification_withIncomingServer_shouldCreateNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING)

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, INCOMING);
        authenticationErrorNotifications.showAuthenticationErrorNotification(account, INCOMING)

        verify(notificationManager).notify(notificationId, notification);
        assertAuthenticationErrorNotificationContents();
        verify(notificationManager).notify(notificationId, notification)
        assertAuthenticationErrorNotificationContents()
    }

    @Test
    public void clearAuthenticationErrorNotification_withIncomingServer_shouldCancelNotification() throws Exception {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING);
    fun clearAuthenticationErrorNotification_withIncomingServer_shouldCancelNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING)

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, INCOMING);
        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, INCOMING)

        verify(notificationManager).cancel(notificationId);
        verify(notificationManager).cancel(notificationId)
    }

    @Test
    public void showAuthenticationErrorNotification_withOutgoingServer_shouldCreateNotification() throws Exception {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING);
    fun showAuthenticationErrorNotification_withOutgoingServer_shouldCreateNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING)

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, OUTGOING);
        authenticationErrorNotifications.showAuthenticationErrorNotification(account, OUTGOING)

        verify(notificationManager).notify(notificationId, notification);
        assertAuthenticationErrorNotificationContents();
        verify(notificationManager).notify(notificationId, notification)
        assertAuthenticationErrorNotificationContents()
    }

    @Test
    public void clearAuthenticationErrorNotification_withOutgoingServer_shouldCancelNotification() throws Exception {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING);
    fun clearAuthenticationErrorNotification_withOutgoingServer_shouldCancelNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING)

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, OUTGOING);
        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, OUTGOING)

        verify(notificationManager).cancel(notificationId);
        verify(notificationManager).cancel(notificationId)
    }

    private void assertAuthenticationErrorNotificationContents() {
        verify(builder).setSmallIcon(resourceProvider.getIconWarning());
        verify(builder).setTicker("Authentication failed");
        verify(builder).setContentTitle("Authentication failed");
        verify(builder).setContentText("Authentication failed for " + ACCOUNT_NAME + ". Update your server settings.");
        verify(builder).setContentIntent(contentIntent);
        verify(builder).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    private fun assertAuthenticationErrorNotificationContents() {
        verify(builder).setSmallIcon(resourceProvider.iconWarning)
        verify(builder).setTicker("Authentication failed")
        verify(builder).setContentTitle("Authentication failed")
        verify(builder).setContentText("Authentication failed for $ACCOUNT_NAME. Update your server settings.")
        verify(builder).setContentIntent(contentIntent)
        verify(builder).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
    }

    private Notification createFakeNotification() {
        return mock(Notification.class);
    private fun createFakeNotificationBuilder(notification: Notification?): NotificationCompat.Builder {
        return mockBuilder {
            on { build() } doReturn notification
        }

    private NotificationManagerCompat createFakeNotificationManager() {
        return mock(NotificationManagerCompat.class);
    }

    private Builder createFakeNotificationBuilder(Notification notification) {
        Builder builder = MockHelper.mockBuilder(Builder.class);
        when(builder.build()).thenReturn(notification);
        return builder;
    private fun createFakeNotificationHelper(
        notificationManager: NotificationManagerCompat,
        builder: NotificationCompat.Builder
    ): NotificationHelper {
        return mock {
            on { getContext() } doReturn ApplicationProvider.getApplicationContext()
            on { getNotificationManager() } doReturn notificationManager
            on { createNotificationBuilder(any(), any()) } doReturn builder
        }

    private NotificationHelper createFakeNotificationHelper(NotificationManagerCompat notificationManager,
            NotificationCompat.Builder builder) {
        NotificationHelper notificationHelper = mock(NotificationHelper.class);
        when(notificationHelper.getContext()).thenReturn(RuntimeEnvironment.application);
        when(notificationHelper.getNotificationManager()).thenReturn(notificationManager);
        when(notificationHelper.createNotificationBuilder(any(Account.class),
                any(NotificationChannelManager.ChannelType.class)))
                .thenReturn(builder);

        return notificationHelper;
    }

    private Account createFakeAccount() {
        Account account = mock(Account.class);
        when(account.getAccountNumber()).thenReturn(ACCOUNT_NUMBER);
        when(account.getDescription()).thenReturn(ACCOUNT_NAME);

        return account;
    private fun createFakeAccount(): Account {
        return mock {
            on { accountNumber } doReturn ACCOUNT_NUMBER
            on { description } doReturn ACCOUNT_NAME
        }

    private PendingIntent createFakeContentIntent() {
        return mock(PendingIntent.class);
    }

    internal inner class TestAuthenticationErrorNotifications :
        AuthenticationErrorNotifications(notificationHelper, mock(), resourceProvider) {

    class TestAuthenticationErrorNotifications extends AuthenticationErrorNotifications {
        public TestAuthenticationErrorNotifications() {
            super(notificationHelper, mock(NotificationActionCreator.class), resourceProvider);
        }

        @Override
        protected PendingIntent createContentIntent(Account account, boolean incoming) {
            return contentIntent;
        override fun createContentIntent(account: Account, incoming: Boolean): PendingIntent {
            return contentIntent
        }
    }
}
+94 −110
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationCompat.Builder;

import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.K9.NotificationQuickDelete;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.testing.MockHelper;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class BaseNotificationsTest {
    private static final int ACCOUNT_COLOR = 0xAABBCC;
    private static final String ACCOUNT_NAME = "AccountName";
    private static final int ACCOUNT_NUMBER = 2;
    private static final String NOTIFICATION_SUMMARY = "Summary";
    private static final String SENDER = "MessageSender";
    private static final String SUBJECT = "Subject";
    private static final String NOTIFICATION_PREVIEW = "Preview";


    private NotificationResourceProvider resourceProvider = new TestNotificationResourceProvider();
    private TestNotifications notifications;


    @Before
    public void setUp() throws Exception {
        notifications = createTestNotifications();
    }
package com.fsck.k9.notification

import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.K9.NotificationQuickDelete
import com.fsck.k9.controller.MessageReference
import com.fsck.k9.testing.MockHelper.mockBuilder
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.mockito.Mockito.verify
import org.mockito.kotlin.any
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock

private const val ACCOUNT_COLOR = 0xAABBCC
private const val ACCOUNT_NAME = "AccountName"
private const val ACCOUNT_NUMBER = 2
private const val NOTIFICATION_SUMMARY = "Summary"
private const val SENDER = "MessageSender"
private const val SUBJECT = "Subject"
private const val NOTIFICATION_PREVIEW = "Preview"

class BaseNotificationsTest {
    private val resourceProvider = TestNotificationResourceProvider()
    private val notifications = createTestNotifications()

    @Test
    public void testCreateAndInitializeNotificationBuilder() throws Exception {
        Account account = createFakeAccount();
    fun testCreateAndInitializeNotificationBuilder() {
        val account = createFakeAccount()

        Builder builder = notifications.createAndInitializeNotificationBuilder(account);
        val builder = notifications.createAndInitializeNotificationBuilder(account)

        verify(builder).setSmallIcon(resourceProvider.getIconNewMail());
        verify(builder).setColor(ACCOUNT_COLOR);
        verify(builder).setAutoCancel(true);
        verify(builder).setSmallIcon(resourceProvider.iconNewMail)
        verify(builder).color = ACCOUNT_COLOR
        verify(builder).setAutoCancel(true)
    }

    @Test
    public void testIsDeleteActionEnabled_NotificationQuickDelete_ALWAYS() throws Exception {
        K9.setNotificationQuickDeleteBehaviour(NotificationQuickDelete.ALWAYS);
    fun testIsDeleteActionEnabled_NotificationQuickDelete_ALWAYS() {
        K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.ALWAYS

        boolean result = notifications.isDeleteActionEnabled();
        val result = notifications.isDeleteActionEnabled()

        assertTrue(result);
        assertThat(result).isTrue()
    }

    @Test
    public void testIsDeleteActionEnabled_NotificationQuickDelete_FOR_SINGLE_MSG() throws Exception {
        K9.setNotificationQuickDeleteBehaviour(NotificationQuickDelete.FOR_SINGLE_MSG);
    fun testIsDeleteActionEnabled_NotificationQuickDelete_FOR_SINGLE_MSG() {
        K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.FOR_SINGLE_MSG

        boolean result = notifications.isDeleteActionEnabled();
        val result = notifications.isDeleteActionEnabled()

        assertTrue(result);
        assertThat(result).isTrue()
    }

    @Test
    public void testIsDeleteActionEnabled_NotificationQuickDelete_NEVER() throws Exception {
        K9.setNotificationQuickDeleteBehaviour(NotificationQuickDelete.NEVER);
    fun testIsDeleteActionEnabled_NotificationQuickDelete_NEVER() {
        K9.notificationQuickDeleteBehaviour = NotificationQuickDelete.NEVER

        boolean result = notifications.isDeleteActionEnabled();
        val result = notifications.isDeleteActionEnabled()

        assertFalse(result);
        assertThat(result).isFalse()
    }

    @Test
    public void testCreateBigTextStyleNotification() throws Exception {
        Account account = createFakeAccount();
        int notificationId = 23;
        NotificationHolder holder = createNotificationHolder(notificationId);

        Builder builder = notifications.createBigTextStyleNotification(account, holder, notificationId);

        verify(builder).setTicker(NOTIFICATION_SUMMARY);
        verify(builder).setGroup("newMailNotifications-" + ACCOUNT_NUMBER);
        verify(builder).setContentTitle(SENDER);
        verify(builder).setContentText(SUBJECT);
        verify(builder).setSubText(ACCOUNT_NAME);

        BigTextStyle bigTextStyle = notifications.bigTextStyle;
        verify(bigTextStyle).bigText(NOTIFICATION_PREVIEW);

        verify(builder).setStyle(bigTextStyle);
    fun testCreateBigTextStyleNotification() {
        val account = createFakeAccount()
        val notificationId = 23
        val holder = createNotificationHolder(notificationId)

        val builder = notifications.createBigTextStyleNotification(account, holder, notificationId)

        verify(builder).setTicker(NOTIFICATION_SUMMARY)
        verify(builder).setGroup("newMailNotifications-$ACCOUNT_NUMBER")
        verify(builder).setContentTitle(SENDER)
        verify(builder).setContentText(SUBJECT)
        verify(builder).setSubText(ACCOUNT_NAME)
        verify(notifications.bigTextStyle).bigText(NOTIFICATION_PREVIEW)
        verify(builder).setStyle(notifications.bigTextStyle)
    }

    private NotificationHolder createNotificationHolder(int notificationId) {
        MessageReference messageReference = new MessageReference("irrelevant", 1, "irrelevant", null);
        NotificationContent content = new NotificationContent(messageReference, SENDER, SUBJECT, NOTIFICATION_PREVIEW,
                NOTIFICATION_SUMMARY, false);
        return new NotificationHolder(notificationId, content);
    private fun createNotificationHolder(notificationId: Int): NotificationHolder {
        return NotificationHolder(
            notificationId = notificationId,
            content = NotificationContent(
                messageReference = MessageReference("irrelevant", 1, "irrelevant", null),
                sender = SENDER,
                subject = SUBJECT,
                preview = NOTIFICATION_PREVIEW,
                summary = NOTIFICATION_SUMMARY,
                isStarred = false
            )
        )
    }

    private TestNotifications createTestNotifications() {
        NotificationHelper notificationHelper = createFakeNotificationHelper();
        NotificationActionCreator actionCreator = mock(NotificationActionCreator.class);

        return new TestNotifications(notificationHelper, actionCreator, resourceProvider);
    private fun createTestNotifications(): TestNotifications {
        return TestNotifications(
            notificationHelper = createFakeNotificationHelper(),
            actionCreator = mock(),
            resourceProvider = resourceProvider
        )
    }

    private NotificationHelper createFakeNotificationHelper() {
        Builder builder = MockHelper.mockBuilder(Builder.class);
        NotificationHelper notificationHelper = mock(NotificationHelper.class);
        when(notificationHelper.createNotificationBuilder(any(Account.class), any(NotificationChannelManager
                .ChannelType.class))).thenReturn(builder);
        when(notificationHelper.getAccountName(any(Account.class))).thenReturn(ACCOUNT_NAME);
        return notificationHelper;
    private fun createFakeNotificationHelper(): NotificationHelper {
        return mock {
            on { createNotificationBuilder(any(), any()) } doReturn mockBuilder()
            on { getAccountName(any()) } doReturn ACCOUNT_NAME
        }

    private Account createFakeAccount() {
        Account account = mock(Account.class);
        when(account.getAccountNumber()).thenReturn(ACCOUNT_NUMBER);
        when(account.getChipColor()).thenReturn(ACCOUNT_COLOR);
        return account;
    }


    static class TestNotifications extends BaseNotifications {

        BigTextStyle bigTextStyle;

        protected TestNotifications(NotificationHelper notificationHelper, NotificationActionCreator actionCreator,
                NotificationResourceProvider resourceProvider) {
            super(notificationHelper, actionCreator, resourceProvider);
            bigTextStyle = mock(BigTextStyle.class);
    private fun createFakeAccount(): Account {
        return mock {
            on { accountNumber } doReturn ACCOUNT_NUMBER
            on { chipColor } doReturn ACCOUNT_COLOR
        }
    }

    internal class TestNotifications(
        notificationHelper: NotificationHelper,
        actionCreator: NotificationActionCreator,
        resourceProvider: NotificationResourceProvider
    ) : BaseNotifications(notificationHelper, actionCreator, resourceProvider) {
        val bigTextStyle = mock<NotificationCompat.BigTextStyle>()

        @Override
        protected BigTextStyle createBigTextStyle(Builder builder) {
            return bigTextStyle;
        override fun createBigTextStyle(builder: NotificationCompat.Builder?): NotificationCompat.BigTextStyle {
            return bigTextStyle
        }
    }
}
+79 −117

File changed.

Preview size limit exceeded, changes collapsed.

Loading