Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Create plugin for Environment Tab in LC #2120

Merged
merged 15 commits into from
May 25, 2021
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.reddeer.eclipse.debug.ui.launchConfigurations;

import java.util.List;

import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.swt.api.TableItem;
import org.eclipse.reddeer.swt.condition.ShellIsAvailable;
import org.eclipse.reddeer.swt.impl.button.OkButton;
import org.eclipse.reddeer.swt.impl.button.PushButton;
import org.eclipse.reddeer.swt.impl.button.RadioButton;
import org.eclipse.reddeer.swt.impl.button.YesButton;
import org.eclipse.reddeer.swt.impl.table.DefaultTable;
import org.eclipse.reddeer.swt.impl.table.DefaultTableItem;
import org.eclipse.reddeer.swt.impl.text.LabeledText;

/**
* Represents Environment tab in Launch Configuration
*
* @author Oleksii Korniienko [email protected]
*
*/
public class EnvironmentTab extends LaunchConfigurationTab {

private static final String ADD_SHELL_TITLE = "New Environment Variable";
private static final String SELECT_SHELL_TITLE = "Select Environment Variables";
private static final String EDIT_SHELL_TITLE = "Edit Environment Variable";
private static final String OVERWRITE_SHELL_TITLE = "Overwrite variable?";

public EnvironmentTab() {
super("Environment");
}

/**
* Return the list of all environment variables
*
* @return list of items
*/
public List<TableItem> getAllVariables() {
return new DefaultTable().getItems();
}

/**
* Return the environment variable by name
*
* @return Environment Tab item
*/
public TableItem getVariable(String variableName) {
return new DefaultTable().getItem(variableName);
}

/**
* Return the environment variable by id
*
* @return Environment Tab item
*/
public TableItem getVariable(int variableId) {
return new DefaultTable().getItem(variableId);
}

/**
* Add new environment variable
*
* @param name variable name
* @param value variable value
*/
public void add(String name, String nalue) {
odockal marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can instead of basically rewriting the method that follows with boolean params, simply call the later implementation with default boolean value. And I would go with default overwrite = true instead of false...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

new PushButton("Add...").click();
new WaitUntil(new ShellIsAvailable(ADD_SHELL_TITLE));
olkornii marked this conversation as resolved.
Show resolved Hide resolved
new LabeledText("Name:").setText(name);
new LabeledText("Value:").setText(nalue);
new OkButton().click();
try {
new WaitUntil(new ShellIsAvailable(OVERWRITE_SHELL_TITLE), TimePeriod.SHORT);
new YesButton().click();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overwriting value should be disabled by default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that overwriting the value should be default choice but the option not to override is also implemented in api. I can imagine that user would like to add the intended value no matter there already exists one as he should know what he wants. Instead of not passing it be default which would require a debugging session to find out why the env is not set, imho.

} catch (WaitTimeoutExpiredException e) {
// variable is new, dont need to overwrite
}
}

/**
* Select environment variable by name
*
* @param variableName variable name for select
*/
public void selectEnvironmentVariable(String variableName) {
new PushButton("Select...").click();
Copy link
Collaborator

@odockal odockal May 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have this similarities in implementation write only once. Like maybe using functional interface to pass specific code into repeated passages? Ie:

	public void versionOne(String param) {
		versionImpl(() -> {
			System.out.println("I am String!");
		});
	}
	
	public void versionTwo(int param) {
		versionImpl(() -> {
			System.out.println("I am int!");
		});
	}
	
	private void versionImpl(Runnable mySpecificImpl) {
		System.out.println("Actions before my functional interface call");
		mySpecificImpl.run();
		System.out.println("Actions after my functional interface call");
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

new WaitUntil(new ShellIsAvailable(SELECT_SHELL_TITLE));
new DefaultTableItem(variableName).setChecked(true);
OkButton oB = new OkButton();
if (oB.isEnabled()) { // no need to select if already selected
oB.click();
}
}

/**
* Select environment variable by id
*
* @param variableId variable index for select
*/
public void selectEnvironmentVariable(int variableId) {
new PushButton("Select...").click();
new WaitUntil(new ShellIsAvailable(SELECT_SHELL_TITLE));
new DefaultTable().getItem(variableId).setChecked(true);
OkButton oB = new OkButton();
if (oB.isEnabled()) { // no need to select if already selected
oB.click();
}
}

/**
* Select/deselect all environment variables in select button list
*
* @param selectAllBool true if need to select all variables, false for deselect
*/
public void selectEnvironmentVariable(boolean selectAllBool) {
new PushButton("Select...").click();
new WaitUntil(new ShellIsAvailable(SELECT_SHELL_TITLE));
if (selectAllBool) {
new PushButton("Select All").click();
} else {
new PushButton("Deselect All").click();
}
OkButton oB = new OkButton();
if (oB.isEnabled()) { // no need to select if already selected
oB.click();
}
}

/**
* edit environment variable
*
* @param variableName variable name
* @param newValue new variable value
*/
public void edit(String variableName, String newValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can call edit(String variableName,String variableName,String newValue) or you can put null instead of second parameter and change edit(String variableName, String newName, String newValue) accordingly(if newName is null, do not change Name text)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

getVariable(variableName).select();
new PushButton("Edit...").click();
new WaitUntil(new ShellIsAvailable(EDIT_SHELL_TITLE));
new LabeledText("Value:").setText(newValue);
new OkButton().click();
}

/**
* edit environment variable
*
* @param variableName variable name
* @param newName new variable name
* @param newValue new variable value
*/
public void edit(String variableName, String newName, String newValue) {
getVariable(variableName).select();
new PushButton("Edit...").click();
new WaitUntil(new ShellIsAvailable(EDIT_SHELL_TITLE));
new LabeledText("Name:").setText(newName);
new LabeledText("Value:").setText(newValue);
new OkButton().click();
}

/**
* Remove environment variable
* @param variableName variable name
*/
public void remove(String variableName) {
getVariable(variableName).select();
new PushButton("Remove").click();
}

/**
* Copy environment variable
* @param variableName variable name
*/
public void copy(String variableName) {
getVariable(variableName).select();
new PushButton("Copy").click();
}

/**
* Paste environment variable
*/
public void paste() {
new PushButton("Paste").click();
}

/**
* Append environment to native environment
*
* @return append radio button
*/
public RadioButton getAppendRadioButton() {
return new RadioButton("Append environment to native environment");
}

/**
* Replace native environment with specified environment
*
* @return replace radio button
*/
public RadioButton getReplaceRadioButton() {
return new RadioButton("Replace native environment with specified environment");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.reddeer.eclipse.test.jdt.environment.launcher;

import static org.junit.Assert.assertTrue;

import org.eclipse.reddeer.common.exception.RedDeerException;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.eclipse.debug.ui.launchConfigurations.EnvironmentTab;
import org.eclipse.reddeer.eclipse.debug.ui.launchConfigurations.JUnitLaunchConfiguration;
import org.eclipse.reddeer.eclipse.debug.ui.launchConfigurations.LaunchConfigurationsDialog;
import org.eclipse.reddeer.eclipse.debug.ui.launchConfigurations.RunConfigurationsDialog;
import org.eclipse.reddeer.swt.condition.ShellIsAvailable;
import org.eclipse.reddeer.swt.impl.button.RadioButton;
import org.eclipse.reddeer.swt.impl.shell.DefaultShell;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import org.junit.Test;

/**
* Tests for Environment Tab in Launch Configuration
*
* @author Oleksii Korniienko [email protected]
*
*/
public class EnvironmentTabTest {

private static final String CONFIGURATION_NAME = EnvironmentTabTest.class + "_test_config";

protected static LaunchConfigurationsDialog dialog;
protected static EnvironmentTab envTab;

@BeforeClass
public static void beforeClass() throws Exception {
dialog = new RunConfigurationsDialog();
dialog.open();
dialog.create(new JUnitLaunchConfiguration(), CONFIGURATION_NAME);
envTab = new EnvironmentTab();
envTab.activate();
}

@AfterClass
public static void closeDialog() {
try {
new WaitUntil(new ShellIsAvailable(dialog.getTitle()), TimePeriod.NONE);
new DefaultShell(dialog.getTitle()).close();
} catch (RedDeerException e) {
// already closed
}
}

@Test
public void addTest() {
int prev_env_count = envTab.getAllVariables().size();
envTab.add("test_name", "test_value");
int add_env_count = envTab.getAllVariables().size();
assertTrue((prev_env_count + 1) == add_env_count);
}

@Test
public void selectTest() {
int prev_env_count = envTab.getAllVariables().size();
envTab.selectEnvironmentVariable(0);
int select_env_count = envTab.getAllVariables().size();
assertTrue((prev_env_count + 1) == select_env_count);
}

@Test
public void removeTest() {
int prev_env_count = envTab.getAllVariables().size();
String first_var_name = envTab.getVariable(0).getText();
envTab.remove(first_var_name);
int remove_env_count = envTab.getAllVariables().size();
assertTrue((prev_env_count - 1) == remove_env_count);
}

@Test
public void editTest() {
envTab.edit("test_name", "test_new", "test_new");
String item_text = envTab.getVariable("test_new").getText();
assertTrue(item_text.contains("test_new"));
}

@Test
public void copyPasteTest() {
envTab.add("test_copy", "test_copy");
int prev_env_count = envTab.getAllVariables().size();

envTab.copy("test_copy");
envTab.remove("test_copy");
int copy_env_count = envTab.getAllVariables().size();
assertTrue((prev_env_count - 1) == copy_env_count);

envTab.paste();
int paste_env_count = envTab.getAllVariables().size();
assertTrue(prev_env_count == paste_env_count);
}

@Test
public void appendReplaceTest() {
envTab.getReplaceRadioButton().click();
assertTrue(new RadioButton("Replace native environment with specified environment").isSelected());
envTab.getAppendRadioButton().click();
assertTrue(new RadioButton("Append environment to native environment").isSelected());
}
}