use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.
the class ElectionHandlerTest method setup.
@Before
public void setup() throws GeneralSecurityException, IOException {
lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
laoRepository = new LAORepository();
when(messageSender.subscribe(any())).then(args -> Completable.complete());
laoRepository = new LAORepository();
messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
// Create one LAO
lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
lao.setLastModified(lao.getCreation());
// Create one Roll Call and add it to the LAO
rollCall = new RollCall(lao.getId(), Instant.now().getEpochSecond(), "roll call 1");
lao.setRollCalls(new HashMap<String, RollCall>() {
{
put(rollCall.getId(), rollCall);
}
});
// Create one Election and add it to the LAO
election = new Election(lao.getId(), Instant.now().getEpochSecond(), "election 1");
election.setStart(Instant.now().getEpochSecond());
election.setEnd(Instant.now().getEpochSecond() + 20L);
election.setChannel(lao.getChannel().subChannel(election.getId()));
electionQuestion = new ElectionQuestion("question", "Plurality", false, Arrays.asList("a", "b"), election.getId());
election.setElectionQuestions(Collections.singletonList(electionQuestion));
lao.setElections(new HashMap<String, Election>() {
{
put(election.getId(), election);
}
});
// Add the LAO to the LAORepository
laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
laoRepository.setAllLaoSubject();
// Add the CreateLao message to the LAORepository
MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.
the class ElectionResultPagerAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
Election election = mLaoDetailViewModel.getCurrentElection();
// setting the question
ElectionQuestion electionQuestion = election.getElectionQuestions().get(position);
String question = electionQuestion.getQuestion();
holder.questionView.setText(question);
List<QuestionResult> questionResults = election.getResultsForQuestionId(electionQuestion.getId());
List<ElectionResultListAdapter.ElectionResult> electionResults = new ArrayList<>();
for (int i = 0; i < questionResults.size(); i++) {
electionResults.add(new ElectionResultListAdapter.ElectionResult(questionResults.get(i).getBallot(), questionResults.get(i).getCount()));
}
adapter.clear();
adapter.addAll(electionResults);
holder.resultListView.setAdapter(adapter);
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.
the class ManageElectionFragment method onCreateView.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ElectionManageFragmentBinding mManageElectionFragBinding = ElectionManageFragmentBinding.inflate(inflater, container, false);
laoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
terminate = mManageElectionFragBinding.terminateElection;
editStartTimeButton = mManageElectionFragBinding.editStartTime;
editEndTimeButton = mManageElectionFragBinding.editEndTime;
editName = mManageElectionFragBinding.editName;
editQuestion = mManageElectionFragBinding.editQuestion;
TextView currentTime = mManageElectionFragBinding.displayedCurrentTime;
TextView startTime = mManageElectionFragBinding.displayedStartTime;
TextView endTime = mManageElectionFragBinding.displayedEndTime;
editStartDateButton = mManageElectionFragBinding.editStartDate;
editEndDateButton = mManageElectionFragBinding.editEndDate;
TextView question = mManageElectionFragBinding.electionQuestion;
TextView laoName = mManageElectionFragBinding.manageElectionLaoName;
TextView electionName = mManageElectionFragBinding.manageElectionTitle;
Date dCurrent = new java.util.Date(// Get's the date based on the unix time stamp
System.currentTimeMillis());
Date dStart = new java.util.Date(laoDetailViewModel.getCurrentElection().getStartTimestampInMillis());
Date dEnd = new java.util.Date(laoDetailViewModel.getCurrentElection().getEndTimestampInMillis());
currentTime.setText(// Set's the start time in the form dd/MM/yyyy HH:mm z
DATE_FORMAT.format(dCurrent));
startTime.setText(DATE_FORMAT.format(dStart));
endTime.setText(DATE_FORMAT.format(dEnd));
laoName.setText(laoDetailViewModel.getCurrentLaoName().getValue());
electionName.setText(laoDetailViewModel.getCurrentElection().getName());
List<ElectionQuestion> electionQuestions = laoDetailViewModel.getCurrentElection().getElectionQuestions();
if (electionQuestions.isEmpty()) {
question.setText("No election question !");
} else {
question.setText("Election Question : " + electionQuestions.get(0).getQuestion());
}
mManageElectionFragBinding.setLifecycleOwner(getActivity());
return mManageElectionFragBinding.getRoot();
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.
the class ElectionSetupFragmentTest method multiplePluralityQuestionsTest.
@Test
public void multiplePluralityQuestionsTest() {
setupViewModel();
electionName().perform(click(), typeText(ELECTION_NAME), closeSoftKeyboard());
pickValidDateAndTime();
// Add Question 1, with 3 ballots options, no write in
questionText().perform(click(), typeText("Question 1"), closeSoftKeyboard());
addBallot().perform(click());
for (int i = 0; i < 3; ++i) {
ballotOptionAtPosition(i).perform(click(), typeText("answer 1." + i), closeSoftKeyboard());
}
// Add Question 2, with 2 ballots options, with write in
addQuestion().perform(click());
questionText().perform(click(), typeText("Question 2"), closeSoftKeyboard());
writeIn().perform(click());
for (int i = 0; i < 2; ++i) {
ballotOptionAtPosition(i).perform(click(), typeText("answer 2." + i), closeSoftKeyboard());
}
// Submit and intercept the ElectionSetup message
long minCreation = Instant.now().getEpochSecond();
submit().perform(click());
ElectionSetup electionSetup = getInterceptedElectionSetupMsg();
long maxCreation = Instant.now().getEpochSecond();
// Check that the creation time was when we submit the ElectionSetup
assertTrue(minCreation <= electionSetup.getCreation());
assertTrue(maxCreation >= electionSetup.getCreation());
// Check the start/end time
Calendar calendar = Calendar.getInstance();
calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS, MINUTES, 0);
long expectedStartTime = calendar.toInstant().getEpochSecond();
calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS + 1, MINUTES, 0);
long expectedEndTime = calendar.toInstant().getEpochSecond();
assertEquals(expectedStartTime, electionSetup.getStartTime());
assertEquals(expectedEndTime, electionSetup.getEndTime());
assertEquals(ELECTION_NAME, electionSetup.getName());
assertEquals(LAO.getId(), electionSetup.getLao());
List<ElectionQuestion> questionList = electionSetup.getQuestions();
assertEquals(2, questionList.size());
ElectionQuestion question1 = questionList.get(0);
ElectionQuestion question2 = questionList.get(1);
// Check the Questions 1
assertEquals("Question 1", question1.getQuestion());
assertFalse(question1.getWriteIn());
List<String> ballotOptions1 = question1.getBallotOptions();
assertEquals(3, ballotOptions1.size());
for (int i = 0; i < 3; ++i) {
assertEquals("answer 1." + i, ballotOptions1.get(i));
}
// Check the Questions 2
assertEquals("Question 2", question2.getQuestion());
// assertTrue(question2.getWriteIn());
List<String> ballotOptions2 = question2.getBallotOptions();
assertEquals(2, ballotOptions2.size());
for (int i = 0; i < 2; ++i) {
assertEquals("answer 2." + i, ballotOptions2.get(i));
}
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion in project popstellar by dedis.
the class CastVoteViewPagerAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
Election election = mLaoDetailViewModel.getCurrentElection();
// setting the question
ElectionQuestion question = election.getElectionQuestions().get(position);
holder.questionView.setText(question.getQuestion());
// this will determine the number of option the user can select and vote for
// If another voting method is implemented in setUp, this can be adapted
// by default
int numberOfChoices = 1;
List<Integer> votes = new ArrayList<>();
// setting the list view with ballot options
List<String> ballotOptions = question.getBallotOptions();
ballotAdapter.clear();
ballotAdapter.addAll(ballotOptions);
ListView ballotsListView = holder.ballotsListView;
ballotsListView.setAdapter(ballotAdapter);
ballotsListView.setChoiceMode(numberOfChoices > 1 ? AbsListView.CHOICE_MODE_MULTIPLE : AbsListView.CHOICE_MODE_SINGLE);
AdapterView.OnItemClickListener itemListener = (parent, view, listPosition, id) -> {
// in this listener the position refers to the index of the question and
// the list position is the index of the ballot that was clicked on
ballotsListView.setClickable(false);
if (numberOfChoices > 1) {
if (votes.contains(listPosition)) {
// without the cast listPosition is treated as the index rather than the list element
votes.remove((Integer) listPosition);
ballotsListView.setItemChecked(listPosition, false);
} else if (votes.size() < numberOfChoices) {
votes.add(listPosition);
ballotsListView.setItemChecked(listPosition, true);
} else {
ballotsListView.setItemChecked(listPosition, false);
}
} else {
if (votes.contains(listPosition)) {
votes.clear();
ballotsListView.setItemChecked(listPosition, false);
} else {
votes.clear();
votes.add(listPosition);
}
}
mLaoDetailViewModel.setCurrentElectionQuestionVotes(votes, position);
ballotsListView.setClickable(true);
voteButton.setEnabled(checkEachQuestion());
};
ballotsListView.setOnItemClickListener(itemListener);
}
Aggregations