Skip to content

Commit

Permalink
ref: use hilt for dependency injection (#3880)
Browse files Browse the repository at this point in the history
* ref: use hilt for dependency injection

* fix: fix hilt/dagger errors regarding DI

* ref: moved CategoryRepository DI from AppModule to Kotlin file

* fix: errors after rebase

* ref: little fix

* fix: tests
  • Loading branch information
VaiTon committed Mar 14, 2021
1 parent a56b473 commit a4a0458
Show file tree
Hide file tree
Showing 78 changed files with 930 additions and 692 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/android-integration.yml
Expand Up @@ -23,12 +23,12 @@ jobs:
- name: Screenshots android tests
uses: eskatos/gradle-command-action@v1
with:
arguments: compileOffPlaystoreScreenshotsAndroidTestSources --stacktrace --info -PtestBuildType=screenshots
arguments: compileOffPlaystoreScreenshotsAndroidTestSources -PtestBuildType=screenshots

- name: Unit tests
uses: eskatos/gradle-command-action@v1
with:
arguments: testObfPlaystoreDebugUnitTest testOffPlaystoreDebugUnitTest testOpfPlaystoreDebugUnitTest testOpffPlaystoreDebugUnitTest --stacktrace
arguments: testObfPlaystoreDebugUnitTest testOffPlaystoreDebugUnitTest testOpfPlaystoreDebugUnitTest testOpffPlaystoreDebugUnitTest

apk:
name: Generate APK
Expand Down
31 changes: 19 additions & 12 deletions app/build.gradle.kts
Expand Up @@ -30,6 +30,7 @@ plugins {
id("kotlin-android")
id("kotlin-parcelize")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
}

fun obtainTestBuildType(): String {
Expand Down Expand Up @@ -68,16 +69,23 @@ dependencies {
val workVersion = "2.5.0"
implementation("androidx.work:work-runtime:$workVersion")
implementation("androidx.work:work-rxjava2:$workVersion")
implementation("androidx.hilt:hilt-work:1.0.0-alpha03")
kapt("androidx.hilt:hilt-compiler:1.0.0-alpha03")


implementation("androidx.startup:startup-runtime:1.0.0")

// ML Kit barcode Scanner
implementation ("com.google.mlkit:barcode-scanning:16.1.1")
implementation("com.google.mlkit:barcode-scanning:16.1.1")

implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")

kapt("com.google.dagger:dagger-compiler:2.33")
implementation("com.google.dagger:dagger:2.33")
implementation("com.google.dagger:hilt-android:${rootProject.extra["hiltVersion"]}")

kapt("com.google.dagger:hilt-compiler:${rootProject.extra["hiltVersion"]}")

compileOnly("javax.annotation:javax.annotation-api:1.3.2")

//Rx
Expand Down Expand Up @@ -156,6 +164,9 @@ dependencies {
// Crash analytics
implementation("io.sentry:sentry-android:4.3.0")

// ShowCaseView dependency
implementation("com.github.mreram:showcaseview:1.0.5")

// Unit Testing
testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:3.8.0")
Expand All @@ -166,14 +177,14 @@ dependencies {
// Instrumented tests
androidTestUtil("androidx.test:orchestrator:1.3.0")

androidTestImplementation("androidx.test:runner:1.3.0") {
exclude("junit")
}
// Hilt for Android Testing
androidTestImplementation("com.google.dagger:hilt-android-testing:2.33-beta")
kaptAndroidTest("com.google.dagger:hilt-android-compiler:2.33-beta")

androidTestImplementation("androidx.test:runner:1.3.0") { exclude("junit") }
androidTestImplementation("androidx.test:rules:1.3.0")

androidTestImplementation("androidx.test.ext:junit:1.1.2") {
exclude("junit")
}
androidTestImplementation("androidx.test.ext:junit:1.1.2") { exclude("junit") }

androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
androidTestImplementation("androidx.test.espresso:espresso-intents:3.3.0")
Expand All @@ -187,12 +198,8 @@ dependencies {
androidTestImplementation("com.jraska:falcon:2.1.1")
androidTestImplementation("tools.fastlane:screengrab:1.2.0")

resourcePlaceholders {
files = listOf("xml/shortcuts.xml")
}
resourcePlaceholders { files = listOf("xml/shortcuts.xml") }

// ShowCaseView dependency
implementation("com.github.mreram:showcaseview:1.0.5")
}


Expand Down
@@ -1,28 +1,55 @@
package openfoodfacts.github.scrachx.openfood.repositories

import android.content.Context
import android.util.Log
import androidx.test.filters.SmallTest
import openfoodfacts.github.scrachx.openfood.app.OFFApplication.Companion.daoSession
import openfoodfacts.github.scrachx.openfood.app.OFFApplication.Companion.instance
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import openfoodfacts.github.scrachx.openfood.models.DaoSession
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.Allergen
import openfoodfacts.github.scrachx.openfood.models.entities.allergen.AllergenName
import org.junit.AfterClass
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.BeforeClass
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.*
import javax.inject.Inject

/**
* Created by Lobster on 05.03.18.
*/
@SmallTest
@HiltAndroidTest
class ProductRepositoryTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)


@Inject
lateinit var productRepository: ProductRepository

@Inject
@ApplicationContext
lateinit var instance: Context

@Inject
lateinit var daoSession: DaoSession


@Before
fun cleanAllergens() {
clearDatabase(daoSession)
productRepository.saveAllergens(createAllergens())
}

@Test
fun testGetAllergens() {
val mSettings = instance.getSharedPreferences("prefs", 0)
val isDownloadActivated = mSettings.getBoolean(Taxonomy.ALLERGEN.downloadActivatePreferencesId, false)
val allergens = ProductRepository.reloadAllergensFromServer().blockingGet()
val allergens = productRepository.reloadAllergensFromServer().blockingGet()
assertNotNull(allergens)
if (!isDownloadActivated) {
assertEquals(0, allergens.size.toLong())
Expand All @@ -41,16 +68,16 @@ class ProductRepositoryTest {

@Test
fun testGetEnabledAllergens() {
val allergens = ProductRepository.getEnabledAllergens()
val allergens = productRepository.getEnabledAllergens()
assertNotNull(allergens)
assertEquals(1, allergens.size.toLong())
assertEquals(TEST_ALLERGEN_TAG, allergens[0].tag)
}

@Test
fun testGetAllergensByEnabledAndLanguageCode() {
val enabledAllergenNames = ProductRepository.getAllergensByEnabledAndLanguageCode(true, TEST_LANGUAGE_CODE).blockingGet()
val notEnabledAllergenNames = ProductRepository.getAllergensByEnabledAndLanguageCode(false, TEST_LANGUAGE_CODE).blockingGet()
val enabledAllergenNames = productRepository.getAllergensByEnabledAndLanguageCode(true, TEST_LANGUAGE_CODE).blockingGet()
val notEnabledAllergenNames = productRepository.getAllergensByEnabledAndLanguageCode(false, TEST_LANGUAGE_CODE).blockingGet()
assertNotNull(enabledAllergenNames)
assertNotNull(notEnabledAllergenNames)
assertEquals(1, enabledAllergenNames.size.toLong())
Expand All @@ -61,27 +88,21 @@ class ProductRepositoryTest {

@Test
fun testGetAllergensByLanguageCode() {
val allergenNames = ProductRepository.getAllergensByLanguageCode(TEST_LANGUAGE_CODE).blockingGet()
val allergenNames = productRepository.getAllergensByLanguageCode(TEST_LANGUAGE_CODE).blockingGet()
assertNotNull(allergenNames)
assertEquals(2, allergenNames.size.toLong())
}


@After
fun close() = clearDatabase(daoSession)

companion object {
private const val TEST_ALLERGEN_TAG = "en:lupin"
private const val TEST_LANGUAGE_CODE = "es"
private const val TEST_ALLERGEN_NAME = "Altramuces"

@BeforeClass
fun cleanAllergens() {
clearDatabase()
ProductRepository.saveAllergens(createAllergens())
}

@AfterClass
fun close() = clearDatabase()

private fun clearDatabase() {
val daoSession = daoSession
private fun clearDatabase(daoSession: DaoSession) {
val db = daoSession.database
db.beginTransaction()
try {
Expand All @@ -97,14 +118,14 @@ class ProductRepositoryTest {
private fun createAllergens(): List<Allergen> {
val allergen1 = Allergen(TEST_ALLERGEN_TAG, ArrayList()).apply {
enabled = true
names.add(AllergenName(tag, TEST_LANGUAGE_CODE, TEST_ALLERGEN_NAME))
names.add(AllergenName(tag, "bg", "袥褍锌懈薪邪"))
names.add(AllergenName(tag, "fr", "Lupin"))
names += AllergenName(tag, TEST_LANGUAGE_CODE, TEST_ALLERGEN_NAME)
names += AllergenName(tag, "bg", "袥褍锌懈薪邪")
names += AllergenName(tag, "fr", "Lupin")
}

val allergen2 = Allergen("en:molluscs", ArrayList()).apply {
names.add(AllergenName(tag, TEST_LANGUAGE_CODE, "Molluschi"))
names.add(AllergenName(tag, "en", "Mollusques"))
names += AllergenName(tag, TEST_LANGUAGE_CODE, "Molluschi")
names += AllergenName(tag, "en", "Mollusques")
}

return listOf(allergen1, allergen2)
Expand Down
Expand Up @@ -7,10 +7,10 @@ import android.content.Intent
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import openfoodfacts.github.scrachx.openfood.app.OFFApplication
import dagger.hilt.android.testing.HiltAndroidTest
import openfoodfacts.github.scrachx.openfood.test.ScreenshotActivityTestRule
import openfoodfacts.github.scrachx.openfood.test.ScreenshotParameter
import openfoodfacts.github.scrachx.openfood.test.ScreenshotsLocaleProvider
import openfoodfacts.github.scrachx.openfood.test.getFilteredParameters
import openfoodfacts.github.scrachx.openfood.utils.LocaleHelper
import org.junit.AfterClass
import org.junit.BeforeClass
Expand All @@ -22,8 +22,10 @@ import java.util.*
* Take screenshots...buil
*/
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
abstract class AbstractScreenshotTest {


@Rule
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Expand All @@ -34,9 +36,10 @@ abstract class AbstractScreenshotTest {
private fun startScreenshotActivityTestRules(
screenshotParameter: ScreenshotParameter,
activityRules: List<ScreenshotActivityTestRule<out Activity?>>,
intents: List<Intent?>
intents: List<Intent?>,
context: Context
) {
changeLocale(screenshotParameter)
changeLocale(screenshotParameter, context)
activityRules.forEach { activityRule ->
intents.forEach { intent ->
activityRule.finishActivity()
Expand All @@ -48,17 +51,18 @@ abstract class AbstractScreenshotTest {
}
}

private fun changeLocale(parameter: ScreenshotParameter, context: Context = OFFApplication.instance) {
private fun changeLocale(parameter: ScreenshotParameter, context: Context) {
Log.d(LOG_TAG, "Change parameters to $parameter")
LocaleHelper.setContextLanguage(context, parameter.locale)
}

protected fun startForAllLocales(
filter: (ScreenshotParameter) -> List<Intent?> = { listOf(null) },
rules: List<ScreenshotActivityTestRule<out Activity?>>
rules: List<ScreenshotActivityTestRule<out Activity?>>,
context: Context
) {
ScreenshotsLocaleProvider.getFilteredParameters().forEach {
startScreenshotActivityTestRules(it, rules, filter(it))
getFilteredParameters().forEach {
startScreenshotActivityTestRules(it, rules, filter(it), context)
}
}

Expand Down
@@ -1,34 +1,47 @@
package openfoodfacts.github.scrachx.openfood

import android.content.Context
import android.content.Intent
import androidx.test.ext.junit.runners.AndroidJUnit4
import openfoodfacts.github.scrachx.openfood.app.OFFApplication
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import openfoodfacts.github.scrachx.openfood.features.search.ProductSearchActivity
import openfoodfacts.github.scrachx.openfood.models.SearchInfo
import openfoodfacts.github.scrachx.openfood.test.ScreenshotActivityTestRule
import openfoodfacts.github.scrachx.openfood.test.ScreenshotParameter
import openfoodfacts.github.scrachx.openfood.models.SearchInfo
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import javax.inject.Inject

/**
* Take screenshots...
*/
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class TakeScreenshotIncompleteProductsTest : AbstractScreenshotTest() {

@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context

@Rule
var incompleteRule = ScreenshotActivityTestRule(
ProductSearchActivity::class.java, "incompleteProducts",
ProductSearchActivity::class.java,
"incompleteProducts",
context
)

@Test
fun testTakeScreenshot() = startForAllLocales(createSearchIntent, listOf(incompleteRule))
fun testTakeScreenshot() = startForAllLocales(createSearchIntent, listOf(incompleteRule), context)

companion object {
private val createSearchIntent: (ScreenshotParameter) -> List<Intent?> = {
listOf(Intent(OFFApplication.instance, ProductSearchActivity::class.java).apply {
putExtra(ProductSearchActivity.SEARCH_INFO, SearchInfo.emptySearchInfo())
})
}
private val createSearchIntent: (ScreenshotParameter) -> List<Intent?> = {
listOf(Intent(context, ProductSearchActivity::class.java).apply {
putExtra(ProductSearchActivity.SEARCH_INFO, SearchInfo.emptySearchInfo())
})
}
}
@@ -1,27 +1,43 @@
package openfoodfacts.github.scrachx.openfood

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import openfoodfacts.github.scrachx.openfood.features.MainActivity
import openfoodfacts.github.scrachx.openfood.features.welcome.WelcomeActivity
import openfoodfacts.github.scrachx.openfood.test.ScreenshotActivityTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import javax.inject.Inject

/**
* Take screenshots...
*/
@RunWith(AndroidJUnit4::class)
@HiltAndroidTest
class TakeScreenshotMainActivityTest : AbstractScreenshotTest() {


@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
@ApplicationContext
lateinit var context: Context


@Rule
var activityRule = ScreenshotActivityTestRule(MainActivity::class.java)
var activityRule = ScreenshotActivityTestRule(MainActivity::class.java, context = context)

@Rule
var welcomeActivityRule = ScreenshotActivityTestRule(WelcomeActivity::class.java)
var welcomeActivityRule = ScreenshotActivityTestRule(WelcomeActivity::class.java, context = context)

@Test
fun testTakeScreenshotMainActivity() {
welcomeActivityRule.firstTimeLaunched = true
startForAllLocales(rules = listOf(welcomeActivityRule, activityRule))
startForAllLocales(rules = listOf(welcomeActivityRule, activityRule), context = context)
}
}

0 comments on commit a4a0458

Please sign in to comment.