1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package it.niedermann.nextcloud.deck.util;
import android.graphics.Color;
import androidx.annotation.ColorInt;
import androidx.core.util.Pair;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.List;
import it.niedermann.owncloud.notes.util.ColorUtil;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
public class ColorUtilTest {
@ColorInt
private static final int[] DARK_COLORS = new int[]{
Color.BLACK,
Color.parseColor("#0082C9"), // "Nextcloud-Blue"
Color.parseColor("#007676")
};
@ColorInt
private static final int[] LIGHT_COLORS = new int[]{
Color.WHITE,
Color.YELLOW
};
@Test
public void testGetForegroundColorForBackgroundColor() {
for (@ColorInt int color : DARK_COLORS) {
assertEquals(
"Expect foreground color for " + String.format("#%06X", (0xFFFFFF & color)) + " to be " + String.format("#%06X", (0xFFFFFF & Color.WHITE)),
Color.WHITE, ColorUtil.getForegroundColorForBackgroundColor(color)
);
}
for (@ColorInt int color : LIGHT_COLORS) {
assertEquals(
"Expect foreground color for " + String.format("#%06X", (0xFFFFFF & color)) + " to be " + String.format("#%06X", (0xFFFFFF & Color.BLACK)),
Color.BLACK, ColorUtil.getForegroundColorForBackgroundColor(color)
);
}
assertEquals(
"Expect foreground color for " + String.format("#%06X", (0xFFFFFF & Color.TRANSPARENT)) + " to be " + String.format("#%06X", (0xFFFFFF & Color.BLACK)),
Color.BLACK, ColorUtil.getForegroundColorForBackgroundColor(Color.TRANSPARENT)
);
}
@Test
public void testIsColorDark() {
for (@ColorInt int color : DARK_COLORS) {
assertTrue(
"Expect " + String.format("#%06X", (0xFFFFFF & color)) + " to be a dark color",
ColorUtil.isColorDark(color)
);
}
for (@ColorInt int color : LIGHT_COLORS) {
assertFalse(
"Expect " + String.format("#%06X", (0xFFFFFF & color)) + " to be a light color",
ColorUtil.isColorDark(color)
);
}
}
@Test
public void testContrastRatioIsSufficient() {
final List<Pair<Integer, Integer>> sufficientContrastColorPairs = new ArrayList<>();
sufficientContrastColorPairs.add(new Pair<>(Color.BLACK, Color.WHITE));
sufficientContrastColorPairs.add(new Pair<>(Color.WHITE, Color.parseColor("#0082C9")));
for (Pair<Integer, Integer> colorPair : sufficientContrastColorPairs) {
assert colorPair.first != null;
assert colorPair.second != null;
assertTrue(
"Expect contrast between " + String.format("#%06X", (0xFFFFFF & colorPair.first)) + " and " + String.format("#%06X", (0xFFFFFF & colorPair.second)) + " to be sufficient",
ColorUtil.contrastRatioIsSufficient(colorPair.first, colorPair.second)
);
}
final List<Pair<Integer, Integer>> insufficientContrastColorPairs = new ArrayList<>();
insufficientContrastColorPairs.add(new Pair<>(Color.WHITE, Color.WHITE));
insufficientContrastColorPairs.add(new Pair<>(Color.BLACK, Color.BLACK));
for (Pair<Integer, Integer> colorPair : insufficientContrastColorPairs) {
assert colorPair.first != null;
assert colorPair.second != null;
assertFalse(
"Expect contrast between " + String.format("#%06X", (0xFFFFFF & colorPair.first)) + " and " + String.format("#%06X", (0xFFFFFF & colorPair.second)) + " to be insufficient",
ColorUtil.contrastRatioIsSufficient(colorPair.first, colorPair.second)
);
}
}
}
|