Java Developer Tools

Test Execution and Lifecycle Explained

  1. Test Execution and Lifecycle Explained
  2. UI Test Cases
  3. Running Tests
  4. Test Execution (A Tale of Two Threads)
    1. SWT UI Thread
    2. Test Thread
    3. Bridging the Thread Divide
  5. Test Lifecycle
    1. Viewing Test Results
    2. Screenshots


UI Test Cases

Test cases in WindowTester Pro subclasses of UITestCaseSWT (for SWT) or UITestCaseSwing (for Swing). The classes are in turn specializations of JUnit’s TestCase. For instance, the class hierarchy of UITestCaseSWT is summarized here:

Running Tests

Since WindowTester Pro tests are just JUnit tests, this means that they can be run using standard JUnit test runners. In other words, running a plug-in WindowTester Pro test is just like running any other PDE test.

Test Execution (A Tale of Two Threads)

WindowTester Pro test execution is a bit more complicated than standard JUnit tests. The key to understanding test execution in WindowTester Pro is understanding its threading story.

SWT UI Thread

SWT follows the threading model supported directly by the operating system. The UI thread is the thread on which the Display is created and on which all widgets must be created and accessed. If this invariant is violated an SWTException is thrown signaling invalid thread access1.

Test Thread

WindowTester Pro tests execute a separate non-UI test thread. This is crucial since the UI thread frequently blocks — for instance when it is in a modal context and is waiting for user input. If tests did not have their own thread, WindowTester Pro would not be able to drive the UI when it is blocked.

Bridging the Thread Divide

To avoid thread access violations, the user must take care to ensure that all access to UI resources is done on the UI thread. The idiom for doing this is to create a Runnable and pass it to the Display for execution. As an example, take the following which safely accesses the text content of a Text widget:

String getText(final Text widget) {
	final String text[] = new String[1];
	Display.getDefault().syncExec(new Runnable() {
		public void run() {
			text[0] = widget.getText();
		}			
	});
	return text[0];
}

In a test, we could use such a method like this:

IWidgetReference ref = (IWidgetReference) ui.find(new SWTWidgetLocator(Text.class));
Text text = (Text)ref.getWidget();
assertEquals("some text", getText(text));

  
  

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.