Skip to content

SetBlockOnOpen Platform Differences #3334

@grissinibread

Description

@grissinibread

While delving into SWT issue #1599 my partner and I noticed a different bug. The setBlockOnOpen function was causing different outcomes on different platforms.

We came across this while testing the following code:

package org.eclipse.swt.tests.graphics;
import static org.junit.Assert.assertTrue;

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.Test;


public class SWTRadioButtonFocusTest {

  private SimpleDialog _dialog;

  @Test
  void testRadioButtonsAuto() {
      Shell shell = new Shell();
      _dialog = new SimpleDialog(shell, true);
      _dialog.setBlockOnOpen(false);
      _dialog.open();

      _dialog.getRadioButton1().setSelection(false);
      _dialog.getRadioButton2().setSelection(true);
      _dialog.getRadioButton2().notifyListeners(SWT.Selection, new Event()); //select radio button 2

      assertTrue(_dialog.getRadioButton2().getSelection()); //check if radio button 2 is selected

      _dialog.getFileChooserButton().notifyListeners(SWT.Selection, new Event()); //click the browse button to open fileChooser

      assertTrue(_dialog.getRadioButton2().getSelection()); //after closing the fileChooser, radio button 2 should be still selected - but radio button 1 is selected instead
  }

  private class SimpleDialog extends TitleAreaDialog {

    private boolean _userTest;
    private Button _radio1;
    private Button _radio2;
    private Button _button;

    public SimpleDialog(Shell parentShell, boolean userTest) {
      this(parentShell);
      _userTest = userTest;
    }

    public SimpleDialog(Shell parentShell) {
      super(parentShell);
    }

    @Override
    public void create() {
      super.create();
      setTitle("SWT Radio Buttons");
      setMessage("Select Radio Button 2 and then open the FileChooser and close it afterwards.");
    }

    @Override
    protected Control createDialogArea(Composite parent) {
      Composite area = (Composite) super.createDialogArea(parent);
      Composite container = new Composite(area, SWT.NONE);
      GridDataFactory.fillDefaults().applyTo(container);
      GridLayoutFactory.fillDefaults().applyTo(container);

      createRadioButtons(container);
      createFileChooserButton(container);

      return area;

    }

    private void createRadioButtons(Composite parent) {
      _radio1 = new Button(parent, SWT.RADIO);
      _radio1.setText("Option 1");
      GridDataFactory.fillDefaults().grab(true, false).applyTo(_radio1);

      _radio2 = new Button(parent, SWT.RADIO);
      _radio2.setText("Option 2");
      GridDataFactory.fillDefaults().grab(true, false).applyTo(_radio2);
    }

    private void createFileChooserButton(Composite parent) {
      _button = new Button(parent, SWT.PUSH);
      _button.setText("Browse...");
      _button.addSelectionListener(createSelectionAdapter());
    }

    private SelectionAdapter createSelectionAdapter() {
      if (_userTest) {
        return new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
            fileDialog.setText("Select File");
            fileDialog.open();
          }
        };
      } else {
        return new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            SimpleDialog simpleDialog = new SimpleDialog(getShell());
            simpleDialog.setBlockOnOpen(false);
            simpleDialog.open();
            simpleDialog.close();
          }
        };
      }
    }

    public Button getRadioButton1() {
      return _radio1;
    }

    public Button getRadioButton2() {
      return _radio2;
    }

    public Button getFileChooserButton() {
      return _button;
    }
  }
}

I was testing on Mac, where the test passes. My partner tested on Windows, and his came out to failed.

I'm not sure how to approach this problem in platform difference when the function is universally made for all those supported unlike SWT that has implementation for each respective platform.

Any suggestions?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions