mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
extracting the test fakes to their own package
This commit is contained in:
parent
ac0c7067e0
commit
509c61c1a8
@ -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
|
||||
}
|
||||
|
50
vector/src/test/java/im/vector/app/test/fakes/FakeContext.kt
Normal file
50
vector/src/test/java/im/vector/app/test/fakes/FakeContext.kt
Normal 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
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
27
vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt
Normal file
27
vector/src/test/java/im/vector/app/test/fakes/FakeSession.kt
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user