Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #183 from brunnerant/sprint11/issue#158
Browse files Browse the repository at this point in the history
Add feedback when you validate a quiz and it is corrected
  • Loading branch information
alexisc99 committed Jun 5, 2020
2 parents 5a292da + 839c65f commit b27fe05
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 62 deletions.
86 changes: 64 additions & 22 deletions app/src/androidTest/java/ch/epfl/qedit/quiz/QuizActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.RootMatchers.withDecorView;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static ch.epfl.qedit.model.StringPool.TITLE_ID;
import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.home.HomeQuizListFragment.QUIZ_ID;
import static ch.epfl.qedit.view.quiz.QuestionFragment.FRAGMENT_TAG;
import static ch.epfl.qedit.view.quiz.QuizActivity.CORRECTION;
import static ch.epfl.qedit.view.quiz.QuizActivity.GOOD_ANSWERS;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.core.IsNot.not;

import android.content.Intent;
Expand All @@ -26,14 +30,16 @@
import ch.epfl.qedit.model.Question;
import ch.epfl.qedit.model.Quiz;
import ch.epfl.qedit.model.StringPool;
import ch.epfl.qedit.model.User;
import ch.epfl.qedit.model.answer.AnswerModel;
import ch.epfl.qedit.model.answer.MatrixFormat;
import ch.epfl.qedit.model.answer.MatrixModel;
import ch.epfl.qedit.view.answer.MatrixFragment;
import ch.epfl.qedit.view.home.HomeActivity;
import ch.epfl.qedit.view.quiz.QuizActivity;
import ch.epfl.qedit.viewmodel.QuizViewModel;
import java.util.ArrayList;
import java.util.HashMap;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -46,18 +52,23 @@ public class QuizActivityTest {
private QuizViewModel model;
private final Integer zero = 0;
private final String answer1 = "1234";
private ArrayList<Integer> correctedQuestions;

@Rule
public final IntentsTestRule<QuizActivity> testRule =
new IntentsTestRule<>(QuizActivity.class, false, false);

public void launchActivity() {
User user = new User("Albert", "Einstein");
Intent intent = new Intent();
Bundle bundle = new Bundle();

initializeStringPoolAndQuiz();

bundle.putSerializable(QUIZ_ID, quiz);
bundle.putSerializable(USER, user);

bundle.putBoolean(CORRECTION, false);
intent.putExtras(bundle);

testRule.launchActivity(intent);
Expand All @@ -76,6 +87,29 @@ public void launchActivity() {
});
}

public void launchCorrection() {
User user = new User("François", "Ferdinand");
correctedQuestions = new ArrayList<>();
correctedQuestions.add(0, 1);
correctedQuestions.add(1, 0);

Intent intent = new Intent();
Bundle bundle = new Bundle();

initializeStringPoolAndQuiz();

bundle.putSerializable(QUIZ_ID, quiz);
bundle.putIntegerArrayList(GOOD_ANSWERS, correctedQuestions);
bundle.putSerializable(USER, user);
bundle.putBoolean(CORRECTION, true);
intent.putExtras(bundle);

testRule.launchActivity(intent);

model = new ViewModelProvider(testRule.getActivity()).get(QuizViewModel.class);
model.setQuiz(quiz.instantiateLanguage(stringPool));
}

public void finishActivity() {
testRule.finishActivity();
}
Expand Down Expand Up @@ -108,6 +142,32 @@ public void testOnCreateState() {
finishActivity();
}

@Test
public void correctionFragmentDisplayed() {
launchCorrection();
onView(withId(R.id.correction_details_container)).check(matches(isDisplayed()));
finishActivity();
}

@Test
public void clickValidate() {
launchCorrection();
onView(withId(R.id.validate)).perform(click());
onView(withId(android.R.id.button1)).perform(click());
intended(allOf(hasComponent(HomeActivity.class.getName())));
finishActivity();
}

@Test
public void textViewsContent() {
launchCorrection();
onView(withId(R.id.correct_ratio)).check(matches(withText("Correct answers :1/2 (50.0%)")));
onView(withId(R.id.correction_score)).check(matches(withText("+1")));
onView(withId(R.id.correction_success)).check(matches(withText("+0")));
onView(withId(R.id.correction_attempts)).check(matches(withText("+1")));
finishActivity();
}

@Test
public void clickPreviousNull() {
launchActivity();
Expand Down Expand Up @@ -146,19 +206,6 @@ public void testUpArrowIsClicked() {
finishActivity();
}

@Test
public void testTimeIsClicked() {
launchActivity();
onView(withId(R.id.time)).perform(click());
onView(withText("Unimplemented Feature"))
.inRoot(
withDecorView(
Matchers.not(
is(testRule.getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
finishActivity();
}

@Test
public void testValidateClicked() {
launchActivity();
Expand All @@ -179,12 +226,7 @@ public void testDoneYesClicked() {
launchActivity();
onView(withId(R.id.validate)).perform(click());
onView(withId(android.R.id.button1)).perform(click());
onView(withText("number of good answers = 0"))
.inRoot(
withDecorView(
Matchers.not(
is(testRule.getActivity().getWindow().getDecorView()))))
.check(matches(isDisplayed()));
intended(allOf(hasComponent(QuizActivity.class.getName())));
finishActivity();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

import android.os.Bundle;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import ch.epfl.qedit.R;
import ch.epfl.qedit.quiz.QuizFragmentsTestUsingDB;
Expand All @@ -29,6 +30,9 @@ public class QuestionFragmentTest extends QuizFragmentsTestUsingDB {

@Before
public void setup() {

Bundle bundle = new Bundle();

model = super.setup(testRule, new QuestionFragment());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.epfl.qedit.backend.database;

import static ch.epfl.qedit.backend.database.MockDBService.MockQuiz.updateMatrixModel;
import static ch.epfl.qedit.model.StringPool.TITLE_ID;

import androidx.test.espresso.IdlingResource;
Expand All @@ -10,6 +11,7 @@
import ch.epfl.qedit.model.StringPool;
import ch.epfl.qedit.model.User;
import ch.epfl.qedit.model.answer.MatrixFormat;
import ch.epfl.qedit.model.answer.MatrixModel;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -22,7 +24,7 @@
public class MockDBService implements DatabaseService {

/** This class simulates a quiz that is stored in Firestore */
private static class MockQuiz {
static class MockQuiz {

private ImmutableList<Question> questions;
private Map<String, StringPool> stringPools;
Expand Down Expand Up @@ -57,8 +59,15 @@ boolean isTreasureHunt() {
return treasureHunt;
}

static final MatrixFormat simpleFormat =
MatrixFormat.singleField(MatrixFormat.Field.textField("hint1"));
static MatrixFormat simpleFormat =
MatrixFormat.singleField(MatrixFormat.Field.numericField(false, false, "hint1"));

public static void updateMatrixModel() {
MatrixModel solution42 = new MatrixModel(1, 1);
solution42.updateAnswer(0, 0, "42");
simpleFormat.setCorrectAnswer(solution42);
}

static final MatrixFormat compoundFormat =
new MatrixFormat.Builder(2, 2)
.withField(0, 0, MatrixFormat.Field.preFilledField("hint2"))
Expand All @@ -69,6 +78,14 @@ boolean isTreasureHunt() {

@SuppressWarnings("SpellCheckingInspection")
static MockQuiz createTestMockQuiz1() {

MatrixModel compoundSolution = new MatrixModel(2, 2);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
compoundSolution.updateAnswer(i, j, "42");
}
}
compoundFormat.setCorrectAnswer(compoundSolution);
HashMap<String, String> stringPool_en = new HashMap<>();
stringPool_en.put(TITLE_ID, "I am a Mock Quiz!");
stringPool_en.put("q1_title", "Banana");
Expand Down Expand Up @@ -128,6 +145,7 @@ static MockQuiz createTestMockQuiz1() {

@SuppressWarnings("SpellCheckingInspection")
static MockQuiz createTestMockQuiz2() {

HashMap<String, String> stringPool_en = new HashMap<>();
stringPool_en.put(TITLE_ID, "An other Quiz");
stringPool_en.put("q1_title", "Banana");
Expand Down Expand Up @@ -188,6 +206,7 @@ static MockQuiz createTestMockQuiz3() {
public MockDBService() {
idlingResource = new CountingIdlingResource("MockDBService");
quizzes = new HashMap<>();
updateMatrixModel();
quizzes.put("quiz0", MockQuiz.createTestMockQuiz1());
quizzes.put("quiz1", MockQuiz.createTestMockQuiz2());
quizzes.put("quiz2", MockQuiz.createTestMockQuiz3());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public abstract class AnswerFormat implements MultiLanguage<AnswerFormat>, Seria
/** The correct answer */
private AnswerModel solution;

/** Getter and setter for the solution */
public void setCorrectAnswer(AnswerModel correctAnswer) {
this.solution = correctAnswer;
}

public AnswerModel getCorrectAnswer() {
return this.solution;
}

public boolean correct(AnswerModel participantAnswer) {
if (participantAnswer == null || solution == null) return false;
return solution.equals(participantAnswer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ public AnswerFormat instantiateLanguage(StringPool pool) {
}
}

return b.build();
MatrixFormat newFormat = b.build();
newFormat.setCorrectAnswer(this.getCorrectAnswer());

return newFormat;
}

@Override
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/ch/epfl/qedit/model/answer/MatrixModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,23 @@ private boolean checkFieldEquals(
String matrixField, MatrixModel objectMatrixModel, int row, int col) {

try {
return matrixField.trim().toLowerCase()
== objectMatrixModel.getAnswer(row, col).trim().toLowerCase();
return matrixField
.trim()
.toLowerCase()
.equals(objectMatrixModel.getAnswer(row, col).trim().toLowerCase());
} catch (IndexOutOfBoundsException e) {
return false;
}
}

public int getNumCols() {
return numCols;
}

public int getNumRows() {
return numRows;
}

@Override
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private EditText createEditableField(MatrixFormat.Field field, String answer) {
editText.setInputType(inputType);
editText.setText(answer);
editText.setHint(field.getText());

return editText;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static ch.epfl.qedit.view.edit.EditQuizSettingsDialog.NO_FILTER;
import static ch.epfl.qedit.view.edit.EditQuizSettingsDialog.QUIZ_BUILDER;
import static ch.epfl.qedit.view.home.HomeActivity.USER;
import static ch.epfl.qedit.view.quiz.QuizActivity.CORRECTION;

import android.app.Dialog;
import android.content.Intent;
Expand Down Expand Up @@ -74,6 +75,7 @@ public class HomeQuizListFragment extends Fragment
private int modifyIndex = -1;

// The user and its quizzes (id and title of the quizzes)

private User user;
private List<Map.Entry<String, String>> quizzes;

Expand Down Expand Up @@ -280,6 +282,8 @@ private void launchQuizActivity(Quiz quiz) {
// We put the quiz into the bundle
Bundle bundle = new Bundle();
bundle.putSerializable(QUIZ_ID, quiz);
bundle.putSerializable(USER, user);
bundle.putBoolean(CORRECTION, false);
intent.putExtras(bundle);

// And start the activity
Expand Down
Loading

0 comments on commit b27fe05

Please sign in to comment.