Problem getting the ToolBar in Eclipse's Expressions view

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

Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Fri Sep 11, 2009 8:46 am

Hello,

I am having some issues trying to get the ToolBar in Eclipse's Expression view using WT locators. I have tried using a ViewLocator and a CTabItemLocator as a parent but neither worked. See the code below which is not working to get the tool bar.

Code: Select all
public void test() throws Exception {
      IUIContext ui = getUI();
      
      ui.ensureThat(view("Expressions").isShowing());
            
      ViewLocator expressionsViewLocator = new ViewLocator(IDebugUIConstants.ID_EXPRESSION_VIEW);
      //ui.click(expressionsViewLocator); // multiple widget exception thrown here...
      
      CTabItemLocator expressionsTabLocator = new CTabItemLocator("Expressions");
      ui.click(expressionsTabLocator);      
      
      IWidgetLocator[] toolBarsInView = ui.findAll(new SWTWidgetLocator(ToolBar.class, expressionsViewLocator));
      System.out.println("Found " + toolBarsInView.length + " toolbars in the Expressions view");      
      
      IWidgetLocator[] toolBarsInTab = ui.findAll(new SWTWidgetLocator(ToolBar.class, expressionsTabLocator));
      System.out.println("Found " + toolBarsInTab.length + " toolbars in the Expressions tab");      
   }


I am seeing 2 issues with this.
1. Can't locate the toolbar. This is important for me as I have created some utilities in a testing framework that involve being able to find ToolBars with locator.
2. When I tried to click using the ViewLocator a multiple widget exception widget was thrown. This just seems incorrect as the view locator is given the view id.

For reference I am using WT 3.9.0 for Eclipse 3.1.

Any help with this would be appreciated.

Thanks,
Joe
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Fri Sep 11, 2009 9:18 am

Hi Joe,

Do you really mean Eclipse 3.1? WT requires Eclipse 3.2 and up...

Also, do you want access to the toolbar itself or its items?

If it's the items, does this work?:

Code: Select all
IWidgetLocator[] toolItemsInView = ui.findAll(new SWTWidgetLocator(ToolItem.class, ".*", new ViewLocator(IDebugUIConstants.ID_EXPRESSION_VIEW)));

Once you have the widgets, what are you trying to verify?
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Fri Sep 11, 2009 10:32 am

Oops, I should have listed Eclipse 3.3.1.1.

I do actually want to create a SWTWidgetLocator that can find a particular ToolBar. The reason being is that I have created a utility class full of convenience methods for operating on ToolIBars and ToolItems. Some of the methods are designed to make use of a locator that can find a particular toolbar. As the code snippet shows, there seems to be a problem with creating such a locator.

Thanks,
Joe
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Fri Sep 11, 2009 10:50 am

I guess I didn't answer you last question.

In this instance I want to click on a ToolItem in the ToolBar in the Expression View. One way I could do it is to specify the id of the tool item in a ContributedToolItemLocator. What I am trying to do is create a layer of testing utility methods that abstracts some WT usage (for ease of use for our test framework and product test developers).

Rather than creating specific locators for each button in the view, I would like to allow the test framework developer to only need a locator for the toolbar. Then they will be able easily create methods for interacting with all of the ToolItems in the ToolBar. An example:
Code: Select all
public static void clickShowTypeNamesButton(IUIContext ui) throws Exception {      
      ToolBarUtils.clickToolItem(ui, "Show Type Names", expressionViewToolBarLocator);
   }
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Mon Sep 14, 2009 8:20 am

I really like this approach. The notion of a helper abstraction is something we use extensively internally. You may also want to look at the WT Commons project (http://code.google.com/p/wt-commons/) for some ideas --- and please do consider contributing!

As for your specific story, is this the standard eclipse "Expressions" view toolbar?
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Mon Sep 14, 2009 10:41 am

Yes, just the standard Eclipse Expressions view toolbar.

Joe
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Mon Sep 14, 2009 2:36 pm

My team is currently under a tight deadline to have automated testing of a product completed. That said, it would be nice to be able to contribute if we are at some point able to cleanup and provide some useful Eclipse testing utility APIs. If we eventually get to do that, what would be the best way to contribute?

Thanks,
Joe
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Mon Sep 14, 2009 3:01 pm

Sorry for the slow motion on this Joe. I was hoping to have time to put something together for you today but the day ran away from me. I'll have something for you in the morning for sure. In the meantime, it looks like you want to do your matching on tooltips. Is this right? (That and the scope of a view's toolbar.)

As for contributing, I'll do whatever I can to make it easy on you. The simplest thing would be to push your contributions to a sandbox repository where we can both access and discuss. This is easy to do and I'll be happy to set it up if and when you get to where you have any spare cycles. Anyway, I'll follow up on this as soon as I have something to show you on the view tool item locator front.

Thanks for jump-starting this conversation!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Mon Sep 14, 2009 7:39 pm

Hey Joe,

Here's something to play with. It's not pretty... but I think you'll find it gets you much of the way there (if I'm understanding your needs).

Code: Select all
import static com.windowtester.runtime.swt.locator.eclipse.EclipseLocators.view;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.PartSite;

import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.locator.IWidgetLocator;
import com.windowtester.runtime.locator.WidgetReference;
import com.windowtester.runtime.swt.UITestCaseSWT;
import com.windowtester.runtime.swt.locator.SWTWidgetLocator;
import com.windowtester.runtime.util.StringComparator;

@SuppressWarnings("restriction") //PartSite is not API
public class ExpressionViewDrivingTest extends UITestCaseSWT{

   static final String ID_EXPRESSION_VIEW= "org.eclipse.debug.ui.ExpressionView";
   
   @Override
   protected void setUp() throws Exception {
      IUIContext ui = getUI();
      ui.ensureThat(view("Welcome").isClosed());
      ui.ensureThat(view("Expressions").isShowing());
   }
   
   public void testDrive() throws Exception {
      IUIContext ui = getUI();
      ui.click(new ViewToolItemLocator("Show Type Names", ID_EXPRESSION_VIEW));
      ui.pause(2000); //for visual verification
   }
   
   
   public static class ViewToolItemLocator extends SWTWidgetLocator {

      private static final long serialVersionUID = 1L;
      private static final IWidgetLocator[] EMPTY_ARRAY = new IWidgetLocator[0];
      private final String itemToolTip;
      private final String viewId;

      public ViewToolItemLocator(String itemToolTip, String viewId) {
         super(ToolItem.class);
         this.itemToolTip = itemToolTip;
         this.viewId = viewId;
      }
      
      /* (non-Javadoc)
       * @see com.windowtester.runtime.WidgetLocator#findAll(com.windowtester.runtime.IUIContext)
       */
      @Override
      public IWidgetLocator[] findAll(IUIContext ui) {
         final Control toolBarControl = ViewFinder.getViewToolBarControl(viewId);
         if (toolBarControl == null || !(toolBarControl instanceof ToolBar))
            return EMPTY_ARRAY;
         final List<IWidgetLocator> itemRefs = new ArrayList<IWidgetLocator>();
         Display.getDefault().syncExec(new Runnable(){
            public void run() {
               ToolBar toolBar = (ToolBar)toolBarControl;
               ToolItem[] items = toolBar.getItems();
               for (ToolItem item : items) {
                  if (matches(item))
                     itemRefs.add(WidgetReference.create(item));
               }
            }
         });
         return itemRefs.toArray(EMPTY_ARRAY);
      }
      
      public boolean matches(ToolItem item) {
         String toolTipText = item.getToolTipText();
         if (toolTipText == null)
            return false;
         return StringComparator.matches(toolTipText, itemToolTip);
      }
      
   }
   

   public static class ViewFinder {
      
      public static IViewPart getViewPart(final String id) {
         final IViewPart[] part = new IViewPart[1];
         final Display display = PlatformUI.getWorkbench().getDisplay();
         display.syncExec(new Runnable() {
            public void run() {
               //be safe here since the workbench might be disposed (or not active)
               IWorkbench workbench = PlatformUI.getWorkbench();
               if (workbench == null) {
                  return;
               }
               IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
               if (window == null) {
                  return;
               }
               IWorkbenchPage page = window.getActivePage();
               if (page == null) {
                  return;
               }
               
               IViewReference[] viewReferences = page.getViewReferences();
               for (int i = 0; i < viewReferences.length; i++) {
                  IViewReference ref = viewReferences[i];
                  if (ref.getId().equals(id)) {
                     part[0] = ref.getView(false); //don't attempt to restore -- OR?
                     return;
                  }
               }
            }
         });
         return part[0];
      }

      public static Control getViewToolBarControl(String viewId) {
         IViewPart viewPart = getViewPart(viewId);
         return getToolBarControl(viewPart);
      }

      private static Control getToolBarControl(IViewPart viewPart) {
         if (viewPart == null)
            return null;
         final IViewSite viewSite = viewPart.getViewSite();
         final Control[] control = new Control[1];
         Display.getDefault().syncExec(new Runnable() {
            public void run() {
               if (viewSite instanceof PartSite) {
                  PartSite partSite = (PartSite)viewSite;
                  control[0] = partSite.getPane().getToolBar();
               }
            }
         });
         return control[0];
      }
   }
}


Please let me know if this does (or does not) work for you and I'll be happy to refine.

-phil
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Tue Sep 15, 2009 10:18 am

Yes, what I was looking for is a way to locate a tool item by tooltip and have the search scoped to a tool bar in a particular view. Thanks for taking the time to show a good example of how I could create a custom locator to exactly that. I had considered creating a custom locator if needed so it is very helpful to see such an implementation.

Before I make the move to start adding a custom locator, I would like to know if my initial example is failing due to a possible bug in the WT APIs. This would be #1 in my original post. The locator created should look for instances of ToolBars in a specified view (via a ViewLocator). I fully expected that to work and would like to understand why it didn't work. Maybe not being able to click using the view locator is an indication of why it is failing? But that still wouldn't explain why the tool bar couldn't be located when the search was scoped to a tab locator.

Thanks,
Joe
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Tue Sep 15, 2009 11:59 am

Hi Joe,

The reason it does not work is because the view locator scopes things within the view's pane. The toolbar is a separate control. In this specific case it's a shortcoming for sure. As for scoping by tab, view locators scope indirect children whereas other locators describe their direct children. That means when you scope by a tab you are specifying a widget that is the direct child of a tab. Hope this helps!

-phil
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby jluebker » Thu Sep 17, 2009 7:17 am

That does help. Thanks Phil.
jluebker
 
Posts: 15
Joined: Thu Jul 02, 2009 11:17 am

Re: Problem getting the ToolBar in Eclipse's Expressions view

Postby Phil Quitslund » Thu Sep 17, 2009 7:25 am

You're welcome Joe. Let us know if anything else comes up. And please share your experiences writing helpers. We're keen to get people factoring test fixtures in just the way you describe so any experiences from the wild will help us to better understand the issues involved and guide us towards things we can do on our end to improve the story.

Again, thanks!
--
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