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

Unverified Commit a36d7e9d authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5633 from k9mail/notification_tests

Convert notification tests to Kotlin
parents d5fc337a c8268dbe
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}"
+0 −62
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);
    }

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

        assertFalse(result.shouldCancelNotification());
    }

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

        result.getNotificationId();
    }

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

        assertTrue(result.shouldCancelNotification());
    }

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

        assertEquals(NOTIFICATION_ID, result.getNotificationId());
    }

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

        assertEquals(notificationHolder, result.getNotificationHolder());
    }
}
+56 −0
Original line number Diff line number Diff line
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
    fun newNotification_shouldCancelNotification_shouldReturnFalse() {
        val result = AddNotificationResult.newNotification(notificationHolder)

        assertThat(result.shouldCancelNotification).isFalse()
    }

    @Test(expected = IllegalStateException::class)
    fun newNotification_getNotificationId_shouldReturnNotificationId() {
        val result = AddNotificationResult.newNotification(notificationHolder)

        result.notificationId
    }

    @Test
    fun replaceNotification_shouldCancelNotification_shouldReturnTrue() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertThat(result.shouldCancelNotification).isTrue()
    }

    @Test
    fun replaceNotification_getNotificationId_shouldReturnNotificationId() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertThat(result.notificationId).isEqualTo(NOTIFICATION_ID)
    }

    @Test
    fun getNotificationHolder_shouldReturnNotificationHolder() {
        val result = AddNotificationResult.replaceNotification(notificationHolder)

        assertThat(result.notificationHolder).isEqualTo(notificationHolder)
    }
}
+0 −148
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();
    }

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

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, INCOMING);

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

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

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, INCOMING);

        verify(notificationManager).cancel(notificationId);
    }

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

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, OUTGOING);

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

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

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, OUTGOING);

        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 Notification createFakeNotification() {
        return mock(Notification.class);
    }

    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 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 PendingIntent createFakeContentIntent() {
        return mock(PendingIntent.class);
    }


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

        @Override
        protected PendingIntent createContentIntent(Account account, boolean incoming) {
            return contentIntent;
        }
    }
}
+110 −0
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.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
    fun showAuthenticationErrorNotification_withIncomingServer_shouldCreateNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING)

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, INCOMING)

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

    @Test
    fun clearAuthenticationErrorNotification_withIncomingServer_shouldCancelNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, INCOMING)

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, INCOMING)

        verify(notificationManager).cancel(notificationId)
    }

    @Test
    fun showAuthenticationErrorNotification_withOutgoingServer_shouldCreateNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING)

        authenticationErrorNotifications.showAuthenticationErrorNotification(account, OUTGOING)

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

    @Test
    fun clearAuthenticationErrorNotification_withOutgoingServer_shouldCancelNotification() {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, OUTGOING)

        authenticationErrorNotifications.clearAuthenticationErrorNotification(account, OUTGOING)

        verify(notificationManager).cancel(notificationId)
    }

    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 fun createFakeNotificationBuilder(notification: Notification?): NotificationCompat.Builder {
        return mockBuilder {
            on { build() } doReturn notification
        }
    }

    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 fun createFakeAccount(): Account {
        return mock {
            on { accountNumber } doReturn ACCOUNT_NUMBER
            on { description } doReturn ACCOUNT_NAME
        }
    }

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

        override fun createContentIntent(account: Account, incoming: Boolean): PendingIntent {
            return contentIntent
        }
    }
}
Loading