/tests/colormap_test.py

https://bitbucket.org/tortoisehg/hgtk/ · Python · 119 lines · 104 code · 11 blank · 4 comment · 4 complexity · 84e7ace96a0c610b278d17a26d08ff62 MD5 · raw file

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