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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
|
""" Test suite for affine transforms.
:Author: Eric Jones, Enthought, Inc., eric@enthought.com
:Copyright: Space Telescope Science Institute
:License: BSD Style
So far, this is mainly a "smoke test" suite to make sure
nothing is obviously wrong.
"""
from __future__ import with_statement
import unittest
from numpy import alltrue, array, ravel
from kiva import affine
from kiva import basecore2d
from kiva import constants
class test_is_fully_transparent(unittest.TestCase):
def test_simple(self):
self.assert_(basecore2d.is_fully_transparent([1, 1, 1, 0]))
self.assert_(not basecore2d.is_fully_transparent([0, 0, 0, 1]))
self.assert_(not basecore2d.is_fully_transparent([0, 0, 0, .5]))
class test_fill_equal(unittest.TestCase):
def test_simple(self):
self.assert_(basecore2d.fill_equal(array([0, 0, 0, 0]),
array([0, 0, 0, 0])))
self.assert_(not basecore2d.fill_equal(array([0, 0, 0, 0]),
array([0, 0, 0, 1])))
self.assert_(not basecore2d.fill_equal(array([0, 0, 0, 0]),
array([1, 0, 0, 0])))
class LineStateTestCase(unittest.TestCase):
def create_ls(self):
color = array([0, 0, 0, 1])
width = 2
join = basecore2d.JOIN_MITER
cap = basecore2d.CAP_ROUND
phase = 0
pattern = array([5, 5])
dash = (phase, pattern)
ls = basecore2d.LineState(color, width, cap, join, dash)
return ls
def test_create(self):
self.create_ls()
def test_color_on_copy(self):
# The following test to make sure that a copy
# was actually made of the line_color container(array).
# If it isn't, both ls1 and ls2 will point at the same
# data, and the change to the color affects both
# line_states instead of just ls1.
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_color[1] = 10
self.assert_(not basecore2d.line_state_equal(ls1, ls2))
def test_dash_on_copy(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_dash[1][0] = 10
self.assert_(not basecore2d.line_state_equal(ls1, ls2))
def test_cmp_for_different_length_dash_patterns(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_dash = (ls1.line_dash[0], array([10, 10, 10, 10]))
self.assert_(not basecore2d.line_state_equal(ls1, ls2))
def test_cmp(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
self.assert_(basecore2d.line_state_equal(ls1, ls2))
#line_dash no longer allowed to be none.
#def test_cmp_with_dash_as_none(self):
# ls1 = self.create_ls()
# ls2 = ls1.copy()
# #ls1.line_dash = None
# assert(not basecore2d.line_state_equal(ls1,ls2))
class GraphicsContextTestCase(unittest.TestCase):
def test_create_gc(self):
gc = basecore2d.GraphicsContextBase()
#----------------------------------------------------------------
# Test ctm transformations
#----------------------------------------------------------------
def test_get_ctm(self):
gc = basecore2d.GraphicsContextBase()
# default ctm should be identity matrix.
desired = affine.affine_identity()
actual = gc.get_ctm()
self.assert_(alltrue(ravel(actual == desired)))
def test_scale_ctm(self):
gc = basecore2d.GraphicsContextBase()
ident = affine.affine_identity()
sx, sy = 2., 3.
desired = affine.scale(ident, sx, sy)
gc.scale_ctm(sx, sy)
actual = gc.get_ctm()
self.assert_(alltrue(ravel(actual == desired)))
def test_rotate_ctm(self):
gc = basecore2d.GraphicsContextBase()
ident = affine.affine_identity()
angle = 2.
desired = affine.rotate(ident, angle)
gc.rotate_ctm(angle)
actual = gc.get_ctm()
self.assert_(alltrue(ravel(actual == desired)))
def test_translate_ctm(self):
gc = basecore2d.GraphicsContextBase()
ident = affine.affine_identity()
x, y = 2., 3.
desired = affine.translate(ident, x, y)
gc.translate_ctm(x, y)
actual = gc.get_ctm()
self.assert_(alltrue(ravel(actual == desired)))
def test_concat_ctm(self):
gc = basecore2d.GraphicsContextBase()
ident = affine.affine_identity()
trans = affine.affine_from_rotation(2.)
x, y = 2., 3.
desired = affine.concat(ident, trans)
gc.concat_ctm(trans)
actual = gc.get_ctm()
self.assert_(alltrue(ravel(actual == desired)))
#-------------------------------------------------------------------------
# Setting drawing state variables
#
# These tests also check that the value is restored correctly with
# save/restore state.
#
# Checks are only done to see if variables are represented correctly in
# the graphics state. The effects on graphics rendering for these
# variables is not checked. That is all pretty much handled in the
# device_update_line_state and device_update_fill_state routines which
# access the state variables.
#
# Note: The tests peek into the state object to see if the if state
# variables are set. This ain't perfect, but core2d doesn't
# define accessor functions...
#-------------------------------------------------------------------------
def test_state_antialias(self):
gc = basecore2d.GraphicsContextBase()
# defaults to 1
self.assertEqual(gc.state.antialias, 1)
gc.set_antialias(0)
gc.save_state()
gc.set_antialias(1)
self.assertEqual(gc.state.antialias, 1)
gc.restore_state()
self.assertEqual(gc.state.antialias, 0)
def test_state_line_width(self):
gc = basecore2d.GraphicsContextBase()
# defaults to 1
self.assertEqual(gc.state.line_width, 1)
gc.set_line_width(5)
gc.save_state()
gc.set_line_width(10)
self.assertEqual(gc.state.line_width, 10)
gc.restore_state()
self.assertEqual(gc.state.line_width, 5)
def test_state_line_join(self):
gc = basecore2d.GraphicsContextBase()
# defaults to JOIN_MITER
self.assertEqual(gc.state.line_join, constants.JOIN_MITER)
gc.set_line_join(constants.JOIN_BEVEL)
gc.save_state()
gc.set_line_join(constants.JOIN_ROUND)
self.assertEqual(gc.state.line_join, constants.JOIN_ROUND)
gc.restore_state()
self.assertEqual(gc.state.line_join, constants.JOIN_BEVEL)
# set_line_join should fail if one attempts to set a bad value.
self.assertRaises(ValueError, gc.set_line_join, (100,))
def test_state_miter_limit(self):
gc = basecore2d.GraphicsContextBase()
# defaults to 1.0
self.assertEqual(gc.state.miter_limit, 1.0)
gc.set_miter_limit(2.0)
gc.save_state()
gc.set_miter_limit(3.0)
self.assertEqual(gc.state.miter_limit, 3.0)
gc.restore_state()
self.assertEqual(gc.state.miter_limit, 2.0)
def test_state_line_cap(self):
gc = basecore2d.GraphicsContextBase()
# defaults to CAP_ROUND
self.assertEqual(gc.state.line_cap, constants.CAP_ROUND)
gc.set_line_cap(constants.CAP_BUTT)
gc.save_state()
gc.set_line_cap(constants.CAP_SQUARE)
self.assertEqual(gc.state.line_cap, constants.CAP_SQUARE)
gc.restore_state()
self.assertEqual(gc.state.line_cap, constants.CAP_BUTT)
# set_line_cap should fail if one attempts to set a bad value.
self.assertRaises(ValueError, gc.set_line_cap, (100,))
def test_state_line_dash(self):
gc = basecore2d.GraphicsContextBase()
# defaults to non-dashed line
self.assert_(not gc.state.is_dashed())
gc.set_line_dash([1.0, 2.0], phase=2.0)
gc.save_state()
gc.set_line_dash([3.0, 4.0])
self.assert_(gc.state.is_dashed())
self.assertEqual(gc.state.line_dash[0], 0)
self.assert_(alltrue(ravel(gc.state.line_dash[1] ==
array([3.0, 4.0]))))
gc.restore_state()
self.assert_(gc.state.is_dashed())
self.assertEqual(gc.state.line_dash[0], 2.0)
self.assert_(alltrue(ravel(gc.state.line_dash[1] ==
array([1.0, 2.0]))))
# pattern must be a container with atleast two values
self.assertRaises(ValueError, gc.set_line_cap, (100,))
self.assertRaises(ValueError, gc.set_line_cap, ([100],))
# phase must be positive.
self.assertRaises(ValueError, gc.set_line_cap, ([100, 200], -1))
def test_state_flatness(self):
gc = basecore2d.GraphicsContextBase()
# defaults to None
self.assertEqual(gc.state.flatness, None)
gc.set_flatness(1.0)
gc.save_state()
gc.set_flatness(2.0)
self.assertEqual(gc.state.flatness, 2.0)
gc.restore_state()
self.assertEqual(gc.state.flatness, 1.0)
def test_state_alpha(self):
gc = basecore2d.GraphicsContextBase()
# defaults to 1.0
self.assertEqual(gc.state.alpha, 1.0)
gc.set_alpha(0.0)
gc.save_state()
gc.set_alpha(0.5)
self.assertEqual(gc.state.alpha, 0.5)
gc.restore_state()
self.assertEqual(gc.state.alpha, 0.0)
def test_state_fill_color(self):
gc = basecore2d.GraphicsContextBase()
# defaults to [0,0,0,1]
self.assert_(alltrue(gc.state.fill_color == array([0, 0, 0, 1])))
gc.set_fill_color((0, 1, 0, 1))
gc.save_state()
gc.set_fill_color((1, 1, 1, 1))
self.assert_(alltrue(gc.state.fill_color == array([1, 1, 1, 1])))
gc.restore_state()
self.assert_(alltrue(gc.state.fill_color == array([0, 1, 0, 1])))
def test_state_stroke_color(self):
gc = basecore2d.GraphicsContextBase()
# defaults to [0,0,0,1]
self.assert_(alltrue(gc.state.line_color == array([0, 0, 0, 1])))
gc.set_stroke_color((0, 1, 0, 1))
gc.save_state()
gc.set_stroke_color((1, 1, 1, 1))
self.assert_(alltrue(gc.state.line_color == array([1, 1, 1, 1])))
gc.restore_state()
self.assert_(alltrue(gc.state.line_color == array([0, 1, 0, 1])))
def test_state_character_spacing(self):
gc = basecore2d.GraphicsContextBase()
# defaults to None
self.assertEqual(gc.state.character_spacing, 0.0)
gc.set_character_spacing(1.0)
gc.save_state()
gc.set_character_spacing(2.0)
self.assertEqual(gc.state.character_spacing, 2.0)
gc.restore_state()
self.assertEqual(gc.state.character_spacing, 1.0)
def test_state_text_drawing_mode(self):
gc = basecore2d.GraphicsContextBase()
# defaults to None
self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_FILL)
gc.set_text_drawing_mode(constants.TEXT_OUTLINE)
gc.save_state()
gc.set_text_drawing_mode(constants.TEXT_CLIP)
self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_CLIP)
gc.restore_state()
self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_OUTLINE)
# try an unacceptable value.
self.assertRaises(ValueError, gc.set_text_drawing_mode, (10,))
#-------------------------------------------------------------------------
# Use context manager for saving and restoring state.
#-------------------------------------------------------------------------
def test_state_context_manager(self):
gc = basecore2d.GraphicsContextBase()
# Set an assortment of state properties.
gc.set_antialias(0)
gc.set_line_width(5)
gc.set_fill_color((0, 1, 0, 1))
with gc:
# Change the state properties.
gc.set_antialias(1)
self.assertEqual(gc.state.antialias, 1)
gc.set_line_width(10)
self.assertEqual(gc.state.line_width, 10)
gc.set_fill_color((1, 1, 1, 1))
self.assert_(alltrue(gc.state.fill_color == array([1, 1, 1, 1])))
# Verify that we're back to the earlier settings.
self.assertEqual(gc.state.antialias, 0)
self.assertEqual(gc.state.line_width, 5)
self.assert_(alltrue(gc.state.fill_color == array([0, 1, 0, 1])))
def test_state_context_manager_nested(self):
gc = basecore2d.GraphicsContextBase()
# Set an assortment of state properties.
gc.set_antialias(0)
gc.set_line_width(5)
gc.set_fill_color((0, 1, 0, 1))
with gc:
# Change the state properties.
gc.set_antialias(1)
self.assertEqual(gc.state.antialias, 1)
gc.set_line_width(10)
self.assertEqual(gc.state.line_width, 10)
gc.set_fill_color((1, 1, 1, 1))
self.assert_(alltrue(gc.state.fill_color == array([1, 1, 1, 1])))
with gc:
# Change the state properties.
gc.set_antialias(0)
self.assertEqual(gc.state.antialias, 0)
gc.set_line_width(2)
self.assertEqual(gc.state.line_width, 2)
gc.set_fill_color((1, 1, 0, 1))
self.assert_(alltrue(gc.state.fill_color ==
array([1, 1, 0, 1])))
# Verify that we're back to the earlier settings.
self.assertEqual(gc.state.antialias, 1)
self.assertEqual(gc.state.line_width, 10)
self.assert_(alltrue(gc.state.fill_color == array([1, 1, 1, 1])))
# Verify that we're back to the earlier settings.
self.assertEqual(gc.state.antialias, 0)
self.assertEqual(gc.state.line_width, 5)
self.assert_(alltrue(gc.state.fill_color == array([0, 1, 0, 1])))
#-------------------------------------------------------------------------
# Begin/End Page
# These are implemented yet. The tests are just here to remind me that
# they need to be.
#-------------------------------------------------------------------------
def test_begin_page(self):
#just to let me know it needs implementation.
gc = basecore2d.GraphicsContextBase()
gc.begin_page()
def test_end_page(self):
#just to let me know it needs implementation.
gc = basecore2d.GraphicsContextBase()
gc.end_page()
#-------------------------------------------------------------------------
# flush/synchronize
# These are implemented yet. The tests are just here to remind me that
# they need to be.
#-------------------------------------------------------------------------
def test_synchronize(self):
#just to let me know it needs implementation.
gc = basecore2d.GraphicsContextBase()
gc.synchronize()
def test_flush(self):
#just to let me know it needs implementation.
gc = basecore2d.GraphicsContextBase()
gc.flush()
#-------------------------------------------------------------------------
# save/restore state.
#
# Note: These test peek into the state object to see if the if state
# variables are set. This ain't perfect, but core2d doesn't
# define accessor functions...
#
# items that need to be tested:
# ctm
# clip region (not implemented)
# line width
# line join
#
#-------------------------------------------------------------------------
def test_save_state_line_width(self):
gc = basecore2d.GraphicsContextBase()
gc.set_line_width(5)
gc.save_state()
gc.set_line_width(10)
self.assertEqual(gc.state.line_width, 10)
gc.restore_state()
self.assertEqual(gc.state.line_width, 5)
#-------------------------------------------------------------------------
# Test drawing path empty
#-------------------------------------------------------------------------
def test_is_path_empty1(self):
""" A graphics context should start with an empty path.
"""
gc = basecore2d.GraphicsContextBase()
self.assert_(gc.is_path_empty())
def test_is_path_empty2(self):
""" A path that has moved to a point, but still hasn't drawn
anything is empty.
"""
gc = basecore2d.GraphicsContextBase()
x, y = 1., 2.
gc.move_to(x, y)
self.assert_(gc.is_path_empty())
def test_is_path_empty3(self):
""" A path that has moved to a point multiple times, but hasn't drawn
anything is empty.
"""
gc = basecore2d.GraphicsContextBase()
x, y = 1., 2.
gc.move_to(x, y)
# this should create another path.
x, y = 1., 2.5
gc.move_to(x, y)
self.assert_(gc.is_path_empty())
def test_is_path_empty4(self):
""" We've added a line, so the path is no longer empty.
"""
gc = basecore2d.GraphicsContextBase()
x, y = 1., 2.
gc.move_to(x, y)
gc.line_to(x, y)
self.assert_(not gc.is_path_empty())
#-------------------------------------------------------------------------
# Test drawing path add line
#
# Testing needed!
#-------------------------------------------------------------------------
def test_add_line(self):
"""
"""
pass
##################################################
if __name__ == "__main__":
unittest.main()
|