google-nomulus/javatests/google/registry/webdriver/ActualScreenshot.java
shicong c8aa6005f2 Use reflection to inject the attempt number
This CL is to address the public static field in RepeatableRunner
for caller to get the current attempt number. We tried to have
a JUnit TestRule to achieve the purpose but it ended up with having
a RuleChain in each class where we already have multiple rules and
need to add the retry rule. This is because we have to make sure
the retry rule is the last one to wrap the test statement so that
the actual retry can include the actions defined in other rules.
Having a rule chain is not scalable and confuses engineer so we
gave it up.

Instead, we decided to expand the current RepeatableRunner to
use reflection to inject the attempt number to the test class.
Doing it this way can reduce the burden from the caller and it also
gets rid of the global state from the previous public static field.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=240789045
2019-03-29 16:17:35 -04:00

96 lines
3.3 KiB
Java

// Copyright 2019 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.webdriver;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
/** An immutable class represents a screenshot taken in a visual regression test. */
public class ActualScreenshot {
public static final String IMAGE_FORMAT = "png";
private String imageKey;
private BufferedImage bufferedImage;
private int attempt;
private ActualScreenshot(String imageKey, BufferedImage bufferedImage, int attempt) {
this.imageKey = imageKey;
this.bufferedImage = bufferedImage;
this.attempt = attempt;
}
/** Creates an ActualScreenshot from the given image format and byte array. */
public static ActualScreenshot create(String imageKey, int attempt, byte[] imageBytes) {
checkNotNull(imageKey);
checkNotNull(imageBytes);
byte[] imageBytesClone = Arrays.copyOf(imageBytes, imageBytes.length);
ByteArrayInputStream imageInputStream = new ByteArrayInputStream(imageBytesClone);
ImageReader imageReader = ImageIO.getImageReadersByFormatName(IMAGE_FORMAT).next();
try {
imageReader.setInput(checkNotNull(ImageIO.createImageInputStream(imageInputStream)));
BufferedImage bufferedImage = checkNotNull(imageReader.read(0));
return new ActualScreenshot(imageKey, bufferedImage, attempt);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/** {@link BufferedImage#getSubimage(int, int, int, int)} */
public ActualScreenshot getSubimage(int x, int y, int w, int h) {
return new ActualScreenshot(imageKey, bufferedImage.getSubimage(x, y, w, h), attempt);
}
/** {@link BufferedImage#getWidth()} */
public int getWidth() {
return bufferedImage.getWidth();
}
/** {@link BufferedImage#getHeight()} */
public int getHeight() {
return bufferedImage.getHeight();
}
/** {@link BufferedImage#getRGB(int, int)} */
public int getRGB(int x, int y) {
return bufferedImage.getRGB(x, y);
}
/** Writes the underlying BufferedImage to the given file. */
public void writeTo(File file) {
try {
checkState(ImageIO.write(bufferedImage, IMAGE_FORMAT, checkNotNull(file)));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/** Returns the attempt number of the test. */
public int getAttempt() {
return attempt;
}
/** Returns the concat of imageKey and imageFormat. */
public String getImageName() {
return String.join(".", imageKey, IMAGE_FORMAT);
}
}