JList item selection uses toString, how to change this?

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

JList item selection uses toString, how to change this?

Postby antti.kekki » Wed Dec 29, 2010 2:17 am

Hello,

I have problem with WindowTester Pro JList item selection. I use custom renderer in JList to display name of objects. I do not use object.toString(). When I record test the following code is generated for JList:

Code: Select all
ui.click(new JListLocator("value from object.toString()", new NamedWidgetLocator("myJList")));


This is breaks up in replay because visible name selected of JList item is "ABC".

What is the correct way to fix this problem? Should I make my own JListLocator? Is there any example code for this for JList or JComboBox? Is there a way to make my own implementation of JListLocator the default one for code generation?
antti.kekki
 
Posts: 4
Joined: Tue Dec 28, 2010 11:25 pm

Re: JList item selection uses toString, how to change this?

Postby keertip » Wed Jan 05, 2011 9:59 am

Looking into this and will get back to you soon.
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am

Re: JList item selection uses toString, how to change this?

Postby keertip » Thu Jan 06, 2011 11:08 am

Did you change the generated code to

Code: Select all
    ui.click(new JListLocator("ABC", new NamedWidgetLocator("myJList")));


and run it? Try it and let us know the results.
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am

Re: JList item selection uses toString, how to change this?

Postby antti.kekki » Thu Jan 06, 2011 11:30 pm

keertip wrote:Did you change the generated code to

Code: Select all
    ui.click(new JListLocator("ABC", new NamedWidgetLocator("myJList")));


and run it? Try it and let us know the results.


No, it did not work. I get the following error:

Code: Select all
abbot.tester.LocationUnavailableException: The index (-1) is not valid
at abbot.tester.JListLocation.indexToPoint(JListLocation.java:50)
at abbot.tester.JListLocation.getPoint(JListLocation.java:81)
at com.windowtester.internal.swing.UIDriverSwing.clickListItem(UIDriverSwing.java:184)
at com.windowtester.runtime.swing.locator.JListLocator.doClick(JListLocator.java:61)
at com.windowtester.runtime.swing.SwingWidgetLocator.click(SwingWidgetLocator.java:266)
at com.windowtester.internal.runtime.selector.ClickHelper.doClick(ClickHelper.java:172)
at com.windowtester.internal.runtime.selector.ClickHelper.click(ClickHelper.java:59)
at com.windowtester.internal.runtime.UIContextCommon.click(UIContextCommon.java:140)
at com.windowtester.internal.swing.UIContextSwing.click(UIContextSwing.java:57)
at com.windowtester.internal.runtime.UIContextCommon.click(UIContextCommon.java:133)
at com.foo.bar.jListTestClass.test1(testi1.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.windowtester.runtime.common.UITestCaseCommon.access$0(UITestCaseCommon.java:1)
at com.windowtester.runtime.common.UITestCaseCommon$2.run(UITestCaseCommon.java:130)
at com.windowtester.runtime.common.UITestCaseCommon$3.run(UITestCaseCommon.java:151)
at com.windowtester.internal.runtime.junit.core.SequenceRunner$1.run(SequenceRunner.java:40)
antti.kekki
 
Posts: 4
Joined: Tue Dec 28, 2010 11:25 pm

Re: JList item selection uses toString, how to change this?

Postby keertip » Fri Jan 07, 2011 8:49 am

Could you post the code for the list and cell renderer? That would help to reproduce the problem at our end and see what we could do to help.
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am

Re: JList item selection uses toString, how to change this?

Postby antti.kekki » Mon Jan 10, 2011 6:31 am

keertip wrote:Could you post the code for the list and cell renderer? That would help to reproduce the problem at our end and see what we could do to help.


Here is a test case. I was wrong in my last post: the test works OK if I replace the toString()-result in JUnit with correct value that is visible in JList. Is there a ways to make this behavior a default one in code generation?

Code: Select all
import java.awt.Component;
import java.util.Locale;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class JListRendererDemo {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JList Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    DataItem[] items = {
          new DataItem("EN 1", "DE 1"),
          new DataItem("EN 2", "DE 2"),
          new DataItem("EN 3", "DE 3"),
          new DataItem("EN 4", "DE 4")
    };
    JList list = new JList(items);
    list.setName("JList1");
    list.setCellRenderer(new JListCellRenderer(Locale.ENGLISH));
    //list.setCellRenderer(new JListCellRenderer(Locale.GERMAN));
    frame.add(new JScrollPane(list));
   
    frame.pack();
    frame.setVisible(true);
  }
 
  /**
   * Renderer for JList
   */
  private static class JListCellRenderer extends DefaultListCellRenderer {
        private Locale userLocale;
    
        public JListCellRenderer(Locale userLocale) {
           this.userLocale = userLocale;
        }
    
      @Override
      public Component getListCellRendererComponent(JList list,
              Object value,
              int index,
              boolean isSelected,
              boolean cellHasFocus) {
         
           JLabel renderer = (JLabel) super.getListCellRendererComponent(list,
                 value,
                 index,
                 isSelected,
                 cellHasFocus);
          
           if(value != null) {
              DataItem dataItem = (DataItem) value;
             
              if(userLocale.equals(Locale.ENGLISH)) {
                 renderer.setText(dataItem.getNameEN());
              }
              else if(userLocale.equals(Locale.GERMAN)) {
                 renderer.setText(dataItem.getNameDE());
              }
           }
          
           return renderer;
      }
  }
 
  /**
   * Item to show in JList
   */
  private static class DataItem {
   private String nameEN;
   private String nameDE;
   
   public DataItem(String nameEN, String nameDE) {
      this.nameEN = nameEN;
      this.nameDE = nameDE;
   }
   public String getNameEN() {
      return nameEN;
   }
   public String getNameDE() {
      return nameDE;
   }
   @Override
   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("DataItem [nameEN=");
      builder.append(nameEN);
      builder.append(", nameDE=");
      builder.append(nameDE);
      builder.append("]");
      return builder.toString();
   }
  }
}


Original WindowTester Pro generated JUnit test with toString() values:
Code: Select all
import com.windowtester.runtime.swing.locator.JListLocator;
import com.windowtester.runtime.swing.locator.NamedWidgetLocator;
import com.windowtester.runtime.swing.UITestCaseSwing;
import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swing.condition.WindowDisposedCondition;

public class JListRendererDemoTest1 extends UITestCaseSwing {

   /**
    * Create an Instance
    */
   public JListRendererDemoTest1() {
      super(JListRendererDemo.class);
   }

   /**
    * Main test method.
    */
   public void testJListRendererDemoTest1() throws Exception {
      IUIContext ui = getUI();
      ui.click(new JListLocator("DataItem [nameEN=EN 1, nameDE=DE 1]",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("DataItem [nameEN=EN 2, nameDE=DE 2]",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("DataItem [nameEN=EN 3, nameDE=DE 3]",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("DataItem [nameEN=EN 4, nameDE=DE 4]",
            new NamedWidgetLocator("JList1")));
      ui.wait(new WindowDisposedCondition("JList Test"));
   }

}


Modified JUnit test with visible JList values:
Code: Select all
import com.windowtester.runtime.swing.locator.JListLocator;
import com.windowtester.runtime.swing.locator.NamedWidgetLocator;
import com.windowtester.runtime.swing.UITestCaseSwing;
import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swing.condition.WindowDisposedCondition;

public class JListRendererDemoTest2 extends UITestCaseSwing {

   /**
    * Create an Instance
    */
   public JListRendererDemoTest2() {
      super(JListRendererDemo.class);
   }

   /**
    * Main test method.
    */
   public void testJListRendererDemoTest2() throws Exception {
      IUIContext ui = getUI();
      ui.click(new JListLocator("EN 1",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("EN 2",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("EN 3",
            new NamedWidgetLocator("JList1")));
      ui.click(new JListLocator("EN 4",
            new NamedWidgetLocator("JList1")));
      ui.wait(new WindowDisposedCondition("JList Test"));
   }

}
antti.kekki
 
Posts: 4
Joined: Tue Dec 28, 2010 11:25 pm

Re: JList item selection uses toString, how to change this?

Postby keertip » Mon Jan 10, 2011 1:59 pm

Could you try out the latest beta from

http://code.google.com/javadevtools/dow ... -beta.html

and see if you still have this problem while recording?
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am

Re: JList item selection uses toString, how to change this?

Postby antti.kekki » Mon Jan 10, 2011 11:05 pm

keertip wrote:Could you try out the latest beta from

http://code.google.com/javadevtools/dow ... -beta.html

and see if you still have this problem while recording?


It works with Beta version! Now recording generates correct code to select Jlist item:

Code: Select all
public void testJListRendererDemoTest3Beta() throws Exception {
   IUIContext ui = getUI();
   ui.click(new JListLocator("EN 1", new NamedWidgetLocator("JList1")));
   ui.click(new JListLocator("EN 2", new NamedWidgetLocator("JList1")));
   ui.click(new JListLocator("EN 3", new NamedWidgetLocator("JList1")));
   ui.click(new JListLocator("EN 4", new NamedWidgetLocator("JList1")));
   ui.wait(new WindowDisposedCondition("JList Test"));
}


Thank you for helping!
antti.kekki
 
Posts: 4
Joined: Tue Dec 28, 2010 11:25 pm

Re: JList item selection uses toString, how to change this?

Postby keertip » Tue Jan 11, 2011 8:13 am

Great! Glad to have helped!
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest

cron