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 27KB

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