This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from MaximeZmt/jwen/scoreboard-basic
Jwen/scoreboard basic
- Loading branch information
Showing
23 changed files
with
474 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/androidTest/java/ch/sdp/vibester/scoreboard/PlayerAdapterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/androidTest/java/ch/sdp/vibester/scoreboard/ScoreBoardActivityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
app/src/main/java/ch/sdp/vibester/scoreboard/PlayerAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Oops, something went wrong.