Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/CU09-TEST' into CU09-TEST
Browse files Browse the repository at this point in the history
  • Loading branch information
COLOSO70 committed Dec 14, 2023
2 parents 89e4c72 + 6e1ca28 commit d39349f
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ android {
dependencies {


testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
val room_version = "2.5.0"

implementation("androidx.preference:preference-ktx:1.2.1")
Expand Down
249 changes: 249 additions & 0 deletions app/src/androidTest/java/es/unex/giis/asee/gepeto/view/TestCU09.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package es.unex.giis.asee.gepeto.view


import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.*
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import es.unex.giis.asee.gepeto.R
import es.unex.giis.asee.gepeto.database.GepetoDatabase
import es.unex.giis.asee.gepeto.database.dao.UserDao
import es.unex.giis.asee.gepeto.model.User
import kotlinx.coroutines.runBlocking
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.`is`
import org.hamcrest.TypeSafeMatcher
import org.hamcrest.core.IsInstanceOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
class TestCU09 {

@Rule
@JvmField
var mActivityScenarioRule = ActivityScenarioRule(LoginActivity::class.java)

private lateinit var volatileDB: GepetoDatabase
private lateinit var userDAO: UserDao

// Se ejecuta antes del test, crea un usuario de prueba
@Before
fun setupUserDB() {
// Obtiene una referencia a la base de datos de Room
var context: Context = ApplicationProvider.getApplicationContext<Context>()
volatileDB = GepetoDatabase.getInstance(context)!!

userDAO = volatileDB.userDao()

// Crea un usuario de prueba
val user = User(1, "admin", "admin")

// Inserta el usuario en la base de datos
runBlocking {
userDAO.insert(user)
}
}

@Test
fun testCU09() {
val appCompatEditText = onView(
allOf(
withId(R.id.et_username),
childAtPosition(
childAtPosition(
withClassName(`is`("androidx.constraintlayout.widget.ConstraintLayout")),
3
),
1
)
)
)
appCompatEditText.perform(scrollTo(), replaceText("admin"), closeSoftKeyboard())

val appCompatEditText2 = onView(
allOf(
withId(R.id.et_password),
childAtPosition(
childAtPosition(
withClassName(`is`("androidx.constraintlayout.widget.ConstraintLayout")),
3
),
3
)
)
)
appCompatEditText2.perform(scrollTo(), replaceText("admin"), closeSoftKeyboard())

val materialButton3 = onView(
allOf(
withId(R.id.bt_login), withText("Login"),
childAtPosition(
childAtPosition(
withClassName(`is`("android.widget.LinearLayout")),
4
),
0
)
)
)
materialButton3.perform(scrollTo(), click())

val bottomNavigationItemView = onView(
allOf(
withId(R.id.ingredientesFragment), withContentDescription("Crear Receta"),
childAtPosition(
childAtPosition(
withId(R.id.bottom_navigation),
0
),
2
),
isDisplayed()
)
)
bottomNavigationItemView.perform(click())

val materialButton4 = onView(
allOf(
withId(R.id.button_item), withText("Ahi tuna"),
childAtPosition(
allOf(
withId(R.id.rv_item),
childAtPosition(
withId(R.id.rvTodosIngredientes),
4
)
),
0
)
)
)
materialButton4.perform(scrollTo(), click())

val materialButton5 = onView(
allOf(
withId(R.id.button_item), withText("Alfredo pasta sauce"),
childAtPosition(
allOf(
withId(R.id.rv_item),
childAtPosition(
withId(R.id.rvTodosIngredientes),
4
)
),
0
)
)
)
materialButton5.perform(scrollTo(), click())

val button = onView(
allOf(
withId(R.id.button_item), withText("Ahi tuna"),
withParent(
allOf(
withId(R.id.rv_item),
withParent(withId(R.id.rvIngredientesSeleccionados))
)
),
isDisplayed()
)
)
button.check(matches(isDisplayed()))

val button2 = onView(
allOf(
withId(R.id.button_item), withText("Alfredo pasta sauce"),
withParent(
allOf(
withId(R.id.rv_item),
withParent(withId(R.id.rvIngredientesSeleccionados))
)
),
isDisplayed()
)
)
button2.check(matches(isDisplayed()))

val materialButton6 = onView(
allOf(
withId(R.id.btnCrearReceta), withText("Crear Receta"),
childAtPosition(
childAtPosition(
withClassName(`is`("android.widget.FrameLayout")),
0
),
2
),
isDisplayed()
)
)
materialButton6.perform(click())

val textView = onView(
allOf(
withId(R.id.ingredientes), withText("Ahi tuna, Alfredo pasta sauce."),
withParent(withParent(IsInstanceOf.instanceOf(android.widget.FrameLayout::class.java))),
isDisplayed()
)
)
textView.check(matches(withText("Ahi tuna, Alfredo pasta sauce.")))

val textView2 = onView(
allOf(
withId(R.id.equipamiento), withText("."),
withParent(withParent(IsInstanceOf.instanceOf(android.widget.FrameLayout::class.java))),
isDisplayed()
)
)
textView2.check(matches(withText(".")))

val materialButton7 = onView(
allOf(
withId(R.id.crear_receta_btn), withText("Crear Receta"),
childAtPosition(
childAtPosition(
withClassName(`is`("android.widget.FrameLayout")),
0
),
7
),
isDisplayed()
)
)
materialButton7.perform(click())

Thread.sleep(5000)
}

private fun childAtPosition(
parentMatcher: Matcher<View>, position: Int
): Matcher<View> {

return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}

public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
}
94 changes: 94 additions & 0 deletions app/src/test/java/es/unex/giis/asee/gepeto/api/MealsAPITest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import es.unex.giis.asee.gepeto.api.MealsAPI
import es.unex.giis.asee.gepeto.data.api.Equipments
import es.unex.giis.asee.gepeto.data.api.Instructions
import es.unex.giis.asee.gepeto.data.api.Recipes
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertTrue
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MealsAPITest {

private lateinit var mockWebServer: MockWebServer
private lateinit var mealsAPI: MealsAPI

@Before
fun setup() {
// Configurar el servidor falso antes de cada prueba
mockWebServer = MockWebServer()
mealsAPI = Retrofit.Builder()
.baseUrl(mockWebServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MealsAPI::class.java)
}

@After
fun tearDown() {
// Apagar el servidor después de cada prueba
mockWebServer.shutdown()
}

@Test
fun getMealByIngredients() {
val responseBody = """
[
{"id": 1, "name": "Recipe 1"},
{"id": 2, "name": "Recipe 2"}
]
""".trimIndent()

val response = MockResponse().setBody(responseBody)
mockWebServer.enqueue(response)
runBlocking {
val result = mealsAPI.getMealByIngredients("apple,flour,sugar")

// Verificar que la solicitud se haya realizado correctamente
assertTrue(response != null)
assert(result is Recipes)
}
}

@Test
fun getMealSteps() {
val responseBody = """
[
{"step": "Step 1"},
{"step": "Step 2"},
{"step": "Step 3"}
]
""".trimIndent()

val response = MockResponse().setBody(responseBody)
mockWebServer.enqueue(response)
runBlocking {
val result = mealsAPI.getMealSteps("123")


// Verificar que la solicitud se haya realizado correctamente
assertTrue(response != null)
assert(result is Instructions)

}
}

@Test
fun getMealEquipments() {
val response = MockResponse().setBody("{}")
mockWebServer.enqueue(response)

runBlocking {
val result = mealsAPI.getMealEquipments("456")

// Verificar que la solicitud se haya realizado correctamente
assertTrue(response != null)
assert(result is Equipments)
}

}
}
46 changes: 46 additions & 0 deletions app/src/test/java/es/unex/giis/asee/gepeto/model/RecetaTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package es.unex.giis.asee.gepeto.model

import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.MockitoAnnotations

class RecetaTest {

private lateinit var receta: Receta

@Before
fun setup() {
MockitoAnnotations.initMocks(this)
receta = Receta(
1,
"Tarta de manzana",
"Tarta de manzana con hojaldre",
false,
"harina;azúcar;huevos",
"",
0,
""
)
}


@Test
fun getIngredientesPreview() {

val resultado = receta.getIngredientesPreview()

assertEquals("Ingredientes: Harina, Azúcar, Huevos.", resultado)
}


@Test
fun testListaIngredientesDetallesConIngredientes() {

val resultado = receta.listaIngredientesDetalles()

assertEquals("Ingredientes:\n\n - Harina\n - Azúcar\n - Huevos", resultado)
}


}

0 comments on commit d39349f

Please sign in to comment.