use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WSelectToggle_Test method testHandleRequest.
@Test
public void testHandleRequest() {
// Create a target panel containing a variety of "selectable" components.
WCheckBox checkBox = new WCheckBox();
WCheckBoxSelect checkBoxSelect = new WCheckBoxSelect(new String[] { "a", "b", "c" });
WMultiSelect multiSelectList = new WMultiSelect(new String[] { "a", "b", "c" });
WDataTable dataTable = new WDataTable();
dataTable.setSelectMode(WDataTable.SelectMode.MULTIPLE);
dataTable.addColumn(new WTableColumn("dummy", WText.class));
dataTable.setDataModel(new MyDataModel());
WPanel targetPanel = new WPanel();
targetPanel.add(checkBox);
targetPanel.add(checkBoxSelect);
targetPanel.add(multiSelectList);
targetPanel.add(dataTable);
// Set some default selections, to ensure that existing selections are not toggled.
checkBoxSelect.setSelected(Arrays.asList(new String[] { "a" }));
multiSelectList.setSelected(Arrays.asList(new String[] { "b" }));
dataTable.setSelectedRows(Arrays.asList(new Integer[] { 2 }));
// Set up the toggle to be tested
WSelectToggle toggle = new WSelectToggle(false, targetPanel);
setActiveContext(createUIContext());
MockRequest request = new MockRequest();
// Check select all
request.setParameter(toggle.getId(), "all");
toggle.serviceRequest(request);
Assert.assertTrue("Checkbox should be checked", checkBox.isSelected());
Assert.assertEquals("CheckboxSelect should have all items checked", 3, checkBoxSelect.getSelected().size());
Assert.assertEquals("List should have all items selected", 3, multiSelectList.getSelected().size());
Assert.assertEquals("Table should have all rows selected", 3, dataTable.getSelectedRows().size());
// Check select none
request.setParameter(toggle.getId(), "none");
toggle.serviceRequest(request);
Assert.assertFalse("Checkbox should not be checked", checkBox.isSelected());
Assert.assertEquals("CheckboxSelect should have no items checked", 0, checkBoxSelect.getSelected().size());
Assert.assertEquals("List should have no items selected", 0, multiSelectList.getSelected().size());
Assert.assertEquals("Table should have no rows selected", 0, dataTable.getSelectedRows().size());
// Check that nothing happens when disabled
toggle.setDisabled(true);
request.setParameter(toggle.getId(), "all");
toggle.serviceRequest(request);
Assert.assertFalse("Checkbox should not be checked", checkBox.isSelected());
Assert.assertEquals("CheckboxSelect should have no items checked", 0, checkBoxSelect.getSelected().size());
Assert.assertEquals("List should have no items selected", 0, multiSelectList.getSelected().size());
Assert.assertEquals("Table should have no rows selected", 0, dataTable.getSelectedRows().size());
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WImage_Test method testHandleRequest.
@Test
public void testHandleRequest() throws IOException {
byte[] data = "WImage_Test.testHandleRequest".getBytes(CHAR_ENCODING);
MockRequest request = new MockRequest();
MockImage content = new MockImage();
content.setBytes(data);
WImage image = new WImage();
image.setLocked(true);
setActiveContext(createUIContext());
image.setImage(content);
// Should not do anything when target is not present
image.handleRequest(request);
try {
request.setParameter(Environment.TARGET_ID, image.getTargetId());
image.handleRequest(request);
Assert.fail("Should have thrown a content escape");
} catch (ContentEscape escape) {
MockResponse response = new MockResponse();
escape.setResponse(response);
escape.escape();
String output = new String(response.getOutput(), CHAR_ENCODING);
Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output);
Assert.assertFalse("Cache flag should not be set", escape.isCacheable());
Assert.assertEquals("Response should have header set for no caching", ConfigurationProperties.RESPONSE_DEFAULT_NO_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
// Test Cached Response
image.setCacheKey("key");
// Should produce the content with cache flag set
try {
image.handleRequest(request);
Assert.fail("Should have thrown a content escape");
} catch (ContentEscape escape) {
MockResponse response = new MockResponse();
escape.setResponse(response);
escape.escape();
String output = new String(response.getOutput(), CHAR_ENCODING);
Assert.assertEquals("Incorrect content returned", new String(data, CHAR_ENCODING), output);
Assert.assertTrue("Cache flag should be set", escape.isCacheable());
Assert.assertEquals("Response should have header set for caching", ConfigurationProperties.RESPONSE_DEFAULT_CACHE_SETTINGS, response.getHeaders().get("Cache-Control"));
}
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WMenuItem_Test method testHandleRequest.
@Test
public void testHandleRequest() {
TestAction action = new TestAction();
WMenu menu = new WMenu();
WMenuItem item = new WMenuItem("", action);
menu.add(item);
menu.setLocked(true);
// Menu not in request
setActiveContext(createUIContext());
MockRequest request = new MockRequest();
menu.serviceRequest(request);
Assert.assertFalse("Action should not have been called when item was not selected", action.wasTriggered());
// Menu in request, but item not selected
request = new MockRequest();
request.setParameter(menu.getId() + "-h", "x");
menu.serviceRequest(request);
Assert.assertFalse("Action should not have been called when item was not selected", action.wasTriggered());
// Menu in request and item selected
request = new MockRequest();
request.setParameter(menu.getId() + "-h", "x");
request.setParameter(item.getId(), "x");
menu.serviceRequest(request);
Assert.assertTrue("Action should have been called when item is selected", action.wasTriggered());
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WDateField_Test method getRequestValueLenient.
@Test
public void getRequestValueLenient() throws ParseException {
WDateField dateField = new WDateField(true);
dateField.setLocked(true);
setActiveContext(createUIContext());
// Request with invalid value but passes with lenient (should return date)
MockRequest request = new MockRequest();
request.setParameter(dateField.getId(), LENIENT_VALID_USER_TEXT);
request.setParameter(dateField.getId() + "-date", LENIENT_VALID_INTERNAL_DATE_TEXT);
Assert.assertEquals("Value from request should have been returned when lenient", LENIENT_VALID_DATE_VALUE, dateField.getRequestValue(request));
}
use of com.github.bordertech.wcomponents.util.mock.MockRequest in project wcomponents by BorderTech.
the class WDateField_Test method processDoHandleRequestWithBadThemeDate.
/**
* @return a datefield processed with a request with a bad theme date
*/
private WDateField processDoHandleRequestWithBadThemeDate() {
resetContext();
WDateField dateField = new WDateField();
String dateFieldId = dateField.getId();
dateField.setLocked(true);
// Valid DateType
setActiveContext(createUIContext());
MockRequest request = new MockRequest();
request.setParameter(dateFieldId, REQUEST_THEME_BAD_DATE_USER_TEXT);
request.setParameter(dateFieldId + "-date", REQUEST_THEME_BAD_INTERNAL_DATE_VALUE);
dateField.doHandleRequest(request);
return dateField;
}
Aggregations