/tests/colormap_test.py
Python | 119 lines | 104 code | 11 blank | 4 comment | 4 complexity | 84e7ace96a0c610b278d17a26d08ff62 MD5 | raw file
Possible License(s): GPL-2.0
1"""Test for tortoisehg.util.colormap"""
2import sys
3from nose.tools import *
4from tortoisehg.util import colormap
5
6def fakectx(userhash, date, tz=0):
7 # AnnotateColorSaturation uses hash() for mapping user to hue
8 class user(object):
9 def __hash__(self):
10 return userhash
11 class ctx(object):
12 def user(self):
13 return user()
14 def date(self):
15 return (float(date), tz)
16 def __repr__(self):
17 return '<fakectx:userhash=%x,date=%d,%d>' % (userhash, date, tz)
18 return ctx()
19
20def test_user_hue():
21 """Is a user mapped to his own color (hue)?"""
22 cm = colormap.AnnotateColorSaturation()
23
24 samples = {0x0: '#ffaaaa', 0x1: '#ffaac9', 0x2: '#ffaae9', 0x3: '#f4aaff',
25 0x4: '#d4aaff', 0x5: '#b4aaff', 0x6: '#aabfff', 0x7: '#aadfff',
26 0x8: '#aaffff', 0x9: '#aaffdf', 0xa: '#aaffbf', 0xb: '#b4ffaa',
27 0xc: '#d4ffaa', 0xd: '#f4ffaa', 0xe: '#ffe9aa', 0xf: '#ffc9aa'}
28 for i, c in sorted(samples.iteritems(), key=lambda a: a[0]):
29 assert_equals(c, cm.get_color(fakectx(sys.maxint / 16 * i, 0), 0))
30
31def test_user_hue_limit():
32 cm = colormap.AnnotateColorSaturation(maxhues=8)
33
34 samples = {0x0: '#ffaaaa', 0x1: '#ffaaaa', 0x2: '#ffaae9', 0x3: '#ffaae9',
35 0x4: '#d4aaff', 0x5: '#d4aaff', 0x6: '#aabfff', 0x7: '#aabfff',
36 0x8: '#aaffff', 0x9: '#aaffff', 0xa: '#aaffbf', 0xb: '#aaffbf',
37 0xc: '#d4ffaa', 0xd: '#d4ffaa', 0xe: '#ffe9aa', 0xf: '#ffe9aa'}
38 for i, c in sorted(samples.iteritems(), key=lambda a: a[0]):
39 assert_equals(c, cm.get_color(fakectx(sys.maxint / 16 * i, 0), 0))
40
41SECS_PER_DAY = 24 * 60 * 60
42
43def test_age_saturation():
44 """Is an age mapped to its saturation?"""
45 cm = colormap.AnnotateColorSaturation()
46
47 samples = {0: '#ffaaaa', 50: '#ffd4d4', 100: '#ffe2e2', 150: '#ffe9e9',
48 200: '#ffeeee', 250: '#fff0f0', 300: '#fff2f2', 350: '#fff4f4',
49 400: '#fff5f5', 450: '#fff6f6', 500: '#fff7f7', 550: '#fff7f7'}
50 for i, c in sorted(samples.iteritems(), key=lambda a: a[0]):
51 assert_equals(c, cm.get_color(fakectx(0, 0), float(i * SECS_PER_DAY)))
52
53def test_age_saturation_limit():
54 cm = colormap.AnnotateColorSaturation(maxsaturations=16)
55
56 samples = {0: '#ffaaaa', 3: '#ffaaaa', 4: '#ffafaf', 7: '#ffafaf',
57 8: '#ffb4b4', 11: '#ffb4b4', 12: '#ffb9b9', 16: '#ffb9b9',
58 17: '#ffbfbf', 22: '#ffbfbf', 23: '#ffc4c4', 29: '#ffc4c4'}
59 for i, c in sorted(samples.iteritems(), key=lambda a: a[0]):
60 assert_equals(c, cm.get_color(fakectx(0, 0), float(i * SECS_PER_DAY)))
61
62def test_age_calc():
63 """Color shouldn't depend on the date but the age"""
64 cm = colormap.AnnotateColorSaturation()
65 age = 50 * SECS_PER_DAY
66 for d in (0, SECS_PER_DAY, 365 * SECS_PER_DAY):
67 assert_equals('#ffd4d4', cm.get_color(fakectx(0, d), d + age))
68
69def test_makeannotatepalette_latest_wins():
70 userstep = sys.maxint / 16
71 filectxs = [fakectx(0 * userstep, 0), fakectx(1 * userstep, 1),
72 fakectx(2 * userstep, 2), fakectx(3 * userstep, 3),
73 fakectx(4 * userstep, 4)]
74
75 palette = colormap.makeannotatepalette(filectxs, now=4, maxcolors=4)
76 palfctxs = set()
77 for _color, fctxs in palette.iteritems():
78 palfctxs.update(fctxs)
79 assert_equals(set(filectxs[1:]), palfctxs)
80
81def test_makeannotatepalette_fold_same_color():
82 userstep = sys.maxint / 16
83 filectxs = [fakectx(0 * userstep, 0), fakectx(1 * userstep, 0),
84 fakectx(2 * userstep, 0), fakectx(3 * userstep, 0),
85 fakectx(4 * userstep, 0)]
86 palette = colormap.makeannotatepalette(filectxs, now=0,
87 maxcolors=4, maxhues=8)
88 assert_equals(3, len(palette))
89 assert_equals(set([filectxs[0], filectxs[1]]), set(palette['#ffaaaa']))
90 assert_equals(set([filectxs[2], filectxs[3]]), set(palette['#ffaae9']))
91 assert_equals(set([filectxs[4]]), set(palette['#d4aaff']))
92
93def test_makeannotatepalette_mindate_included():
94 agestep = 10 * SECS_PER_DAY
95 filectxs = [fakectx(0, 0 * agestep), fakectx(0, 1 * agestep),
96 fakectx(0, 2 * agestep), fakectx(0, 3 * agestep),
97 fakectx(0, 4 * agestep), fakectx(0, 5 * agestep),
98 fakectx(0, 6 * agestep), fakectx(0, 7 * agestep)]
99 palette = colormap.makeannotatepalette(filectxs, now=7 * agestep,
100 maxcolors=4, maxhues=4,
101 maxsaturations=255,
102 mindate=2 * agestep)
103 palfctxs = set()
104 for _color, fctxs in palette.iteritems():
105 palfctxs.update(fctxs)
106 for fctx in filectxs[2:]:
107 assert fctx in palfctxs
108
109def test_makeannotatepalette_mindate_earlier_than_rev0():
110 agestep = 50 * SECS_PER_DAY
111 filectxs = [fakectx(0, 1 * agestep), fakectx(0, 2 * agestep)]
112 palette = colormap.makeannotatepalette(filectxs, now=2 * agestep,
113 maxcolors=1, maxhues=1,
114 maxsaturations=255, mindate=0)
115 palfctxs = set()
116 for _color, fctxs in palette.iteritems():
117 palfctxs.update(fctxs)
118 for fctx in filectxs:
119 assert fctx in palfctxs