Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify testing selector #183

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import static org.eclipse.swtbot.swt.finder.waits.Conditions.shellIsActive;
import static org.junit.Assert.fail;

import java.io.Closeable;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotRadio;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
Expand All @@ -41,7 +43,7 @@

import com.google.common.collect.ImmutableList;

public class RunConfigurationSelector {
public class RunConfigurationSelector implements Closeable {

private static final String RUN = "Run";
private static final String RUN_CONFIGURATIONS = "Run Configurations";
Expand Down Expand Up @@ -93,14 +95,11 @@ private PitRunConfiguration getPitConfiguration(SWTBotTreeItem treeItem) {
.withTestClassOrDir(isTestClass).build();
}

private SWTBotShell activateShell() {
// look if shell is already open
for (SWTBotShell shell : bot.shells()) {
if (shell.getText().equals(RUN_CONFIGURATIONS)) {
shell.activate();
return shell;
}
}
/**
* Opens the run configuration shell and activates it
* @return the run configuration shell
*/
public SWTBotShell openRunConfigurationShell() {
// open the "Run Configurations" dialog
SWTBotMenuHelper menuHelper = new SWTBotMenuHelper();
menuHelper.findMenu(menuHelper.findWorkbenchMenu(bot, RUN), RUN_CONFIGURATIONS + "...").click();
Expand All @@ -112,7 +111,6 @@ private SWTBotShell activateShell() {
}

private SWTBotTreeItem getPitConfigurationItem() {
activateShell();
final String itemName = "PIT Mutation Test";
for (SWTBotTreeItem treeItem : bot.tree().getAllItems()) {
if (itemName.equals(treeItem.getText())) {
Expand Down Expand Up @@ -142,34 +140,38 @@ public void activatePitTab(String configurationName) {
}

private void activateConfigurationTab(String configurationName, String name) {
activateShell();
activateConfiguration(configurationName);
bot.cTabItem(PitMutatorsTab.NAME).activate();
}

public void createRunConfiguration(String configurationName, String projectName, String className) {
getPitConfigurationItem().contextMenu("New Configuration").click();
bot.textWithLabel("Name:").setText(configurationName);
activateShell().bot().button("Apply").click();
PitRunConfiguration config = new Builder().withName(configurationName).withProjects(projectName)
.withTestClass(className).build();
setConfiguration(config);
}

public void setProjectForConfiguration(String configurationName, String project) {
setConfiguration(new Builder(getConfiguration(configurationName)).withProjects(project).build());
final PitRunConfiguration config = getConfiguration(configurationName);
setConfiguration(new Builder(config).withProjects(project).build());
}

public void setTestClassForConfiguration(String configurationName, String testClass) {
setConfiguration(new Builder(getConfiguration(configurationName)).withTestClass(testClass).build());
final PitRunConfiguration config = getConfiguration(configurationName);
setConfiguration(new Builder(config).withTestClass(testClass).build());
}

public void setTestDirForConfiguration(String configurationName, String testDir) {
setConfiguration(new Builder(getConfiguration(configurationName)).withTestDir(testDir).build());
final PitRunConfiguration config = getConfiguration(configurationName);
setConfiguration(new Builder(config).withTestDir(testDir).build());
}

/**
* Sets the values of the currently open pit run configuration
* @param config where the values are extracted from
*/
private void setConfiguration(PitRunConfiguration config) {
activateConfiguration(config.getName());
bot.textWithLabel(PitArgumentsTab.PROJECT_TEXT).setText(getProjectsAsString(config));
if (config.isTestClass()) {
bot.radio(PitArgumentsTab.TEST_CLASS_RADIO_TEXT).click();
Expand All @@ -192,8 +194,7 @@ private void setConfiguration(PitRunConfiguration config) {
bot.textWithLabel(PitPreferences.EXCLUDED_CLASSES_LABEL).setText(config.getExcludedClasses());
bot.textWithLabel(PitPreferences.EXCLUDED_METHODS_LABEL).setText(config.getExcludedMethods());
bot.textWithLabel(PitPreferences.AVOID_CALLS_TO_LABEL).setText(config.getAvoidCallsTo());
// close shell and save
closeConfigurationShell();
pressApplyIfEnabled();
}

/**
Expand All @@ -210,7 +211,7 @@ private String getProjectsAsString(PitRunConfiguration config) {
public void setMutatorGroup(String configurationName, Mutators mutatorGroup) {
activateMutatorsTab(configurationName);
bot.radio(mutatorGroup.getDescriptor()).click();
closeConfigurationShell();
pressApplyIfEnabled();
}

public void toggleCustomMutator(String configurationName, Mutators mutator) {
Expand All @@ -219,7 +220,7 @@ public void toggleCustomMutator(String configurationName, Mutators mutator) {
radioButton.click();
SWTBotTable table = bot.table();
toggleMutator(table, mutator);
closeConfigurationShell();
pressApplyIfEnabled();
}

public void setOneCustomMutator(String configurationName, Mutators mutator) {
Expand All @@ -228,7 +229,7 @@ public void setOneCustomMutator(String configurationName, Mutators mutator) {
uncheckAllMutators();
SWTBotTable table = bot.table();
toggleMutator(table, mutator);
closeConfigurationShell();
pressApplyIfEnabled();
}

public void checkAllMutators(String configurationName) {
Expand All @@ -241,7 +242,7 @@ public void checkAllMutators(String configurationName) {
table.getTableItem(i).check();
}
});
closeConfigurationShell();
pressApplyIfEnabled();
}

/**
Expand All @@ -255,6 +256,7 @@ private void uncheckAllMutators() {
}
});
// don't close, because you cannot apply empty mutators
pressApplyIfEnabled();
}

/**
Expand All @@ -264,18 +266,42 @@ private void uncheckAllMutators() {
*/
private void toggleMutator(SWTBotTable table, Mutators mutator) {
table.getTableItem(mutator.getDescriptor()).toggleCheck();
pressApplyIfEnabled();
}

private void pressApplyIfEnabled() {
SWTBotButton apply = bot.shell(RUN_CONFIGURATIONS).bot().button("Apply");
if (apply.isEnabled()) {
apply.click();
}
}

/**
* @return the run configuration shell or null, if it is not open
*/
private SWTBotShell getRunConfigShellIfOpen() {
// look if shell is already open
for (SWTBotShell shell : bot.shells()) {
if (shell.getText().equals(RUN_CONFIGURATIONS)) {
shell.activate();
return shell;
}
}
return null;
}

/**
* Closes the configuration shell and applies, if possible.
*/
private void closeConfigurationShell() {
SWTBotShell shell = bot.shell(RUN_CONFIGURATIONS);
SWTBotButton apply = shell.bot().button("Apply");
if (apply.isEnabled()) {
apply.click();
@Override
public void close() {
SWTBotShell shell = getRunConfigShellIfOpen();
if (shell == null) {
// shell is not open
return;
}
shell.bot().button("Close").click();
bot.waitUntil(Conditions.shellCloses(shell));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public void runPit() {
}

public void runPitWithConfiguration(String configurationName) {
runConfigurationSelector.runWithConfigurationAndWaitForIt(configurationName);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.runWithConfigurationAndWaitForIt(configurationName);
}
}

/**
Expand All @@ -86,19 +88,31 @@ private void ensureSelectTestConfigurationDialogIsClosed() {
public List<PitRunConfiguration> runConfigurations() {
SWTBotMenuHelper menuHelper = new SWTBotMenuHelper();
menuHelper.findMenu(menuHelper.findWorkbenchMenu(bot, RUN), RUN_CONFIGURATIONS).click();
return runConfigurationSelector.getConfigurations();
return openRunMenu().andThen().getConfigurations();
}

public void createRunConfiguration(String configurationName, String projectName, String className) {
runConfigurationSelector.createRunConfiguration(configurationName, projectName, className);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.createRunConfiguration(configurationName, projectName, className);
}
}

public void setProjectForConfiguration(String configurationName, String project) {
runConfigurationSelector.setProjectForConfiguration(configurationName, project);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.setProjectForConfiguration(configurationName, project);
}
}

public void setTestClassForConfiguration(String configurationName, String testClass) {
runConfigurationSelector.setTestClassForConfiguration(configurationName, testClass);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.setTestClassForConfiguration(configurationName, testClass);
}
}

public void setTestDirForConfiguration(String configurationName, String testDir) {
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.setTestDirForConfiguration(configurationName, testDir);
}
}

public PitOptions getLastUsedPitOptions() {
Expand All @@ -111,15 +125,19 @@ public PitOptions getLastUsedPitOptions() {
* @param mutatorGroup which group to select
*/
public void setMutatorGroup(String configurationName, Mutators mutatorGroup) {
runConfigurationSelector.setMutatorGroup(configurationName, mutatorGroup);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.setMutatorGroup(configurationName, mutatorGroup);
}
}

/**
* Checks all custom mutators for the given configuration
* @param configurationName of the configuration, where to select all mutators
*/
public void checkAllMutators(String configurationName) {
runConfigurationSelector.checkAllMutators(configurationName);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.checkAllMutators(configurationName);
}
}

/**
Expand All @@ -128,7 +146,10 @@ public void checkAllMutators(String configurationName) {
* @param mutator which should be toggled
*/
public void toggleCustomMutator(String configurationName, Mutators mutator) {
runConfigurationSelector.toggleCustomMutator(configurationName, mutator);

try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.toggleCustomMutator(configurationName, mutator);
}
}

/**
Expand All @@ -138,15 +159,32 @@ public void toggleCustomMutator(String configurationName, Mutators mutator) {
* @param mutator which to select in the configuration
*/
public void setOneCustomMutator(String configurationName, Mutators mutator) {
runConfigurationSelector.setOneCustomMutator(configurationName, mutator);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.setOneCustomMutator(configurationName, mutator);
}
}

/**
* Removes the pit run configuration, which matches the given name.
* @param configurationName which should be removed
*/
public void removeConfig(String configurationName) {
runConfigurationSelector.removeConfig(configurationName);
try (RunConfigurationSelector selector = openRunMenu().andThen()) {
selector.removeConfig(configurationName);
}
}

/**
* Opens the run menu and activates it
*/
public RunConficurationDsl openRunMenu() {
runConfigurationSelector.openRunConfigurationShell();
return new RunConficurationDsl();
}

public class RunConficurationDsl {
public RunConfigurationSelector andThen() {
return runConfigurationSelector;
}
}
}