google-nomulus/javatests/google/registry/webdriver/ActualScreenshot.java
shicong 756a3b41e1 Implement screenshot comparison
This change added the implementation of screenshot comparison with
ChromeWebDriver which will be used in the open-source code base.

This change also set a default window size(1200x2000) for each screenshot
test. This is to make the size of screenshot deterministic to help
build the screenshot comparison tests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=235539713
2019-03-05 14:13:13 -05:00

94 lines
3.2 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 . */
public class ActualScreenshot {
public static final String IMAGE_FORMAT = "png";
private String imageKey;
private BufferedImage bufferedImage;
private ActualScreenshot(String imageKey, BufferedImage bufferedImage) {
this.imageKey = imageKey;
this.bufferedImage = bufferedImage;
}
/** Creates an ActualScreenshot from the given image format and byte array. */
public static ActualScreenshot create(String imageKey, 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);
} 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));
}
/** {@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 imageKey of the screenshot. */
public String getImageKey() {
return imageKey;
}
/** Returns the concat of imageKey and imageFormat. */
public String getImageName() {
return String.join(".", imageKey, IMAGE_FORMAT);
}
}