Skip to content

Commit

Permalink
Add class test and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Oct 2, 2023
1 parent 4b89471 commit 3a97abe
Showing 11 changed files with 120 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -34,6 +34,27 @@ sealed class TunnelState : Parcelable {
}
}

override fun toString(): String =
when (this) {
is Disconnected -> DISCONNECTED
is Connecting -> CONNECTING
is Connected -> CONNECTED
is Disconnecting -> {
if (actionAfterDisconnect == ActionAfterDisconnect.Reconnect) {
RECONNECTING
} else {
DISCONNECTING
}
}
is Error -> {
if (errorState.isBlocking) {
BLOCKING
} else {
ERROR
}
}
}

companion object {
const val DISCONNECTED = "disconnected"
const val CONNECTING = "connecting"
@@ -58,25 +79,4 @@ sealed class TunnelState : Parcelable {
}
}
}

override fun toString(): String =
when (this) {
is Disconnected -> DISCONNECTED
is Connecting -> CONNECTING
is Connected -> CONNECTED
is Disconnecting -> {
if (actionAfterDisconnect == ActionAfterDisconnect.Reconnect) {
RECONNECTING
} else {
DISCONNECTING
}
}
is Error -> {
if (errorState.isBlocking) {
BLOCKING
} else {
ERROR
}
}
}
}
Original file line number Diff line number Diff line change
@@ -28,13 +28,6 @@ import net.mullvad.talpid.TalpidVpnService
import org.koin.core.context.loadKoinModules

class MullvadVpnService : TalpidVpnService() {
companion object {
private val TAG = "mullvad"

init {
System.loadLibrary("mullvad_jni")
}
}

private enum class PendingAction {
Connect,
@@ -273,4 +266,12 @@ class MullvadVpnService : TalpidVpnService() {

startActivity(intent)
}

companion object {
private val TAG = "mullvad"

init {
System.loadLibrary("mullvad_jni")
}
}
}
Original file line number Diff line number Diff line change
@@ -17,15 +17,6 @@ import net.mullvad.mullvadvpn.model.GetAccountDataResult
import net.mullvad.talpid.util.EventNotifier

class AccountCache(private val endpoint: ServiceEndpoint) {
companion object {
private sealed class Command {
object CreateAccount : Command()

data class Login(val account: String) : Command()

object Logout : Command()
}
}

private val commandChannel = spawnActor()

@@ -179,4 +170,14 @@ class AccountCache(private val endpoint: ServiceEndpoint) {
private suspend fun fetchAccountData(accountToken: String): GetAccountDataResult {
return daemon.await().getAccountData(accountToken)
}

companion object {
private sealed class Command {
object CreateAccount : Command()

data class Login(val account: String) : Command()

object Logout : Command()
}
}
}
Original file line number Diff line number Diff line change
@@ -11,12 +11,6 @@ import net.mullvad.mullvadvpn.lib.ipc.Event
import net.mullvad.mullvadvpn.lib.ipc.Request

class AuthTokenCache(endpoint: ServiceEndpoint) {
companion object {
private enum class Command {
Fetch
}
}

private val daemon = endpoint.intermittentDaemon
private val requestQueue = spawnActor()

@@ -46,4 +40,10 @@ class AuthTokenCache(endpoint: ServiceEndpoint) {
// Closed sender, so stop the actor
}
}

companion object {
private enum class Command {
Fetch
}
}
}
Original file line number Diff line number Diff line change
@@ -22,12 +22,6 @@ import net.mullvad.mullvadvpn.service.util.ExponentialBackoff
import net.mullvad.talpid.tunnel.ActionAfterDisconnect

class LocationInfoCache(private val endpoint: ServiceEndpoint) {
companion object {
private enum class RequestFetch {
ForRealLocation,
ForRelayLocation,
}
}

private val fetchRetryDelays =
ExponentialBackoff().apply {
@@ -135,4 +129,11 @@ class LocationInfoCache(private val endpoint: ServiceEndpoint) {

selectedRelayLocation = constraint?.value?.toGeographicLocationConstraint()?.location
}

companion object {
private enum class RequestFetch {
ForRealLocation,
ForRelayLocation,
}
}
}
Original file line number Diff line number Diff line change
@@ -19,12 +19,6 @@ import net.mullvad.mullvadvpn.model.WireguardConstraints
import net.mullvad.mullvadvpn.service.MullvadDaemon

class RelayListListener(endpoint: ServiceEndpoint) {
companion object {
private enum class Command {
SetRelayLocation,
SetWireguardConstraints
}
}

private val commandChannel = spawnActor()
private val daemon = endpoint.intermittentDaemon
@@ -105,4 +99,11 @@ class RelayListListener(endpoint: ServiceEndpoint) {

daemon.await().updateRelaySettings(update)
}

companion object {
private enum class Command {
SetRelayLocation,
SetWireguardConstraints
}
}
}
Original file line number Diff line number Diff line change
@@ -27,13 +27,6 @@ class ServiceEndpoint(
val connectivityListener: ConnectivityListener,
context: Context
) {
companion object {
sealed class Command {
data class RegisterListener(val listener: Messenger) : Command()

data class UnregisterListener(val listenerId: Int) : Command()
}
}

private val listeners = mutableMapOf<Int, Messenger>()
private val commands: SendChannel<Command> = startRegistrator()
@@ -165,4 +158,12 @@ class ServiceEndpoint(

return listenerId
}

companion object {
sealed class Command {
data class RegisterListener(val listener: Messenger) : Command()

data class UnregisterListener(val listenerId: Int) : Command()
}
}
}
Original file line number Diff line number Diff line change
@@ -28,11 +28,6 @@ class AccountExpiryNotification(
val daemon: Intermittent<MullvadDaemon>,
val accountCache: AccountCache
) {
companion object {
val NOTIFICATION_ID: Int = 2
val REMAINING_TIME_FOR_REMINDERS = Duration.standardDays(2)
val TIME_BETWEEN_CHECKS: Long = 12 /* h */ * 60 /* min */ * 60 /* s */ * 1000 /* ms */
}

private val jobTracker = JobTracker()
private val resources = context.resources
@@ -143,4 +138,10 @@ class AccountExpiryNotification(
private fun getRemainingText(pluralId: Int, quantity: Int): String {
return resources.getQuantityString(pluralId, quantity, quantity)
}

companion object {
val NOTIFICATION_ID: Int = 2
val REMAINING_TIME_FOR_REMINDERS = Duration.standardDays(2)
val TIME_BETWEEN_CHECKS: Long = 12 /* h */ * 60 /* min */ * 60 /* s */ * 1000 /* ms */
}
}
Original file line number Diff line number Diff line change
@@ -18,10 +18,6 @@ import net.mullvad.talpid.tunnel.ActionAfterDisconnect
import net.mullvad.talpid.tunnel.ErrorStateCause

class TunnelStateNotification(val context: Context) {
companion object {
val NOTIFICATION_ID: Int = 1
}

private val channel =
NotificationChannel(
context,
@@ -149,4 +145,8 @@ class TunnelStateNotification(val context: Context) {

return NotificationCompat.Action(action.icon, label, pendingIntent)
}

companion object {
val NOTIFICATION_ID: Int = 1
}
}
Original file line number Diff line number Diff line change
@@ -12,28 +12,6 @@ enum class TunnelStateNotificationAction {
Cancel,
Dismiss;

companion object {
fun from(tunnelState: TunnelState) =
when (tunnelState) {
is TunnelState.Disconnected -> Connect
is TunnelState.Connecting -> Cancel
is TunnelState.Connected -> Disconnect
is TunnelState.Disconnecting -> {
when (tunnelState.actionAfterDisconnect) {
ActionAfterDisconnect.Reconnect -> Cancel
else -> Connect
}
}
is TunnelState.Error -> {
if (tunnelState.errorState.isBlocking) {
Disconnect
} else {
Dismiss
}
}
}
}

val text
get() =
when (this) {
@@ -56,4 +34,26 @@ enum class TunnelStateNotificationAction {
Connect -> R.drawable.icon_notification_connect
else -> R.drawable.icon_notification_disconnect
}

companion object {
fun from(tunnelState: TunnelState) =
when (tunnelState) {
is TunnelState.Disconnected -> Connect
is TunnelState.Connecting -> Cancel
is TunnelState.Connected -> Disconnect
is TunnelState.Disconnecting -> {
when (tunnelState.actionAfterDisconnect) {
ActionAfterDisconnect.Reconnect -> Cancel
else -> Connect
}
}
is TunnelState.Error -> {
if (tunnelState.errorState.isBlocking) {
Disconnect
} else {
Dismiss
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.mullvad.mullvadvpn.test.arch.classes

import com.lemonappdev.konsist.api.verify.assert
import net.mullvad.mullvadvpn.test.arch.extensions.projectScope
import org.junit.Test

class ClassTests {
@Test
fun `companion object is last declaration in the class`() {
projectScope().classes(includeNested = true).assert {
val companionObject =
it.objects(includeNested = false).lastOrNull { obj -> obj.hasCompanionModifier }
if (companionObject != null) {
it.declarations(includeNested = false, includeLocal = false).last() ==
companionObject
} else {
true
}
}
}
}

0 comments on commit 3a97abe

Please sign in to comment.