Skip to content

Commit

Permalink
Merge branch 'feature/506399-MostrarHorarioGasolinera' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
HERRERA99 committed Nov 7, 2024
2 parents 226f7cf + f5adaaf commit 75db675
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 263 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package es.unican.gasolineras.activities.common;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.swipeUp;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anything;
import static org.hamcrest.CoreMatchers.is;
import static org.mockito.Mockito.when;
import static es.unican.gasolineras.utils.Matchers.withTextColor;
import static es.unican.gasolineras.utils.MockRepositories.getTestRepository;
import static es.unican.gasolineras.common.UtilsHorario.obtenerDiaActual;
import static es.unican.gasolineras.common.UtilsHorario.procesaHorario;
import static es.unican.gasolineras.utils.MockRepositories.getTestRepositoryList;

import android.content.Context;

Expand All @@ -23,19 +22,20 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.testing.BindValue;
import dagger.hilt.android.testing.HiltAndroidRule;
import dagger.hilt.android.testing.HiltAndroidTest;
import dagger.hilt.android.testing.UninstallModules;
import es.unican.gasolineras.R;
import es.unican.gasolineras.activities.main.MainView;
import es.unican.gasolineras.common.TimeProvider;
import es.unican.gasolineras.common.UtilsHorario;
import es.unican.gasolineras.injection.RepositoriesModule;
import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.repository.IGasolinerasRepository;

@UninstallModules(RepositoriesModule.class)
Expand All @@ -49,19 +49,81 @@ public class MostrarHorarioGasolineraUITest {
// I need the context to access resources, such as the json with test gas stations
final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

private final List<Gasolinera> listaGasolineras = getListaGasolineras();

// Mock repository that provides data from a JSON file instead of downloading it from the internet.
@BindValue
public final IGasolinerasRepository repository = getTestRepository(context, R.raw.gasolineras_prueba_horarios);

@Mock
private static TimeProvider mockObtenerDiaHora;
public final IGasolinerasRepository repository = getTestRepositoryList(listaGasolineras);

public static List<Gasolinera> getListaGasolineras() {
List<Gasolinera> aux = new ArrayList<>();

//creo las gasolineras
Gasolinera gasolinera1 = new Gasolinera();
gasolinera1.setId("Gasolinera1");
gasolinera1.setRotulo("REPSOL");
gasolinera1.setDireccion("Sardinero");
gasolinera1.setGasoleoA(1.5);
gasolinera1.setGasolina95E5(1.48);
gasolinera1.setHorario("L-D: 24H");
aux.add(gasolinera1);

//creo las gasolineras
Gasolinera gasolinera2 = new Gasolinera();
gasolinera2.setId("Gasolinera2");
gasolinera2.setRotulo("REPSOL");
gasolinera2.setDireccion("Sardinero");
gasolinera2.setGasoleoA(1.5);
gasolinera2.setGasolina95E5(1.48);
gasolinera2.setHorario(UtilsHorario.obtenerHorarioAbiertoSimple());
aux.add(gasolinera2);

//creo las gasolineras
Gasolinera gasolinera3 = new Gasolinera();
gasolinera3.setId("Gasolinera3");
gasolinera3.setRotulo("REPSOL");
gasolinera3.setDireccion("Sardinero");
gasolinera3.setGasoleoA(1.5);
gasolinera3.setGasolina95E5(1.48);
gasolinera3.setHorario(UtilsHorario.obtenerHorarioAbiertoIntervalo());
aux.add(gasolinera3);

//creo las gasolineras
Gasolinera gasolinera4 = new Gasolinera();
gasolinera4.setId("Gasolinera4");
gasolinera4.setRotulo("REPSOL");
gasolinera4.setDireccion("Sardinero");
gasolinera4.setGasoleoA(1.5);
gasolinera4.setGasolina95E5(1.48);
gasolinera4.setHorario(UtilsHorario.obtenerHorarioCerradoTodoElDia());
aux.add(gasolinera4);

//creo las gasolineras
Gasolinera gasolinera5 = new Gasolinera();
gasolinera5.setId("Gasolinera5");
gasolinera5.setRotulo("REPSOL");
gasolinera5.setDireccion("Sardinero");
gasolinera5.setGasoleoA(1.5);
gasolinera5.setGasolina95E5(1.48);
gasolinera5.setHorario(UtilsHorario.obtenerHorarioCerradoIntervalo());
aux.add(gasolinera5);

//creo las gasolineras
Gasolinera gasolinera6 = new Gasolinera();
gasolinera6.setId("Gasolinera6");
gasolinera6.setRotulo("REPSOL");
gasolinera6.setDireccion("Sardinero");
gasolinera6.setGasoleoA(1.5);
gasolinera6.setGasolina95E5(1.48);
gasolinera6.setHorario("");
aux.add(gasolinera6);

return aux;
}

@Before
public void inicializa() {
//inicializo los mocks
MockitoAnnotations.openMocks(this);

when(mockObtenerDiaHora.obtenerDiaHora()).thenReturn(LocalDateTime.of(2024, 10, 30, 18, 0, 0));
public void setUp() {
hiltRule.inject();
}

@Test
Expand All @@ -70,25 +132,25 @@ public void test24H() {
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(0);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("Abierto")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(24H)")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(R.color.verde)));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(ContextCompat.getColor(context, R.color.verde))));
}

@Test
public void testAbiertaSimple() {
//comprueba la direccion de la primera gasolinera
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(1);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("Abierto")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(08:00-21:00)")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(R.color.verde)));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(" + procesaHorario(UtilsHorario.obtenerHorarioAbiertoSimple(), obtenerDiaActual(LocalDateTime.now().getDayOfWeek())) + ")")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(ContextCompat.getColor(context, R.color.verde))));
}

@Test
public void testAbiertaIntervalo() {
//comprueba la direccion de la primera gasolinera
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(2);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("Abierto")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(09:00-14:00 y 16:00-21:00)")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(R.color.verde)));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(" + procesaHorario(UtilsHorario.obtenerHorarioAbiertoIntervalo(), obtenerDiaActual(LocalDateTime.now().getDayOfWeek())) + ")")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(ContextCompat.getColor(context, R.color.verde))));
}

@Test
Expand All @@ -97,21 +159,23 @@ public void testCerradaTodoElDia() {
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(3);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("Cerrado")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(Todo el día)")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(R.color.rojo)));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(ContextCompat.getColor(context, R.color.rojo))));
}

@Test
public void testCerradaParcial() {
//comprueba la direccion de la primera gasolinera
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(4);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("Cerrado")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(08:00-14:00 y 19:00-21:00)")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(R.color.rojo)));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(" + procesaHorario(UtilsHorario.obtenerHorarioCerradoIntervalo(), obtenerDiaActual(LocalDateTime.now().getDayOfWeek())) + ")")));
// g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withTextColor(ContextCompat.getColor(context, R.color.rojo))));
}

@Test
public void testSinDetallesDeHorario() throws InterruptedException {
//comprueba la direccion de la primera gasolinera
// Scroll puesto que se sale de la pantalla y no se alcanza a ver
onView(withId(R.id.lvStations)).perform(swipeUp());
DataInteraction g1 = onData(anything()).inAdapterView(withId(R.id.lvStations)).atPosition(5);
g1.onChildView(withId(R.id.tvAbiertoCerrado)).check(matches(withText("")));
g1.onChildView(withId(R.id.tvHorarioGasolinera)).check(matches(withText("(Sin detalles de horario)")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;
import androidx.test.espresso.matcher.BoundedMatcher;

import org.hamcrest.Description;
Expand All @@ -14,12 +16,18 @@
* Custom matchers for Espresso tests.
*/
public class Matchers {
/**
* Matcher para el color del texto
* @param expectedId color esperado
* @return Matcher
*/
public static Matcher<View> withTextColor(final int expectedId) {
return new BoundedMatcher<View, TextView>(TextView.class) {

@Override
protected boolean matchesSafely(TextView textView) {
return expectedId == textView.getCurrentTextColor();
int colorId = ContextCompat.getColor(textView.getContext(), expectedId);
return textView.getCurrentTextColor() == colorId;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

import android.content.Context;

import java.util.List;

import es.unican.gasolineras.common.Utils;
import es.unican.gasolineras.model.Gasolinera;
import es.unican.gasolineras.repository.ICallBack;
import es.unican.gasolineras.repository.IGasolinerasRepository;

Expand All @@ -32,4 +35,19 @@ public static IGasolinerasRepository getTestRepository(Context context, int json
return mock;
}

/**
* Create a mock repository that uses the data in the given List<Gasolineras> resource
*
* @param stations lista con las gasolineras
* @return mock repository
*/
public static IGasolinerasRepository getTestRepositoryList(List<Gasolinera> stations) {
IGasolinerasRepository mock = mock(IGasolinerasRepository.class);
doAnswer(invocation -> {
ICallBack callBack = invocation.getArgument(0);
callBack.onSuccess(stations);
return null;
}).when(mock).requestGasolineras(any(ICallBack.class), any(String.class));
return mock;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package es.unican.gasolineras.activities.main;

import static java.util.Collections.emptyList;

import static es.unican.gasolineras.common.UtilsHorario.gasolineraAbierta;
import static es.unican.gasolineras.common.UtilsHorario.obtenerDiaActual;
import static es.unican.gasolineras.common.UtilsHorario.procesaHorario;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
Expand All @@ -22,10 +18,10 @@
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import java.time.LocalDateTime;
import java.util.List;

import es.unican.gasolineras.R;
import es.unican.gasolineras.common.TimeProvider;
import es.unican.gasolineras.model.Gasolinera;

/**
Expand All @@ -50,6 +46,7 @@ public GasolinerasArrayAdapter(@NonNull Context context, @NonNull List<Gasoliner
this.context = context;
}


@Override
public int getCount() {
return gasolineras.size();
Expand Down Expand Up @@ -137,8 +134,8 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
{
TextView tv = convertView.findViewById(R.id.tvAbiertoCerrado);
String estado;
String textoHorario = procesaHorario(gasolinera.getHorario(), obtenerDiaActual());
boolean compruebaEstado = gasolineraAbierta(textoHorario, new TimeProvider().obtenerDiaHora().toLocalTime());
String textoHorario = procesaHorario(gasolinera.getHorario(), obtenerDiaActual(LocalDateTime.now().getDayOfWeek()));
boolean compruebaEstado = gasolineraAbierta(textoHorario, LocalDateTime.now().toLocalTime());
if (compruebaEstado) {
estado = "Abierto";
tv.setTextColor(ContextCompat.getColor(context, R.color.verde));
Expand All @@ -162,7 +159,7 @@ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup
if (gasolinera.getHorario() == null || gasolinera.getHorario().isEmpty()) {
textoHorario = "(Sin detalles de horario)";
} else {
textoHorario = "(" + procesaHorario(gasolinera.getHorario(), obtenerDiaActual()) + ")";
textoHorario = "(" + procesaHorario(gasolinera.getHorario(), obtenerDiaActual(LocalDateTime.now().getDayOfWeek())) + ")";
}
tv.setText(textoHorario);
}
Expand Down

This file was deleted.

Loading

0 comments on commit 75db675

Please sign in to comment.