-
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 #61 from BruceTrindade/develop
Develop
- Loading branch information
Showing
35 changed files
with
274 additions
and
211 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.
Binary file not shown.
Binary file not shown.
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,49 @@ | ||
package com.example.pokedex | ||
|
||
import android.content.ComponentName | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentFactory | ||
import androidx.fragment.app.testing.FragmentScenario | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.espresso.core.internal.deps.dagger.internal.Preconditions | ||
import com.example.pokedex.testing.HiltTestActivity | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
|
||
@ExperimentalCoroutinesApi | ||
inline fun <reified T : Fragment> launchFragmentInHiltContainer( | ||
fragmentArgs: Bundle? = null, | ||
themeResId: Int = R.style.FragmentScenarioEmptyFragmentActivityTheme, | ||
fragmentFactory: FragmentFactory? = null, | ||
crossinline action: T.() -> Unit = {} | ||
) { | ||
val mainActivityIntent = Intent.makeMainActivity( | ||
ComponentName( | ||
ApplicationProvider.getApplicationContext(), | ||
HiltTestActivity::class.java | ||
) | ||
).putExtra( | ||
"androidx.fragment.app.testing.FragmentScenario.EmptyFragmentActivity.THEME_EXTRAS_BUNDLE_KEY", | ||
themeResId | ||
) | ||
|
||
ActivityScenario.launch<HiltTestActivity>(mainActivityIntent).onActivity { activity -> | ||
fragmentFactory?.let { | ||
activity.supportFragmentManager.fragmentFactory = it | ||
} | ||
val fragment = activity.supportFragmentManager.fragmentFactory.instantiate( | ||
Preconditions.checkNotNull(T::class.java.classLoader), | ||
T::class.java.name | ||
) | ||
fragment.arguments = fragmentArgs | ||
|
||
activity.supportFragmentManager.beginTransaction() | ||
.add(android.R.id.content, fragment, "") | ||
.commitNow() | ||
|
||
(fragment as T).action() | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
app/src/androidTest/kotlin/com/example/pokedex/HiltTestRunner.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,15 @@ | ||
package com.example.pokedex | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import androidx.test.runner.AndroidJUnitRunner | ||
import dagger.hilt.android.testing.HiltTestApplication | ||
|
||
@Suppress("unused") | ||
class HiltTestRunner : AndroidJUnitRunner() { | ||
override fun newApplication( | ||
cl: ClassLoader?, | ||
className: String?, | ||
context: Context? | ||
): Application = super.newApplication(cl, HiltTestApplication::class.java.name, context) | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/androidTest/kotlin/com/example/pokedex/MockPokemon.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,20 @@ | ||
package com.example.pokedex | ||
|
||
import androidx.navigation.NavController | ||
import com.example.pokedex.data.Pokemon | ||
import com.example.pokedex.data.PokemonType | ||
import org.mockito.Mockito | ||
|
||
object MockPokemon { | ||
|
||
val navController = Mockito.mock(NavController::class.java) | ||
|
||
val list: List<PokemonType> = listOf(PokemonType("grass"), PokemonType("poison")) | ||
|
||
val mockPokemon: Pokemon = Pokemon( | ||
number = "3", | ||
name = "venusaur", | ||
types = list, | ||
imageUrl = "3" | ||
) | ||
} |
59 changes: 59 additions & 0 deletions
59
app/src/androidTest/kotlin/com/example/pokedex/ui/details/PokedexDetailsFragmentTest.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,59 @@ | ||
package com.example.pokedex.ui.details | ||
|
||
import androidx.core.os.bundleOf | ||
import androidx.navigation.Navigation | ||
import androidx.test.espresso.Espresso | ||
import androidx.test.espresso.assertion.ViewAssertions | ||
import androidx.test.espresso.matcher.ViewMatchers | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import com.example.pokedex.MockPokemon | ||
import com.example.pokedex.MockPokemon.navController | ||
import com.example.pokedex.R | ||
import com.example.pokedex.launchFragmentInHiltContainer | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@ExperimentalCoroutinesApi | ||
@HiltAndroidTest | ||
class PokedexDetailsFragmentTest { | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@Before | ||
fun setup() { | ||
hiltRule.inject() | ||
|
||
launchFragmentInHiltContainer<PokedexDetailsFragment>(fragmentArgs = bundleOf("pokemon" to MockPokemon.mockPokemon)) { | ||
Navigation.setViewNavController(requireView(), navController) | ||
} | ||
} | ||
|
||
@Test | ||
fun checkIfAfterPassArgumentThePokemonNameIsCorrect() { | ||
Espresso.onView(withId(R.id.pokemon_name)) | ||
.check(ViewAssertions.matches(ViewMatchers.withText("Venusaur"))) | ||
} | ||
|
||
@Test | ||
fun checkIfAfterPassArgumentThePokemonNumberIsCorrect() { | ||
Espresso.onView(withId(R.id.pokemon_number)) | ||
.check(ViewAssertions.matches(ViewMatchers.withText("003"))) | ||
} | ||
|
||
@Test | ||
fun checkIfAfterPassArgumentThePokemonTypeIsCorrect() { | ||
Espresso.onView(withId(R.id.pokemon_primary_type)) | ||
.check(ViewAssertions.matches(ViewMatchers.withText("grass"))) | ||
} | ||
|
||
@Test | ||
fun checkIfAfterPassArgumentThePokemonSecondTypeIsCorrect() { | ||
Espresso.onView(withId(R.id.pokemon_second_type)) | ||
.check(ViewAssertions.matches(ViewMatchers.withText("poison"))) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/androidTest/kotlin/com/example/pokedex/ui/home/PokedexHomeFragmentTest.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,49 @@ | ||
package com.example.pokedex.ui.home | ||
|
||
import androidx.navigation.Navigation | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.action.ViewActions.click | ||
import androidx.test.espresso.contrib.RecyclerViewActions | ||
import androidx.test.espresso.matcher.ViewMatchers.* | ||
import com.example.pokedex.MockPokemon.mockPokemon | ||
import com.example.pokedex.MockPokemon.navController | ||
import com.example.pokedex.R | ||
import com.example.pokedex.launchFragmentInHiltContainer | ||
import dagger.hilt.android.testing.HiltAndroidRule | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.Mockito.verify | ||
|
||
@ExperimentalCoroutinesApi | ||
@HiltAndroidTest | ||
class PokedexHomeFragmentTest { | ||
|
||
@get:Rule | ||
var hiltRule = HiltAndroidRule(this) | ||
|
||
@Before | ||
fun setup() { | ||
hiltRule.inject() | ||
|
||
launchFragmentInHiltContainer<PokedexHomeFragment> { | ||
Navigation.setViewNavController(requireView(), navController) | ||
} | ||
} | ||
|
||
@Test | ||
fun navigate_to_detailsPokedex() { | ||
onView(withId(R.id.pokemon_recycler)) | ||
.perform( | ||
RecyclerViewActions | ||
.actionOnItemAtPosition<RecyclerView.ViewHolder>(2, click()) | ||
) | ||
|
||
verify(navController).navigate( | ||
PokedexHomeFragmentDirections.actionFirstDestinationToPokedexDetailsFragment(mockPokemon) | ||
) | ||
} | ||
} |
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,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.pokedex"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application> | ||
<activity | ||
android:name=".testing.HiltTestActivity" | ||
android:exported="true"> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
7 changes: 7 additions & 0 deletions
7
app/src/debug/java/com/example/pokedex/testing/HiltTestActivity.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,7 @@ | ||
package com.example.pokedex.testing | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class HiltTestActivity : AppCompatActivity() |
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
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 |
---|---|---|
@@ -1,45 +1,15 @@ | ||
package com.example.pokedex.data | ||
|
||
import android.os.Parcelable | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import androidx.room.TypeConverters | ||
import com.example.pokedex.data.local.PokedexConverters | ||
import kotlinx.android.parcel.Parcelize | ||
import kotlinx.android.parcel.RawValue | ||
|
||
@Parcelize | ||
@Entity(tableName = "pokemon") | ||
@TypeConverters(PokedexConverters::class) | ||
data class Pokemon( | ||
@PrimaryKey | ||
val number: String, | ||
val name: String, | ||
val types: @RawValue List<PokemonType>, | ||
val imageUrl: String | ||
) : Parcelable { | ||
|
||
// val formattedNumber = when { | ||
// number.length < 2 -> "00$number" | ||
// number.length < 3 -> "0$number" | ||
// else -> number | ||
// } | ||
// | ||
// val formattedName = name.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } | ||
// | ||
// val imageUrl = "https://assets.pokemon.com/assets/cms2/img/pokedex/full/$formattedNumber.png" | ||
} | ||
|
||
/* | ||
@Entity(tableName = "pokemon") | ||
data class Pokemon( | ||
@PrimaryKey(autoGenerate = true) | ||
@SerializedName("number") | ||
val number: String, | ||
@SerializedName("name") | ||
val name: String, | ||
@SerializedName("types") | ||
val types: @RawValue List<PokemonType> | ||
) : Serializable { | ||
*/ | ||
) : Parcelable |
10 changes: 2 additions & 8 deletions
10
app/src/main/kotlin/com/example/pokedex/data/PokemonRepository.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 |
---|---|---|
@@ -1,18 +1,12 @@ | ||
package com.example.pokedex.data | ||
|
||
import com.example.pokedex.api.PokemonService | ||
import com.example.pokedex.data.local.PokedexDao | ||
import javax.inject.Inject | ||
|
||
class PokemonRepository @Inject constructor( | ||
private val service: PokemonService, | ||
private val dao: PokedexDao | ||
private val service: PokemonService | ||
) { | ||
|
||
suspend fun listPokemons(limit: Int = 100) = service.listPokemons(limit) | ||
suspend fun listPokemons(limit: Int = 500) = service.listPokemons(limit) | ||
suspend fun getPokemons(number: Int) = service.getPokemon(number) | ||
|
||
suspend fun insert(pokemonModel: Pokemon) = dao.insert(pokemonModel) | ||
fun getAll() = dao.getAll() | ||
suspend fun delete(pokemonModel: Pokemon) = dao.delete(pokemonModel) | ||
} |
Oops, something went wrong.