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.

ScreenshotBrowser.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package com.vaadin.screenshotbrowser;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.Properties;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10. import org.apache.commons.io.FileUtils;
  11. import org.apache.commons.io.filefilter.DirectoryFileFilter;
  12. import org.apache.commons.io.filefilter.SuffixFileFilter;
  13. import com.vaadin.event.ShortcutAction.KeyCode;
  14. import com.vaadin.event.ShortcutListener;
  15. import com.vaadin.server.ExternalResource;
  16. import com.vaadin.server.FileResource;
  17. import com.vaadin.server.VaadinRequest;
  18. import com.vaadin.ui.BrowserFrame;
  19. import com.vaadin.ui.Button;
  20. import com.vaadin.ui.Button.ClickListener;
  21. import com.vaadin.ui.CustomComponent;
  22. import com.vaadin.ui.HorizontalLayout;
  23. import com.vaadin.ui.Label;
  24. import com.vaadin.ui.UI;
  25. import com.vaadin.ui.VerticalLayout;
  26. import com.vaadin.v7.data.util.BeanItemContainer;
  27. import com.vaadin.v7.ui.Table;
  28. public class ScreenshotBrowser extends UI {
  29. private static final File screenshotDir = findScreenshotDir();
  30. /*-
  31. * Groups:
  32. * 1 - test class
  33. * 2 - test method
  34. * 3 - platform
  35. * 4 - browser name
  36. * 5 - browser version
  37. * 6 - identifier
  38. * 7 - additional identifiers
  39. */
  40. private static final Pattern screenshotNamePattern = Pattern
  41. .compile("(.+?)-(.+?)_(.+?)_(.+?)_(.*?)_(.+?)(_.+)?\\.png\\.html");
  42. public static enum Action {
  43. ACCEPT {
  44. @Override
  45. public void apply(File screenshotFile) {
  46. File targetFile = new File(getReferenceDir(),
  47. screenshotFile.getName());
  48. // Delete previous file as well as any alternatives
  49. if (targetFile.exists()) {
  50. for (int i = 1; true; i++) {
  51. File alternative = getAlternative(targetFile, i);
  52. if (alternative.exists()) {
  53. alternative.delete();
  54. } else {
  55. break;
  56. }
  57. }
  58. targetFile.delete();
  59. }
  60. screenshotFile.renameTo(targetFile);
  61. }
  62. },
  63. IGNORE {
  64. @Override
  65. public void apply(File screenshotFile) {
  66. screenshotFile.delete();
  67. }
  68. },
  69. ALTERNATIVE {
  70. @Override
  71. public void apply(File screenshotFile) {
  72. File baseFile = new File(getReferenceDir(),
  73. screenshotFile.getName());
  74. // Iterate until we find the first alternative id not yet used
  75. File targetFile = baseFile;
  76. int alternativeNumber = 1;
  77. while (targetFile.exists()) {
  78. targetFile = getAlternative(baseFile, alternativeNumber++);
  79. }
  80. screenshotFile.renameTo(targetFile);
  81. }
  82. };
  83. public void commit(File htmlFile) {
  84. String screenshotName = htmlFile.getName().substring(0,
  85. htmlFile.getName().length() - ".html".length());
  86. apply(new File(htmlFile.getParentFile(), screenshotName));
  87. htmlFile.delete();
  88. }
  89. private static File getReferenceDir() {
  90. return new File(screenshotDir, "reference-screenshots");
  91. }
  92. private static File getAlternative(File baseFile,
  93. int alternativeNumber) {
  94. assert alternativeNumber >= 1;
  95. String alternativeName = baseFile.getName().replaceFirst("\\.png",
  96. "_" + alternativeNumber + ".png");
  97. return new File(baseFile.getParentFile(), alternativeName);
  98. }
  99. protected abstract void apply(File screenshotFile);
  100. }
  101. public static class ComparisonFailure {
  102. private final Matcher matcher;
  103. private final File file;
  104. private Action action;
  105. public ComparisonFailure(File file) {
  106. this.file = file;
  107. matcher = screenshotNamePattern.matcher(file.getName());
  108. if (!matcher.matches()) {
  109. throw new RuntimeException("Could not parse screenshot name "
  110. + file.getAbsolutePath());
  111. }
  112. }
  113. public File getFile() {
  114. return file;
  115. }
  116. public String getName() {
  117. return matcher.group();
  118. }
  119. public String getTestClass() {
  120. return matcher.group(1);
  121. }
  122. public String getTestMethod() {
  123. return matcher.group(2);
  124. }
  125. public String getBrowser() {
  126. return matcher.group(4) + " " + matcher.group(5);
  127. }
  128. public String getIdentifier() {
  129. String additional = matcher.group(7);
  130. if (additional != null) {
  131. return matcher.group(6) + additional;
  132. }
  133. return matcher.group(6);
  134. }
  135. public void setAction(Action action) {
  136. this.action = action;
  137. }
  138. public Action getAction() {
  139. return action;
  140. }
  141. }
  142. private class Viewer extends CustomComponent {
  143. private BrowserFrame preview = new BrowserFrame();
  144. private VerticalLayout left = new VerticalLayout();
  145. private HorizontalLayout root = new HorizontalLayout(left, preview);
  146. private Collection<ComparisonFailure> items;
  147. public Viewer() {
  148. preview.setWidth("1500px");
  149. preview.setHeight("100%");
  150. left.setMargin(true);
  151. left.setSpacing(true);
  152. left.setSizeUndefined();
  153. left.setWidth("270px");
  154. left.addComponent(
  155. createActionButton("Accept changes", 'j', Action.ACCEPT));
  156. left.addComponent(
  157. createActionButton("Ignore changes", 'k', Action.IGNORE));
  158. left.addComponent(createActionButton("Use as alternative", 'l',
  159. Action.ALTERNATIVE));
  160. left.addComponent(
  161. new Button("Clear action", createSetActionListener(null)));
  162. left.addComponent(createSpacer());
  163. left.addComponent(
  164. new Button("Commit actions", event -> commitActions()));
  165. left.addComponent(createSpacer());
  166. left.addComponent(
  167. new Button("Refresh", event -> refreshTableContainer()));
  168. Label expandSpacer = createSpacer();
  169. left.addComponent(expandSpacer);
  170. left.setExpandRatio(expandSpacer, 1);
  171. Label instructions = new Label(
  172. "Press the j, k or l keys to quickly select an action for the selected item.");
  173. instructions.setWidth("100%");
  174. left.addComponent(instructions);
  175. root.setExpandRatio(left, 1);
  176. root.setSizeFull();
  177. setCompositionRoot(root);
  178. setHeight("850px");
  179. setWidth("1800px");
  180. }
  181. private Button createActionButton(String caption, char shortcut,
  182. Action action) {
  183. Button button = new Button(
  184. caption + " <strong>" + shortcut + "</strong>",
  185. createSetActionListener(action));
  186. button.setCaptionAsHtml(true);
  187. if (!Action.IGNORE.equals(action)) {
  188. // other actions disabled for now since the functionality was
  189. // designed for a different directory structure, needs reworking
  190. button.setEnabled(false);
  191. }
  192. return button;
  193. }
  194. private Label createSpacer() {
  195. // Poor man's spacer, non-breaking space
  196. return new Label("\u00a0");
  197. }
  198. private ClickListener createSetActionListener(final Action action) {
  199. return event -> setActions(action);
  200. }
  201. public void setActions(final Action action) {
  202. for (ComparisonFailure comparisonFailure : items) {
  203. comparisonFailure.setAction(action);
  204. }
  205. table.refreshRowCache();
  206. }
  207. public void setItems(Collection<ComparisonFailure> items) {
  208. this.items = items;
  209. if (items.size() == 1) {
  210. ComparisonFailure failure = items.iterator().next();
  211. preview.setSource(new FileResource(failure.getFile()));
  212. } else {
  213. preview.setSource(new ExternalResource("about:blank"));
  214. }
  215. }
  216. }
  217. private final Table table = new Table();
  218. private final Viewer viewer = new Viewer();
  219. @Override
  220. protected void init(VaadinRequest request) {
  221. table.setWidth("100%");
  222. table.setPageLength(10);
  223. table.setMultiSelect(true);
  224. table.addValueChangeListener(event -> {
  225. @SuppressWarnings("unchecked")
  226. Collection<ComparisonFailure> selectedRows = (Collection<ComparisonFailure>) table
  227. .getValue();
  228. viewer.setItems(selectedRows);
  229. });
  230. table.addShortcutListener(
  231. createShortcutListener(KeyCode.J, Action.ACCEPT));
  232. table.addShortcutListener(
  233. createShortcutListener(KeyCode.K, Action.IGNORE));
  234. table.addShortcutListener(
  235. createShortcutListener(KeyCode.L, Action.ALTERNATIVE));
  236. refreshTableContainer();
  237. VerticalLayout mainLayout = new VerticalLayout(viewer, table);
  238. mainLayout.setSizeUndefined();
  239. setSizeFull();
  240. setContent(mainLayout);
  241. table.focus();
  242. }
  243. private void commitActions() {
  244. for (ComparisonFailure comparisonFailure : getContainer()
  245. .getItemIds()) {
  246. Action action = comparisonFailure.getAction();
  247. if (action != null) {
  248. action.commit(comparisonFailure.getFile());
  249. }
  250. }
  251. refreshTableContainer();
  252. }
  253. private ShortcutListener createShortcutListener(int keyCode,
  254. final Action action) {
  255. return new ShortcutListener(action.toString(), keyCode, null) {
  256. @Override
  257. public void handleAction(Object sender, Object target) {
  258. viewer.setActions(action);
  259. selectNextWithoutAction();
  260. }
  261. };
  262. }
  263. private void selectNextWithoutAction() {
  264. Collection<?> selected = (Collection<?>) table.getValue();
  265. BeanItemContainer<ComparisonFailure> container = getContainer();
  266. // Find where to start
  267. ComparisonFailure candidate;
  268. if (selected == null || selected.isEmpty()) {
  269. candidate = container.firstItemId();
  270. } else {
  271. candidate = (ComparisonFailure) selected.iterator().next();
  272. }
  273. // Find first one without action
  274. while (candidate != null && candidate.getAction() != null) {
  275. candidate = container.nextItemId(candidate);
  276. }
  277. // Select it
  278. if (candidate == null) {
  279. table.setValue(Collections.emptySet());
  280. } else {
  281. table.setValue(Collections.singleton(candidate));
  282. }
  283. }
  284. @SuppressWarnings("unchecked")
  285. private BeanItemContainer<ComparisonFailure> getContainer() {
  286. return (BeanItemContainer<ComparisonFailure>) table
  287. .getContainerDataSource();
  288. }
  289. private void refreshTableContainer() {
  290. File errorsDir = new File(screenshotDir, "error-screenshots");
  291. Collection<File> failures = FileUtils.listFiles(errorsDir,
  292. new SuffixFileFilter(".html"), DirectoryFileFilter.DIRECTORY);
  293. BeanItemContainer<ComparisonFailure> container = new BeanItemContainer<>(
  294. ComparisonFailure.class);
  295. for (File failure : failures) {
  296. container.addBean(new ComparisonFailure(failure));
  297. }
  298. table.setContainerDataSource(container);
  299. table.setVisibleColumns("testClass", "testMethod", "browser",
  300. "identifier", "action");
  301. if (container.size() > 0) {
  302. table.select(container.firstItemId());
  303. }
  304. }
  305. private static File findScreenshotDir() {
  306. File propertiesFile = new File(
  307. "../work/eclipse-run-selected-test.properties");
  308. if (!propertiesFile.exists()) {
  309. throw new RuntimeException(
  310. "File " + propertiesFile.getAbsolutePath() + " not found.");
  311. }
  312. FileInputStream in = null;
  313. try {
  314. in = new FileInputStream(propertiesFile);
  315. Properties properties = new Properties();
  316. properties.load(in);
  317. String screenShotDirName = properties
  318. .getProperty("com.vaadin.testbench.screenshot.directory");
  319. if (screenShotDirName == null
  320. || screenShotDirName.startsWith("<")) {
  321. throw new RuntimeException(
  322. "com.vaadin.testbench.screenshot.directory has not been configred in "
  323. + propertiesFile.getAbsolutePath());
  324. }
  325. File screenshotDir = new File(screenShotDirName);
  326. if (!screenshotDir.isDirectory()) {
  327. throw new RuntimeException(screenshotDir.getAbsolutePath()
  328. + " is not a directory");
  329. }
  330. return screenshotDir;
  331. } catch (IOException e) {
  332. throw new RuntimeException(e);
  333. } finally {
  334. try {
  335. if (in != null) {
  336. in.close();
  337. }
  338. } catch (IOException e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. }
  343. }