Convert class 'Clock' to an interface
This commit is contained in:
parent
bbe015eea8
commit
20dfc9a75b
9 changed files with 27 additions and 45 deletions
|
@ -1,32 +1,13 @@
|
||||||
/*
|
package com.fsck.k9
|
||||||
* Copyright (C) 2010 The Android Open Source Project
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.fsck.k9;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class provide the current time (like {@link System#currentTimeMillis()}).
|
* An interface to provide the current time.
|
||||||
* It's intended to be mocked out for unit tests.
|
|
||||||
*/
|
*/
|
||||||
public class Clock {
|
interface Clock {
|
||||||
public static final Clock INSTANCE = new Clock();
|
val time: Long
|
||||||
|
|
||||||
protected Clock() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTime() {
|
internal class RealClock : Clock {
|
||||||
return System.currentTimeMillis();
|
override val time: Long
|
||||||
}
|
get() = System.currentTimeMillis()
|
||||||
}
|
}
|
||||||
|
|
|
@ -268,7 +268,8 @@ object K9 : EarlyInit {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
val quietTimeChecker = QuietTimeChecker(Clock.INSTANCE, quietTimeStarts, quietTimeEnds)
|
val clock = DI.get<Clock>()
|
||||||
|
val quietTimeChecker = QuietTimeChecker(clock, quietTimeStarts, quietTimeEnds)
|
||||||
return quietTimeChecker.isQuietTime
|
return quietTimeChecker.isQuietTime
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ val mainModule = module {
|
||||||
single { TrustManagerFactory.createInstance(get()) }
|
single { TrustManagerFactory.createInstance(get()) }
|
||||||
single { LocalKeyStoreManager(get()) }
|
single { LocalKeyStoreManager(get()) }
|
||||||
single<TrustedSocketFactory> { DefaultTrustedSocketFactory(get(), get()) }
|
single<TrustedSocketFactory> { DefaultTrustedSocketFactory(get(), get()) }
|
||||||
single { Clock.INSTANCE }
|
single<Clock> { RealClock() }
|
||||||
factory { ServerNameSuggester() }
|
factory { ServerNameSuggester() }
|
||||||
factory { EmailAddressValidator() }
|
factory { EmailAddressValidator() }
|
||||||
factory { ServerSettingsSerializer() }
|
factory { ServerSettingsSerializer() }
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package com.fsck.k9.job
|
package com.fsck.k9.job
|
||||||
|
|
||||||
import androidx.work.WorkerFactory
|
import androidx.work.WorkerFactory
|
||||||
import com.fsck.k9.Clock
|
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
|
|
||||||
val jobModule = module {
|
val jobModule = module {
|
||||||
|
@ -9,5 +8,5 @@ val jobModule = module {
|
||||||
single<WorkerFactory> { K9WorkerFactory(get(), get()) }
|
single<WorkerFactory> { K9WorkerFactory(get(), get()) }
|
||||||
single { get<WorkManagerProvider>().getWorkManager() }
|
single { get<WorkManagerProvider>().getWorkManager() }
|
||||||
single { K9JobManager(get(), get(), get()) }
|
single { K9JobManager(get(), get(), get()) }
|
||||||
factory { MailSyncWorkerManager(get(), Clock.INSTANCE) }
|
factory { MailSyncWorkerManager(workManager = get(), clock = get()) }
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,9 @@ import java.util.Calendar
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.assertTrue
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.mockito.Mockito.mock
|
|
||||||
import org.mockito.kotlin.whenever
|
|
||||||
|
|
||||||
class QuietTimeCheckerTest {
|
class QuietTimeCheckerTest {
|
||||||
private val clock = mock(Clock::class.java)
|
private val clock = TestClock()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun endTimeBeforeStartTime_timeIsBeforeEndOfQuietTime() {
|
fun endTimeBeforeStartTime_timeIsBeforeEndOfQuietTime() {
|
||||||
|
@ -113,7 +111,6 @@ class QuietTimeCheckerTest {
|
||||||
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
|
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
|
||||||
calendar.set(Calendar.MINUTE, minute)
|
calendar.set(Calendar.MINUTE, minute)
|
||||||
|
|
||||||
val timeInMillis = calendar.timeInMillis
|
clock.time = calendar.timeInMillis
|
||||||
whenever(clock.time).thenReturn(timeInMillis)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
5
app/testing/src/main/java/com/fsck/k9/TestClock.kt
Normal file
5
app/testing/src/main/java/com/fsck/k9/TestClock.kt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package com.fsck.k9
|
||||||
|
|
||||||
|
class TestClock(initialTime: Long = 0L) : Clock {
|
||||||
|
override var time: Long = initialTime
|
||||||
|
}
|
|
@ -70,6 +70,7 @@ class MessageListFragment :
|
||||||
private val folderNameFormatter: FolderNameFormatter by lazy { folderNameFormatterFactory.create(requireContext()) }
|
private val folderNameFormatter: FolderNameFormatter by lazy { folderNameFormatterFactory.create(requireContext()) }
|
||||||
private val messagingController: MessagingController by inject()
|
private val messagingController: MessagingController by inject()
|
||||||
private val preferences: Preferences by inject()
|
private val preferences: Preferences by inject()
|
||||||
|
private val clock: Clock by inject()
|
||||||
|
|
||||||
private val handler = MessageListHandler(this)
|
private val handler = MessageListHandler(this)
|
||||||
private val activityListener = MessageListActivityListener()
|
private val activityListener = MessageListActivityListener()
|
||||||
|
@ -252,7 +253,7 @@ class MessageListFragment :
|
||||||
contactsPictureLoader = ContactPicture.getContactPictureLoader(),
|
contactsPictureLoader = ContactPicture.getContactPictureLoader(),
|
||||||
listItemListener = this,
|
listItemListener = this,
|
||||||
appearance = messageListAppearance,
|
appearance = messageListAppearance,
|
||||||
relativeDateTimeFormatter = RelativeDateTimeFormatter(requireContext(), Clock.INSTANCE)
|
relativeDateTimeFormatter = RelativeDateTimeFormatter(requireContext(), clock)
|
||||||
)
|
)
|
||||||
|
|
||||||
adapter.activeMessage = activeMessage
|
adapter.activeMessage = activeMessage
|
||||||
|
|
|
@ -13,11 +13,11 @@ import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import com.fsck.k9.Account
|
import com.fsck.k9.Account
|
||||||
import com.fsck.k9.Clock
|
|
||||||
import com.fsck.k9.FontSizes
|
import com.fsck.k9.FontSizes
|
||||||
import com.fsck.k9.FontSizes.FONT_DEFAULT
|
import com.fsck.k9.FontSizes.FONT_DEFAULT
|
||||||
import com.fsck.k9.FontSizes.LARGE
|
import com.fsck.k9.FontSizes.LARGE
|
||||||
import com.fsck.k9.RobolectricTest
|
import com.fsck.k9.RobolectricTest
|
||||||
|
import com.fsck.k9.TestClock
|
||||||
import com.fsck.k9.contacts.ContactPictureLoader
|
import com.fsck.k9.contacts.ContactPictureLoader
|
||||||
import com.fsck.k9.mail.Address
|
import com.fsck.k9.mail.Address
|
||||||
import com.fsck.k9.textString
|
import com.fsck.k9.textString
|
||||||
|
@ -464,7 +464,7 @@ class MessageListAdapterTest : RobolectricTest() {
|
||||||
contactsPictureLoader = contactsPictureLoader,
|
contactsPictureLoader = contactsPictureLoader,
|
||||||
listItemListener = listItemListener,
|
listItemListener = listItemListener,
|
||||||
appearance = appearance,
|
appearance = appearance,
|
||||||
relativeDateTimeFormatter = RelativeDateTimeFormatter(context, Clock.INSTANCE)
|
relativeDateTimeFormatter = RelativeDateTimeFormatter(context, TestClock())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package com.fsck.k9.ui.helper
|
package com.fsck.k9.ui.helper
|
||||||
|
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
import com.fsck.k9.Clock
|
|
||||||
import com.fsck.k9.RobolectricTest
|
import com.fsck.k9.RobolectricTest
|
||||||
|
import com.fsck.k9.TestClock
|
||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
|
@ -10,8 +10,6 @@ import java.util.TimeZone
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.mockito.Mockito
|
|
||||||
import org.mockito.kotlin.whenever
|
|
||||||
import org.robolectric.RuntimeEnvironment
|
import org.robolectric.RuntimeEnvironment
|
||||||
import org.robolectric.annotation.Config
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@ -19,7 +17,7 @@ import org.robolectric.annotation.Config
|
||||||
class RelativeDateTimeFormatterTest : RobolectricTest() {
|
class RelativeDateTimeFormatterTest : RobolectricTest() {
|
||||||
|
|
||||||
private val context = RuntimeEnvironment.application.applicationContext
|
private val context = RuntimeEnvironment.application.applicationContext
|
||||||
private val clock = Mockito.mock(Clock::class.java)
|
private val clock = TestClock()
|
||||||
private val dateTimeFormatter = RelativeDateTimeFormatter(context, clock)
|
private val dateTimeFormatter = RelativeDateTimeFormatter(context, clock)
|
||||||
|
|
||||||
private val zoneId = "Europe/Berlin"
|
private val zoneId = "Europe/Berlin"
|
||||||
|
@ -123,7 +121,7 @@ class RelativeDateTimeFormatterTest : RobolectricTest() {
|
||||||
val dateTime = LocalDateTime.parse(time)
|
val dateTime = LocalDateTime.parse(time)
|
||||||
val timeInMillis = dateTime.toEpochMillis()
|
val timeInMillis = dateTime.toEpochMillis()
|
||||||
SystemClock.setCurrentTimeMillis(timeInMillis) // Is handled by ShadowSystemClock
|
SystemClock.setCurrentTimeMillis(timeInMillis) // Is handled by ShadowSystemClock
|
||||||
whenever(clock.time).thenReturn(timeInMillis)
|
clock.time = timeInMillis
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.toEpochMillis() = LocalDateTime.parse(this).toEpochMillis()
|
private fun String.toEpochMillis() = LocalDateTime.parse(this).toEpochMillis()
|
||||||
|
|
Loading…
Reference in a new issue