diff --git a/matrix-sdk-android/build.gradle b/matrix-sdk-android/build.gradle index c10aaf3545..514d1accae 100644 --- a/matrix-sdk-android/build.gradle +++ b/matrix-sdk-android/build.gradle @@ -71,6 +71,15 @@ android { kotlinOptions { jvmTarget = "1.8" } + + sourceSets { + androidTest { + java.srcDirs += "src/sharedTest/java" + } + test { + java.srcDirs += "src/sharedTest/java" + } + } } static def gitRevision() { @@ -160,6 +169,8 @@ dependencies { testImplementation 'io.mockk:mockk:1.9.2.kotlin12' testImplementation 'org.amshove.kluent:kluent-android:1.44' testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" + // Plant Timber tree for test + testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' androidTestImplementation 'androidx.test:core:1.2.0' androidTestImplementation 'androidx.test:runner:1.2.0' @@ -171,5 +182,6 @@ dependencies { androidTestImplementation 'io.mockk:mockk-android:1.9.2.kotlin12' androidTestImplementation "androidx.arch.core:core-testing:$arch_version" androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" - + // Plant Timber tree for test + androidTestImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/InstrumentedTest.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/InstrumentedTest.kt index 56b358c69f..374a1fc399 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/InstrumentedTest.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/InstrumentedTest.kt @@ -18,10 +18,15 @@ package im.vector.matrix.android import android.content.Context import androidx.test.core.app.ApplicationProvider +import im.vector.matrix.android.test.shared.createTimberTestRule +import org.junit.Rule import java.io.File interface InstrumentedTest { + @Rule + fun timberTestRule() = createTimberTestRule() + fun context(): Context { return ApplicationProvider.getApplicationContext() } diff --git a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt index d168fe8ea6..5bc8653f3d 100644 --- a/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt +++ b/matrix-sdk-android/src/androidTest/java/im/vector/matrix/android/common/CommonTestHelper.kt @@ -44,7 +44,6 @@ import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue -import timber.log.Timber import java.util.ArrayList import java.util.UUID import java.util.concurrent.CountDownLatch @@ -59,8 +58,6 @@ class CommonTestHelper(context: Context) { val matrix: Matrix init { - Timber.plant(Timber.DebugTree()) - Matrix.initialize(context, MatrixConfiguration("TestFlavor")) matrix = Matrix.getInstance(context) diff --git a/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt b/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt index c96786ba3b..eb05e26362 100644 --- a/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt +++ b/matrix-sdk-android/src/debug/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt @@ -20,7 +20,6 @@ package im.vector.matrix.android.internal.network.interceptors import im.vector.matrix.android.internal.di.MatrixScope import okhttp3.Interceptor import okhttp3.Response -import okhttp3.logging.HttpLoggingInterceptor import okio.Buffer import timber.log.Timber import java.io.IOException @@ -37,7 +36,7 @@ import javax.inject.Inject * non-production environment. */ @MatrixScope -internal class CurlLoggingInterceptor @Inject constructor(private val logger: HttpLoggingInterceptor.Logger) +internal class CurlLoggingInterceptor @Inject constructor() : Interceptor { /** @@ -97,8 +96,8 @@ internal class CurlLoggingInterceptor @Inject constructor(private val logger: Ht // Add Json formatting curlCmd += " | python -m json.tool" - logger.log("--- cURL (" + request.url + ")") - logger.log(curlCmd) + Timber.d("--- cURL (${request.url})") + Timber.d(curlCmd) return chain.proceed(request) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt index c573e44a6a..b7ab257363 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/di/NetworkModule.kt @@ -57,7 +57,7 @@ internal object NetworkModule { @Provides @JvmStatic fun providesCurlLoggingInterceptor(): CurlLoggingInterceptor { - return CurlLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT) + return CurlLoggingInterceptor() } @MatrixScope diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/eventbus/EventBusTimberLogger.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/eventbus/EventBusTimberLogger.kt new file mode 100644 index 0000000000..79d828dcab --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/eventbus/EventBusTimberLogger.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 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.matrix.android.internal.eventbus + +import org.greenrobot.eventbus.Logger +import timber.log.Timber +import java.util.logging.Level + +class EventBusTimberLogger : Logger { + override fun log(level: Level, msg: String) { + Timber.d(msg) + } + + override fun log(level: Level, msg: String, th: Throwable) { + Timber.e(th, msg) + } +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt index 908c610914..8bdfff062f 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/SessionModule.kt @@ -49,6 +49,7 @@ import im.vector.matrix.android.internal.di.SessionId import im.vector.matrix.android.internal.di.Unauthenticated import im.vector.matrix.android.internal.di.UserId import im.vector.matrix.android.internal.di.UserMd5 +import im.vector.matrix.android.internal.eventbus.EventBusTimberLogger import im.vector.matrix.android.internal.network.AccessTokenInterceptor import im.vector.matrix.android.internal.network.DefaultNetworkConnectivityChecker import im.vector.matrix.android.internal.network.FallbackNetworkCallbackStrategy @@ -205,7 +206,10 @@ internal abstract class SessionModule { @Provides @SessionScope fun providesEventBus(): EventBus { - return EventBus.builder().build() + return EventBus + .builder() + .logger(EventBusTimberLogger()) + .build() } @JvmStatic diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt index ecc39cfdc2..77100ffc49 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/pushers/AddHttpPusherWorker.kt @@ -74,7 +74,6 @@ internal class AddHttpPusherWorker(context: Context, params: WorkerParameters) it.state = PusherState.FAILED_TO_REGISTER } } - // always return success, or the chain will be stuck for ever! Result.failure() } } diff --git a/matrix-sdk-android/src/release/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt b/matrix-sdk-android/src/release/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt index 8f8a12d0b1..56b2ac0d48 100644 --- a/matrix-sdk-android/src/release/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt +++ b/matrix-sdk-android/src/release/java/im/vector/matrix/android/internal/network/interceptors/CurlLoggingInterceptor.kt @@ -20,7 +20,6 @@ package im.vector.matrix.android.internal.network.interceptors import im.vector.matrix.android.internal.di.MatrixScope import okhttp3.Interceptor import okhttp3.Response -import okhttp3.logging.HttpLoggingInterceptor import java.io.IOException import javax.inject.Inject @@ -28,7 +27,7 @@ import javax.inject.Inject * No op interceptor */ @MatrixScope -internal class CurlLoggingInterceptor @Inject constructor(private val logger: HttpLoggingInterceptor.Logger) +internal class CurlLoggingInterceptor @Inject constructor() : Interceptor { @Throws(IOException::class) diff --git a/matrix-sdk-android/src/sharedTest/java/im/vector/matrix/android/test/shared/TestRules.kt b/matrix-sdk-android/src/sharedTest/java/im/vector/matrix/android/test/shared/TestRules.kt new file mode 100644 index 0000000000..77408322c1 --- /dev/null +++ b/matrix-sdk-android/src/sharedTest/java/im/vector/matrix/android/test/shared/TestRules.kt @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020 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.matrix.android.test.shared + +import net.lachlanmckee.timberjunit.TimberTestRule + +fun createTimberTestRule(): TimberTestRule { + return TimberTestRule.builder() + .showThread(false) + .showTimestamp(false) + .onlyLogWhenTestFails(false) + .build() +} diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/MatrixTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/MatrixTest.kt new file mode 100644 index 0000000000..cef9bb69d7 --- /dev/null +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/MatrixTest.kt @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2020 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.matrix.android + +import im.vector.matrix.android.test.shared.createTimberTestRule +import org.junit.Rule + +interface MatrixTest { + + @Rule + fun timberTestRule() = createTimberTestRule() +} diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt index 1d5d6a4d19..21ea1cbd6a 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushRuleActionsTest.kt @@ -16,6 +16,7 @@ package im.vector.matrix.android.api.pushrules +import im.vector.matrix.android.MatrixTest import im.vector.matrix.android.api.pushrules.rest.PushRule import im.vector.matrix.android.internal.di.MoshiProvider import org.junit.Assert.assertEquals @@ -23,7 +24,7 @@ import org.junit.Assert.assertNotNull import org.junit.Assert.assertTrue import org.junit.Test -class PushRuleActionsTest { +class PushRuleActionsTest: MatrixTest { @Test fun test_action_parsing() { diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt index 960d585bdc..1bf40222f8 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/api/pushrules/PushrulesConditionTest.kt @@ -16,6 +16,7 @@ package im.vector.matrix.android.api.pushrules +import im.vector.matrix.android.MatrixTest import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toContent import im.vector.matrix.android.api.session.room.Room @@ -30,7 +31,7 @@ import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -class PushrulesConditionTest { +class PushrulesConditionTest: MatrixTest { /* ========================================================================================== * Test EventMatchCondition diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/Base58Test.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/Base58Test.kt index 42295dada0..6d7ddce0b4 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/Base58Test.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/Base58Test.kt @@ -16,6 +16,7 @@ package im.vector.matrix.android.internal.crypto.keysbackup.util +import im.vector.matrix.android.MatrixTest import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.FixMethodOrder @@ -23,7 +24,7 @@ import org.junit.Test import org.junit.runners.MethodSorters @FixMethodOrder(MethodSorters.JVM) -class Base58Test { +class Base58Test: MatrixTest { @Test fun encode() { diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/RecoveryKeyTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/RecoveryKeyTest.kt index 2566275952..606be13ed9 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/RecoveryKeyTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/keysbackup/util/RecoveryKeyTest.kt @@ -16,13 +16,15 @@ package im.vector.matrix.android.internal.crypto.keysbackup.util +import im.vector.matrix.android.MatrixTest import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test -class RecoveryKeyTest { +class RecoveryKeyTest: MatrixTest { + private val curve25519Key = byteArrayOf( 0x77.toByte(), 0x07.toByte(), 0x6D.toByte(), 0x0A.toByte(), 0x73.toByte(), 0x18.toByte(), 0xA5.toByte(), 0x7D.toByte(), 0x3C.toByte(), 0x16.toByte(), 0xC1.toByte(), 0x72.toByte(), 0x51.toByte(), 0xB2.toByte(), 0x66.toByte(), 0x45.toByte(), diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/store/db/HelperTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/store/db/HelperTest.kt index d4740bdc4f..b8524c95e0 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/store/db/HelperTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/store/db/HelperTest.kt @@ -16,11 +16,12 @@ package im.vector.matrix.android.internal.crypto.store.db +import im.vector.matrix.android.MatrixTest import im.vector.matrix.android.internal.util.md5 import org.junit.Assert.assertEquals import org.junit.Test -class HelperTest { +class HelperTest: MatrixTest { @Test fun testHash_ok() { diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/verification/qrcode/BinaryStringTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/verification/qrcode/BinaryStringTest.kt index df5499cb6f..8fc93c139e 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/verification/qrcode/BinaryStringTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/crypto/verification/qrcode/BinaryStringTest.kt @@ -16,13 +16,14 @@ package im.vector.matrix.android.internal.crypto.verification.qrcode +import im.vector.matrix.android.MatrixTest import org.amshove.kluent.shouldEqualTo import org.junit.FixMethodOrder import org.junit.Test import org.junit.runners.MethodSorters @FixMethodOrder(MethodSorters.JVM) -class BinaryStringTest { +class BinaryStringTest: MatrixTest { /** * I want to put bytes to a String, and vice versa diff --git a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/task/CoroutineSequencersTest.kt b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/task/CoroutineSequencersTest.kt index a70713c44b..7d460861b1 100644 --- a/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/task/CoroutineSequencersTest.kt +++ b/matrix-sdk-android/src/test/java/im/vector/matrix/android/internal/task/CoroutineSequencersTest.kt @@ -16,6 +16,7 @@ package im.vector.matrix.android.internal.task +import im.vector.matrix.android.MatrixTest import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.delay @@ -26,7 +27,7 @@ import org.junit.Assert.assertEquals import org.junit.Test import java.util.concurrent.Executors -class CoroutineSequencersTest { +class CoroutineSequencersTest: MatrixTest { private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()