Problems with QTest::mouseClick on QListWidget - qtestlib

I am trying to use QTest to do some testing. I have a QListWidget that I would like to click on to get a selection. But after the click, nothing is selected. Does anyone have any ideas?
Here is my test class
void TestGui::List() {
TestDialog dlg;
dlg.show ();
// Click on the centre of the second object
QListWidget *list = dlg.ListWidget ();
QListWidgetItem *item = list->item ( 1 );
QRect rect = list->visualItemRect ( item );
QTest::mouseClick ( list, Qt::LeftButton, 0, rect.center() );
// Check if something was selected
QCOMPARE ( list->currentRow (), 1 );
QVERIFY ( list->currentItem () != NULL );
QCOMPARE ( list->currentItem ()->text (), QString ( "Two" ) );
}
Below is the testing class
class TestGui: public QObject {
Q_OBJECT
private slots:
void List();
};
And here is the TestDialog class used to display the problem
class TestDialog : public QDialog {
Q_OBJECT
public:
TestDialog ( QWidget *parent = NULL )
: QDialog ( parent, Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint ) {
QVBoxLayout *layout = new QVBoxLayout ( this );
m_list = new QListWidget ( this );
m_list->addItem ( "One" );
m_list->addItem ( "Two" );
m_list->addItem ( "Three" );
m_list->addItem ( "Four" );
layout->addWidget ( m_list );
QPushButton *close_button = new QPushButton( "Close" );
connect ( close_button, SIGNAL ( clicked () ), this, SLOT ( close () ) );
layout->addWidget ( close_button );
setWindowTitle( "Test" );
}
QListWidget *ListWidget ( void ) {
return m_list;
};
private:
QListWidget *m_list;
}; // TestDialog

After some more thought, it turns out that the click needs to be on the view widget and not the list itself. So the line should look like this
QTest::mouseClick ( list->viewport (), Qt::LeftButton, 0, rect.center() );
Thanks

Related

ActionEvent not recognizing CTRL keybinding

I'm trying to integrate keybindings into a program I'm making, but as that program is long, I'm trying to learn on a smaller similarly coded program that I found on StackOverflow. Here's the code I'm using:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
#SuppressWarnings("serial")
private void initGUI() {
JPanel content = new JPanel(new FlowLayout());
content.add(new JLabel("Test:"));
AbstractAction buttonPressed = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
System.out.println(e.getSource());
if ("a".equals(e.getActionCommand()))
content.setBackground(new Color(227, 19, 19));
if ("b".equals(e.getActionCommand()))
content.setBackground(new Color(0, 225, 19));
}
};
JButton submit = new JButton("Submit");
submit.addActionListener(buttonPressed);
submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_A,
java.awt.event.InputEvent.CTRL_DOWN_MASK),
"A_pressed");
submit.getActionMap().put("A_pressed", buttonPressed);
submit.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_B, 0),
"B_pressed");
submit.getActionMap().put("B_pressed", buttonPressed);
content.add(submit);
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().initGUI();
}
});
}
}
What I what to happen is to press CTRL+A and have the background change color. However, when I do this, System.out.println(e.getActionCommand()) is returning a character that looks like a question mark in a box like it's an unknown character or something. The program works if you press B, but adding the modifier CTRL is not working correctly.Is the problem something I'm not doing right? Is the program working correctly and I don't know how to compare e.getActionCommand() and what ever string CTRL+A returns as a ActionEvent? Please help.
in my knowledge if an Action doesn't have its actionCommmand explicitly set it will be set when the ActionEvent is created. in your code the actionCommmand will be the string representation of the charKey of your keyStroke (b and ctrl+a not a)
so i could suggest to put a different action for every action :
class simpleAction extends AbstractAction {
public simpleAction ( String name ) {
super ();
putValue ( Action.ACTION_COMMAND_KEY, name );
}
#Override
public void actionPerformed ( ActionEvent e ) {
System.out.println ( "getActionCommand----->" + e.getActionCommand () );
System.out.println ( "getSource----->" + e.getSource () );
if ( "a".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}
if ( "b".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 0, 225, 19 ) );
}
}
}
JButton submit = new JButton ( "Submit" );
submit.addActionListener ( new simpleAction ( "Submit" ) );
submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
javax.swing.KeyStroke.getKeyStroke ( java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_DOWN_MASK ), "A_pressed" );
submit.getActionMap ().put ( "A_pressed", new simpleAction ( "a" ) );
submit.getInputMap ( javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ).put (
javax.swing.KeyStroke.getKeyStroke (
java.awt.event.KeyEvent.VK_B, 0 ),
"B_pressed" );
submit.getActionMap ().put ( "B_pressed", new simpleAction ( "b" ) );
or a second solution is check the string representation for the ctrl+a
so you change
if ( "a".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}
by
if ( "\u0001".equals ( e.getActionCommand () ) ) {
content.setBackground ( new Color ( 227, 19, 19 ) );
}

Having trouble with rule that checks two instances of same class

I have a rule that says
rule "bcs-set"
when
Param( Feature == "BCS", Name == "primary" )
Param( Feature == "BCS", Name == "seconday" )
then
insert ("addition")
end
I have created two Param objects but it seems like drools can't find both of the Param objects.
If I take out the first Param check it works but not with both of the Param checks in the rule.
The Param class is as follows:
public class Param {
private String feature;
private String name;
public String getFeature(){
return feature;
}
public void setFeature(String feature){
this.feature = feature;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
Anyone has any ideas?
It's very likely that you have done something like
Param p = new Param();
p.setFeature( "BCS" );
p.setName( "primary" );
kSession.insert( p );
p.setName( "secondary" );
kSession.insert( p );
kSession.fireAllRules();
Note that insert doesn't copy; it just uses the reference. - This is how it should be done:
Param p1 = new Param();
p1.setFeature( "BCS" );
p1.setName( "primary" );
kSession.insert( p1 );
Param p2 = new Param();
p2.setFeature( "BCS" );
p2.setName( "secondary" );
kSession.insert( p2 );
kSession.fireAllRules();
It is, of course, possible that you have done something else, but this fits the facts as you have related them. Sadly, you have omitted this highly important part of the picture.

ZK Disbale Div,Window,Layout Component?

I am using ZK Framework in my project i have plenty of other component inside a div or Window Component ,Can any one tell me how can i disable a Div or Window component in certain condition.As i checked there is no any disable attribute for these components.
Any other way we can i disable a Div or Window otherwise i have to disable each component inside the Div or Window or Layout
Here a very easy way to disable all components that implement the
Disable interface.
#Wire("disable")
private List<Disable> allToDisable;
private disableAll(List<Disable> list){
for(Disable d : list){
d.setDisabled(true);
}
}
You could edit the path of #Wire to fit your needs,
use a method of Selectors or any other method
that takes a zk selector path. Just let it end with
"disable", so it should select every Component that
implements the interface.
I think there is no simple way, I would try something like this (found this on google but I remember doing something similar on my last project)
public static void disableComponents( AbstractComponent pComponent ) {
for( Object o : pComponent.getChildren() ) {
AbstractComponent ac = ( AbstractComponent ) o;
try {
Method m = ac.getClass().getMethod( "setDisabled", Boolean.TYPE );
m.invoke( ac, true );
} catch( Exception e ) {
}
List children = ac.getChildren();
if( children != null ) {
disableComponents( ac );
}
}
}
We can improve Gatekeeper's (May 16 '13 at 9:22) solution adding the condition "if".
if (ac instanceof Disable) { --- code -- }
public static void disableComponents( AbstractComponent pComponent ) {
for( Object o : pComponent.getChildren() ) {
AbstractComponent ac = ( AbstractComponent ) o;
try {
if (ac instanceof Disable) {
Method m = ac.getClass().getMethod("setDisabled", Boolean.TYPE);
m.invoke(ac, true);
}
} catch( Exception e ) {
e.printStackTrace();
}
List children = ac.getChildren();
if( children != null ) {
disableComponents( ac );
}
}
}

Selecting a listbox dropdown item in GWT

I have a GWT view from which I grab the value of a dropdown and store it in a DB. The dropdown has the values "one" "two" "three". When I go back to the same view and I have "Two" stored in the DB then I want "Two" to be the selected item. However the only way I can get this to work at the moment is by iterating through each item in the listbox to find the one which matches and then set this as the selected one. Is there a better way to achieve this? I don't want to have to save the selected index.
I recommend you to extend ListBox and implement TakesValue interface. And in this class maintain a list variable which holds all the items in the ListBox. setValue and getValue should looks like the following code snippet -
private List<String> listItems = new ArrayList<String>();
public class MyListBox extends ListBox implements TakesValue<String>
{
public void setValue( String value )
{
if ( listItems.size() > 0 )
{
int valueIndex = 0;
if ( listItems.contains( value ) )
{
valueIndex = listItems.indexOf( value );
this.value = value;
}
setItemSelected( valueIndex, true );
}
}
public String getValue()
{
int selectedIndex = super.getSelectedIndex();
String value = null;
if ( selectedIndex >= 0 )
{
value = super.getValue( selectedIndex );
if ( "null".equals( value ) )
{
value = null;
}
}
return value;
}
public void setOptions(List<String> options)
{
listItems.clear();
listItems.addAll( items );
for ( String item : listItems )
{
addItem( item, item );
}
}
}
Now its just a matter of doing listBox.setValue( value ) method call from the view java file. Prior to this options must be set.

Specifying which Cell should receive focus next in a CellTable

I'm using GWT 2.1.0
I have a CellTable populated with Columns that use different Cells to edit different types of values (e.g. date, string, etc). I want the user to be able to click in a cell, type a value, and hit enter to go directly to editing the next cell down, or tab to go directly to editing the next cell over.
I've been looking through the Cell and CellTable interfaces but can't find anything that looks relevant. How can I achieve this effect?
I had a similar requirement and I could not find a out-of-the-box solution. I ended up subclassing TextInputCell and add tabIndex support myself.
Here's some bits and pieces of the subclass (hopefully it will compile, too lazy to check). Unfortunately I cannot post the entire subclass, since it has lot may other things which are not related to the current question. This solution takes care of tabbing to the next cell, but for enter support, you may need to override onBrowserEvent.
public class EditTextInputCell extends TextInputCell
{
int startTabIndex;
interface TabbedTemplate extends SafeHtmlTemplates
{
#Template( "<input type=\"text\" value=\"{0}\" tabindex=\"{1}\" class=\"{2}\" title=\"{3}\"></input>" )
SafeHtml input( String value, String tabindex, String styleClass, String title );
}
private static TabbedTemplate template;
public EditTextInputCell( int startTabIndex )
{
this.startTabIndex = startTabIndex;
}
#Override
public boolean isEditing( Context context, Element parent, String value )
{
return true;
}
#Override
public void render( Context context, String value, SafeHtmlBuilder sb )
{
// Get the view data.
Object key = context.getKey( );
ValidationData viewData = getViewData( key );
if ( viewData != null && value.equals( viewData.getCurrentValue( ) ) )
{
clearViewData( key );
viewData = null;
}
String strToDisp = ( viewData != null && viewData.getCurrentValue( ) != null ) ? viewData.getCurrentValue( ) : value;
String tabIndex = "" + startTabIndex + context.getIndex( ) + context.getColumn( );
boolean invalid = ( viewData == null ) ? false : viewData.isInvalid( );
String styleClass = "cellTableCell-valid";
String errorMessage = "";
if ( invalid )
{
styleClass = "cellTableCell-invalid";
errorMessage = viewData.getMessage( );
}
if ( strToDisp != null )
{
SafeHtml html = SimpleSafeHtmlRenderer.getInstance( ).render( strToDisp );
// Note: template will not treat SafeHtml specially
sb.append( getTemplate( ).input( html.asString( ), tabIndex, styleClass, errorMessage ) );
}
else
{
sb.appendHtmlConstant( "<input type=\"text\" tabindex=\"" + tabIndex + "\" class=\"" + styleClass + "\" title=\"" + errorMessage + "\"></input>" );
}
}
private TabbedTemplate getTemplate( )
{
if ( template == null )
{
template = GWT.create( TabbedTemplate.class );
}
return template;
}}