Skip to content

Commit

Permalink
REDDEER-1854 Move getContextMenu implementation to AbstractWorkbenchP…
Browse files Browse the repository at this point in the history
…art so it may be used in all View. Some views may need to override getRegisteredControl method, as I did in ConsoleView.

Signed-off-by: Lukáš Valach <lvalach@redhat.com>
  • Loading branch information
luvalach authored and rawagner committed Feb 13, 2018
1 parent a1f3d49 commit b7d743a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 31 deletions.
Expand Up @@ -10,8 +10,6 @@
*******************************************************************************/
package org.eclipse.reddeer.eclipse.ui.console;

import org.hamcrest.Matcher;
import org.hamcrest.core.IsEqual;
import org.eclipse.reddeer.common.condition.AbstractWaitCondition;
import org.eclipse.reddeer.common.wait.TimePeriod;
import org.eclipse.reddeer.common.wait.WaitUntil;
Expand All @@ -27,6 +25,9 @@
import org.eclipse.reddeer.swt.impl.styledtext.DefaultStyledText;
import org.eclipse.reddeer.swt.impl.toolbar.DefaultToolItem;
import org.eclipse.reddeer.workbench.impl.view.WorkbenchView;
import org.eclipse.swt.widgets.Control;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsEqual;

/**
* Represents Console view in Eclipse
Expand Down Expand Up @@ -244,4 +245,23 @@ public String getConsoleLabel(){
org.eclipse.swt.widgets.Widget swtWidget = widgetIsFound.getResult();
return (swtWidget == null) ? null : LabelHandler.getInstance().getText((org.eclipse.swt.widgets.Label)swtWidget);
}

/**
* Returns a control registered via adapters. This is usually StyledText or
* Canvas.
*
* @return registered control
*/
protected Control getRegisteredControl() {
activate();
WidgetIsFound widgetIsFound = new WidgetIsFound(org.eclipse.swt.custom.StyledText.class, cTabItem.getControl());
new WaitUntil(widgetIsFound, TimePeriod.SHORT, false);
// Check whether there is a console to display or not
if (widgetIsFound.getResult() == null) {
log.debug("There is no console in console view.");
return null;
}
return (org.eclipse.swt.custom.StyledText) widgetIsFound.getResult();
}

}
Expand Up @@ -121,11 +121,4 @@ public interface Editor extends WorkbenchPart {
* @return Editor file associated to the editor
*/
EditorFile getAssociatedFile();

/**
* Returns a context menu associated to the editor.
*
* @return Context menu associated to the editor
*/
Menu getContextMenu();
}
Expand Up @@ -10,8 +10,9 @@
*******************************************************************************/
package org.eclipse.reddeer.workbench.api;

import org.eclipse.swt.graphics.Image;
import org.eclipse.reddeer.core.reference.ReferencedComposite;
import org.eclipse.reddeer.swt.api.Menu;
import org.eclipse.swt.graphics.Image;

/**
* Interface with base operations which can be performed with workbench part.
Expand Down Expand Up @@ -70,4 +71,10 @@ public interface WorkbenchPart extends ReferencedComposite {
*/
void restore();

/**
* Returns a context menu associated to the WorkbenchPart.
*
* @return Context menu associated to the WorkbenchPart
*/
Menu getContextMenu();
}
Expand Up @@ -26,16 +26,13 @@
import org.eclipse.reddeer.common.wait.WaitUntil;
import org.eclipse.reddeer.common.wait.WaitWhile;
import org.eclipse.reddeer.core.handler.MenuItemHandler;
import org.eclipse.reddeer.core.lookup.MenuLookup;
import org.eclipse.reddeer.core.lookup.ShellLookup;
import org.eclipse.reddeer.core.matcher.WithTextMatcher;
import org.eclipse.reddeer.core.reference.ReferencedComposite;
import org.eclipse.reddeer.core.util.InstanceValidator;
import org.eclipse.reddeer.jface.text.contentassist.ContentAssistant;
import org.eclipse.reddeer.swt.api.Menu;
import org.eclipse.reddeer.swt.api.MenuItem;
import org.eclipse.reddeer.swt.impl.ctab.DefaultCTabItem;
import org.eclipse.reddeer.swt.impl.menu.DefaultMenu;
import org.eclipse.reddeer.swt.impl.menu.ShellMenuItem;
import org.eclipse.reddeer.swt.keyboard.KeyboardFactory;
import org.eclipse.reddeer.workbench.api.Editor;
Expand Down Expand Up @@ -327,21 +324,6 @@ public EditorFile getAssociatedFile() {
return new DefaultEditorFile(iFile);
}

