Skip to main content
Jump to: navigation, search

Difference between revisions of "Riena/Getting Started with Controller Tests"

(Testing a simple navigation)
(Testing the navigation to a specific Ridget with a NavigationArgument)
Line 87: Line 87:
 
   bean.setPerson(new Person("Doe", "Jane"));
 
   bean.setPerson(new Person("Doe", "Jane"));
  
   getMockNavigationProcessor().navigate(EasyMock.eq(getController().getNavigationNode()), EasyMock.eq(new NavigationNodeId("org.eclipse.riena.example.combo")),
+
   getMockNavigationProcessor().navigate(EasyMock.eq(getController().getNavigationNode()),
 +
    EasyMock.eq(new NavigationNodeId("org.eclipse.riena.example.combo")),
 
     EasyMock.cmp(new NavigationArgument(bean, "textFirst"), new Comparator<NavigationArgument>() {
 
     EasyMock.cmp(new NavigationArgument(bean, "textFirst"), new Comparator<NavigationArgument>() {
  
    public int compare(NavigationArgument o1, NavigationArgument o2) {
+
      public int compare(NavigationArgument o1, NavigationArgument o2) {
      if (o1.getParameter() instanceof PersonModificationBean && o2.getParameter() instanceof PersonModificationBean) {
+
        if (o1.getParameter() instanceof PersonModificationBean && o2.getParameter() instanceof PersonModificationBean) {
        return comparePersonModificationBeans((PersonModificationBean) o1.getParameter(), (PersonModificationBean) o2.getParameter());
+
          return comparePersonModificationBeans((PersonModificationBean) o1.getParameter(), (PersonModificationBean) o2.getParameter());
      } else {
+
        } else {
        return -1;
+
          return -1;
 +
        }
 
       }
 
       }
    }
 
  
 
     }, LogicalOperator.EQUAL));
 
     }, LogicalOperator.EQUAL));

Revision as of 04:36, 18 January 2010

Testing your SubModuleControllers is an essential part of assuring the quality of your client application. With Riena's Controller Testing support it is quite easy to find problems in your SubModuleController logic at an early stage.

Prerequisites

In order to set up test cases, the SubModuleController has to be prepared to acquire Ridgets without a bound view. For this the method getRidget(Class<R> ridgetClazz, String id) has to be used in the SubModuleController to test. On the one hand this method wraps getRidget(String id) to return an already known (bound) instance of the Ridget and on the other hand it creates a new instance of the Ridget if run in test mode and no bound widget is existent.

Setting up a test case

All SubModuleControllerTests should extend AbstractSubModuleControllerTest. This class provides the essential operations needed to test a controller. Every test class has to implement the abstract method createController(ISubModuleNode node) where the SubModuleController to test has to be instantiated and returned. Sometimes it is necessary to set a NavigationNode on the SubModuleController (if things like navigation or marker have to be tested). E.g.:

@Override
protected MarkerSubModuleController createController(ISubModuleNode node) {
  MarkerSubModuleController newInst = new MarkerSubModuleController();
  node.setNodeId(new NavigationNodeId("org.eclipse.riena.example.marker"));
  newInst.setNavigationNode(node);
  return newInst;
}

Testing scenarios

This section covers some in-depth examples on how to test your Controllers. More examples can be found in org.eclipse.riena.client.controller.test.

Testing Ridget behavior

The most basic things you might want to test is the logic behind Ridgets. A Ridget can be accessed by calling getController().getRidget(Class<R> ridgetClazz, String id). In addition to the normal methods that can be called on a Ridget, there are some special features for Ridgets in a Controller Test:

  • fireAction() in IActionRidget: simulates the pushing of a Button
  • triggerListener() in ITraverseRidget: simulates the "dragging" (ScaleRidget) or "clicking" (SpinnerRidget) on a TraverseRidget.


Here is an example on how to test simple correlations between Ridgets (see ChoiceSubModuleController from the Riena Example Client):

public void testPriceAstonMartinWithOptions() {
    ISingleChoiceRidget compositeCarModel = getController().getRidget(ISingleChoiceRidget.class, "compositeCarModel");
    IMultipleChoiceRidget compositeCarExtras = getController().getRidget(IMultipleChoiceRidget.class, "compositeCarExtras");
 
    compositeCarModel.setSelection(CarModels.ASTON_MARTIN);
    compositeCarExtras.setSelection(Arrays.asList(CarOptions.FRONT_GUNS, CarOptions.UNDERWATER));
 
    assertEquals(compositeCarModel.getSelection(), CarModels.ASTON_MARTIN);
    assertEquals(compositeCarExtras.getSelection().size(), 2);
    assertEquals(compositeCarExtras.getSelection().get(0), CarOptions.FRONT_GUNS);
    assertEquals(compositeCarExtras.getSelection().get(1), CarOptions.UNDERWATER);
 
    assertEquals(getController().getCarConfig().getPrice(), 150000);
}

Testing the navigation

Another interesting behavior to test is the navigation between SubModuleControllers. Therefore the AbstractSubModuleControllerTest provides a mockNavigationProcessor that can be used to test several navigation conditions.

Testing a simple navigation

This example shows how to test whether a navigate command was executed by pressing a button.

  1. Call "navigate" on the mockNavigationProcessor to record it with the proper parameters. In this case the button should navigate to the SubModuleController "comboAndList" with no NavigationArgument.
  2. Replay the Mock.
  3. Get the button responsible for the navigation event and call fireAction() on it.
  4. Verify the Mock.
public void testNavigateCombo() {
  getMockNavigationProcessor().navigate(EasyMock.eq(getController().getNavigationNode()),
    EasyMock.eq(new NavigationNodeId("org.eclipse.riena.example.navigate.comboAndList")), (NavigationArgument) EasyMock.isNull());
 
  EasyMock.replay(getMockNavigationProcessor());
 
  IActionRidget navigateToComboButton = getController().getRidget(IActionRidget.class, "comboAndList");
  navigateToComboButton.fireAction();
 
  EasyMock.verify(getMockNavigationProcessor());
}

Testing the navigation to a specific Ridget with a NavigationArgument

Often it is necessary to navigate to a specific Ridget or even transfer an Object to it by using a NavigationArgument. Here are two examples on how to test if the NavigationArgument is the right one.

public void testNavigateToRidgetWithNotNull() {
  getMockNavigationProcessor().navigate(EasyMock.eq(getController().getNavigationNode()), EasyMock.eq(new NavigationNodeId("org.eclipse.riena.example.combo")),
    new NavigationArgument(EasyMock.notNull(), "textFirst"));
 
  EasyMock.replay(getMockNavigationProcessor());
  IActionRidget navigateToNavigateRidget = getController().getRidget(IActionRidget.class, "btnNavigateToRidget");
  navigateToNavigateRidget.fireAction();
  EasyMock.verify(getMockNavigationProcessor());
}
public void testNavigateToRidgetWithCompare() {
  PersonModificationBean bean = new PersonModificationBean();
  bean.setPerson(new Person("Doe", "Jane"));
 
  getMockNavigationProcessor().navigate(EasyMock.eq(getController().getNavigationNode()),
    EasyMock.eq(new NavigationNodeId("org.eclipse.riena.example.combo")),
    EasyMock.cmp(new NavigationArgument(bean, "textFirst"), new Comparator<NavigationArgument>() {
 
      public int compare(NavigationArgument o1, NavigationArgument o2) {
        if (o1.getParameter() instanceof PersonModificationBean && o2.getParameter() instanceof PersonModificationBean) {
          return comparePersonModificationBeans((PersonModificationBean) o1.getParameter(), (PersonModificationBean) o2.getParameter());
        } else {
          return -1;
        }
      }
 
    }, LogicalOperator.EQUAL));
 
  EasyMock.replay(getMockNavigationProcessor());
 
  IActionRidget navigateToNavigateRidget = getController().getRidget(IActionRidget.class, "btnNavigateToRidget");
  navigateToNavigateRidget.fireAction();
 
  EasyMock.verify(getMockNavigationProcessor());
}

Back to the top