1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.screenshotbrowser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.vaadin.annotations.Theme;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutListener;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.BrowserFrame;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.data.Property.ValueChangeEvent;
import com.vaadin.v7.data.Property.ValueChangeListener;
import com.vaadin.v7.data.util.BeanItemContainer;
import com.vaadin.v7.ui.Table;
@Theme("valo")
public class ScreenshotBrowser extends UI {
private static final File screenshotDir = findScreenshotDir();
/*-
* Groups:
* 1 - test class
* 2 - test method
* 3 - platform
* 4 - browser name
* 5 - browser version
* 6 - additional qualifiers
* 7 - identifier
*/
private static final Pattern screenshotNamePattern = Pattern
.compile("(.+?)-(.+?)_(.+?)_(.+?)_(.+?)(_.+)?_(.+?)\\.png\\.html");
public static enum Action {
ACCEPT {
@Override
public void apply(File screenshotFile) {
File targetFile = new File(getReferenceDir(),
screenshotFile.getName());
// Delete previous file as well as any alternatives
if (targetFile.exists()) {
for (int i = 1; true; i++) {
File alternative = getAlternative(targetFile, i);
if (alternative.exists()) {
alternative.delete();
} else {
break;
}
}
targetFile.delete();
}
screenshotFile.renameTo(targetFile);
}
},
IGNORE {
@Override
public void apply(File screenshotFile) {
screenshotFile.delete();
}
},
ALTERNATIVE {
@Override
public void apply(File screenshotFile) {
File baseFile = new File(getReferenceDir(),
screenshotFile.getName());
// Iterate until we find the first alternative id not yet used
File targetFile = baseFile;
int alternativeNumber = 1;
while (targetFile.exists()) {
targetFile = getAlternative(baseFile, alternativeNumber++);
}
screenshotFile.renameTo(targetFile);
}
};
public void commit(File htmlFile) {
String screenshotName = htmlFile.getName().substring(0,
htmlFile.getName().length() - ".html".length());
apply(new File(htmlFile.getParentFile(), screenshotName));
htmlFile.delete();
}
private static File getReferenceDir() {
return new File(screenshotDir, "reference");
}
private static File getAlternative(File baseFile,
int alternativeNumber) {
assert alternativeNumber >= 1;
String alternativeName = baseFile.getName().replaceFirst("\\.png",
"_" + alternativeNumber + ".png");
return new File(baseFile.getParentFile(), alternativeName);
}
protected abstract void apply(File screenshotFile);
}
public static class ComparisonFailure {
private final Matcher matcher;
private final File file;
private Action action;
public ComparisonFailure(File file) {
this.file = file;
matcher = screenshotNamePattern.matcher(file.getName());
if (!matcher.matches()) {
throw new RuntimeException("Could not parse screenshot name "
+ file.getAbsolutePath());
}
}
public File getFile() {
return file;
}
public String getName() {
return matcher.group();
}
public String getTestClass() {
return matcher.group(1);
}
public String getTestMethod() {
return matcher.group(2);
}
public String getBrowser() {
return matcher.group(4) + " " + matcher.group(5);
}
public String getQualifiers() {
return matcher.group(6);
}
public String getIdentifier() {
return matcher.group(7);
}
public void setAction(Action action) {
this.action = action;
}
public Action getAction() {
return action;
}
}
private class Viewer extends CustomComponent {
private BrowserFrame preview = new BrowserFrame();
private VerticalLayout left = new VerticalLayout();
private HorizontalLayout root = new HorizontalLayout(left, preview);
private Collection<ComparisonFailure> items;
public Viewer() {
preview.setWidth("1500px");
preview.setHeight("100%");
left.setMargin(true);
left.setSpacing(true);
left.setSizeFull();
left.addComponent(
createActionButton("Accept changes", 'j', Action.ACCEPT));
left.addComponent(
createActionButton("Ignore changes", 'k', Action.IGNORE));
left.addComponent(createActionButton("Use as alternative", 'l',
Action.ALTERNATIVE));
left.addComponent(
new Button("Clear action", createSetActionListener(null)));
left.addComponent(createSpacer());
left.addComponent(
new Button("Commit actions", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
commitActions();
}
}));
left.addComponent(createSpacer());
left.addComponent(new Button("Refresh", new Button.ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
refreshTableContainer();
}
}));
Label expandSpacer = createSpacer();
left.addComponent(expandSpacer);
left.setExpandRatio(expandSpacer, 1);
left.addComponent(new Label(
"Press the j, k or l keys to quickly select an action for the selected item."));
root.setExpandRatio(left, 1);
root.setSizeFull();
setCompositionRoot(root);
setHeight("850px");
setWidth("100%");
}
private Button createActionButton(String caption, char shortcut,
Action action) {
Button button = new Button(
caption + " <strong>" + shortcut + "</strong>",
createSetActionListener(action));
button.setCaptionAsHtml(true);
return button;
}
private Label createSpacer() {
// Poor man's spacer, non-breaking space
return new Label("\u00a0");
}
private ClickListener createSetActionListener(final Action action) {
return new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
setActions(action);
}
};
}
public void setActions(final Action action) {
for (ComparisonFailure comparisonFailure : items) {
comparisonFailure.setAction(action);
}
table.refreshRowCache();
}
public void setItems(Collection<ComparisonFailure> items) {
this.items = items;
if (items.size() == 1) {
ComparisonFailure failure = items.iterator().next();
preview.setSource(new FileResource(failure.getFile()));
} else {
preview.setSource(new ExternalResource("about:blank"));
}
}
}
private final Table table = new Table();
private final Viewer viewer = new Viewer();
@Override
protected void init(VaadinRequest request) {
table.setWidth("100%");
table.setHeight("100%");
table.setMultiSelect(true);
table.addValueChangeListener(new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
@SuppressWarnings("unchecked")
Collection<ComparisonFailure> selectedRows = (Collection<ComparisonFailure>) table
.getValue();
viewer.setItems(selectedRows);
}
});
table.addShortcutListener(
createShortcutListener(KeyCode.J, Action.ACCEPT));
table.addShortcutListener(
createShortcutListener(KeyCode.K, Action.IGNORE));
table.addShortcutListener(
createShortcutListener(KeyCode.L, Action.ALTERNATIVE));
refreshTableContainer();
VerticalLayout mainLayout = new VerticalLayout(table, viewer);
mainLayout.setExpandRatio(table, 1);
mainLayout.setSizeFull();
setSizeFull();
setContent(mainLayout);
table.focus();
}
private void commitActions() {
for (ComparisonFailure comparisonFailure : getContainer()
.getItemIds()) {
Action action = comparisonFailure.getAction();
if (action != null) {
action.commit(comparisonFailure.getFile());
}
}
refreshTableContainer();
}
private ShortcutListener createShortcutListener(int keyCode,
final Action action) {
return new ShortcutListener(action.toString(), keyCode, null) {
@Override
public void handleAction(Object sender, Object target) {
viewer.setActions(action);
selectNextWithoutAction();
}
};
}
private void selectNextWithoutAction() {
Collection<?> selected = (Collection<?>) table.getValue();
BeanItemContainer<ComparisonFailure> container = getContainer();
// Find where to start
ComparisonFailure candidate;
if (selected == null || selected.isEmpty()) {
candidate = container.firstItemId();
} else {
candidate = (ComparisonFailure) selected.iterator().next();
}
// Find first one without action
while (candidate != null && candidate.getAction() != null) {
candidate = container.nextItemId(candidate);
}
// Select it
if (candidate == null) {
table.setValue(Collections.emptySet());
} else {
table.setValue(Collections.singleton(candidate));
}
}
@SuppressWarnings("unchecked")
private BeanItemContainer<ComparisonFailure> getContainer() {
return (BeanItemContainer<ComparisonFailure>) table
.getContainerDataSource();
}
private void refreshTableContainer() {
File errorsDir = new File(screenshotDir, "errors");
File[] failures = errorsDir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".html");
}
});
BeanItemContainer<ComparisonFailure> container = new BeanItemContainer<ComparisonFailure>(
ComparisonFailure.class);
for (File failure : failures) {
container.addBean(new ComparisonFailure(failure));
}
table.setContainerDataSource(container);
table.setVisibleColumns("testClass", "testMethod", "browser",
"qualifiers", "identifier", "action");
if (container.size() > 0) {
table.select(container.firstItemId());
}
}
private static File findScreenshotDir() {
File propertiesFile = new File(
"../work/eclipse-run-selected-test.properties");
if (!propertiesFile.exists()) {
throw new RuntimeException(
"File " + propertiesFile.getAbsolutePath() + " not found.");
}
FileInputStream in = null;
try {
in = new FileInputStream(propertiesFile);
Properties properties = new Properties();
properties.load(in);
String screenShotDirName = properties
.getProperty("com.vaadin.testbench.screenshot.directory");
if (screenShotDirName == null
|| screenShotDirName.startsWith("<")) {
throw new RuntimeException(
"com.vaadin.testbench.screenshot.directory has not been configred in "
+ propertiesFile.getAbsolutePath());
}
File screenshotDir = new File(screenShotDirName);
if (!screenshotDir.isDirectory()) {
throw new RuntimeException(screenshotDir.getAbsolutePath()
+ " is not a directory");
}
return screenshotDir;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|