1 /* MovingTextTest.java
2 Copyright (C) 2012 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012;
38 
39 import net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012.MovingText;
40 import java.awt.Color;
41 import java.awt.Font;
42 import java.awt.Graphics2D;
43 import java.awt.image.BufferedImage;
44 import org.junit.Assert;
45 import org.junit.Test;
46 
47 public class MovingTextTest {
48 
49     @Test
setGetTest()50     public void setGetTest() {
51         BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
52         Graphics2D g2d = bi.createGraphics();
53         Font f1 = g2d.getFont().deriveFont(Font.ITALIC);
54         Font f2 = g2d.getFont().deriveFont(Font.BOLD);
55         String s = "Watter";
56         MovingText tw = new MovingText(s, f1);
57         Assert.assertEquals(-1, tw.getHeight());
58         Assert.assertEquals(-1, tw.getWidth());
59         Assert.assertEquals(f1, tw.getFont());
60         Assert.assertNull(tw.getImg());
61         Assert.assertEquals(s, tw.getText());
62         Assert.assertEquals(Color.BLACK, tw.getTextOutline());
63         Assert.assertEquals(Color.blue, tw.getWaterColor());
64         Assert.assertEquals(Color.white, tw.getBgColor());
65         Assert.assertEquals(0, tw.getPercentageOfWater());
66         tw.setBgColor(Color.yellow);
67         tw.setWaterColor(Color.orange);
68         tw.setPercentageOfWater(20);
69         Assert.assertEquals(Color.orange, tw.getWaterColor());
70         Assert.assertEquals(Color.yellow, tw.getBgColor());
71         Assert.assertEquals(20, tw.getPercentageOfWater());
72 
73     }
74 
getAvgColor(Color c)75     private static int getAvgColor(Color c) {
76         Assert.assertEquals(c.getRed(), c.getBlue());
77         Assert.assertEquals(c.getRed(), c.getGreen());
78         return c.getRed();
79     }
80 
getAvgColor(int i)81     private static int getAvgColor(int i) {
82         return getAvgColor(new Color(i));
83     }
84 
getAvgColor(BufferedImage m, int x, int y)85     private static int getAvgColor(BufferedImage m, int x, int y) {
86         return getAvgColor(m.getRGB(x, y));
87     }
88 
89     @Test
getBackground()90     public void getBackground() {
91         MovingText ifc = getInstance();
92         BufferedImage bic = ifc.getBackground();
93         int w = bic.getWidth();
94         int h = bic.getHeight();
95         ifc.setPercentageOfWater(0);
96         bic = ifc.getBackground();
97         Assert.assertTrue(getAvgColor(bic, 3 * w / 4, h / 2) > getAvgColor(bic, 2 * w / 4, h / 2));
98         Assert.assertTrue(getAvgColor(bic, w / 4, h / 2) < getAvgColor(bic, 2 * w / 4, h / 2));
99         Assert.assertEquals(new Color(bic.getRGB(w - w / 4, 2 * h / 3)), new Color(bic.getRGB(w - w / 4, h / 3)));
100         ifc.setPercentageOfWater(w / 2);
101         bic = ifc.getBackground();
102         Assert.assertTrue(getAvgColor(bic, 3 * w / 4, h / 2) > getAvgColor(bic, 2 * w / 4, h / 2));
103         Assert.assertTrue(getAvgColor(bic, w / 4, h / 2) > getAvgColor(bic, 2 * w / 4, h / 2));
104         Assert.assertEquals(new Color(bic.getRGB(w - w / 3, 2 * h / 3)), new Color(bic.getRGB(w - w / 3, h / 3)));
105         ifc.setPercentageOfWater(w);
106         bic = ifc.getBackground();
107         Assert.assertTrue(getAvgColor(bic, 3 * w / 4, h / 2) < getAvgColor(bic, 2 * w / 4, h / 2));
108         Assert.assertTrue(getAvgColor(bic, w / 4, h / 2) > getAvgColor(bic, 2 * w / 4, h / 2));
109         Assert.assertEquals(new Color(bic.getRGB(w - w / 4, h / 3)), new Color(bic.getRGB(w - w / 4, h / 2)));
110 
111 
112 
113     }
114 
getInstance()115     private MovingText getInstance() {
116         BufferedImage bi1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
117         Font f = bi1.createGraphics().getFont().deriveFont(Font.BOLD, 130);
118         MovingText ifc = new MovingText("O O", f);
119         return ifc;
120     }
121 
assertNotEquals(Object o1, Object o2)122     private static void assertNotEquals(Object o1, Object o2) {
123         Assert.assertFalse(o1.equals(o2));
124 
125     }
126 
127     @Test
cutToTest()128     public void cutToTest() {
129         MovingText ifc = getInstance();
130         BufferedImage bic = ifc.getBackground();
131         int w = bic.getWidth();
132         int h = bic.getHeight();
133         bic = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
134         ifc.cutTo(bic.createGraphics(), 0, h);
135         Color cc = new Color(0, 0, 0);
136         assertNotEquals(cc, new Color(bic.getRGB(52, 142)));
137         assertNotEquals(cc, new Color(bic.getRGB(170, 110)));
138         assertNotEquals(cc, new Color(bic.getRGB(52, 62)));
139         assertNotEquals(cc, new Color(bic.getRGB(245, 85)));
140 
141         //well this should be acctually rgba 0,0,0,0 but somehow this was no passig
142         //you can confirm with:
143         //ImageFontCutterTest.save(bic, "halfFiledOus")
144         Assert.assertEquals(cc, new Color(bic.getRGB(137, 127)));
145         Assert.assertEquals(cc, new Color(bic.getRGB(137, 2)));
146 
147 
148 
149 
150     }
151 }
152