Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.util

File ColorConverterTest.java

 
failsOnNull: Can't decode color null: please provide either an #ffffff...
failsOnBadHexa: Can't decode color #12345G: please provide either an #fff...
failsOnAlphaValueWithAlphaLessFunction: Can't decode color rgb(10,20,255, 0.5): the rgb() functio...
failsOnBadAlpha: Can't decode color rgba(10,20,255, 10): 10 is not in the ...
percentage: Can't decode color blah abc blah: abc should be a percent...
failsOnBadRgb: Can't decode color rgb(10,20,256): 256 is not in the allo...
failsOnUnknownName: Can't decode color foo: please provide either an #ffffff ...
 

Code metrics

0
54
18
1
175
116
18
0.33
3
18
1

Classes

Class Line # Actions
ColorConverterTest 44 54 0% 18 30
0.583333358.3%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.util;
35   
36    import static org.junit.Assert.assertEquals;
37   
38    import java.awt.Color;
39   
40    import org.junit.Rule;
41    import org.junit.Test;
42    import org.junit.rules.ExpectedException;
43   
 
44    public class ColorConverterTest {
45   
46    @Rule
47    public ExpectedException expectedException = ExpectedException.none();
48   
 
49  1 toggle @Test
50    public void canDecodeHexa() {
51  1 assertEquals(Color.white, ColorConverter.toColor("#ffffff"));
52  1 assertEquals(Color.black, ColorConverter.toColor("#000000"));
53  1 assertEquals(Color.red, ColorConverter.toColor("#ff0000"));
54  1 assertEquals(new Color(128, 64, 23), ColorConverter.toColor("#804017"));
55    }
56   
 
57  1 toggle @Test
58    public void canDecode3CharHexa() {
59  1 assertEquals(Color.white, ColorConverter.toColor("#fff"));
60  1 assertEquals(Color.black, ColorConverter.toColor("#000"));
61  1 assertEquals(Color.red, ColorConverter.toColor("#f00"));
62  1 assertEquals(new Color(136, 68, 34), ColorConverter.toColor("#842"));
63    }
64   
 
65  1 toggle @Test
66    public void canDecodeNames() {
67  1 assertEquals(Color.white, ColorConverter.toColor("white"));
68  1 assertEquals(Color.red, ColorConverter.toColor("RED"));
69    }
70   
 
71  1 toggle @Test
72    public void canDecodeNamesCaseInsensitively() {
73  1 assertEquals(Color.black, ColorConverter.toColor("Black"));
74  1 assertEquals(Color.yellow, ColorConverter.toColor("yelLoW"));
75    }
76   
 
77  1 toggle @Test
78    public void canDecodeRgbCssLikeFunction() {
79  1 assertEquals(new Color(128, 64, 23, 255), ColorConverter.toColor("rgb(128,64,23)"));
80  1 assertEquals(new Color(128, 64, 23, 255), ColorConverter.toColor(" rgb ( 128, 064, 23 ) "));
81    }
82   
 
83  1 toggle @Test
84    public void canDecodeRgbaCssLikeFunction() {
85  1 assertEquals(new Color(128, 64, 23, 51), ColorConverter.toColor("rgba(128, 64, 23, 0.2)"));
86    }
87   
 
88  1 toggle @Test
89    public void canDecodeHslCssLikeFunction() {
90  1 assertEquals(new Color(166, 102, 89), ColorConverter.toColor("hsl(10, 30%, 50%)"));
91    }
92   
 
93  1 toggle @Test
94    public void canDecodeHslaCssLikeFunction() {
95  1 final Color actual = ColorConverter.toColor("hsla(10, 30%, 50%, 0.5)");
96  1 final Color expected = new Color(166, 102, 89, 127);
97  1 assertEquals(expected, actual);
98    }
99   
 
100  0 toggle @Test
101    public void failsOnUnknownName() {
102  0 Test failure here assertFailsOnColor("foo", "Can't decode color foo: please provide either an #ffffff or #fff hexadecimal value or a known named color.");
103    }
104   
 
105  0 toggle @Test
106    public void failsOnNull() {
107  0 Test failure here assertFailsOnColor(null, "Can't decode color null: please provide either an #ffffff or #fff hexadecimal value or a known named color.");
108    }
109   
 
110  0 toggle @Test
111    public void failsOnBadHexa() {
112  0 Test failure here assertFailsOnColor("#12345G", "Can't decode color #12345G: please provide either an #ffffff or #fff hexadecimal value or a known named color.");
113  0 assertFailsOnColor("#1234", "Can't decode color #1234: please provide a 6 or 3 digit long hexadecimal string, e.g #ffffff or #fff.");
114    }
115   
 
116  0 toggle @Test
117    public void failsOnBadRgb() {
118  0 Test failure here assertFailsOnColor("rgb(10,20,256)", "Can't decode color rgb(10,20,256): 256 is not in the allowed 0-255 range.");
119    // -10 is not in the allowed 0-255 range either, but the minus sign isn't even matched by the pattern, so...
120  0 assertFailsOnColor("rgb(-10,20,256)", "Can't decode color rgb(-10,20,256): please provide either an #ffffff or #fff hexadecimal value or a known named color.");
121    }
122   
 
123  0 toggle @Test
124    public void failsOnBadAlpha() {
125  0 Test failure here assertFailsOnColor("rgba(10,20,255, 10)", "Can't decode color rgba(10,20,255, 10): 10 is not in the allowed 0.0-1.0 <alphavalue> range.");
126    // -0.2 is not in the allowed 0.0-1.0 <alphavalue> range either, but the minus sign isn't even matched by the pattern, so...
127  0 assertFailsOnColor("rgba(10,20,255, -0.2)", "Can't decode color rgba(10,20,255, -0.2): please provide either an #ffffff or #fff hexadecimal value or a known named color.");
128  0 assertFailsOnColor("rgba(10,20,255, 1.2)", "Can't decode color rgba(10,20,255, 1.2): 1.2 is not in the allowed 0.0-1.0 <alphavalue> range.");
129    }
130   
 
131  0 toggle @Test
132    public void failsOnAlphaValueWithAlphaLessFunction() {
133  0 Test failure here assertFailsOnColor("rgb(10,20,255, 0.5)", "Can't decode color rgb(10,20,255, 0.5): the rgb() function does not take an <alphavalue> argument.");
134    }
135   
 
136  1 toggle @Test
137    public void normalizeAngle() {
138  1 assertEquals(0, ColorConverter.normalizeAngle(0));
139  1 assertEquals(0, ColorConverter.normalizeAngle(-0));
140  1 assertEquals(1, ColorConverter.normalizeAngle(1));
141  1 assertEquals(359, ColorConverter.normalizeAngle(-1));
142  1 assertEquals(0, ColorConverter.normalizeAngle(360));
143  1 assertEquals(0, ColorConverter.normalizeAngle(720));
144  1 assertEquals(0, ColorConverter.normalizeAngle(3600));
145  1 assertEquals(10, ColorConverter.normalizeAngle(370));
146  1 assertEquals(10, ColorConverter.normalizeAngle(730));
147  1 assertEquals(10, ColorConverter.normalizeAngle(1450));
148  1 assertEquals(10, ColorConverter.normalizeAngle(-350));
149  1 assertEquals(10, ColorConverter.normalizeAngle(-710));
150  1 assertEquals(10, ColorConverter.normalizeAngle(-1070));
151  1 assertEquals(10, ColorConverter.normalizeAngle(-1430));
152    }
153   
 
154  0 toggle @Test
155    public void percentage() {
156  0 Test failure here assertFailsOnPercentage("abc", "blah abc blah", "Can't decode color blah abc blah: abc should be a percentage value.");
157  0 assertFailsOnPercentage("88", "blah 88 blah", "Can't decode color blah 88 blah: 88 should be a percentage value.");
158  0 assertFailsOnPercentage("120%", "blah 120% blah", "Can't decode color blah 120% blah: 120 is not in the allowed 0-100 range.");
159  0 assertFailsOnPercentage("-12%", "blah -12% blah", "Can't decode color blah -12% blah: -12 is not in the allowed 0-100 range.");
160  0 assertEquals(ColorConverter.percentage("88%", ""), 88);
161    }
162   
 
163  0 toggle private void assertFailsOnColor(final String input, String expectedMessage) {
164  0 expectedException.expect(RuntimeException.class);
165  0 expectedException.expectMessage(expectedMessage);
166  0 Test failure here ColorConverter.toColor(input);
167    }
168   
 
169  0 toggle private void assertFailsOnPercentage(final String percentage, final String input, String expectedMessage) {
170  0 expectedException.expect(IllegalArgumentException.class);
171  0 expectedException.expectMessage(expectedMessage);
172  0 Test failure here ColorConverter.percentage(percentage, input);
173    }
174   
175    }