<commit_before>/*
 * Copyright (c) 2021 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.notification.widget

import android.app.Notification
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Test

class NotificationWidgetTest {

    @MockK
    private lateinit var notificationManagerCompat: NotificationManagerCompat

    @MockK
    private lateinit var builder: NotificationCompat.Builder

    @MockK
    private lateinit var notification: Notification

    @Before
    fun setUp() {
        MockKAnnotations.init(this)
        every { notificationManagerCompat.cancel(any()) }.answers { Unit }
        every { notificationManagerCompat.notify(any(), any()) }.answers { Unit }

        mockkStatic(NotificationManagerCompat::class)
        every { NotificationManagerCompat.from(any()) }.returns(notificationManagerCompat)

        mockkConstructor(RemoteViewsFactory::class)
        every { anyConstructed<RemoteViewsFactory>().invoke(any()) }.returns(mockk())

        mockkConstructor(NotificationCompat.Builder::class)
        every { anyConstructed<NotificationCompat.Builder>().setSmallIcon(any()) }.returns(builder)
        every { builder.setCustomContentView(any()) }.returns(builder)
        every { builder.setOngoing(any()) }.returns(builder)
        every { builder.setAutoCancel(any()) }.returns(builder)
        every { builder.setVisibility(any()) }.returns(builder)
        every { builder.build() }.returns(notification)
    }

    @After
    fun tearDown() {
        unmockkAll()
    }

    @Test
    fun show() {
        NotificationWidget.show(mockk())

        verify(exactly = 1) { NotificationManagerCompat.from(any()) }
        verify(exactly = 0) { notificationManagerCompat.cancel(any()) }
        verify(exactly = 1) { notificationManagerCompat.notify(any(), any()) }
        verify(exactly = 1) { anyConstructed<RemoteViewsFactory>().invoke(any()) }
        verify(exactly = 1) { anyConstructed<NotificationCompat.Builder>().setSmallIcon(any()) }
        verify(exactly = 1) { builder.setCustomContentView(any()) }
        verify(exactly = 1) { builder.setOngoing(any()) }
        verify(exactly = 1) { builder.setAutoCancel(any()) }
        verify(exactly = 1) { builder.setVisibility(any()) }
        verify(exactly = 1) { builder.build() }
    }

    @Test
    fun hide() {
        NotificationWidget.hide(mockk())

        verify(exactly = 1) { NotificationManagerCompat.from(any()) }
        verify(exactly = 1) { notificationManagerCompat.cancel(any()) }
    }

    @Test
    fun refresh() {
        NotificationWidget.refresh(mockk())

        verify(exactly = 1) { NotificationManagerCompat.from(any()) }
        verify(exactly = 1) { notificationManagerCompat.cancel(any()) }
        verify(exactly = 1) { notificationManagerCompat.notify(any(), any()) }
        verify(exactly = 1) { anyConstructed<RemoteViewsFactory>().invoke(any()) }
        verify(exactly = 1) { anyConstructed<NotificationCompat.Builder>().setSmallIcon(any()) }
        verify(exactly = 1) { builder.setCustomContentView(any()) }
        verify(exactly = 1) { builder.setOngoing(any()) }
        verify(exactly = 1) { builder.setAutoCancel(any()) }
        verify(exactly = 1) { builder.setVisibility(any()) }
        verify(exactly = 1) { builder.build() }
    }

}<commit_msg>refactor #283 Fix incorrect test.<commit_after>/*
 * Copyright (c) 2021 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.notification.widget

import android.app.Notification
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockkConstructor
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Test

class NotificationWidgetTest {

    private lateinit var notificationWidget: NotificationWidget

    @MockK
    private lateinit var notificationManagerCompat: NotificationManagerCompat

    @MockK
    private lateinit var builder: NotificationCompat.Builder

    @MockK
    private lateinit var notification: Notification

    @MockK
    private lateinit var context: Context

    @Before
    fun setUp() {
        MockKAnnotations.init(this)
        every { notificationManagerCompat.cancel(any()) }.answers { Unit }
        every { notificationManagerCompat.notify(any(), any()) }.answers { Unit }
        every { context.getPackageName() }.returns("test")

        mockkStatic(NotificationManagerCompat::class)
        every { NotificationManagerCompat.from(any()) }.returns(notificationManagerCompat)

        mockkConstructor(RemoteViewsFactory::class)
        every { anyConstructed<RemoteViewsFactory>().invoke(any()) }.returns(mockk())

        mockkConstructor(NotificationCompat.Builder::class)
        every { anyConstructed<NotificationCompat.Builder>().setSmallIcon(any()) }.returns(builder)
        every { builder.setCustomContentView(any()) }.returns(builder)
        every { builder.setOngoing(any()) }.returns(builder)
        every { builder.setAutoCancel(any()) }.returns(builder)
        every { builder.setVisibility(any()) }.returns(builder)
        every { builder.build() }.returns(notification)

        notificationWidget = spyk(NotificationWidget, recordPrivateCalls = true)
    }

    @After
    fun tearDown() {
        unmockkAll()
    }

    @Test
    fun refresh() {
        notificationWidget.refresh(context)

        verify(atLeast = 1) { NotificationManagerCompat.from(any()) }
        verify(exactly = 1) { notificationManagerCompat.cancel(any()) }
        verify(exactly = 1) { notificationManagerCompat.notify(any(), any()) }
        verify(exactly = 1) { anyConstructed<RemoteViewsFactory>().invoke(any()) }
        verify(exactly = 1) { anyConstructed<NotificationCompat.Builder>().setSmallIcon(any()) }
        verify(exactly = 1) { builder.setCustomContentView(any()) }
        verify(exactly = 1) { builder.setOngoing(any()) }
        verify(exactly = 1) { builder.setAutoCancel(any()) }
        verify(exactly = 1) { builder.setVisibility(any()) }
        verify(exactly = 1) { builder.build() }
    }

}
{'fraction_non_alphanumeric': 0.1175480432038154, 'fraction_numerical': 0.007153878524337214, 'mean_word_length': 3.734395750332005, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4158003', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1969, 'n_words': 405}