initial commit for regression testing

This commit is contained in:
bancika
2020-04-19 16:50:25 +02:00
parent ac606bb472
commit 7754922711
26 changed files with 858 additions and 20 deletions
@@ -399,6 +399,11 @@ public class ActionFactory {
public void showMessage(String message, String title, int messageType) {
JOptionPane.showMessageDialog(null, message, title, messageType);
}
@Override
public String showInputDialog(String message, String title) {
return JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE);
}
@Override
public File promptFileSave() {
@@ -46,5 +46,10 @@ public class DummyView implements IView {
@Override
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) {
return false;
}
@Override
public String showInputDialog(String message, String title) {
return null;
}
}
@@ -82,6 +82,7 @@ import org.diylc.swing.plugins.file.FileMenuPlugin;
import org.diylc.swing.plugins.help.HelpMenuPlugin;
import org.diylc.swing.plugins.layers.LayersMenuPlugin;
import org.diylc.swing.plugins.statusbar.StatusBar;
import org.diylc.swing.plugins.test.TestMenuPlugin;
import org.diylc.swing.plugins.toolbox.ToolBox;
import org.diylc.swing.plugins.tree.ComponentTree;
import org.diylc.swingframework.ButtonDialog;
@@ -126,7 +127,9 @@ public class MainFrame extends JFrame implements ISwingUI {
presenter.installPlugin(new EditMenuPlugin(this));
presenter.installPlugin(new ConfigPlugin(this));
presenter.installPlugin(new LayersMenuPlugin(this));
presenter.installPlugin(new CloudPlugIn(this));
presenter.installPlugin(new CloudPlugIn(this));
if ("true".equalsIgnoreCase(System.getProperty("org.diylc.enableTests")))
presenter.installPlugin(new TestMenuPlugin(this));
presenter.installPlugin(new HelpMenuPlugin(this));
presenter.installPlugin(new ActionBarPlugin(this));
@@ -282,6 +285,11 @@ public class MainFrame extends JFrame implements ISwingUI {
@Override
public int showConfirmDialog(String message, String title, int optionType, int messageType) {
return JOptionPane.showConfirmDialog(this, message, title, optionType, messageType);
}
@Override
public String showInputDialog(String message, String title) {
return JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE);
}
@Override
@@ -96,6 +96,11 @@ public class TemplateDialog extends JDialog {
@Override
public void showMessage(String message, String title, int messageType) {
JOptionPane.showMessageDialog(TemplateDialog.this, message, title, messageType);
}
@Override
public String showInputDialog(String message, String title) {
return JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE);
}
@Override
@@ -29,7 +29,7 @@ public enum FileFilterEnum {
PNG("PNG Images (*.png)", "png"), PDF("PDF Files (*.pdf)", "pdf"), DIY("DIY Project Files (*.diy)", "diy"), EXCEL(
"Excel Workbooks (*.xls)", "xls"), CSV("Comma Separated Files (*.csv)", "csv"), HTML("HTML Files (*.html)",
"html"), IMAGES("Image Files (*.png, *.jpg, *.gif)", "png", "jpg", "gif"), XML("XML Files (*.xml)", "xml");
"html"), IMAGES("Image Files (*.png, *.jpg, *.gif)", "png", "jpg", "gif"), XML("XML Files (*.xml)", "xml"), TEST("Test Files (*.tst)", "tst");
FileFilter filter;
String[] extensions;
@@ -0,0 +1,239 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.swing.plugins.test;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;
import org.apache.log4j.Logger;
import org.diylc.MockView;
import org.diylc.common.EventType;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.common.ITask;
import org.diylc.presenter.Presenter;
import org.diylc.presenter.ProjectFileManager;
import org.diylc.swing.ISwingUI;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.plugins.file.FileFilterEnum;
import org.diylc.test.DIYTest;
import org.diylc.test.TestPlayer;
import org.diylc.test.TestPlayer.StepResult;
import junit.framework.TestResult;
/**
* Entry point class for help-related utilities.
*
* @author Branislav Stojkovic
*/
public class TestMenuPlugin implements IPlugIn {
private static final String TEST_TITLE = "Test";
private static final Logger LOG = Logger.getLogger(TestMenuPlugin.class);
private IPlugInPort plugInPort;
private ISwingUI swingUI;
private ProjectFileManager projectFileManager;
private TestPlayer testPlayer;
public TestMenuPlugin(ISwingUI swingUI) {
this.swingUI = swingUI;
swingUI.injectMenuAction(new StartRecordAction(), TEST_TITLE);
swingUI.injectMenuAction(new AddValidationAction(), TEST_TITLE);
swingUI.injectMenuAction(new StopRecordAction(), TEST_TITLE);
swingUI.injectMenuAction(new PlayTestAction(), TEST_TITLE);
}
@Override
public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
projectFileManager = new ProjectFileManager(null);
testPlayer = new TestPlayer();
}
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return null;
}
@Override
public void processMessage(EventType eventType, Object... params) {}
class StartRecordAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public StartRecordAction() {
super();
putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R,
ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
putValue(AbstractAction.NAME, "Start Recording");
}
@Override
public void actionPerformed(ActionEvent e) {
String name = swingUI.showInputDialog("Test Name:", "Start Recording");
if (name == null)
return;
LOG.info("Starting test recording...");
plugInPort.startRecording(name);
}
}
class StopRecordAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public StopRecordAction() {
super();
putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_T,
ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
putValue(AbstractAction.NAME, "Stop Recording");
}
@Override
public void actionPerformed(ActionEvent e) {
DIYTest test = plugInPort.stopRecording();
if (test == null)
return;
LOG.info("Stopping test recording...");
final File file = DialogFactory.getInstance().showSaveDialog(swingUI.getOwnerFrame(),
FileFilterEnum.TEST.getFilter(), new File(test.getName() + ".tst"), FileFilterEnum.TEST.getExtensions()[0], null);
if (file != null) {
swingUI.executeBackgroundTask(new ITask<Void>() {
@Override
public Void doInBackground() throws Exception {
LOG.debug("Saving test to " + file.getAbsolutePath());
projectFileManager.serializeTestToFile(test, file.getAbsolutePath());
return null;
}
@Override
public void complete(Void result) {}
@Override
public void failed(Exception e) {
swingUI.showMessage("Could not save test: " + e.getMessage(), "Error",
ISwingUI.ERROR_MESSAGE);
}
}, true);
}
}
}
class PlayTestAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public PlayTestAction() {
super();
putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_P,
ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
putValue(AbstractAction.NAME, "Play Test");
}
@Override
public void actionPerformed(ActionEvent e) {
LOG.info("Starting test recording...");
final File file = DialogFactory.getInstance().showOpenDialog(FileFilterEnum.TEST.getFilter(),
null, FileFilterEnum.TEST.getExtensions()[0], null);
final Presenter presenter = new Presenter(new MockView());
if (file != null) {
swingUI.executeBackgroundTask(new ITask<List<StepResult>>() {
@Override
public List<StepResult> doInBackground() throws Exception {
DIYTest test = projectFileManager.deserializeTestFromFile(file.getAbsolutePath());
List<StepResult> testResults = testPlayer.play(presenter, test);
return testResults;
}
@Override
public void complete(List<StepResult> result) {
StringBuilder sb = new StringBuilder();
boolean hasError = false;
for (StepResult r : result) {
if (r.hasError()) {
sb.append("Step ").append(r.getStep()).append(" status: ").append(r.getStatus()).append("\n");
hasError = true;
}
}
if (!hasError)
sb.append("Test executed SUCCESFULLY.");
swingUI.showMessage(sb.toString(), "Test Results", hasError ? ISwingUI.ERROR_MESSAGE : ISwingUI.INFORMATION_MESSAGE);
}
@Override
public void failed(Exception e) {
swingUI.showMessage("Could not play test: " + e.getMessage(), "Error",
ISwingUI.ERROR_MESSAGE);
}
}, true);
}
}
}
class AddValidationAction extends AbstractAction {
private static final long serialVersionUID = 1L;
public AddValidationAction() {
super();
putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_V,
ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
putValue(AbstractAction.NAME, "Add Validation Step");
}
@Override
public void actionPerformed(ActionEvent e) {
LOG.info("Adding test validation step");
Map<String, Object> params = new HashMap<String, Object>();
params.put(DIYTest.SNAPSHOT, plugInPort.createTestSnapshot());
plugInPort.addValidation(params);
swingUI.showMessage("Validation step added.", "Test", ISwingUI.INFORMATION_MESSAGE);
}
}
}