Skip to content

Commit

Permalink
Fixes buffers not being added in the correct position, adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Jun 17, 2019
1 parent 316d3e0 commit 60f1776
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import de.kuschku.libquassel.quassel.syncables.interfaces.IBufferViewConfig
import de.kuschku.libquassel.session.SignalProxy
import de.kuschku.libquassel.util.flag.hasFlag
import de.kuschku.libquassel.util.helper.clampOf
import de.kuschku.libquassel.util.irc.IrcCaseMappers
import io.reactivex.Observable
import io.reactivex.subjects.BehaviorSubject

Expand Down Expand Up @@ -327,12 +328,12 @@ class BufferViewConfig constructor(

fun insertBufferSorted(info: BufferInfo, bufferSyncer: BufferSyncer) {
if (!_buffers.contains(info.bufferId)) {
val position = if (_sortAlphabetically) {
val sortedBuffers = _buffers.mapNotNull { bufferSyncer.bufferInfo(it)?.bufferName }
-sortedBuffers.binarySearch(info.bufferName)
} else {
_buffers.size
}
val element = IrcCaseMappers.unicode.toLowerCaseNullable(info.bufferName)
val position =
if (_sortAlphabetically) -_buffers.mapNotNull {
IrcCaseMappers.unicode.toLowerCaseNullable(bufferSyncer.bufferInfo(it)?.bufferName)
}.binarySearch(element) - 1
else _buffers.size
requestAddBuffer(info.bufferId, position)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2019 Janne Mareike Koschinski
* Copyright (c) 2019 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package de.kuschku.libquassel.integration

import de.kuschku.libquassel.protocol.BufferId
import de.kuschku.libquassel.protocol.QType
import de.kuschku.libquassel.protocol.QVariant_
import de.kuschku.libquassel.protocol.Type
import de.kuschku.libquassel.util.TestSession
import de.kuschku.libquassel.util.setupTestSession
import de.kuschku.libquassel.util.with
import org.junit.Before
import org.junit.Test

class BufferViewConfigTest {
lateinit var session: TestSession

@Before
fun setUp() {
session = setupTestSession()
}

// Test positioning of added channel
@Test
fun addChannelAutomatically() = session.with {
val bufferViewConfig = bufferViewManager.bufferViewConfig(0)!!

ensure {
bufferViewConfig.insertBufferSorted(bufferSyncer.bufferInfo(BufferId(4))!!, bufferSyncer)
}.does {
callSync(bufferViewConfig, "requestAddBuffer", listOf(
QVariant_.of(BufferId(4), QType.BufferId),
QVariant_.of(2, Type.Int)
))
}
}
}
9 changes: 9 additions & 0 deletions lib/src/test/java/de/kuschku/libquassel/util/SetupTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ fun TestSession.with(f: TestSession.() -> Unit) = f.invoke(this)
fun withTestSession(f: TestSession.() -> Unit) = f.invoke(setupTestSession())

fun setupTestSession() = TestSession().provideTestData {
bufferViewConfigs = listOf(
buildBufferViewConfig(0) {
setBufferViewName("All Chats")
addBuffer(BufferId(1), 0)
addBuffer(BufferId(2), 1)
addBuffer(BufferId(3), 2)
}
)

identities = listOf(
buildIdentity(IdentityId(1)) {
setIdentityName("Default Identity")
Expand Down
23 changes: 23 additions & 0 deletions lib/src/test/java/de/kuschku/libquassel/util/TestSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ class TestSession : ProtocolHandler({ throw it }), ISession {
synchronize(network)
}

fun addBufferViewConfig(bufferViewConfig: BufferViewConfig, initialize: Boolean = false) {
if (!initialize)
bufferViewConfig.initialized = true
bufferViewManager.addBufferViewConfig(bufferViewConfig)
synchronize(bufferViewConfig)
}

override fun removeNetwork(networkId: NetworkId) {
val network = networks.remove(networkId)
stopSynchronize(network)
Expand All @@ -244,6 +251,13 @@ class TestSession : ProtocolHandler({ throw it }), ISession {
return identity
}

fun buildBufferViewConfig(bufferViewConfigId: Int,
f: (BufferViewConfig.() -> Unit)? = null): BufferViewConfig {
val bufferViewConfig = BufferViewConfig(bufferViewConfigId, proxy)
f?.invoke(bufferViewConfig)
return bufferViewConfig
}

data class BufferTestData(
val bufferInfo: BufferInfo,
val activity: Message_Types = Message_Type.of(),
Expand All @@ -254,11 +268,17 @@ class TestSession : ProtocolHandler({ throw it }), ISession {

data class TestData(
val session: TestSession,
var bufferViewConfigs: List<BufferViewConfig> = emptyList(),
var networks: List<Network> = emptyList(),
var identities: List<Identity> = emptyList(),
var buffers: List<BufferTestData> = emptyList(),
var aliases: List<IAliasManager.Alias> = emptyList()
) {
fun buildBufferViewConfig(bufferViewConfigId: Int,
f: (BufferViewConfig.() -> Unit)? = null): BufferViewConfig {
return session.buildBufferViewConfig(bufferViewConfigId, f)
}

fun buildNetwork(networkId: NetworkId, f: (Network.() -> Unit)? = null): Network {
return session.buildNetwork(networkId, f)
}
Expand Down Expand Up @@ -287,6 +307,9 @@ class TestSession : ProtocolHandler({ throw it }), ISession {
fun provideTestData(f: TestData.() -> Unit): TestSession {
val data = TestData(this)
f.invoke(data)
for (bufferViewConfig in data.bufferViewConfigs) {
addBufferViewConfig(bufferViewConfig)
}
for (network in data.networks) {
addNetwork(network)
}
Expand Down

0 comments on commit 60f1776

Please sign in to comment.