Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/util/GeneratorTest.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.android.ide.common.util;
17
18 import junit.framework.TestCase;
19
20 import javax.imageio.ImageIO;
21 import java.awt.*;
22 import java.awt.image.BufferedImage;
23 import java.io.File;
24 import java.io.IOException;
25
26 /**
27 * Shared test infrastructure for asset (either bitmap or vector) generator.
28 */
29 public abstract class GeneratorTest extends TestCase {
30 protected abstract String getTestDataRelPath();
31
32 protected String generateGoldenImage(File targetDir,
33 BufferedImage goldenImage,
34 String missingFilePath,
35 String filePath) throws IOException {
36 if (targetDir == null) {
37 fail(
38 "Did not find " + missingFilePath + ". Set $ANDROID_SRC to have it created automatically");
39 }
40 File fileName = new File(targetDir, filePath);
41 assertFalse(fileName.exists());
42 if (!fileName.getParentFile().exists()) {
43 boolean mkdir = fileName.getParentFile().mkdirs();
44 assertTrue(fileName.getParent(), mkdir);
45 }
46
47 ImageIO.write(goldenImage, "PNG", fileName);
48 return fileName.getPath();
49 }
50
51 public static void assertImageSimilar(String imageName,
52 BufferedImage goldenImage,
53 BufferedImage image,
54 float maxPercentDifferent) throws IOException {
55 assertTrue("Widths differ too much for " + imageName,
56 Math.abs(goldenImage.getWidth() - image.getWidth()) < 2);
57 assertTrue("Widths differ too much for " + imageName,
58 Math.abs(goldenImage.getHeight() - image.getHeight()) < 2);
59
60 assertEquals(BufferedImage.TYPE_INT_ARGB, image.getType());
61
62 if (goldenImage.getType() != BufferedImage.TYPE_INT_ARGB) {
63 BufferedImage temp = new BufferedImage(goldenImage.getWidth(), goldenImage.getHeight(),
64 BufferedImage.TYPE_INT_ARGB);
65 temp.getGraphics().drawImage(goldenImage, 0, 0, null);
66 goldenImage = temp;
67 }
68 assertEquals(BufferedImage.TYPE_INT_ARGB, goldenImage.getType());
69
70 int imageWidth = Math.min(goldenImage.getWidth(), image.getWidth());
71 int imageHeight = Math.min(goldenImage.getHeight(), image.getHeight());
72
73 // Blur the images to account for the scenarios where there are pixel
74 // differences
75 // in where a sharp edge occurs
76 // goldenImage = blur(goldenImage, 6);
77 // image = blur(image, 6);
78
79 int width = 3 * imageWidth;
80 int height = imageHeight;
81 BufferedImage deltaImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
82 Graphics g = deltaImage.getGraphics();
83
84 // Compute delta map
85 long delta = 0;
86 for (int y = 0; y < imageHeight; y++) {
87 for (int x = 0; x < imageWidth; x++) {
88 int goldenRgb = goldenImage.getRGB(x, y);
89 int rgb = image.getRGB(x, y);
90 if (goldenRgb == rgb) {
91 deltaImage.setRGB(imageWidth + x, y, 0x00808080);
92 continue;
93 }
94
95 // If the pixels have no opacity, don't delta colors at all
96 if (((goldenRgb & 0xFF000000) == 0) && (rgb & 0xFF000000) == 0) {
97 deltaImage.setRGB(imageWidth + x, y, 0x00808080);
98 continue;
99 }
100
101 int deltaR = ((rgb & 0xFF0000) >>> 16) - ((goldenRgb & 0xFF0000) >>> 16);
102 int newR = 128 + deltaR & 0xFF;
103 int deltaG = ((rgb & 0x00FF00) >>> 8) - ((goldenRgb & 0x00FF00) >>> 8);
104 int newG = 128 + deltaG & 0xFF;
105 int deltaB = (rgb & 0x0000FF) - (goldenRgb & 0x0000FF);
106 int newB = 128 + deltaB & 0xFF;
107
108 int avgAlpha =
109 ((((goldenRgb & 0xFF000000) >>> 24) + ((rgb & 0xFF000000) >>> 24)) / 2) << 24;
110
111 int newRGB = avgAlpha | newR << 16 | newG << 8 | newB;
112 deltaImage.setRGB(imageWidth + x, y, newRGB);
113
114 delta += Math.abs(deltaR);
115 delta += Math.abs(deltaG);
116 delta += Math.abs(deltaB);
117 }
118 }
119
120 // 3 different colors, 256 color levels
121 long total = imageHeight * imageWidth * 3L * 256L;
122 float percentDifference = (float)(delta * 100 / (double)total);
123
124 if (percentDifference > maxPercentDifferent) {
125 // Expected on the left
126 // Golden on the right
127 g.drawImage(goldenImage, 0, 0, null);
128 g.drawImage(image, 2 * imageWidth, 0, null);
129
130 // Labels
131 if (imageWidth > 80) {
132 g.setColor(Color.RED);
133 g.drawString("Expected", 10, 20);
134 g.drawString("Actual", 2 * imageWidth + 10, 20);
135 }
136
137 File output = new File(getTempDir(), "delta-" + imageName.replace(File.separatorChar, '_'));
138 if (output.exists()) {
139 output.delete();
140 }
141 ImageIO.write(deltaImage, "PNG", output);
142 String message =
143 String.format("Images differ (by %.1f%%) - see details in %s", percentDifference, output);
144 System.out.println(message);
145 fail(message);
146 }
147
148 g.dispose();
149 }
150
151 protected static File getTempDir() {
152 if (System.getProperty("os.name").equals("Mac OS X")) {
153 return new File("/tmp"); //$NON-NLS-1$
154 }
155
156 return new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$
157 }
158
159 /**
160 * Get the location to write missing golden files to
161 */
162 protected File getTargetDir() {
163 // Set $ANDROID_SRC to point to your git AOSP working tree
164 String sdk = System.getenv("ANDROID_SRC");
165 if (sdk != null) {
166 File root = new File(sdk);
167 if (root.exists()) {
168 File testData = new File(root, getTestDataRelPath().replace('/', File.separatorChar));
169 if (testData.exists()) {
170 return testData;
171 }
172 }
173 }
174
175 return null;
176 }
177 }