activeWorkbenchWindow

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

activeWorkbenchWindow

Postby mwa » Mon Jun 30, 2008 6:53 am

Hi,

With WindowTester I open a xml file in an editor and do some typing. Now I want to see, if the contents are correct.

With the following code I normally I can get the active page:
Code: Select all
      IWorkbench wb = PlatformUI.getWorkbench();
      IWorkbenchWindow wbWindow = wb.getActiveWorkbenchWindow();
      IWorkbenchPage page = wbWindow.getActivePage();

But the call of wb.getActiveWorkbenchWindow() returns null. Does that mean that I am not on the ui thread?

But why? The debugger shows me if I set a breakpoint at the line, that the active Thread is the "WT Test Thread". That should be the ui thread, shouldnt it?

How can I get the active page respective the actual editor contents, if I am in a test case?

Thanks for help.

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: activeWorkbenchWindow

Postby Phil Quitslund » Mon Jun 30, 2008 8:28 am

The WT Test Thread is not the UI thread (and this is important, lest it get blocked when the UI is in a modal state). As a result, whenever you need to do things on the UI thread you need to consciously wrap them in a runnable and pass them to the display to syncExec.
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: activeWorkbenchWindow

Postby mwa » Tue Jul 01, 2008 5:05 am

Yes. That makes a lot of sense, of course. Because I want always something from the ui test to see if an input is done properly, I wrote the follwoing helper method.
Code: Select all
   /**
    * Returns the active page or null if no page is available. A page is a
    * composition of views and editors which are meant to show at the same
    * time.
    *
    * @return The active page.
    */
   public static IWorkbenchPage getPage() throws Exception {
      final IWorkbenchPage page[] = new IWorkbenchPage[]{null};
       final String errorMessages[] = new String[]{""};

       Display.getDefault().syncExec(new Runnable() {
          public void run() {
             try {
                IWorkbench wb = PlatformUI.getWorkbench();
                IWorkbenchWindow wbWindow = wb.getActiveWorkbenchWindow();
                page[0] = wbWindow.getActivePage();
             } catch(Exception e){
                errorMessages[0] = e.toString();
             }
          }
       });
       if(errorMessages[0].length() > 0){
          throw new Exception(errorMessages[0]);
       }
      return page[0];
   }


No I can easily implement other helpers that use this method without thinking about threading:-) For example:
Code: Select all
   /**
    * Returns the current active editor or null if no editor is opened.
    * A editor is a visual component of a workbench page. Therefore this method
    * first calls {@link getPage()} for determining the active editor.
    *
    * @return The active editor or null if no editor is opened.
    * @see getPage()
    */
   public static IEditorPart getEditor() throws Exception {
      IEditorPart actEditor = null;
      IWorkbenchPage page = getPage();
      if (page.isEditorAreaVisible() && page.getActiveEditor() != null) {
         actEditor = page.getActiveEditor();
      }
      return actEditor;
   }


That works for me now. Thanks for help!

Michael
mwa
 
Posts: 24
Joined: Mon Jun 16, 2008 4:38 am

Re: activeWorkbenchWindow

Postby Phil Quitslund » Tue Jul 01, 2008 7:03 am

Glad you got it working. And many thanks for sharing your helpful snippets!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest