From 9c8ff7de7fee5f7a8bf27de0b0dcb06819fa7ebf Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 28 Apr 2020 14:15:23 +0200 Subject: [PATCH] Add Android test for EmojiDataSource --- vector/build.gradle | 18 ++++ .../java/im/vector/riotx/InstrumentedTest.kt | 32 +++++++ .../reactions/data/EmojiDataSourceTest.kt | 85 +++++++++++++++++++ .../im/vector/riotx/test/shared/TestRules.kt | 27 ++++++ 4 files changed, 162 insertions(+) create mode 100644 vector/src/androidTest/java/im/vector/riotx/InstrumentedTest.kt create mode 100644 vector/src/androidTest/java/im/vector/riotx/features/reactions/data/EmojiDataSourceTest.kt create mode 100644 vector/src/sharedTest/java/im/vector/riotx/test/shared/TestRules.kt diff --git a/vector/build.gradle b/vector/build.gradle index 2a898f5621..69608cf712 100644 --- a/vector/build.gradle +++ b/vector/build.gradle @@ -235,6 +235,15 @@ android { kotlinOptions { jvmTarget = "1.8" } + + sourceSets { + androidTest { + java.srcDirs += "src/sharedTest/java" + } + test { + java.srcDirs += "src/sharedTest/java" + } + } } dependencies { @@ -250,6 +259,7 @@ dependencies { def daggerVersion = '2.25.4' def autofill_version = "1.0.0" def work_version = '2.3.3' + def arch_version = '2.1.0' implementation project(":matrix-sdk-android") implementation project(":matrix-sdk-android-rx") @@ -378,10 +388,18 @@ dependencies { // TESTS testImplementation 'junit:junit:4.12' testImplementation 'org.amshove.kluent:kluent-android:1.44' + // 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' + androidTestImplementation 'androidx.test:rules:1.2.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'org.amshove.kluent:kluent-android:1.44' + androidTestImplementation "androidx.arch.core:core-testing:$arch_version" + // Plant Timber tree for test + androidTestImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1' } if (getGradle().getStartParameter().getTaskRequests().toString().contains("Gplay")) { diff --git a/vector/src/androidTest/java/im/vector/riotx/InstrumentedTest.kt b/vector/src/androidTest/java/im/vector/riotx/InstrumentedTest.kt new file mode 100644 index 0000000000..c34a3ef67b --- /dev/null +++ b/vector/src/androidTest/java/im/vector/riotx/InstrumentedTest.kt @@ -0,0 +1,32 @@ +/* + * 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.riotx + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import im.vector.riotx.test.shared.createTimberTestRule +import org.junit.Rule + +interface InstrumentedTest { + + @Rule + fun timberTestRule() = createTimberTestRule() + + fun context(): Context { + return ApplicationProvider.getApplicationContext() + } +} diff --git a/vector/src/androidTest/java/im/vector/riotx/features/reactions/data/EmojiDataSourceTest.kt b/vector/src/androidTest/java/im/vector/riotx/features/reactions/data/EmojiDataSourceTest.kt new file mode 100644 index 0000000000..88ca1eb01a --- /dev/null +++ b/vector/src/androidTest/java/im/vector/riotx/features/reactions/data/EmojiDataSourceTest.kt @@ -0,0 +1,85 @@ +/* + * 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.riotx.features.reactions.data + +import im.vector.riotx.InstrumentedTest +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.FixMethodOrder +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.runners.MethodSorters +import kotlin.system.measureTimeMillis + +@RunWith(JUnit4::class) +@FixMethodOrder(MethodSorters.JVM) +class EmojiDataSourceTest : InstrumentedTest { + + @Test + fun checkParsingTime() { + val time = measureTimeMillis { + EmojiDataSource(context().resources) + } + + assertTrue("Too long to parse", time < 100) + } + + @Test + fun checkNumberOfResult() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertEquals("Wrong number of emojis", 1545, emojiDataSource.rawData.emojis.size) + assertEquals("Wrong number of categories", 8, emojiDataSource.rawData.categories.size) + assertEquals("Wrong number of aliases", 57, emojiDataSource.rawData.aliases.size) + } + + @Test + fun searchTestEmptySearch() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertEquals("Empty search should return 1545 results", 1545, emojiDataSource.filterWith("").size) + } + + @Test + fun searchTestNoResult() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertTrue("Should not have result", emojiDataSource.filterWith("noresult").isEmpty()) + } + + @Test + fun searchTestOneResult() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertEquals("Should have 1 result", 1, emojiDataSource.filterWith("france").size) + } + + @Test + fun searchTestManyResult() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertTrue("Should have many result", emojiDataSource.filterWith("fra").size > 1) + } + + @Test + fun testQuickReaction() { + val emojiDataSource = EmojiDataSource(context().resources) + + assertEquals("Should have 8 quick reactions", 8, emojiDataSource.getQuickReactions().size) + } +} diff --git a/vector/src/sharedTest/java/im/vector/riotx/test/shared/TestRules.kt b/vector/src/sharedTest/java/im/vector/riotx/test/shared/TestRules.kt new file mode 100644 index 0000000000..d5790ba497 --- /dev/null +++ b/vector/src/sharedTest/java/im/vector/riotx/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.riotx.test.shared + +import net.lachlanmckee.timberjunit.TimberTestRule + +fun createTimberTestRule(): TimberTestRule { + return TimberTestRule.builder() + .showThread(false) + .showTimestamp(false) + .onlyLogWhenTestFails(false) + .build() +}