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

#929 Made waiting for event/dialog synchronized

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