You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractComponentTest.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. package com.vaadin.tests.components;
  2. import java.util.EnumSet;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import java.util.Map;
  9. import java.util.Set;
  10. import com.vaadin.event.FieldEvents.BlurEvent;
  11. import com.vaadin.event.FieldEvents.BlurListener;
  12. import com.vaadin.event.FieldEvents.BlurNotifier;
  13. import com.vaadin.event.FieldEvents.FocusEvent;
  14. import com.vaadin.event.FieldEvents.FocusListener;
  15. import com.vaadin.event.FieldEvents.FocusNotifier;
  16. import com.vaadin.server.DefaultErrorHandler;
  17. import com.vaadin.server.Resource;
  18. import com.vaadin.server.ThemeResource;
  19. import com.vaadin.tests.util.Log;
  20. import com.vaadin.tests.util.LoremIpsum;
  21. import com.vaadin.ui.AbstractComponent;
  22. import com.vaadin.ui.Component.Focusable;
  23. import com.vaadin.ui.MenuBar;
  24. import com.vaadin.ui.MenuBar.MenuItem;
  25. import com.vaadin.ui.themes.BaseTheme;
  26. public abstract class AbstractComponentTest<T extends AbstractComponent>
  27. extends AbstractComponentTestCase<T> implements FocusListener,
  28. BlurListener {
  29. protected static final String TEXT_SHORT = "Short";
  30. protected static final String TEXT_MEDIUM = "This is a semi-long text that might wrap.";
  31. protected static final String TEXT_LONG = "This is a long text. "
  32. + LoremIpsum.get(500);
  33. protected static final String TEXT_VERY_LONG = "This is a very, very long text. "
  34. + LoremIpsum.get(5000);
  35. private static final Resource SELECTED_ICON = new ThemeResource(
  36. "../runo/icons/16/ok.png");
  37. private static final LinkedHashMap<String, String> sizeOptions = new LinkedHashMap<String, String>();
  38. static {
  39. sizeOptions.put("auto", null);
  40. sizeOptions.put("50%", "50%");
  41. sizeOptions.put("100%", "100%");
  42. for (int w = 200; w < 1000; w += 100) {
  43. sizeOptions.put(w + "px", w + "px");
  44. }
  45. }
  46. // Menu related
  47. private MenuItem mainMenu;
  48. private MenuBar menu;
  49. private MenuItem settingsMenu;
  50. private T component;
  51. // Used to determine if a menuItem should be selected and the other
  52. // unselected on click
  53. private Set<MenuItem> parentOfSelectableMenuItem = new HashSet<MenuItem>();
  54. /**
  55. * Maps the category name to a menu item
  56. */
  57. private Map<String, MenuItem> categoryToMenuItem = new HashMap<String, MenuItem>();
  58. private Map<MenuItem, String> menuItemToCategory = new HashMap<MenuItem, String>();
  59. // Logging
  60. private Log log;
  61. protected static final String CATEGORY_STATE = "State";
  62. protected static final String CATEGORY_SIZE = "Size";
  63. protected static final String CATEGORY_SELECTION = "Selection";
  64. protected static final String CATEGORY_LISTENERS = "Listeners";
  65. protected static final String CATEGORY_FEATURES = "Features";
  66. protected static final String CATEGORY_ACTIONS = "Actions";
  67. protected static final String CATEGORY_DECORATIONS = "Decorations";
  68. @Override
  69. protected final void setup() {
  70. setTheme("tests-components");
  71. // Create menu here so it appears before the components
  72. addComponent(createMainMenu());
  73. getLayout().setSizeFull();
  74. createLog();
  75. super.setup();
  76. // Create menu actions and trigger default actions
  77. createActions();
  78. // Clear initialization log messages
  79. log.clear();
  80. }
  81. private MenuBar createMainMenu() {
  82. menu = new MenuBar();
  83. menu.setId("menu");
  84. mainMenu = menu.addItem("Component", null);
  85. settingsMenu = menu.addItem("Settings", null);
  86. populateSettingsMenu(settingsMenu);
  87. return menu;
  88. }
  89. /**
  90. * Override to add items to the "settings" menu.
  91. *
  92. * NOTE, Call super class first to preserve current order. If you override
  93. * this in a class and another class overrides it you might break tests
  94. * because the wrong items will be selected.
  95. *
  96. * @param settingsMenu
  97. */
  98. protected void populateSettingsMenu(MenuItem settingsMenu) {
  99. MenuItem showEventLog = settingsMenu.addItem("Show event log",
  100. new MenuBar.Command() {
  101. @Override
  102. public void menuSelected(MenuItem selectedItem) {
  103. boolean selected = !isSelected(selectedItem);
  104. setLogVisible(selected);
  105. setSelected(selectedItem, selected);
  106. }
  107. });
  108. setSelected(showEventLog, true);
  109. settingsMenu.addItem("Clear log", new MenuBar.Command() {
  110. @Override
  111. public void menuSelected(MenuItem selectedItem) {
  112. log.clear();
  113. }
  114. });
  115. MenuItem layoutSize = settingsMenu.addItem("Parent layout size", null);
  116. MenuItem layoutWidth = layoutSize.addItem("Width", null);
  117. MenuItem layoutHeight = layoutSize.addItem("Height", null);
  118. for (final String name : sizeOptions.keySet()) {
  119. layoutWidth.addItem(name, new MenuBar.Command() {
  120. @Override
  121. public void menuSelected(MenuItem selectedItem) {
  122. getTestComponents().get(0).getParent()
  123. .setWidth(sizeOptions.get(name));
  124. log("Parent layout width set to " + name);
  125. }
  126. });
  127. layoutHeight.addItem(name, new MenuBar.Command() {
  128. @Override
  129. public void menuSelected(MenuItem selectedItem) {
  130. getTestComponents().get(0).getParent()
  131. .setHeight(sizeOptions.get(name));
  132. log("Parent layout height set to " + name);
  133. }
  134. });
  135. }
  136. }
  137. protected void setLogVisible(boolean visible) {
  138. // This is only to be screenshot-compatible with Vaadin 6, where
  139. // invisible components cause spacing
  140. if (visible) {
  141. log.removeStyleName("displaynone");
  142. log.setCaption((String) log.getData());
  143. } else {
  144. log.addStyleName("displaynone");
  145. log.setCaption(null);
  146. }
  147. }
  148. private void createLog() {
  149. log = new Log(5).setNumberLogRows(true);
  150. log.setData(log.getCaption());
  151. log.setStyleName(BaseTheme.CLIP);
  152. getLayout().addComponent(log, 1);
  153. }
  154. /**
  155. * By default initializes just one instance of {@link #getTestClass()} using
  156. * {@link #constructComponent()}.
  157. */
  158. @Override
  159. protected void initializeComponents() {
  160. component = constructComponent();
  161. component.setId("testComponent");
  162. addTestComponent(component);
  163. }
  164. public T getComponent() {
  165. return component;
  166. }
  167. @Override
  168. protected void addTestComponent(T c) {
  169. super.addTestComponent(c);
  170. getLayout().setExpandRatio(c, 1);
  171. }
  172. /**
  173. * Construct the component that is to be tested. This method uses a no-arg
  174. * constructor by default. Override to customize.
  175. *
  176. * @return Instance of the component that is to be tested.
  177. * @throws IllegalAccessException
  178. * @throws InstantiationException
  179. */
  180. protected T constructComponent() {
  181. try {
  182. return getTestClass().newInstance();
  183. } catch (Exception e) {
  184. throw new RuntimeException("Failed to instantiate "
  185. + getTestClass(), e);
  186. }
  187. }
  188. /**
  189. * Create actions for the component. Remember to call super.createActions()
  190. * when overriding.
  191. */
  192. protected void createActions() {
  193. createBooleanAction("Immediate", CATEGORY_STATE, true, immediateCommand);
  194. createBooleanAction("Enabled", CATEGORY_STATE, true, enabledCommand);
  195. createBooleanAction("Readonly", CATEGORY_STATE, false, readonlyCommand);
  196. createBooleanAction("Visible", CATEGORY_STATE, true, visibleCommand);
  197. createBooleanAction("Error indicator", CATEGORY_STATE, false,
  198. errorIndicatorCommand);
  199. createLocaleSelect(CATEGORY_STATE);
  200. createErrorMessageSelect(CATEGORY_DECORATIONS);
  201. createDescriptionSelect(CATEGORY_DECORATIONS);
  202. createCaptionSelect(CATEGORY_DECORATIONS);
  203. createIconSelect(CATEGORY_DECORATIONS);
  204. createWidthAndHeightActions(CATEGORY_SIZE);
  205. createStyleNameSelect(CATEGORY_DECORATIONS);
  206. createFocusActions();
  207. }
  208. protected Command<T, Boolean> focusListenerCommand = new Command<T, Boolean>() {
  209. @Override
  210. public void execute(T c, Boolean value, Object data) {
  211. FocusNotifier fn = (FocusNotifier) c;
  212. if (value) {
  213. fn.addFocusListener(AbstractComponentTest.this);
  214. } else {
  215. fn.removeFocusListener(AbstractComponentTest.this);
  216. }
  217. }
  218. };
  219. protected Command<T, Boolean> blurListenerCommand = new Command<T, Boolean>() {
  220. @Override
  221. public void execute(T c, Boolean value, Object data) {
  222. BlurNotifier bn = (BlurNotifier) c;
  223. if (value) {
  224. bn.addBlurListener(AbstractComponentTest.this);
  225. } else {
  226. bn.removeBlurListener(AbstractComponentTest.this);
  227. }
  228. }
  229. };
  230. protected void createFocusListener(String category) {
  231. createBooleanAction("Focus listener", category, false,
  232. focusListenerCommand);
  233. }
  234. protected void createBlurListener(String category) {
  235. createBooleanAction("Blur listener", category, false,
  236. blurListenerCommand);
  237. }
  238. private void createFocusActions() {
  239. if (FocusNotifier.class.isAssignableFrom(getTestClass())) {
  240. createFocusListener(CATEGORY_LISTENERS);
  241. }
  242. if (BlurNotifier.class.isAssignableFrom(getTestClass())) {
  243. createBlurListener(CATEGORY_LISTENERS);
  244. }
  245. if (Focusable.class.isAssignableFrom(getTestClass())) {
  246. LinkedHashMap<String, Integer> tabIndexes = new LinkedHashMap<String, Integer>();
  247. tabIndexes.put("0", 0);
  248. tabIndexes.put("-1", -1);
  249. tabIndexes.put("10", 10);
  250. createSelectAction("Tab index", "State", tabIndexes, "0",
  251. new Command<T, Integer>() {
  252. @Override
  253. public void execute(T c, Integer tabIndex, Object data) {
  254. ((Focusable) c).setTabIndex(tabIndex);
  255. }
  256. });
  257. createClickAction("Set focus", "State", new Command<T, Void>() {
  258. @Override
  259. public void execute(T c, Void value, Object data) {
  260. ((Focusable) c).focus();
  261. }
  262. }, null);
  263. }
  264. }
  265. private void createStyleNameSelect(String category) {
  266. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  267. options.put("-", null);
  268. options.put("Light blue background (background-lightblue)",
  269. "background-lightblue");
  270. options.put("1px red border (border-red-1px)", "border-red-1px");
  271. options.put("2px blue border (border-blue-2px)", "border-blue-2px");
  272. createComponentStyleNames(options);
  273. createSelectAction("Style name", category, options, "-",
  274. styleNameCommand);
  275. }
  276. protected void createComponentStyleNames(
  277. LinkedHashMap<String, String> options) {
  278. }
  279. private void createErrorMessageSelect(String category) {
  280. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  281. options.put("-", null);
  282. options.put(TEXT_SHORT, TEXT_SHORT);
  283. options.put("Medium", TEXT_MEDIUM);
  284. options.put("Long", TEXT_LONG);
  285. options.put("Very long", TEXT_VERY_LONG);
  286. createSelectAction("Error message", category, options, "-",
  287. errorMessageCommand);
  288. }
  289. private void createDescriptionSelect(String category) {
  290. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  291. options.put("-", null);
  292. options.put(TEXT_SHORT, TEXT_SHORT);
  293. options.put("Medium", TEXT_MEDIUM);
  294. options.put("Long", TEXT_LONG);
  295. options.put("Very long", TEXT_VERY_LONG);
  296. createSelectAction("Description / tooltip", category, options, "-",
  297. descriptionCommand);
  298. }
  299. private void createCaptionSelect(String category) {
  300. createSelectAction("Caption", category, createCaptionOptions(),
  301. "Short", captionCommand);
  302. }
  303. protected LinkedHashMap<String, String> createCaptionOptions() {
  304. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  305. options.put("-", null);
  306. options.put("Short", TEXT_SHORT);
  307. options.put("Medium", TEXT_MEDIUM);
  308. options.put("Long", TEXT_LONG);
  309. options.put("Very long", TEXT_VERY_LONG);
  310. return options;
  311. }
  312. private void createWidthAndHeightActions(String category) {
  313. String widthCategory = "Width";
  314. String heightCategory = "Height";
  315. createCategory(widthCategory, category);
  316. createCategory(heightCategory, category);
  317. for (String name : sizeOptions.keySet()) {
  318. createClickAction(name, widthCategory, widthCommand,
  319. sizeOptions.get(name));
  320. createClickAction(name, heightCategory, heightCommand,
  321. sizeOptions.get(name));
  322. }
  323. // Default to undefined size
  324. for (T c : getTestComponents()) {
  325. c.setWidth(null);
  326. c.setHeight(null);
  327. }
  328. }
  329. private void createIconSelect(String category) {
  330. LinkedHashMap<String, Resource> options = new LinkedHashMap<String, Resource>();
  331. options.put("-", null);
  332. options.put("16x16", ICON_16_USER_PNG_CACHEABLE);
  333. options.put("32x32", ICON_32_ATTENTION_PNG_CACHEABLE);
  334. options.put("64x64", ICON_64_EMAIL_REPLY_PNG_CACHEABLE);
  335. createSelectAction("Icon", category, options, "-", iconCommand, null);
  336. }
  337. private void createLocaleSelect(String category) {
  338. LinkedHashMap<String, Locale> options = new LinkedHashMap<String, Locale>();
  339. options.put("-", null);
  340. options.put("fi_FI", new Locale("fi", "FI"));
  341. options.put("en_US", Locale.US);
  342. options.put("zh_CN", Locale.SIMPLIFIED_CHINESE);
  343. options.put("fr_FR", Locale.FRANCE);
  344. createSelectAction("Locale", category, options, "en_US", localeCommand,
  345. null);
  346. }
  347. protected void createBooleanAction(String caption, String category,
  348. boolean initialState, final Command<T, Boolean> command) {
  349. createBooleanAction(caption, category, initialState, command, null);
  350. }
  351. protected <DATATYPE> void createBooleanAction(String caption,
  352. String category, boolean initialState,
  353. final Command<T, Boolean> command, Object data) {
  354. MenuItem categoryItem = getCategoryMenuItem(category);
  355. MenuItem item = categoryItem.addItem(caption,
  356. menuBooleanCommand(command, data));
  357. setSelected(item, initialState);
  358. doCommand(caption, command, initialState, data);
  359. }
  360. protected <DATATYPE> void createClickAction(String caption,
  361. String category, final Command<T, DATATYPE> command, DATATYPE value) {
  362. createClickAction(caption, category, command, value, null);
  363. }
  364. protected <DATATYPE> void createClickAction(String caption,
  365. String category, final Command<T, DATATYPE> command,
  366. DATATYPE value, Object data) {
  367. MenuItem categoryItem = getCategoryMenuItem(category);
  368. categoryItem.addItem(caption, menuClickCommand(command, value, data));
  369. }
  370. private MenuItem getCategoryMenuItem(String category) {
  371. if (category == null) {
  372. return getCategoryMenuItem("Misc");
  373. }
  374. MenuItem item = categoryToMenuItem.get(category);
  375. if (item != null) {
  376. return item;
  377. }
  378. return createCategory(category, null);
  379. }
  380. /**
  381. * Creates category named "category" with id "categoryId" in parent category
  382. * "parentCategory". Each categoryId must be globally unique.
  383. *
  384. * @param category
  385. * @param categoryId
  386. * @param parentCategory
  387. * @return
  388. */
  389. protected MenuItem createCategory(String category, String parentCategory) {
  390. if (hasCategory(category)) {
  391. return categoryToMenuItem.get(category);
  392. }
  393. MenuItem item;
  394. if (parentCategory == null) {
  395. item = mainMenu.addItem(category, null);
  396. } else {
  397. item = getCategoryMenuItem(parentCategory).addItem(category, null);
  398. }
  399. categoryToMenuItem.put(category, item);
  400. menuItemToCategory.put(item, category);
  401. return item;
  402. }
  403. protected boolean hasCategory(String categoryId) {
  404. return categoryToMenuItem.containsKey(categoryId);
  405. }
  406. protected void removeCategory(String categoryId) {
  407. if (!hasCategory(categoryId)) {
  408. throw new IllegalArgumentException("Category '" + categoryId
  409. + "' does not exist");
  410. }
  411. MenuItem item = getCategoryMenuItem(categoryId);
  412. Object[] children = item.getChildren().toArray();
  413. for (Object child : children) {
  414. if (menuItemToCategory.containsKey(child)) {
  415. removeCategory(menuItemToCategory.get(child));
  416. }
  417. }
  418. // Detach from parent
  419. item.getParent().removeChild(item);
  420. // Clean mappings
  421. categoryToMenuItem.remove(categoryId);
  422. menuItemToCategory.remove(item);
  423. }
  424. private MenuBar.Command menuBooleanCommand(
  425. final com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> booleanCommand,
  426. final Object data) {
  427. return new MenuBar.Command() {
  428. @Override
  429. public void menuSelected(MenuItem selectedItem) {
  430. boolean selected = !isSelected(selectedItem);
  431. doCommand(getText(selectedItem), booleanCommand, selected, data);
  432. setSelected(selectedItem, selected);
  433. }
  434. };
  435. }
  436. private <DATATYPE> MenuBar.Command menuClickCommand(
  437. final com.vaadin.tests.components.ComponentTestCase.Command<T, DATATYPE> command,
  438. final DATATYPE value, final Object data) {
  439. return new MenuBar.Command() {
  440. @Override
  441. public void menuSelected(MenuItem selectedItem) {
  442. doCommand(getText(selectedItem), command, value, data);
  443. }
  444. };
  445. }
  446. protected void setSelected(MenuItem item, boolean selected) {
  447. if (selected) {
  448. item.setIcon(SELECTED_ICON);
  449. } else {
  450. item.setIcon(null);
  451. }
  452. }
  453. protected boolean isSelected(MenuItem item) {
  454. return (item.getIcon() != null);
  455. }
  456. private <VALUETYPE> MenuBar.Command singleSelectMenuCommand(
  457. final com.vaadin.tests.components.ComponentTestCase.Command<T, VALUETYPE> cmd,
  458. final VALUETYPE object, final Object data) {
  459. return new MenuBar.Command() {
  460. @Override
  461. public void menuSelected(MenuItem selectedItem) {
  462. doCommand(getText(selectedItem), cmd, object, data);
  463. if (parentOfSelectableMenuItem.contains(selectedItem
  464. .getParent())) {
  465. unselectChildren(selectedItem.getParent());
  466. setSelected(selectedItem, true);
  467. }
  468. }
  469. };
  470. }
  471. /**
  472. * Unselect all child menu items
  473. *
  474. * @param parent
  475. */
  476. protected void unselectChildren(MenuItem parent) {
  477. List<MenuItem> children = parent.getChildren();
  478. if (children == null) {
  479. return;
  480. }
  481. for (MenuItem child : children) {
  482. setSelected(child, false);
  483. }
  484. }
  485. protected String getText(MenuItem item) {
  486. String path = "";
  487. MenuItem parent = item.getParent();
  488. while (!isCategory(parent)) {
  489. path = parent.getText() + "/" + path;
  490. parent = parent.getParent();
  491. }
  492. return path + "/" + item.getText();
  493. }
  494. private boolean isCategory(MenuItem item) {
  495. return item.getParent() == mainMenu;
  496. }
  497. protected <TYPE> void createSelectAction(
  498. String caption,
  499. String category,
  500. LinkedHashMap<String, TYPE> options,
  501. String initialValue,
  502. com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command) {
  503. createSelectAction(caption, category, options, initialValue, command,
  504. null);
  505. }
  506. protected <TYPE extends Enum<TYPE>> void createSelectAction(
  507. String caption,
  508. String category,
  509. Class<TYPE> enumType,
  510. TYPE initialValue,
  511. com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command) {
  512. LinkedHashMap<String, TYPE> options = new LinkedHashMap<String, TYPE>();
  513. for (TYPE value : EnumSet.allOf(enumType)) {
  514. options.put(value.toString(), value);
  515. }
  516. createSelectAction(caption, category, options, initialValue.toString(),
  517. command);
  518. }
  519. protected <TYPE> void createMultiClickAction(
  520. String caption,
  521. String category,
  522. LinkedHashMap<String, TYPE> options,
  523. com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command,
  524. Object data) {
  525. MenuItem categoryItem = getCategoryMenuItem(category);
  526. MenuItem mainItem = categoryItem.addItem(caption, null);
  527. for (String option : options.keySet()) {
  528. MenuBar.Command cmd = menuClickCommand(command,
  529. options.get(option), data);
  530. mainItem.addItem(option, cmd);
  531. }
  532. }
  533. protected <TYPE> void createMultiToggleAction(
  534. String caption,
  535. String category,
  536. LinkedHashMap<String, TYPE> options,
  537. com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> command,
  538. boolean defaultValue) {
  539. LinkedHashMap<String, Boolean> defaultValues = new LinkedHashMap<String, Boolean>();
  540. for (String option : options.keySet()) {
  541. defaultValues.put(option, defaultValue);
  542. }
  543. createMultiToggleAction(caption, category, options, command,
  544. defaultValues);
  545. }
  546. protected <TYPE> void createMultiToggleAction(
  547. String caption,
  548. String category,
  549. LinkedHashMap<String, TYPE> options,
  550. com.vaadin.tests.components.ComponentTestCase.Command<T, Boolean> command,
  551. LinkedHashMap<String, Boolean> defaultValues) {
  552. createCategory(caption, category);
  553. for (String option : options.keySet()) {
  554. createBooleanAction(option, caption, defaultValues.get(option),
  555. command, options.get(option));
  556. }
  557. }
  558. protected <TYPE> void createSelectAction(
  559. String caption,
  560. String category,
  561. LinkedHashMap<String, TYPE> options,
  562. String initialValue,
  563. com.vaadin.tests.components.ComponentTestCase.Command<T, TYPE> command,
  564. Object data) {
  565. MenuItem parentItem = getCategoryMenuItem(category);
  566. MenuItem mainItem = parentItem.addItem(caption, null);
  567. parentOfSelectableMenuItem.add(mainItem);
  568. for (String option : options.keySet()) {
  569. MenuBar.Command cmd = singleSelectMenuCommand(command,
  570. options.get(option), data);
  571. MenuItem item = mainItem.addItem(option, cmd);
  572. if (option.equals(initialValue)) {
  573. cmd.menuSelected(item);
  574. }
  575. }
  576. }
  577. protected LinkedHashMap<String, Integer> createIntegerOptions(int max) {
  578. LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
  579. for (int i = 0; i <= 9 && i <= max; i++) {
  580. options.put(String.valueOf(i), i);
  581. }
  582. for (int i = 10; i <= max; i *= 10) {
  583. options.put(String.valueOf(i), i);
  584. if (2 * i <= max) {
  585. options.put(String.valueOf(2 * i), 2 * i);
  586. }
  587. if (5 * i <= max) {
  588. options.put(String.valueOf(5 * i), 5 * i);
  589. }
  590. }
  591. return options;
  592. }
  593. protected LinkedHashMap<String, Double> createDoubleOptions(double max) {
  594. LinkedHashMap<String, Double> options = new LinkedHashMap<String, Double>();
  595. for (double d = 0; d <= max && d < 10; d += 0.5) {
  596. options.put(String.valueOf(d), d);
  597. }
  598. for (double d = 10; d <= max; d *= 10) {
  599. options.put(String.valueOf(d), d);
  600. if (2.5 * d <= max) {
  601. options.put(String.valueOf(2 * d), 2 * d);
  602. }
  603. if (5 * d <= max) {
  604. options.put(String.valueOf(5 * d), 5 * d);
  605. }
  606. }
  607. return options;
  608. }
  609. protected LinkedHashMap<String, Resource> createIconOptions(
  610. boolean cacheable) {
  611. LinkedHashMap<String, Resource> options = new LinkedHashMap<String, Resource>();
  612. options.put("-", null);
  613. if (cacheable) {
  614. options.put("16x16", ICON_16_USER_PNG_CACHEABLE);
  615. options.put("32x32", ICON_32_ATTENTION_PNG_CACHEABLE);
  616. options.put("64x64", ICON_64_EMAIL_REPLY_PNG_CACHEABLE);
  617. } else {
  618. options.put("16x16", ICON_16_USER_PNG_UNCACHEABLE);
  619. options.put("32x32", ICON_32_ATTENTION_PNG_UNCACHEABLE);
  620. options.put("64x64", ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE);
  621. }
  622. return options;
  623. }
  624. protected void log(String msg) {
  625. log.log(msg);
  626. }
  627. protected boolean hasLog() {
  628. return log != null;
  629. }
  630. @Override
  631. protected <VALUET> void doCommand(String commandName,
  632. AbstractComponentTestCase.Command<T, VALUET> command, VALUET value,
  633. Object data) {
  634. if (hasLog()) {
  635. log("Command: " + commandName + "(" + value + ")");
  636. }
  637. super.doCommand(commandName, command, value, data);
  638. }
  639. @Override
  640. public void error(com.vaadin.server.ErrorEvent event) {
  641. final Throwable throwable = DefaultErrorHandler
  642. .findRelevantThrowable(event.getThrowable());
  643. log.log("Exception occured, " + throwable.getClass().getName() + ": "
  644. + throwable.getMessage());
  645. throwable.printStackTrace();
  646. }
  647. @Override
  648. public void focus(FocusEvent event) {
  649. log(event.getClass().getSimpleName());
  650. }
  651. @Override
  652. public void blur(BlurEvent event) {
  653. log(event.getClass().getSimpleName());
  654. }
  655. }