Skip to content

Commit

Permalink
07 introduction to mockito (#2)
Browse files Browse the repository at this point in the history
* Updating chapter 7 dependency versions

* Updating chapter 7 copyright year

* Updated to Kotlin 1.5.0.

* Updating chapter 7 starter project so that it shows a blank screen

* Remove extra blank comment.

* Update challenge to use 1.5.0.

Co-authored-by: Jonathan Wong <[email protected]>
  • Loading branch information
fernandospr and jonathanwong committed Jun 1, 2021
1 parent eb7124c commit 8e297c3
Show file tree
Hide file tree
Showing 108 changed files with 344 additions and 473 deletions.
36 changes: 15 additions & 21 deletions 07-introduction-to-mockito/projects/challenge/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,16 +32,14 @@ apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "com.raywenderlich.android.cocktails"
minSdkVersion 19
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -57,26 +55,22 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.3.0'

implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.13.1'
implementation 'com.github.bumptech.glide:glide:4.9.0'
kapt 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

testImplementation 'junit:junit:4.12'
testImplementation 'com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0'
testImplementation 'androidx.arch.core:core-testing:2.0.1'
}
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
implementation 'com.github.bumptech.glide:glide:4.11.0'

androidExtensions {
experimental = true
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito.kotlin:mockito-kotlin:3.2.0'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2019 Razeware LLC
~ Copyright (c) 2021 Razeware LLC
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,126 +30,125 @@

package com.raywenderlich.android.cocktails

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.raywenderlich.android.cocktails.databinding.ActivityGameBinding
import com.raywenderlich.android.cocktails.game.model.Question
import com.raywenderlich.android.cocktails.game.model.Score
import com.raywenderlich.android.cocktails.game.viewmodel.CocktailsGameViewModel
import com.raywenderlich.android.cocktails.game.viewmodel.CocktailsGameViewModelFactory
import kotlinx.android.synthetic.main.activity_game.*
import kotlinx.android.synthetic.main.view_error.*
import kotlinx.android.synthetic.main.view_game_over.*
import kotlinx.android.synthetic.main.view_loading.*

class CocktailsGameActivity : AppCompatActivity() {

private lateinit var viewModel: CocktailsGameViewModel

private lateinit var binding: ActivityGameBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
binding = ActivityGameBinding.inflate(layoutInflater)
setContentView(binding.root)

val repository = (application as CocktailsApplication).repository
val factory = (application as CocktailsApplication).gameFactory
viewModel = ViewModelProviders.of(this, CocktailsGameViewModelFactory(repository, factory))
viewModel = ViewModelProvider(this, CocktailsGameViewModelFactory(repository, factory))
.get(CocktailsGameViewModel::class.java)

viewModel.getLoading().observe(this, Observer {
viewModel.getLoading().observe(this) {
it?.let { loading -> showLoading(loading) }
})
viewModel.getError().observe(this, Observer {
}
viewModel.getError().observe(this) {
it?.let { error -> showError(error) }
})
viewModel.getQuestion().observe(this, Observer {
}
viewModel.getQuestion().observe(this) {
showQuestion(it)
})
viewModel.getScore().observe(this, Observer {
}
viewModel.getScore().observe(this) {
it?.let { score -> showScore(score) }
})
viewModel.getGameOver().observe(this, Observer {
}
viewModel.getGameOver().observe(this) {
it?.let { isOver -> showGameOver(isOver) }
})
}

nextButton.setOnClickListener { viewModel.nextQuestion() }
binding.nextButton.setOnClickListener { viewModel.nextQuestion() }

viewModel.initGame()
}

private fun showScore(score: Score) {
scoreTextView.text = getString(R.string.game_score, score.current)
highScoreTextView.text = getString(R.string.game_highscore, score.highest)
binding.scoreTextView.text = getString(R.string.game_score, score.current)
binding.highScoreTextView.text = getString(R.string.game_highscore, score.highest)
}

private fun showQuestion(question: Question?) {
if (question != null) {
mainGroup.visibility = View.VISIBLE
binding.mainGroup.visibility = View.VISIBLE

if (question.answeredOption != null) {
showAnsweredQuestion(question)
} else {
showUnansweredQuestion(question)
}
} else {
mainGroup.visibility = View.GONE
noResultsTextView.visibility = View.VISIBLE
questionResultImageView.visibility = View.GONE
binding.mainGroup.visibility = View.GONE
binding.noResultsTextView.visibility = View.VISIBLE
binding.questionResultImageView.visibility = View.GONE
}
}

private fun showUnansweredQuestion(question: Question) {
val options = question.getOptions()
firstOptionButton.text = options[0]
firstOptionButton.setOnClickListener { viewModel.answerQuestion(question, firstOptionButton.text.toString()) }
firstOptionButton.isEnabled = true
secondOptionButton.text = options[1]
secondOptionButton.setOnClickListener { viewModel.answerQuestion(question, secondOptionButton.text.toString()) }
secondOptionButton.isEnabled = true

Glide.with(cocktailImageView).load(question.imageUrl).into(cocktailImageView)
questionResultImageView.visibility = View.GONE
binding.firstOptionButton.text = options[0]
binding.firstOptionButton.setOnClickListener { viewModel.answerQuestion(question, binding.firstOptionButton.text.toString()) }
binding.firstOptionButton.isEnabled = true
binding.secondOptionButton.text = options[1]
binding.secondOptionButton.setOnClickListener { viewModel.answerQuestion(question, binding.secondOptionButton.text.toString()) }
binding.secondOptionButton.isEnabled = true

Glide.with(binding.cocktailImageView).load(question.imageUrl).into(binding.cocktailImageView)
binding.questionResultImageView.visibility = View.GONE
}

private fun showAnsweredQuestion(question: Question) {
firstOptionButton.setOnClickListener(null)
firstOptionButton.isEnabled = false
secondOptionButton.setOnClickListener(null)
secondOptionButton.isEnabled = false
binding.firstOptionButton.setOnClickListener(null)
binding.firstOptionButton.isEnabled = false
binding.secondOptionButton.setOnClickListener(null)
binding.secondOptionButton.isEnabled = false
if (question.isAnsweredCorrectly) {
questionResultImageView.setImageResource(R.drawable.ic_check_24dp)
binding.questionResultImageView.setImageResource(R.drawable.ic_check_24dp)
} else {
questionResultImageView.setImageResource(R.drawable.ic_error_24dp)
binding.questionResultImageView.setImageResource(R.drawable.ic_error_24dp)
}
questionResultImageView.visibility = View.VISIBLE
binding.questionResultImageView.visibility = View.VISIBLE
}

private fun showError(show: Boolean) {
if (show) {
errorContainer.visibility = View.VISIBLE
retryButton.setOnClickListener { viewModel.initGame() }
binding.errorContainer.root.visibility = View.VISIBLE
binding.errorContainer.retryButton.setOnClickListener { viewModel.initGame() }
} else {
errorContainer.visibility = View.GONE
binding.errorContainer.root.visibility = View.GONE
}
}

private fun showLoading(show: Boolean) {
loadingContainer.visibility = if (show) View.VISIBLE else View.GONE
mainGroup.visibility = View.GONE
noResultsTextView.visibility = View.GONE
questionResultImageView.visibility = View.GONE
binding.loadingContainer.root.visibility = if (show) View.VISIBLE else View.GONE
binding.mainGroup.visibility = View.GONE
binding.noResultsTextView.visibility = View.GONE
binding.questionResultImageView.visibility = View.GONE
}

private fun showGameOver(show: Boolean) {
if (show) {
gameOverContainer.visibility = View.VISIBLE
mainGroup.visibility = View.GONE
restartButton.setOnClickListener { viewModel.initGame() }
questionResultImageView.visibility = View.GONE
binding.gameOverContainer.root.visibility = View.VISIBLE
binding.mainGroup.visibility = View.GONE
binding.gameOverContainer.restartButton.setOnClickListener { viewModel.initGame() }
binding.questionResultImageView.visibility = View.GONE
} else {
gameOverContainer.visibility = View.GONE
binding.gameOverContainer.root.visibility = View.GONE
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,7 +33,7 @@ package com.raywenderlich.android.cocktails.common.network
import android.os.Parcelable
import com.google.gson.GsonBuilder
import com.raywenderlich.android.cocktails.BuildConfig
import kotlinx.android.parcel.Parcelize
import kotlinx.parcelize.Parcelize
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Razeware LLC
* Copyright (c) 2021 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (c) 2019 Razeware LLC
~ Copyright (c) 2021 Razeware LLC
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
~ Copyright (c) 2019 Razeware LLC
~ Copyright (c) 2021 Razeware LLC
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 8e297c3

Please sign in to comment.