Commit 37c71b4f authored by Aneta Steimarova's avatar Aneta Steimarova

#929 Made waiting for event/dialog synchronized

parent d47fd6f3
NewUserScenario
TestScenarioSpecial
TestScenarioHallOfFameRobberyTrophies
TestScenarioHeapOfTrophies
TestGameShootingRangeEasyNoneStar
TestGameRobberyMediumOneStar
TestGameInTheDarknessMedium
TestGameNightWatchMediumNoneStar
TestGamePursuitHardThreeStar
TestGameCrimeSceneHardOneStar
TestGameKidnappingEasyTwoStar
TestGameRunesMediumThreeStar
TestGameProtocolEasyOneStar
TestGameSafeHardNoneStars
TestStatisticsInTheDarkness
TestEncyclopedia
TestProfile
OfficeTestScenario
......@@ -54,6 +54,7 @@ import java.util.Locale;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import java.util.Timer;
import java.util.concurrent.atomic.AtomicBoolean;
import cz.nic.tablexia.Tablexia;
import cz.nic.tablexia.TablexiaApplication;
......@@ -113,7 +114,9 @@ public abstract class AbstractTestScenario implements Runnable {
private static final int STEPS_SWIPE = 30;
private static final Object EVENT_LOCK = new Object();
private static final Object LAST_AWAITING_EVENT_LOCK = new Object();
private static final Object DIALOG_LOCK = new Object();
private static final Object LAST_AWAITING_DIALOG_LOCK = new Object();
private static final Object SCREEN_LOCK = new Object();
private static final Object SCREENSHOT_LOCK = new Object();
private static final Object END_APP_LOCK = new Object();
......@@ -147,6 +150,8 @@ public abstract class AbstractTestScenario implements Runnable {
private static String lastEvent = null;
private String awaitingDialog = null;
private String lastDialog = null;
private AtomicBoolean waitingEvent = new AtomicBoolean(false);
private AtomicBoolean waitingDialog = new AtomicBoolean(false);
private final Tablexia tablexia;
......@@ -960,17 +965,20 @@ public abstract class AbstractTestScenario implements Runnable {
@Handler
public void handleScenarioStepEvent(final AbstractTablexiaScreen.ScenarioStepEvent<String> step){
if(step.getStep()!=null) {
if (awaitingEvent != null) {
if (awaitingEvent.equals(step.getStep())){
clearTimeout();
wakeUp(EVENT_LOCK);
return;
synchronized (LAST_AWAITING_EVENT_LOCK) {
String handledEvent = step.getStep().toString();
if (step.getStep() != null) {
if (awaitingEvent != null) {
if (awaitingEvent.equals(handledEvent)) {
clearTimeout();
waitingEvent.set(false);
wakeUp(EVENT_LOCK);
return;
} else lastEvent = handledEvent;
}
else lastEvent = step.getStep();
//lastEvent will be always set to last event (empty if this is first caught event)
else lastEvent = handledEvent;
}
//lastEvent will be always set to last event (empty if this is first caught event)
else lastEvent = step.getStep();
}
}
......@@ -980,11 +988,14 @@ public abstract class AbstractTestScenario implements Runnable {
protected void waitForEvent(String infoMessage, String eventName, int timeout) {
logInfo(infoMessage);
if(lastEvent==null || !lastEvent.equals(eventName)) { //event has been already caught
awaitingEvent = eventName;
setTimeout(timeout, eventName);
putToSleep(EVENT_LOCK);
synchronized (LAST_AWAITING_EVENT_LOCK) {
if (lastEvent == null || !lastEvent.equals(eventName)) { //event has been already caught
awaitingEvent = eventName;
setTimeout(timeout, eventName);
waitingEvent.set(true);
}
}
if(waitingEvent.get()) putToSleep(EVENT_LOCK);
logOK();
awaitingEvent=null;
lastEvent=null;
......@@ -992,33 +1003,39 @@ public abstract class AbstractTestScenario implements Runnable {
@Handler
public void handleShownDialog(final TablexiaComponentDialog.DialogVisibleEvent dialog) {
String dialogName = dialog.getDialogName();
if(Tablexia.LANGUAGE_REQUEST.equals(dialogName)){
if (clickDialogButton("\n\tClick \"Česky\" in language dialog", Tablexia.LANGUAGE_REQUEST, 0)) {
writeToLogFile("\tSelected language: czech\t");
}
}
if (ZipAssetLoader.DOWNLOAD_REQUEST_DIALOG.equals(dialogName)) {
if (clickDialogButton("\n\tClick yes in download dialog", ZipAssetLoader.DOWNLOAD_REQUEST_DIALOG, 1)) {
writeToLogFile("\tDownloading done, data loaded\t");
synchronized (LAST_AWAITING_DIALOG_LOCK) {
String dialogName = dialog.getDialogName();
if (Tablexia.LANGUAGE_REQUEST.equals(dialogName)) {
if (clickDialogButton("\n\tClick \"Česky\" in language dialog", Tablexia.LANGUAGE_REQUEST, 0)) {
writeToLogFile("\tSelected language: czech\t");
}
}
} else if (dialogName != null){
if(dialogName.equals(awaitingDialog)){
clearTimeout();
wakeUp(DIALOG_LOCK);
if (ZipAssetLoader.DOWNLOAD_REQUEST_DIALOG.equals(dialogName)) {
if (clickDialogButton("\n\tClick yes in download dialog", ZipAssetLoader.DOWNLOAD_REQUEST_DIALOG, 1)) {
writeToLogFile("\tDownloading done, data loaded\t");
}
} else if (dialogName != null) {
if (dialogName.equals(awaitingDialog)) {
clearTimeout();
waitingDialog.set(false);
wakeUp(DIALOG_LOCK);
}
//lastDialog will be always set to last shown dialog name (null if this is first caught dialog name)
lastDialog = dialogName;
}
//lastDialog will be always set to last shown dialog name (null if this is first caught dialog name)
lastDialog = dialogName;
}
}
protected void waitForDialog(String infoMessage, String dialogName){
logInfo(infoMessage);
if(lastDialog==null || !lastDialog.equals(dialogName)) {
awaitingDialog = dialogName;
setTimeout(DEFAULT_TASK_TIMEOUT, dialogName);
putToSleep(DIALOG_LOCK);
synchronized (LAST_AWAITING_DIALOG_LOCK) {
if (lastDialog == null || !lastDialog.equals(dialogName)) {
awaitingDialog = dialogName;
setTimeout(DEFAULT_TASK_TIMEOUT, dialogName);
waitingDialog.set(true);
}
}
if(waitingDialog.get()) putToSleep(DIALOG_LOCK);
logOK();
awaitingDialog=null;
lastDialog=null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment