Skip to content

Commit

Permalink
implement kw to retrieve all open internal jframes, with test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kontulai committed Feb 4, 2013
1 parent bf77128 commit fedeb7b
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.robotframework.swing.container;

import java.awt.*;
import java.util.ArrayList;

public abstract class ContainerIterator {
private int level;
private Container container;
protected java.util.List<String> resultComponentList;

public ContainerIterator(Container container) {
this.container = container;
resultComponentList = new ArrayList<String>();
}

public java.util.List<String> iterate() {
processComponent(container);
return resultComponentList;
}

public abstract void operateOnComponent(Component component, int level);

private void processComponent(Component component) {
operateOnComponent(component, level);
level++;
if (component instanceof Container) {
Component[] subComponents = ((Container) component).getComponents();
for (int i = 0; i < subComponents.length; i++) {
processComponent(subComponents[i]);
level--;
}
}
}

protected String componentToString(Component component) {
String componentString = component.toString();
int indexToStartOfDetails = componentString.indexOf('[');
if (indexToStartOfDetails == -1)
return componentString;
return componentString.substring(0, indexToStartOfDetails);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.robotframework.swing.container;

import org.robotframework.swing.keyword.development.ComponentOccurences;

import java.awt.*;

public class ContainerIteratorForListing extends ContainerIterator {

private ComponentOccurences occurences = new ComponentOccurences();
public ContainerIteratorForListing(Container container) {
super(container);
}

private void printSpacesToFormatOutputAsTree(int level) {
for (int i = 0; i < level; i++)
System.out.print(" ");
}

public void operateOnComponent(Component component, int level) {
printSpacesToFormatOutputAsTree(level);
String componentName = componentToString(component);
System.out.println(level + " " + componentName + " " + occurences.countIndexOf(component) + ": " + component.getName());
resultComponentList.add(componentName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.robotframework.swing.internalframe;

import org.robotframework.swing.container.ContainerIterator;

import javax.swing.*;
import java.awt.*;

public class InternalFrameIteratorForListing extends ContainerIterator{
public InternalFrameIteratorForListing(Container container) {
super(container);
}

@Override
public void operateOnComponent(Component component, int level) {
if (JInternalFrame.class.isAssignableFrom(component.getClass())) {
if(component.isVisible()) {
resultComponentList.add(componentToString(component));
}
}
}

@Override
protected String componentToString(Component component) {
String title = ((JInternalFrame)component).getTitle();
if (title.isEmpty()) {
return super.componentToString(component);
}
return title;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@

package org.robotframework.swing.keyword.development;

import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;

import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.swing.container.ContainerIteratorForListing;
import org.robotframework.swing.context.Context;
import org.robotframework.swing.operator.ComponentWrapper;

import java.awt.*;

@RobotKeywords
public class DevelopmentKeywords {
private List<String> resultComponentList = new ArrayList<String>();

@RobotKeyword("Prints components (their types and their internal names) from the selected context.\n"
+ "The internal name is set with component's setName method: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#setName(java.lang.String).\n"
Expand All @@ -38,61 +35,6 @@ public class DevelopmentKeywords {
+ "| List Components In Context |\n")
public String listComponentsInContext() {
ComponentWrapper operator = Context.getContext();
new ContainerIteratorForListing((Container) operator.getSource()).iterate();
return resultComponentList.toString();
}

private void printSpacesToFormatOutputAsTree(int level) {
for (int i = 0; i < level; i++)
System.out.print(" ");
}

private String componentToString(Component component) {
String componentString = component.toString();
int indexToStartOfDetails = componentString.indexOf('[');
if (indexToStartOfDetails == -1)
return componentString;
return componentString.substring(0, indexToStartOfDetails);
}

private class ContainerIteratorForListing extends ContainerIterator {
private ComponentOccurences occurences = new ComponentOccurences();
public ContainerIteratorForListing(Container container) {
super(container);
}

public void operateOnComponent(Component component, int level) {
printSpacesToFormatOutputAsTree(level);
String componentName = componentToString(component);
System.out.println(level + " " + componentName + " " + occurences.countIndexOf(component) + ": " + component.getName());
resultComponentList.add(componentName);
}
}

private static abstract class ContainerIterator {
private int level;
private Container container;

public ContainerIterator(Container container) {
this.container = container;
}

public void iterate() {
processComponent(container);
}

public abstract void operateOnComponent(Component component, int level);

private void processComponent(Component component) {
operateOnComponent(component, level);
level++;
if (component instanceof Container) {
Component[] subComponents = ((Container) component).getComponents();
for (int i = 0; i < subComponents.length; i++) {
processComponent(subComponents[i]);
level--;
}
}
}
return new ContainerIteratorForListing((Container) operator.getSource()).iterate().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@

package org.robotframework.swing.keyword.internalframe;

import java.beans.PropertyVetoException;

import javax.swing.JInternalFrame;

import org.junit.Assert;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.swing.context.Context;
import org.robotframework.swing.factory.IdentifierParsingOperatorFactory;
import org.robotframework.swing.internalframe.InternalFrameIteratorForListing;
import org.robotframework.swing.internalframe.InternalFrameOperator;
import org.robotframework.swing.internalframe.InternalFrameOperatorFactory;
import org.robotframework.swing.operator.ComponentWrapper;
import org.robotframework.swing.util.ComponentExistenceResolver;
import org.robotframework.swing.util.IComponentConditionResolver;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyVetoException;
import java.util.List;

@RobotKeywords
public class InternalFrameKeywords {
private final IdentifierParsingOperatorFactory<InternalFrameOperator> operatorFactory = new InternalFrameOperatorFactory();
Expand Down Expand Up @@ -126,6 +129,16 @@ public void internalFrameShouldNotBeOpen(String identifier) {
createOperator(identifier).isVisible());
}

@RobotKeyword("Returns all frames that are open in the current context." + "\n\n"
+ "Returns empty list if the context is not selected.\n"
+ "Example:\n"
+ "| ${frames}= | Get Internal Frames In Context |"
+ "| Should Contain | ${frames} | testInternalFrame |")
public List<String> getInternalFramesInContext() {
ComponentWrapper operator = Context.getContext();
return new InternalFrameIteratorForListing((Container) operator.getSource()).iterate();
}

private InternalFrameOperator createOperator(String identifier) {
return operatorFactory.createOperator(identifier);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2008-2011 Nokia Siemens Networks Oyj
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/robot-tests/comboboxkeywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Get Selected Item From Dropdown Menu By Name
shouldBeEqual ${comboboxItem1} ${selectedItem}

Get Selected Item From Disabled Combobox
${selectedItem}= getSelectedItemFromComboBox disabledComboBox
${selectedItem}= getSelectedItemFromComboBox disabledComboBox
shouldBeEqual ${comboboxItem1} ${selectedItem}

Select From Combobox By Index
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/robot-tests/internalframekeywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ Internal Frame Keywords Should Fail Context Is Not Correct
runKeywordAndExpectError *To use this keyword you must first select a correct context* internalFrameShouldNotExist someFrame
[Teardown] selectMainWindow

Get Internal Frames In Context
${frames}= getInternalFramesInContext
lengthShouldBe ${frames} 0
selectFromMainMenuAndWait ${internalFrameMenu}
${frames}= getInternalFramesInContext
Length Should Be ${frames} 1
shouldContain ${frames} Test Internal Frame
[Teardown] closeInternalFrame ${internalFrameTitle}

*** Keywords ***
openInternalFrame
selectFromMainMenuAndWait ${internalFrameMenu}
Expand Down

0 comments on commit fedeb7b

Please sign in to comment.