Skip to content

Commit

Permalink
Some more abstraction into different product flavors
Browse files Browse the repository at this point in the history
Sms module now has a mock() variant for easier testing, this was cared to not be
possible to ship with mockRelease (check variant filtering in sms module build.gradle)

With this, I hope to get a better better abstraction hence, better enviroment for integration tests.
Notice how the main folder of the sms module keeps the interface which represents the contract to
outer layers (mobile-ui) in this case, slowly but surely this moves us closer to the onion/clean arch.

For #29
See also: #25, #38
  • Loading branch information
JoaquimLey committed Apr 13, 2018
1 parent 321e0f5 commit cd1a8cc
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 76 deletions.
64 changes: 33 additions & 31 deletions transport-eta-android/mobile-ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,8 @@ android {
versionName app_version.versionName
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "com.joaquimley.transporteta.ui.test.TestRunner"
}

buildTypes {
debug {
testCoverageEnabled true
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

productFlavors {
flavorDimensions "city", "enviroment"

lx {
dimension "city"
}

dev {
// applicationIdSuffix ".dev"
dimension "enviroment"
}

mock {
// applicationIdSuffix ".mock"
dimension "enviroment"
}


missingDimensionStrategy 'environment', 'mock', 'prod'
}

packagingOptions {
Expand All @@ -59,6 +30,38 @@ android {
abortOnError false
ignoreWarnings true
}

buildTypes {
debug {
versionNameSuffix "-dev"
applicationIdSuffix ".debug"
debuggable true
testCoverageEnabled true
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

// productFlavors {
// flavorDimensions "city", "environment"
//
// lx {
// dimension "city"
// }
//
// dev {
// applicationIdSuffix ".dev"
// dimension "environment"
// }
//
// mock {
// applicationIdSuffix ".mock"
// dimension "environment"
// }
// }
}

kapt {
Expand All @@ -76,7 +79,6 @@ configurations.all {
}

dependencies {

// Modules
implementation project(':sms')

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.joaquimley.transporteta.ui.di.component

import android.app.Application
import com.joaquimley.transporteta.ui.di.module.ActivityBindingModule
import com.joaquimley.transporteta.ui.di.module.TestAppModule
import com.joaquimley.transporteta.ui.injection.scope.PerApplication
import com.joaquimley.transporteta.sms.SmsBroadcastReceiver
Expand All @@ -11,7 +10,7 @@ import dagger.BindsInstance
import dagger.Component
import dagger.android.support.AndroidSupportInjectionModule

@Component(modules = arrayOf(TestAppModule::class, ActivityBindingModule::class,
@Component(modules = arrayOf(TestAppModule::class, TestActivityBindingModule::class,
AndroidSupportInjectionModule::class))
@PerApplication
interface TestAppComponent : AppComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package com.joaquimley.transporteta.ui.di.module

import android.app.Application
import android.content.Context
import com.joaquimley.transporteta.ui.injection.scope.PerApplication
import com.joaquimley.transporteta.sms.SmsBroadcastReceiver
import com.joaquimley.transporteta.sms.SmsBroadcastReceiverImpl
import com.joaquimley.transporteta.sms.SmsController
import com.joaquimley.transporteta.sms.SmsControllerImpl
import com.joaquimley.transporteta.ui.injection.scope.PerApplication
import com.nhaarman.mockito_kotlin.mock
import dagger.Module
import dagger.Provides
import org.mockito.Mockito

@Module
class TestAppModule {
Expand All @@ -23,14 +21,13 @@ class TestAppModule {
@Provides
@PerApplication
fun provideSmsController(): SmsController {
return Mockito.mock(SmsControllerImpl::class.java)
return mock()
}


@Provides
@PerApplication
internal fun provideSmsBroadcastReceiver(): SmsBroadcastReceiver {
return Mockito.mock(SmsBroadcastReceiverImpl::class.java)
return mock()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.joaquimley.transporteta.R
import com.joaquimley.transporteta.ui.home.favorite.FavoritesFragment
import com.joaquimley.transporteta.ui.test.TestFragmentActivity
import com.joaquimley.transporteta.ui.test.factory.FavoriteViewTestFactory
import com.joaquimley.transporteta.ui.testing.TestFragmentActivity
import com.joaquimley.transporteta.ui.testing.factory.FavoriteViewTestFactory
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -22,13 +22,13 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class FavoritesFragmentTest {

@get:Rule val activityRule = ActivityTestRule(HomeActivity::class.java)
@get:Rule val activityRule = ActivityTestRule(TestFragmentActivity::class.java, true, true)
private lateinit var favoritesFragment: FavoritesFragment

@Before
fun setup() {
favoritesFragment = FavoritesFragment.newInstance()
// activityRule.activity.addFragment(favoritesFragment)
activityRule.activity.addFragment(favoritesFragment)
}

@Test
Expand Down
9 changes: 9 additions & 0 deletions transport-eta-android/mobile-ui/src/dev/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.joaquimley.transporteta"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity android:name=".ui.testing.TestFragmentActivity">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.joaquimley.transporteta.ui.test
package com.joaquimley.transporteta.ui.testing

import android.os.Bundle
import android.support.v4.app.Fragment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.joaquimley.transporteta.ui.test.factory.ui
package com.joaquimley.transporteta.ui.testing.factory.ui

import java.util.concurrent.ThreadLocalRandom

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.joaquimley.transporteta.ui.test.factory
package com.joaquimley.transporteta.ui.testing.factory

import android.support.annotation.RestrictTo
import com.joaquimley.transporteta.ui.model.FavoriteView
import com.joaquimley.transporteta.ui.test.factory.ui.DataFactory
import java.util.*
import com.joaquimley.transporteta.ui.testing.factory.ui.DataFactory
import kotlin.collections.ArrayList

@RestrictTo(RestrictTo.Scope.TESTS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.joaquimley.transporteta.ui.di.component

import com.joaquimley.transporteta.ui.di.module.HomeFragmentsBuildersModule
import com.joaquimley.transporteta.ui.injection.scope.PerActivity
import com.joaquimley.transporteta.ui.testing.TestFragmentActivity
import dagger.Module
import dagger.android.ContributesAndroidInjector

@Module
abstract class TestActivityBindingModule {

@PerActivity
@ContributesAndroidInjector(modules = arrayOf(HomeFragmentsBuildersModule::class))
abstract fun bindTestFragmentActivity(): TestFragmentActivity
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.joaquimley.transporteta.ui

import com.joaquimley.transporteta.sms.SmsBroadcastReceiver
import com.joaquimley.transporteta.sms.SmsControllerImpl
import com.joaquimley.transporteta.sms.model.SmsModel
import io.reactivex.Observable
import java.util.*
import javax.inject.Inject

class SmsControllerMockImpl @Inject constructor(smsBroadcastReceiver: SmsBroadcastReceiver) : SmsControllerImpl(smsBroadcastReceiver) {
override fun dispose() {
}

override fun observeIncomingSms(): Observable<SmsModel> {
return Observable.just(SmsModel(Random().nextInt(), UUID.randomUUID().toString()),
SmsModel(Random().nextInt(), UUID.randomUUID().toString()),
SmsModel(Random().nextInt(), UUID.randomUUID().toString()),
SmsModel(Random().nextInt(), UUID.randomUUID().toString()))
}

override fun requestEta(busStopCode: Int) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.joaquimley.transporteta.ui.model.data.Resource
import com.joaquimley.transporteta.ui.model.data.ResourceState
import com.joaquimley.transporteta.sms.SmsController
import com.joaquimley.transporteta.sms.model.SmsModel
import com.joaquimley.transporteta.ui.home.favorite.FavoritesViewModel
import com.nhaarman.mockito_kotlin.KArgumentCaptor
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
Expand Down
35 changes: 35 additions & 0 deletions transport-eta-android/sms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,41 @@ android {
disable 'IconMissingDensityFolder' //For testing purpose. This is safe to remove.
}

buildTypes {
debug {
testCoverageEnabled true
}

release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

productFlavors {
flavorDimensions "environment"

prod {
dimension "environment"
}

mock {
dimension "environment"
}
}

/**
* Prevent shipping the mock flavor with release variant
* read more at: https://developer.android.com/studio/build/build-variants.html
*/
variantFilter { variant ->
def names = variant.flavors*.name
// To check for a certain build type, use variant.buildType.name == "<buildType>"
if (names.contains("mock") && variant.buildType.name == "release") {
// Gradle ignores any variants that satisfy the conditions above.
setIgnore(true)
}
}
}

androidExtensions {
Expand Down
3 changes: 1 addition & 2 deletions transport-eta-android/sms/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joaquimley.transporteta.sms"/>
<manifest package="com.joaquimley.transporteta.sms"/>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController {
open class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController {

private val serviceSms: PublishSubject<SmsModel> = PublishSubject.create()
private var busStopCode: Int = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.joaquimley.transporteta.sms

import android.content.Context
import android.content.Intent
import android.provider.Telephony
import javax.inject.Inject

/**
* A broadcast receiver who listens for incoming SMS
*/

class SmsBroadcastReceiverImpl @Inject constructor(serviceNumber: String, serviceSmsCondition: String)
: SmsBroadcastReceiver(serviceNumber, serviceSmsCondition) {

override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
var smsBody = ""
var smsSender = ""
for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsSender = smsMessage.displayOriginatingAddress
smsBody += smsMessage.messageBody
}

if (smsSender == serviceNumber && smsBody.startsWith(serviceSmsCondition)) {
broadcastServiceSms.onNext(smsBody)
}
}
}
}
Loading

0 comments on commit cd1a8cc

Please sign in to comment.