Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from MaximeZmt/jwen/scoreboard-basic
Browse files Browse the repository at this point in the history
Jwen/scoreboard basic
  • Loading branch information
Tsathogguaa authored Mar 9, 2022
2 parents 5db04e2 + 2049cd0 commit bcc9d6a
Show file tree
Hide file tree
Showing 23 changed files with 474 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'jacoco'
id 'kotlin-kapt'
}

android {
Expand Down Expand Up @@ -33,6 +34,12 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

testOptions {
unitTests {
includeAndroidResources = true
}
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
Expand All @@ -44,13 +51,26 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.6'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'org.mockito.kotlin:mockito-kotlin:4.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
implementation platform('com.google.firebase:firebase-bom:29.1.0')
implementation 'com.google.dagger:dagger:2.38.1'
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'de.hdodenhof:circleimageview:3.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation ('com.github.bumptech.glide:glide:4.13.0', {
exclude group: 'com.android.support'
})
kapt 'com.github.bumptech.glide:compiler:4.13.0'
kapt 'com.google.dagger:dagger-compiler:2.28.3'
}

tasks.withType(Test) {
Expand Down
24 changes: 20 additions & 4 deletions app/src/androidTest/java/ch/sdp/vibester/MainActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.typeText
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasExtra
import androidx.test.espresso.intent.matcher.IntentMatchers.toPackage
import androidx.test.espresso.intent.matcher.IntentMatchers.*
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.scoreboard.ScoreBoardActivity
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -22,15 +24,29 @@ class MainActivityTest {
MainActivity::class.java
)

@Before
fun setUp() {
Intents.init()
}

@After
fun clean() {
Intents.release()
}

@Test
fun mainTest(){
val inputName = "SDPUser"
Intents.init()
onView(withId(R.id.mainNameInput)).perform(typeText(inputName))
onView(withId(R.id.mainButton)).perform(click())
intended(toPackage("ch.sdp.vibester"))
intended(hasExtra("name", inputName))
Intents.release()
}

@Test
fun scoreboardTest() {
onView(withId(R.id.scoreboardButton)).perform(click())
intended(hasComponent(ScoreBoardActivity::class.qualifiedName))
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ch.sdp.vibester.scoreboard

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
import ch.sdp.vibester.scoreboard.Player
import ch.sdp.vibester.scoreboard.PlayerAdapter
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test


class PlayerAdapterTest {
@Test
fun recyclerViewShowsCorrectCount() {
val player1 = Player(111, "Brownie","https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", 687 )
val player2 = Player(555, "Cheesecake", "https://images.app.goo.gl/REJnoWR2t3mi2kYJA", 678)
val players: MutableList<Player> = arrayListOf()
players.addAll(listOf(player1, player2))
val playerViewHolder: RecyclerView.Adapter<PlayerAdapter.PlayerViewHolder> = PlayerAdapter(players)
assertThat(playerViewHolder.itemCount, equalTo(2))
}

@Test
fun itemTypeIsCorrect() {
val player1 = Player(111, "Brownie","https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", 687 )
val player2 = Player(555, "Cheesecake", "https://images.app.goo.gl/REJnoWR2t3mi2kYJA", 678)
val players: MutableList<Player> = arrayListOf()
players.addAll(listOf(player1, player2))
val playerViewHolder: RecyclerView.Adapter<PlayerAdapter.PlayerViewHolder> = PlayerAdapter(players)
val defaultType = 0
assertThat(playerViewHolder.getItemViewType(0), equalTo(defaultType))
}

@Test
fun setupAdapterForRecyclerView() {
val player1 = Player(111, "Brownie","https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", 687 )
val player2 = Player(555, "Cheesecake", "https://images.app.goo.gl/REJnoWR2t3mi2kYJA", 678)
val players: MutableList<Player> = arrayListOf()
players.addAll(listOf(player1, player2))
val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext())
recyclerView.layoutManager = LinearLayoutManager(ApplicationProvider.getApplicationContext())
val playerAdapter = PlayerAdapter(players)
recyclerView.adapter = playerAdapter
val newPlayers: MutableList<Player> = arrayListOf()
newPlayers.add(Player(444, "Scone", "https://images.app.goo.gl/YkBi16zwyjB7ejj96", 659))
playerAdapter.addPlayers(newPlayers)
playerAdapter.notifyDataSetChanged()
}

@Test
fun addPlayersWorks() {
val player1 = Player(111, "Brownie","https://images.app.goo.gl/yiPpy7JDRFaZRiAg9", 687 )
val player2 = Player(555, "Cheesecake", "https://images.app.goo.gl/REJnoWR2t3mi2kYJA", 678)
val players: MutableList<Player> = arrayListOf()
players.addAll(listOf(player1, player2))
val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext())
recyclerView.layoutManager = LinearLayoutManager(ApplicationProvider.getApplicationContext())
val playerAdapter = PlayerAdapter(players)
val player3 = Player(444, "Scone", "https://images.app.goo.gl/YkBi16zwyjB7ejj96", 659)
val updatedList = arrayListOf(player3)
playerAdapter.addPlayers(updatedList)
assertThat(playerAdapter.players == updatedList, equalTo(true))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ch.sdp.vibester.scoreboard

import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.rules.ActivityScenarioRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import ch.sdp.vibester.R

@RunWith(AndroidJUnit4::class)
class ScoreBoardActivityTest {

@Rule
@JvmField
val activityRule = ActivityScenarioRule(ScoreBoardActivity::class.java)

@Test
fun recycleViewToViewTest() {
onView(withId(R.id.recycler_view)).check(matches(isDisplayed()))
}

@Test
fun recycleViewClickTest() {
onView((withId(R.id.recycler_view)))
.perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(2, click()))
}

@Test
fun recycleViewScrollDownTest() {
val recyclerView = RecyclerView(ApplicationProvider.getApplicationContext())
val itemCount = recyclerView.adapter?.itemCount
if(itemCount != null) {
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(itemCount.minus(1)))
}
}

@Test
fun recycleViewShowItemTest() {
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(5, click()))
val nameItem = "Cinnamon Roll"
onView(withText(nameItem)).check(matches(isDisplayed()))
}
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ch.sdp.vibester">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -12,6 +14,8 @@
<activity
android:name="ch.sdp.vibester.GreetingActivity"
android:exported="false" />
<activity android:name=".scoreboard.ScoreBoardActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name="ch.sdp.vibester.MainActivity"
android:exported="true">
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/ch/sdp/vibester/AppGlideModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.sdp.vibester
import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.request.RequestOptions

@GlideModule
class AppGlideModule : AppGlideModule() {
override fun applyOptions(context: Context, builder: GlideBuilder) {
super.applyOptions(context, builder)
builder.apply { RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL) }
}

}
11 changes: 8 additions & 3 deletions app/src/main/java/ch/sdp/vibester/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import ch.sdp.vibester.scoreboard.ScoreBoardActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -16,12 +17,16 @@ class MainActivity : AppCompatActivity() {
val btnGreeting = findViewById<Button>(R.id.mainButton)
val greetingIntent = Intent(this, GreetingActivity::class.java)




btnGreeting.setOnClickListener {
greetingIntent.putExtra("name", txtInput.text.toString())
startActivity(greetingIntent)
}

val btnScoreboard = findViewById<Button>(R.id.scoreboardButton)
val scoreboardIntent = Intent(this, ScoreBoardActivity::class.java)

btnScoreboard.setOnClickListener {
startActivity(scoreboardIntent)
}
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/ch/sdp/vibester/scoreboard/Helper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.sdp.vibester.scoreboard

import android.widget.ImageView
import ch.sdp.vibester.GlideApp

/**
* Helper function to load image from given url
*/
fun ImageView.loadImg(url: String) {
GlideApp.with(context).load(url).into(this)
}
7 changes: 7 additions & 0 deletions app/src/main/java/ch/sdp/vibester/scoreboard/Player.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ch.sdp.vibester.scoreboard

data class Player (
val nik: Int = 0,
val name: String = "",
val photo: String = "",
val score: Int = 0)
44 changes: 44 additions & 0 deletions app/src/main/java/ch/sdp/vibester/scoreboard/PlayerAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch.sdp.vibester.scoreboard

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import ch.sdp.vibester.R

class PlayerAdapter(playersInit: List<Player>) : RecyclerView.Adapter<PlayerAdapter.PlayerViewHolder>() {

var players: MutableList<Player> = playersInit.toMutableList()

override fun getItemCount(): Int = players.size

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayerViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.scoreboard_item_player, parent, false)
return PlayerViewHolder(view)
}

override fun onBindViewHolder(holder: PlayerViewHolder, position: Int) {
holder.bind(players[position], position)
}

fun addPlayers(players: List<Player>) {
this.players.apply {
clear()
addAll(players)
}
notifyDataSetChanged()
}

inner class PlayerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bind(player: Player, position: Int) {
val newPosition = position + 1
itemView.findViewById<TextView>(R.id.tv_position).text = (newPosition).toString()
itemView.findViewById<TextView>(R.id.tv_name).text = player.name
itemView.findViewById<TextView>(R.id.tv_score).text = player.score.toString()
itemView.findViewById<ImageView>(R.id.iv_photo).loadImg(player.photo)
}
}
}
Loading

0 comments on commit bcc9d6a

Please sign in to comment.