Search in sources :

Example 1 with NonDisplayableMenu

use of org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu in project scout.rt by eclipse.

the class JsonMenuTest method testDontSendNonDisplayableMenus.

/**
 * Tests whether non displayable menus are sent.
 * <p>
 * This reduces response size and also leverages security because the menus are never visible to the user, not even
 * with the dev tools of the browser
 */
@Test
public void testDontSendNonDisplayableMenus() throws Exception {
    IMenu menu = new MenuWithNonDisplayableChild();
    ActionUtility.initActions(CollectionUtility.arrayList(menu));
    JsonMenu<IMenu> jsonMenu = UiSessionTestUtility.newJsonAdapter(m_uiSession, menu, null);
    JsonMenu<IMenu> jsonDisplayableMenu = jsonMenu.getAdapter(new ActionFinder().findAction(menu.getChildActions(), MenuWithNonDisplayableChild.DisplayableMenu.class));
    JsonMenu<IMenu> jsonNonDisplayableMenu = jsonMenu.getAdapter(new ActionFinder().findAction(menu.getChildActions(), MenuWithNonDisplayableChild.NonDisplayableMenu.class));
    // Adapter for NonDisplayableMenu must not exist
    assertNull(jsonNonDisplayableMenu);
    // Json response must not contain NonDisplayableMenu
    JSONObject json = jsonMenu.toJson();
    JSONArray jsonMenus = json.getJSONArray("childActions");
    assertEquals(1, jsonMenus.length());
    assertEquals(jsonDisplayableMenu.getId(), jsonMenus.get(0));
}
Also used : IMenu(org.eclipse.scout.rt.client.ui.action.menu.IMenu) NonDisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu) DisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.DisplayableMenu) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) MenuWithNonDisplayableChild(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild) NonDisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu) ActionFinder(org.eclipse.scout.rt.client.ui.action.ActionFinder) Test(org.junit.Test)

Example 2 with NonDisplayableMenu

use of org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu in project scout.rt by eclipse.

the class JsonMenuTest method testDontSendNonDisplayableMenusSpecialCase.

/**
 * Special case: Menu is attached, then it is set to visibleGranted=false, then sent to the UI, then a property change
 * event occurs on that menu. We expect, that the adapter is automatically disposed again when setVisibleGranted=false
 * is called, because the response is still writable.
 */
@Test
public void testDontSendNonDisplayableMenusSpecialCase() throws Exception {
    IMenu menu = new MenuWithNonDisplayableChild();
    DisplayableMenu displayableMenu = new ActionFinder().findAction(menu.getChildActions(), MenuWithNonDisplayableChild.DisplayableMenu.class);
    NonDisplayableMenu nonDisplayableMenu = new ActionFinder().findAction(menu.getChildActions(), MenuWithNonDisplayableChild.NonDisplayableMenu.class);
    JsonMenu<IMenu> jsonMenu = UiSessionTestUtility.newJsonAdapter(m_uiSession, menu, null);
    JsonMenu<IMenu> jsonDisplayableMenu = jsonMenu.getAdapter(displayableMenu);
    JsonMenu<IMenu> jsonNonDisplayableMenu = jsonMenu.getAdapter(nonDisplayableMenu);
    // Both adapters exist
    assertNotNull(jsonDisplayableMenu);
    assertNotNull(jsonNonDisplayableMenu);
    // After attachment of adapter!
    ActionUtility.initActions(CollectionUtility.arrayList(menu));
    jsonDisplayableMenu = jsonMenu.getAdapter(displayableMenu);
    jsonNonDisplayableMenu = jsonMenu.getAdapter(nonDisplayableMenu);
    // Now only one adapter exists anymore
    assertNotNull(jsonDisplayableMenu);
    assertNull(jsonNonDisplayableMenu);
    // Check that no traces of the invisible menu are in JSON
    JSONObject json = m_uiSession.currentJsonResponse().toJson();
    assertEquals(1, json.getJSONObject("adapterData").length());
    assertNotNull(json.getJSONObject("adapterData").getJSONObject(jsonDisplayableMenu.getId()));
    // --------
    // Property change on invisible menu -> must not trigger anything in the JSON layer
    JsonTestUtility.endRequest(m_uiSession);
    nonDisplayableMenu.setTooltipText("Test-Tooltip");
    json = m_uiSession.currentJsonResponse().toJson();
    assertFalse(json.has("adapterData"));
    assertFalse(json.has("events"));
    // Property change on visible menu -> triggers event
    JsonTestUtility.endRequest(m_uiSession);
    displayableMenu.setTooltipText("Test-Tooltip");
    json = m_uiSession.currentJsonResponse().toJson();
    assertFalse(json.has("adapterData"));
    assertEquals(1, json.getJSONArray("events").length());
    // --------------------------------
    // Test case where the menu is already sent to the UI, then setVisibleGranted=false (this is too late, but it will only trigger a warning in the log)
    JsonTestUtility.endRequest(m_uiSession);
    displayableMenu.setVisibleGranted(false);
    IJsonAdapter<?> jsonDisplayableMenu2 = jsonMenu.getAdapter(displayableMenu);
    assertSame(jsonDisplayableMenu, jsonDisplayableMenu2);
    json = m_uiSession.currentJsonResponse().toJson();
    assertFalse(json.has("adapterData"));
    assertEquals(1, json.getJSONArray("events").length());
}
Also used : IMenu(org.eclipse.scout.rt.client.ui.action.menu.IMenu) NonDisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu) DisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.DisplayableMenu) JSONObject(org.json.JSONObject) MenuWithNonDisplayableChild(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild) NonDisplayableMenu(org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu) ActionFinder(org.eclipse.scout.rt.client.ui.action.ActionFinder) Test(org.junit.Test)

Aggregations

ActionFinder (org.eclipse.scout.rt.client.ui.action.ActionFinder)2 IMenu (org.eclipse.scout.rt.client.ui.action.menu.IMenu)2 MenuWithNonDisplayableChild (org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild)2 DisplayableMenu (org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.DisplayableMenu)2 NonDisplayableMenu (org.eclipse.scout.rt.ui.html.json.menu.fixtures.MenuWithNonDisplayableChild.NonDisplayableMenu)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2 JSONArray (org.json.JSONArray)1