Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Request Challenge Joao Alvares Neto #3

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*.iml
.gradle
.idea
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
70 changes: 70 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.joao.photoscodechallenge"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
androidExtensions {
experimental = true
}
}

dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation "com.android.support:design:27.1.1"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

implementation "org.mockito:mockito-core:2.12.0"
implementation "com.squareup.okhttp3:mockwebserver:3.8.0"
testImplementation "com.nhaarman:mockito-kotlin-kt1.1:1.5.0"
androidTestImplementation "org.mockito:mockito-android:2.9.0"
testImplementation "android.arch.core:core-testing:1.1.1"

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}

implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.10.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"

implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation "com.jakewharton.rxbinding2:rxbinding-kotlin:2.1.1"
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"


implementation "android.arch.lifecycle:viewmodel:1.1.1"
implementation "android.arch.lifecycle:extensions:1.1.1"

implementation "com.github.salomonbrys.kodein:kodein:4.1.0"
implementation "com.github.salomonbrys.kodein:kodein-conf:4.1.0"
implementation "com.github.salomonbrys.kodein:kodein-android:4.1.0"
implementation "com.github.salomonbrys.kodein:kodein-core:4.1.0"

implementation 'com.squareup.picasso:picasso:2.71828'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
582 changes: 582 additions & 0 deletions app/src/androidTest/assets/success.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.joao.photoscodechallenge

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.test.InstrumentationRegistry
import android.support.test.espresso.intent.Intents
import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.github.salomonbrys.kodein.Kodein
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.runner.RunWith


/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
@LargeTest
@RunWith(AndroidJUnit4::class)
abstract class AcceptanceTest<T : Activity>(clazz: Class<T>){

val server = MockWebServer()

@Rule
@JvmField
val testRule: ActivityTestRule<T> = ActivityTestRule(clazz, true, false)

@Before
fun setup() {
val app = InstrumentationRegistry.getInstrumentation().targetContext.asApplication()
app.resetInjection()
app.addModule(testDependencies)
Intents.init()
server.start()
}

protected fun startActivity(args: Bundle = Bundle()): T {
val intent = Intent()
intent.putExtras(args)
return testRule.launchActivity(intent)
}

fun getJson(fileName : String) = FileHandler().readResource(InstrumentationRegistry.getInstrumentation().context,fileName)

abstract val testDependencies: Kodein.Module

@After
fun tearDown(){
Intents.release()
server.shutdown()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.joao.photoscodechallenge

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("com.example.joao.photoscodechallenge", appContext.packageName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.joao.photoscodechallenge

import android.content.Context
import java.util.*

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class FileHandler {

fun readResource(context: Context, fileName: String): String{

val stream = context.resources.assets.open(fileName)
val sb = StringBuilder()

try {
val scanner = Scanner(stream)
while (scanner.hasNextLine()) {
val line = scanner.nextLine()
sb.append(line).append("\n")
}
}catch (e: Exception){
e.printStackTrace()
}finally {
stream.close()
}
return sb.toString()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.joao.photoscodechallenge

import okhttp3.Interceptor

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class RequestInterceptorMock constructor(private val exception: Exception): Interceptor {
override fun intercept(chain: Interceptor.Chain) = throw exception
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.joao.photoscodechallenge.home

import com.example.joao.photoscodechallenge.AcceptanceTest
import com.example.joao.photoscodechallenge.RequestInterceptorMock
import com.example.joao.photoscodechallenge.di.Injector
import com.example.joao.photoscodechallenge.robots.robot
import com.example.joao.photoscodechallenge.ui.MainActivity
import com.example.joao.photoscodechallenge.webservice.exceptions.BadRequestException
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.provider
import okhttp3.Interceptor
import org.junit.Test

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class HomeActivityBadRequestTest : AcceptanceTest<MainActivity>(MainActivity::class.java) {

@Test
fun testWithBadRequestState() {

startActivity()

robot {

} withBadRequest {
errorHasBeenShown()
}
}

override val testDependencies = Kodein.Module(allowSilentOverride = true) {
bind<Interceptor>(tag = Injector.REQUEST_INTERCEPTOR,overrides = true) with provider {
RequestInterceptorMock(BadRequestException())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.joao.photoscodechallenge.home


import com.example.joao.photoscodechallenge.AcceptanceTest
import com.example.joao.photoscodechallenge.RequestInterceptorMock
import com.example.joao.photoscodechallenge.di.Injector
import com.example.joao.photoscodechallenge.robots.robot
import com.example.joao.photoscodechallenge.ui.MainActivity
import com.example.joao.photoscodechallenge.webservice.exceptions.Error4XXException
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.provider
import okhttp3.Interceptor
import org.junit.Test

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class HomeActivityClientErrorTest : AcceptanceTest<MainActivity>(MainActivity::class.java) {

@Test
fun testWithClientErrorState() {

startActivity()

robot {

} withClientError {
errorHasBeenShown()
}
}

override val testDependencies = Kodein.Module(allowSilentOverride = true) {
bind<Interceptor>(tag = Injector.REQUEST_INTERCEPTOR, overrides = true) with provider {
RequestInterceptorMock(Error4XXException())
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.joao.photoscodechallenge.home


import com.example.joao.photoscodechallenge.AcceptanceTest
import com.example.joao.photoscodechallenge.robots.robot
import com.example.joao.photoscodechallenge.ui.MainActivity
import com.github.salomonbrys.kodein.Kodein
import okhttp3.mockwebserver.MockResponse
import org.junit.Test

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class HomeActivityContentTest : AcceptanceTest<MainActivity>(MainActivity::class.java) {

@Test
fun testWithContentState() {

server.enqueue(MockResponse()
.setResponseCode(200)
.setBody(getJson("success.json")))

startActivity()

robot{

} withContent {
isSuccess()
}
}
override val testDependencies = Kodein.Module(allowSilentOverride = true) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.joao.photoscodechallenge.home


import com.example.joao.photoscodechallenge.AcceptanceTest
import com.example.joao.photoscodechallenge.RequestInterceptorMock
import com.example.joao.photoscodechallenge.di.Injector
import com.example.joao.photoscodechallenge.robots.robot
import com.example.joao.photoscodechallenge.ui.MainActivity
import com.example.joao.photoscodechallenge.webservice.exceptions.NoDataException
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.bind
import com.github.salomonbrys.kodein.provider
import okhttp3.Interceptor
import org.junit.Test

/**
* Created by Joao Alvares Neto on 07/05/2018.
*/
class HomeActivityEmptyStateTest : AcceptanceTest<MainActivity>(MainActivity::class.java) {

@Test
fun testWithEmptyState() {

startActivity()

robot {

} withEmptyState {
errorHasBeenShown()
}
}

override val testDependencies = Kodein.Module(allowSilentOverride = true) {
bind<Interceptor>(tag = Injector.REQUEST_INTERCEPTOR,overrides = true) with provider {
RequestInterceptorMock(NoDataException())
}
}
}
Loading