extracting the test fakes to their own package

This commit is contained in:
Adam Brown 2021-09-27 17:19:39 +01:00
parent ac0c7067e0
commit 509c61c1a8
4 changed files with 107 additions and 43 deletions

View File

@ -16,11 +16,12 @@
package im.vector.app.features.crypto.keys
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.ParcelFileDescriptor
import im.vector.app.core.dispatchers.CoroutineDispatchers
import im.vector.app.test.fakes.FakeContext
import im.vector.app.test.fakes.FakeCryptoService
import im.vector.app.test.fakes.FakeSession
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
@ -29,9 +30,6 @@ import kotlinx.coroutines.runBlocking
import org.amshove.kluent.internal.assertFailsWith
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.CryptoService
import java.io.OutputStream
private val A_URI = mockk<Uri>()
private val A_ROOM_KEYS_EXPORT = ByteArray(size = 111)
@ -97,41 +95,3 @@ class KeysExporterTest {
}
}
}
class FakeContext {
private val contentResolver = mockk<ContentResolver>()
val instance = mockk<Context>()
init {
every { instance.contentResolver } returns contentResolver
}
fun givenFileDescriptor(uri: Uri, mode: String, factory: () -> ParcelFileDescriptor?) {
val fileDescriptor = factory()
every { contentResolver.openFileDescriptor(uri, mode, null) } returns fileDescriptor
}
fun givenOutputStreamFor(uri: Uri): OutputStream {
val outputStream = mockk<OutputStream>(relaxed = true)
every { contentResolver.openOutputStream(uri) } returns outputStream
return outputStream
}
fun givenMissingOutputStreamFor(uri: Uri) {
every { contentResolver.openOutputStream(uri) } returns null
}
}
class FakeSession(
private val cryptoService: CryptoService = FakeCryptoService()
) : Session by mockk(relaxed = true) {
override fun cryptoService() = cryptoService
}
class FakeCryptoService : CryptoService by mockk() {
var roomKeysExport = ByteArray(size = 1)
override suspend fun exportRoomKeys(password: String) = roomKeysExport
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* 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 im.vector.app.test.fakes
import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.ParcelFileDescriptor
import io.mockk.every
import io.mockk.mockk
import java.io.OutputStream
class FakeContext {
private val contentResolver = mockk<ContentResolver>()
val instance = mockk<Context>()
init {
every { instance.contentResolver } returns contentResolver
}
fun givenFileDescriptor(uri: Uri, mode: String, factory: () -> ParcelFileDescriptor?) {
val fileDescriptor = factory()
every { contentResolver.openFileDescriptor(uri, mode, null) } returns fileDescriptor
}
fun givenOutputStreamFor(uri: Uri): OutputStream {
val outputStream = mockk<OutputStream>(relaxed = true)
every { contentResolver.openOutputStream(uri) } returns outputStream
return outputStream
}
fun givenMissingOutputStreamFor(uri: Uri) {
every { contentResolver.openOutputStream(uri) } returns null
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* 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 im.vector.app.test.fakes
import io.mockk.mockk
import org.matrix.android.sdk.api.session.crypto.CryptoService
class FakeCryptoService : CryptoService by mockk() {
var roomKeysExport = ByteArray(size = 1)
override suspend fun exportRoomKeys(password: String) = roomKeysExport
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* 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 im.vector.app.test.fakes
import io.mockk.mockk
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.crypto.CryptoService
class FakeSession(
private val cryptoService: CryptoService = FakeCryptoService()
) : Session by mockk(relaxed = true) {
override fun cryptoService() = cryptoService
}