public void testExtensionContributionExpression() throws Exception {
    IAction a =
        new Action() {
          @Override
          public void run() {
            System.out.println("Hello action");
          }
        };
    final MenuManager manager = new MenuManager();
    final ActionContributionItem aci = new ActionContributionItem(a);

    IExtensionRegistry reg = Platform.getExtensionRegistry();
    IExtensionPoint menusExtension = reg.getExtensionPoint("org.eclipse.ui.menus");
    IExtension extension = menusExtension.getExtension(EXTENSION_ID);

    IConfigurationElement[] mas = extension.getConfigurationElements();
    final Expression activeContextExpr[] = new Expression[1];
    for (IConfigurationElement ma : mas) {
      IConfigurationElement[] items = ma.getChildren();
      for (IConfigurationElement item : items) {
        String id = item.getAttribute("id");
        if (id != null && id.equals("org.eclipse.ui.tests.menus.itemX1")) {
          IConfigurationElement visibleWhenElement = item.getChildren("visibleWhen")[0];
          activeContextExpr[0] =
              ExpressionConverter.getDefault().perform(visibleWhenElement.getChildren()[0]);
        }
      }
    }
    assertNotNull("Failed to find expression", activeContextExpr[0]);
    AbstractContributionFactory factory =
        new AbstractContributionFactory(LOCATION, TestPlugin.PLUGIN_ID) {
          @Override
          public void createContributionItems(
              IServiceLocator menuService, IContributionRoot additions) {
            additions.addContributionItem(aci, activeContextExpr[0]);
          }
        };

    menuService.addContributionFactory(factory);
    menuService.populateContributionManager(manager, LOCATION);

    assertFalse("starting state", aci.isVisible());

    activeContext = contextService.activateContext(MenuContributionHarness.CONTEXT_TEST1_ID);
    final Menu menu = manager.createContextMenu(window.getShell());
    menu.notifyListeners(SWT.Show, new Event());
    assertTrue("active context", aci.isVisible());
    menu.notifyListeners(SWT.Hide, new Event());

    contextService.deactivateContext(activeContext);
    activeContext = null;

    menu.notifyListeners(SWT.Show, new Event());
    assertFalse("after deactivation", aci.isVisible());
    menu.notifyListeners(SWT.Hide, new Event());

    menuService.releaseContributions(manager);
    menuService.removeContributionFactory(factory);
    manager.dispose();
  }
  public void testBasicContribution() throws Exception {

    IAction a =
        new Action() {
          @Override
          public void run() {
            System.out.println("Hello action");
          }
        };
    final MenuManager manager = new MenuManager();
    final ActionContributionItem item = new ActionContributionItem(a);
    final Expression activeContextExpr =
        new ActiveContextExpression(
            MenuContributionHarness.CONTEXT_TEST1_ID, new String[] {ISources.ACTIVE_CONTEXT_NAME});
    AbstractContributionFactory factory =
        new AbstractContributionFactory(LOCATION, TestPlugin.PLUGIN_ID) {
          @Override
          public void createContributionItems(
              IServiceLocator menuService, IContributionRoot additions) {
            additions.addContributionItem(item, activeContextExpr);
          }
        };

    menuService.addContributionFactory(factory);
    menuService.populateContributionManager(manager, LOCATION);

    Shell shell = window.getShell();

    // Test the initial menu creation
    final Menu menuBar = manager.createContextMenu(shell);
    Event e = new Event();
    e.type = SWT.Show;
    e.widget = menuBar;
    menuBar.notifyListeners(SWT.Show, e);

    assertFalse("starting state", item.isVisible());

    activeContext = contextService.activateContext(MenuContributionHarness.CONTEXT_TEST1_ID);
    menuBar.notifyListeners(SWT.Show, e);

    assertTrue("active context", item.isVisible());

    contextService.deactivateContext(activeContext);
    activeContext = null;
    menuBar.notifyListeners(SWT.Show, e);

    assertFalse("after deactivation", item.isVisible());

    menuService.releaseContributions(manager);
    menuService.removeContributionFactory(factory);
    manager.dispose();
  }
 private static MenuItem show(final Menu menu, final Matcher<?> matcher) {
   if (menu != null) {
     menu.notifyListeners(SWT.Show, new Event());
     MenuItem[] items = menu.getItems();
     for (final MenuItem menuItem : items) {
       if (matcher.matches(menuItem)) {
         return menuItem;
       }
     }
     menu.notifyListeners(SWT.Hide, new Event());
   }
   return null;
 }
  public void testCreateMenu() {
    final MWindow window = createWindowWithOneViewAndMenu();
    wb = new E4Workbench(window, appContext);

    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    final Menu menuBar = shell.getMenuBar();
    assertNotNull(menuBar);
    assertEquals(1, menuBar.getItemCount());
    final MenuItem fileItem = menuBar.getItem(0);
    assertEquals("File", fileItem.getText());
    final Menu fileMenu = fileItem.getMenu();
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    MMenu mainMenu = window.getMainMenu();
    MMenu modelFileMenu = (MMenu) mainMenu.getChildren().get(0);
    final MMenuItem item2Model = (MMenuItem) modelFileMenu.getChildren().get(0);
    item2Model.setToBeRendered(false);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(1, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);

    item2Model.setToBeRendered(true);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
  }
 private static void hide(final Menu menu) {
   menu.notifyListeners(SWT.Hide, new Event());
   if (menu.getParentMenu() != null) {
     hide(menu.getParentMenu());
   }
 }