Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 344484

Summary: SWTBotRadio method click() doesn't work properly
Product: [Technology] SWTBot Reporter: Vlado Pakan <vpakan>
Component: SWTBotAssignee: Project Inbox <swtbot-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: cabernal, geoff.bache, glubglub, hubert+eclipseorg, itac, michal.anglart, mistria, pascale.prost, pierre-charles.david, reprogrammer, richard.adams, sbouchet, tmm-bugs.eclipse, tobias.schwarz, Wael.mashal
Version: unspecified   
Target Milestone: 2.3.0   
Hardware: PC   
OS: Linux   
See Also: https://git.eclipse.org/r/42546
https://git.eclipse.org/c/swtbot/org.eclipse.swtbot.git/commit/?id=b181b0b6da7b59ebfccdc969f638ce47c0ebdcdf
Whiteboard:

Description Vlado Pakan CLA 2011-05-02 12:19:49 EDT
Build Identifier: 2.0.4.20110304_0338-e5aff47-dev-e36

After changes related to https://bugs.eclipse.org/bugs/show_bug.cgi?id=286897 method click() doesn't work for SWTBotRadio.
When selecting not selected radio button currently selected radio button is deselected new radio button is selected and finally previously selected radio button is again selected.

Reason is this new code from SWTBotRadio click() method:
if (otherSelectedButton != null) {
  otherSelectedButton.notify(SWT.Selection);
}

This is not correct behavior because application and not test has to deselect currently selected radio button. When user selects deselected radio button he is not deselecting previously selected radio button so there is difference between what user is doing and what SWTBot is simulating.

Reproducible: Always

Steps to Reproduce:
1. Create dialog with 2 radio buttons
2. Add logic to this radio buttons selection listener to disable previously selected radio button
3. Use SWTBotRadio method click() to click on not selected radio button.
4. New radio button is selected and then previously selected radio button is selected again
Comment 1 Richard Adams CLA 2011-07-05 07:50:53 EDT
Came across  same problem on MacOSX  and posted here in user group

http://www.eclipse.org/forums/index.php/t/215850/

A temporary fix is to use this method in your test code which deselects the current selection,  then  select the desired button:
/**
*@param currSelection The index of the radiobutton to deselect
*/
void deselectDefaultSelection(int currSelection) {
		UIThreadRunnable
		.syncExec(new VoidResult() {

			public void run() {
				Matcher<Widget> matcher = allOf(widgetOfType(Button.class), withStyle(SWT.RADIO, "SWT.RADIO"));

				Button b = (Button) bot.widget(matcher, currSelection); // the current selection
				b.setSelection(false);
				
			}
			
		});
	}
Comment 2 Mohsen Vakilian CLA 2011-07-11 12:36:13 EDT
(In reply to comment #1)

My test suffered from the same problem in selecting radio buttons (See my report at <http://stackoverflow.com/q/6641244/130224>). And, your temporary fix worked for me.

I'm using Eclipse 3.7 (Build id: 20110615-0604) with SWTBot (2.0.4.20110304_0338-e5aff47-dev-e36) on Ubuntu.

> Came across  same problem on MacOSX  and posted here in user group
> 
> http://www.eclipse.org/forums/index.php/t/215850/
> 
> A temporary fix is to use this method in your test code which deselects the
> current selection,  then  select the desired button:
> /**
> *@param currSelection The index of the radiobutton to deselect
> */
> void deselectDefaultSelection(int currSelection) {
>         UIThreadRunnable
>         .syncExec(new VoidResult() {
> 
>             public void run() {
>                 Matcher<Widget> matcher = allOf(widgetOfType(Button.class),
> withStyle(SWT.RADIO, "SWT.RADIO"));
> 
>                 Button b = (Button) bot.widget(matcher, currSelection); // the
> current selection
>                 b.setSelection(false);
> 
>             }
> 
>         });
>     }
Comment 3 Michal Anglart CLA 2011-09-08 04:46:07 EDT
It seems that I've also met this problem and posted about it here: http://www.eclipse.org/forums/index.php/t/238897/

Thanks for workaround - I will try it
Comment 4 Tim Moore CLA 2011-11-02 08:44:05 EDT
We also encountered this.

To be clear, when clicking on a radio button normally the SWT.Selection events are received by the buttons in the order: current, new.

Isn't fixing this properly as simple as swapping the "notify" lines of code at the bottom of SWTBotRadio.click()??
Comment 5 Georges Dupont CLA 2012-02-29 09:19:40 EST
Same question as Tim:

(In reply to comment #4)
[...]
> Isn't fixing this properly as simple as swapping the "notify" lines of code at
> the bottom of SWTBotRadio.click()??

I updated my plugin "org.eclipse.swtbot.swt.finder_2.0.5" changing the order of the selections and it seems ok now.

The code is the following (only 2 changes):


/**
 * Selects the radio button.
 */
public SWTBotRadio click() {
	if (isSelected()) {
		log.debug(MessageFormat.format("Widget {0} is already selected, not clicking again.", this)); //$NON-NLS-1$
		return this;
	}
	waitForEnabled();

	log.debug(MessageFormat.format("Clicking on {0}", this)); //$NON-NLS-1$

	final SWTBotRadio otherSelectedButton = otherSelectedButton();

	if (otherSelectedButton != null) {
		otherSelectedButton.notify(SWT.Deactivate);
		asyncExec(new VoidResult() {
			public void run() {
				otherSelectedButton.widget.setSelection(false);
			}
		});
		otherSelectedButton.notify(SWT.Selection); /* FIXME 344484: OTHER SELECTION MOVED HERE */
	}

	notify(SWT.Activate);
	notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
	notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));
	asyncExec(new VoidResult() {
		public void run() {
			widget.setSelection(true);
		}
	});
	notify(SWT.Selection);
	/* FIXME 344484: OTHER SELECTION WAS THERE */
	/* if (otherSelectedButton != null) {
		otherSelectedButton.notify(SWT.Selection);
	} */
	log.debug(MessageFormat.format("Clicked on {0}", this)); //$NON-NLS-1$
	return this;
}
Comment 6 Geoff Bache CLA 2012-06-05 03:55:06 EDT
If the suggested patch works, is somebody going to apply it and release a new SWTBot? Just got bitten by this as well (again).
Comment 7 Camilo Bernal CLA 2012-12-03 12:52:30 EST
Can we get this patch committed? Several people have stumbled onto this problem, and the fix seems to be fairly straightforward.
Comment 8 Mickael Istria CLA 2012-12-03 13:13:47 EST
@Georges: can you please submit this change as a Gerrit contribution to share it with all other interested parties?
http://wiki.eclipse.org/SWTBot/Contributing#Provide_a_contribution_using_Gerrit
Comment 9 Tobias Schwarz CLA 2013-02-12 02:39:46 EST
i see the same problem with SWTBotRadio.click() working on eclipse 3.8.1.

but the suggested patch does not solve this issue!
Comment 10 Tobias Schwarz CLA 2013-02-12 03:52:10 EST
sorry - ignore my last comment - the fix _IS_ working.

i ran into a problem in the dialog i tried to test with SWTBotRadio.
Comment 11 Wael Mashal CLA 2013-10-22 02:20:11 EDT
Hi

I build the finder jar with the above code and its OK on the level click() method exception , But

The other case i have ,I enabling some components depend on radio selection but still not work , in my case a Text field will be enabled when I click on the radio if I do it manually it works fine But if I call these steps using SWTBot the radio selected but the Text field still disabled , see my code below always the condition false in the if statement 

SWTBot shellBot = bot.activeShell().bot();
SWTBotRadio radio = shellBot.radio("Platform Manager URL");
radio.click();

if (shellBot.text(1).isEnabled()) {
	shellBot.text(1).typeText("http://url");
	shellBot.text(2).typeText("Admin");
	shellBot.text(3).typeText("Admin");
}

Any Idea Friends ? #Georges Dupont ?
Comment 12 pascale prost CLA 2014-09-10 06:59:22 EDT
I am using Eclipse CDT Kepler SR2 (4.3.2) and SWTBot 2.2.2 on Windows 7.

My tests contain SWTBotRadio.click(). I tried to implement the suggested patch with deselectDefaultSelection method. The "deselection" works fine (I can see it happening on the screen), but after the call to SWTBotRadio.click(), new radio button is selected and just after previously selected radio button is selected again.

Any advice is welcome, I cannot go further with my tests...
Comment 13 pascale prost CLA 2014-09-16 08:46:41 EDT
I have also tried to implement the suggested patch SWTBotRadio.click() method, but the issue still remains.
Comment 14 Mickael Istria CLA 2014-09-17 10:59:50 EDT
@Vlado: do you think you could provide a patch for that issue?
Comment 15 pascale prost CLA 2015-01-30 04:02:52 EST
I was finally able to solve my issue, using the patch suggested by Georges Dupont, but I had to add the a "setFocus();" statement before the "notify(SWT.Activate);" :

replacing :

	notify(SWT.Activate);
	notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
	notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));

with :


	notify(SWT.Activate);
	notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
	notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));
Comment 16 pascale prost CLA 2015-01-30 04:07:57 EST
Sorry, discard my previous comment (it got posted before I actually finished writing it).

I was finally able to solve my issue, using the patch suggested by Georges Dupont, but I had to add the a "setFocus();" statement before the "notify(SWT.Activate);".

So I replaced :

	notify(SWT.Activate);
	notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
	notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));

with :

        setFocus();
	notify(SWT.Activate);
	notify(SWT.MouseDown, createMouseEvent(0, 0, 1, 0, 1));
	notify(SWT.MouseUp, createMouseEvent(0, 0, 1, SWT.BUTTON1, 1));

Now my tests work fine :-)
Comment 17 Bouchet Stéphane CLA 2015-02-24 11:01:45 EST
Hi,

I'm getting the same problem trying to set some eclipse preferences.

I am able to fix thanks to previous comments.

i'll make a patch that includes a testcase and the fix thru gerrit.
Comment 18 Eclipse Genie CLA 2015-02-24 11:21:27 EST
New Gerrit change created: https://git.eclipse.org/r/42546
Comment 20 Mickael Istria CLA 2015-02-25 02:54:01 EST
Thanks Stephane. Your change was merged.
SWTBot 2.2.2 snapshot with this change will be available within a few minutes in http://download.eclipse.org/technology/swtbot/snapshots.