/**
* Returns a context menu associated to the editor. The context menu is obtained from a registered control. If this
* control doesn't meet your requirements you can change it by overriding {{@link #getRegisteredControl()}}.
*
* @return Context menu associated to the editor
*/
@Override
public Menu getContextMenu() {
Control registeredControl = getRegisteredControl();
if (registeredControl == null) {
throw new WorkbenchLayerException("No control is registered with the editor");
}
return new DefaultMenu(MenuLookup.getInstance().getControlMenu(registeredControl));
}

/**
* Returns a control registered via adapters. This is usually StyledText or Canvas.
*
Expand Down
Expand Up @@ -10,13 +10,17 @@
*******************************************************************************/
package org.eclipse.reddeer.workbench.part;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.core.lookup.MenuLookup;
import org.eclipse.reddeer.swt.api.CTabItem;
import org.eclipse.reddeer.swt.api.Menu;
import org.eclipse.reddeer.swt.impl.menu.DefaultMenu;
import org.eclipse.reddeer.workbench.api.WorkbenchPart;
import org.eclipse.reddeer.workbench.exception.WorkbenchLayerException;
import org.eclipse.reddeer.workbench.handler.WorkbenchPartHandler;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.actions.ActionFactory;

/**
* Abstract class for all WorkbenchPart implementations
Expand Down Expand Up @@ -80,5 +84,36 @@ public void restore() {
// in order to restore maximized window maximized action has to be called
WorkbenchPartHandler.getInstance().performAction(ActionFactory.MAXIMIZE);
}

/**
* Returns a context menu associated to the workbench. The context menu is
* obtained from a registered control. If this control doesn't meet your
* requirements you can change it by overriding
* {{@link #getRegisteredControl()}}.
*
* @return Context menu associated to the editor
*/
@Override
public Menu getContextMenu() {
Control registeredControl = getRegisteredControl();
if (registeredControl == null) {
throw new WorkbenchLayerException("No control is registered with the workbench");
}
return new DefaultMenu(MenuLookup.getInstance().getControlMenu(registeredControl));
}

/**
* Returns a control registered via adapters. This is usually StyledText or
* Canvas.
*
* @return registered control
*/
protected Control getRegisteredControl() {
CTabItem cTabItem = getCTabItem();
return cTabItem.getControl();
}

public CTabItem getCTabItem() {
return cTabItem;
}
}
Expand Up @@ -14,6 +14,7 @@
import static org.eclipse.reddeer.common.wait.WaitProvider.waitWhile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

import org.eclipse.reddeer.common.matcher.RegexMatcher;
Expand All @@ -38,6 +39,8 @@
import org.eclipse.reddeer.eclipse.utils.DeleteUtils;
import org.eclipse.reddeer.junit.runner.RedDeerSuite;
import org.eclipse.reddeer.requirements.openperspective.OpenPerspectiveRequirement.OpenPerspective;
import org.eclipse.reddeer.swt.api.Menu;
import org.eclipse.reddeer.swt.api.MenuItem;
import org.eclipse.reddeer.swt.api.StyledText;
import org.eclipse.reddeer.swt.impl.menu.ShellMenuItem;
import org.eclipse.reddeer.swt.impl.styledtext.DefaultStyledText;
Expand Down Expand Up @@ -180,6 +183,19 @@ public void toggleShowConsoleOnStandardOutChange() {
consoleView.toggleShowConsoleOnStandardOutChange(true);
consoleView.toggleShowConsoleOnStandardOutChange(false);
}

@Test
public void getContextMenuTest() {
ConsoleView consoleView = new ConsoleView();
consoleView.open();

runTestClass(TEST_CLASS_NAME);
AbstractWait.sleep(TimePeriod.SHORT);

Menu contextMenu = consoleView.getContextMenu();
MenuItem clear = contextMenu.getItem("Clear");
assertNotNull(clear);
}

@After
public void tearDown(){
Expand Down

0 comments on commit b7d743a

Please sign in to comment.