mirror of
https://github.com/correl/mage.git
synced 2024-11-15 03:00:16 +00:00
StartMultiGamesTest and class for getting access for Mage.Client GUI components.
This commit is contained in:
parent
8b77a67574
commit
11bec637ee
3 changed files with 162 additions and 0 deletions
|
@ -0,0 +1,17 @@
|
|||
package mage.client.components;
|
||||
|
||||
public enum MageComponents {
|
||||
TABLES_MENU_BUTTON("btnGames"),
|
||||
NEW_GAME_BUTTON("btnNewTable"),
|
||||
NEW_TABLE_OK_BUTTON("btnOK"),
|
||||
TABLE_WAITING_START_BUTTON("btnStart");
|
||||
|
||||
private String name;
|
||||
MageComponents(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
71
Mage.Client/src/main/java/mage/client/components/MageUI.java
Normal file
71
Mage.Client/src/main/java/mage/client/components/MageUI.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
package mage.client.components;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javax.swing.JButton;
|
||||
|
||||
public class MageUI {
|
||||
|
||||
private Map<MageComponents, Component> ui = new HashMap<MageComponents, Component>();
|
||||
private Map<MageComponents, Object> sync = new HashMap<MageComponents, Object>();
|
||||
|
||||
public JButton getButton(MageComponents name) throws InterruptedException {
|
||||
//System.out.println("request for " + name);
|
||||
Object buttonSync;
|
||||
synchronized (ui) {
|
||||
if (ui.containsKey(name)) {
|
||||
//System.out.println("clicking " + name);
|
||||
return (JButton) ui.get(name);
|
||||
} else {
|
||||
buttonSync = new Object();
|
||||
sync.put(name, buttonSync);
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (buttonSync) {
|
||||
//System.out.println("waiting " + name + " to be created");
|
||||
buttonSync.wait();
|
||||
//System.out.println(name + "was created");
|
||||
if (!ui.containsKey(name)) {
|
||||
throw new IllegalStateException("Component wasn't initialized. This should not happen.");
|
||||
}
|
||||
return (JButton) ui.get(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void addButton(MageComponents name, JButton button) {
|
||||
synchronized (ui) {
|
||||
//System.out.println("added " + name);
|
||||
ui.put(name, button);
|
||||
if (sync.containsKey(name)) {
|
||||
synchronized (sync.get(name)) {
|
||||
//System.out.println("notifyAll - " + name);
|
||||
sync.get(name).notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void doClick(MageComponents name) throws InterruptedException {
|
||||
doClick(name, 0);
|
||||
}
|
||||
|
||||
public void doClick(MageComponents name, int waitBeforeClick) throws InterruptedException {
|
||||
final JButton j = getButton(name);
|
||||
Thread.sleep(waitBeforeClick);
|
||||
while (!j.isEnabled()) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
Thread t = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
j.doClick();
|
||||
}
|
||||
});
|
||||
t.start();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package mage.client.game;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import mage.client.MageFrame;
|
||||
import mage.client.components.MageComponents;
|
||||
import mage.client.components.MageUI;
|
||||
import mage.util.Logging;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StartMultiGamesTest {
|
||||
|
||||
private final static Logger logger = Logging.getLogger(StartMultiGamesTest.class.getName());
|
||||
|
||||
/**
|
||||
* Amount of games to be started from this test.
|
||||
*/
|
||||
private final static Integer GAME_START_COUNT = 10;
|
||||
|
||||
private MageFrame frame = null;
|
||||
private Object sync = new Object();
|
||||
private MageUI ui;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
for (int i = 0; i < GAME_START_COUNT; i++) {
|
||||
logger.log(Level.INFO, "Starting game");
|
||||
startGame();
|
||||
}
|
||||
}
|
||||
|
||||
private void startGame() throws Exception {
|
||||
frame = null;
|
||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||
public void uncaughtException(Thread t, Throwable e) {
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
});
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
synchronized (sync) {
|
||||
frame = new MageFrame();
|
||||
frame.setVisible(true);
|
||||
sync.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (sync) {
|
||||
if (frame == null) {
|
||||
sync.wait();
|
||||
}
|
||||
ui = MageFrame.getSession().getUI();
|
||||
ui.doClick(MageComponents.TABLES_MENU_BUTTON);
|
||||
ui.doClick(MageComponents.NEW_GAME_BUTTON);
|
||||
ui.doClick(MageComponents.NEW_TABLE_OK_BUTTON, 500);
|
||||
ui.doClick(MageComponents.TABLE_WAITING_START_BUTTON);
|
||||
}
|
||||
|
||||
sleep(3000);
|
||||
frame.setVisible(false);
|
||||
}
|
||||
|
||||
private void sleep(int ms) {
|
||||
try {
|
||||
Thread.sleep(ms);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue