Initial checkin of Pika from heckimp

This commit is contained in:
2023-09-25 15:35:21 -07:00
commit 891e999216
6761 changed files with 5240685 additions and 0 deletions

View File

@ -0,0 +1,263 @@
"""
Python scripts to test libpika API.
Specifically, test PikaResource class and subclasses.
Test methods common to most resource classes, and class methods.
Methods specific to each class are tested elsewhere.
Usage:
=====
This is not a PIKA plugin.
Just copy and paste into the PIKA Python Console.
Expect no exceptions.
You can quickly look for red text.
There are no messages from the test program,
only exceptions from assert to indicate a failure.
Requires PyGObject binding to libpika,
which the PIKA Python Console does automatically.
Trash:
=====
This test program may leave trash, especially for failed runs.
Trash means resources persisted by PIKA.
!!! Do not run this in a production environment (using PIKA to create graphics.)
You can run it in a PIKA development environment,
but you may need to clean up trash manually.
Deleting the .config/PIKA directory will clean up the trash.
Setup in the PIKA app:
=====================
Choose current resources to match what this test uses:
e.g. brush '2. Hardness 050'
Use the standard default initial resources for a clean build,
by deleting .config/PIKA.
Ensure test resources from prior runs are deleted
otherwise they interfere with testing i.e. name clash.
E.g. delete brush '2. Hardness 050 copy #1'
"""
"""
Test return value is-a resource.
Get resource from context
"""
brush = Pika.context_get_brush()
font = Pika.context_get_font()
gradient = Pika.context_get_gradient()
palette = Pika.context_get_palette()
pattern = Pika.context_get_pattern()
assert isinstance(brush, Pika.Brush)
assert isinstance(font, Pika.Font)
assert isinstance(gradient, Pika.Gradient)
assert isinstance(palette, Pika.Palette)
assert isinstance(pattern, Pika.Pattern)
"""
Test is_valid method
Expect True
"""
assert brush.is_valid()
assert font.is_valid()
assert gradient.is_valid()
assert palette.is_valid()
assert pattern.is_valid()
"""
Test get_id method
Expect string
"""
print( brush.get_id() )
print( font.get_id() )
print( gradient.get_id() )
print( palette.get_id() )
print( pattern.get_id() )
assert brush.get_id() == '2. Hardness 050'
assert font.get_id() == 'Sans-serif'
assert gradient.get_id() == 'FG to BG (RGB)'
assert palette.get_id() == 'Color History'
assert pattern.get_id() == 'Pine'
"""
Test resource as an arg
Pass the resource back to the context
Expect no exceptions
"""
assert Pika.context_set_brush(brush) == True
assert Pika.context_set_font(font) == True
assert Pika.context_set_gradient(gradient) == True
assert Pika.context_set_palette(palette) == True
assert Pika.context_set_pattern(pattern) == True
"""
Test new() methods
Not Font, Pattern.
Pass desired name.
"""
brush_new = Pika.Brush.new("New Brush")
# expect a valid brush object, not a name
assert brush_new.is_valid()
# expect id is the desired one (assuming no clash)
assert brush_new.get_id()=="New Brush"
gradient_new = Pika.Gradient.new("New Gradient")
assert gradient_new.is_valid()
assert gradient_new.get_id()=="New Gradient"
palette_new = Pika.Palette.new("New Palette")
assert palette_new.is_valid()
assert palette_new.get_id()=="New Palette"
"""
Test delete methods.
Delete the new resources we just made
After deletion, reference is invalid
"""
assert brush_new.delete() == True
assert brush_new.is_valid()==False
assert gradient_new.delete() == True
assert gradient_new.is_valid()==False
assert palette_new.delete() == True
assert palette_new.is_valid()==False
"""
Test copy methods.
Copy the original resources from context.
Assert that PIKA adds " copy"
"""
brush_copy = brush.duplicate()
assert brush_copy.is_valid()
assert brush_copy.get_id()=='2. Hardness 050 copy'
gradient_copy = gradient.duplicate()
assert gradient_copy.is_valid()
assert gradient_copy.get_id()=='FG to BG (RGB) copy'
palette_copy = palette.duplicate()
assert palette_copy.is_valid()
assert palette_copy.get_id()== 'Color History copy'
"""
Test rename methods.
Renaming returns a reference that must be kept. !!!
Rename existing copy to a desired name.
!!! assign to a new named var
"""
brush_renamed = brush_copy.rename("Renamed Brush")
assert brush_renamed.is_valid()
assert brush_renamed.get_id()=="Renamed Brush"
# assert renaming invalidates any old reference,
assert brush_copy.is_valid()==False
gradient_renamed = gradient_copy.rename("Renamed Gradient")
assert gradient_renamed.is_valid()
assert gradient_renamed.get_id()=="Renamed Gradient"
# assert renaming invalidates any old reference,
assert gradient_copy.is_valid()==False
palette_renamed = palette_copy.rename("Renamed Palette")
assert palette_renamed.is_valid()
assert palette_renamed.get_id()=="Renamed Palette"
# assert renaming invalidates any old reference,
assert palette_copy.is_valid()==False
"""
Test renaming when name is already in use.
If "Renamed Brush" is already in use, then the name will be
"Renamed Brush copy #1"
that is, renaming added suffix "#1"
This is due to core behavior, not libpika resource class per se,
so we don't test it here.
"""
"""
Test delete methods
Delete the renamed copies we just made
After deletion, reference is invalid
"""
brush_renamed.delete()
assert brush_renamed.is_valid()==False
gradient_renamed.delete()
assert gradient_renamed.is_valid()==False
palette_renamed.delete()
assert palette_renamed.is_valid()==False
'''
Test class method
!!! Expect no error dialog in PIKA.
The app code generated from foo.pdb should clear the GError.
'''
assert Pika.Brush.id_is_valid ("invalid name")==False
assert Pika.Font.id_is_valid ("invalid name")==False
assert Pika.Gradient.id_is_valid("invalid name")==False
assert Pika.Palette.id_is_valid ("invalid name")==False
assert Pika.Pattern.id_is_valid ("invalid name")==False
'''
Test constructor
Not something author's should do, but test it anyway
'''
brush2 = Pika.Brush()
font2 = Pika.Font()
gradient2 = Pika.Gradient()
palette2 = Pika.Palette()
pattern2 = Pika.Pattern()
assert isinstance(brush2, Pika.Brush)
assert isinstance(font2, Pika.Font)
assert isinstance(gradient2, Pika.Gradient)
assert isinstance(palette2, Pika.Palette)
assert isinstance(pattern2, Pika.Pattern)
# a created instance is invalid until it's ID is set
assert brush2.is_valid()==False
assert font2.is_valid()==False
assert gradient2.is_valid()==False
assert palette2.is_valid()==False
assert pattern2.is_valid()==False
"""
test set_property methods
Use a name that exists. Creating de novo a reference to an existing brush.
Reference is valid since it's data exists in app core
"""
brush2.set_property("id", "2. Star")
assert brush2.get_id()=="2. Star"
assert brush2.is_valid()
font2.set_property("id", "Sans-serif")
assert font2.get_id() =="Sans-serif"
assert font2.is_valid()
gradient2.set_property("id", 'FG to BG (RGB)')
assert gradient2.get_id() =='FG to BG (RGB)'
assert gradient2.is_valid()
palette2.set_property("id", 'Color History')
assert palette2.get_id() =='Color History'
assert palette2.is_valid()
pattern2.set_property("id", 'Pine')
assert pattern2.get_id() =='Pine'
assert pattern2.is_valid()

336
libpika/test/test_brush.py Normal file
View File

@ -0,0 +1,336 @@
"""
Python scripts to test PikaBrush class of libpika API.
See comments about usage and setup in test-resource-class.py
Setup: "2. Hardness 050" brush is selected.
Testing context_set_brush
and a few other tests of class methods
are in test_resource_class.py
FUTURE: revise comparison of floats to within epsilon
"""
"""
Test a known, stock, parametric brush "2. Hardness 050"
********************************************************************************
Numeric literals might change if pika devs change defaults.
"""
brush_default = Pika.context_get_brush()
print( brush_default.get_id() )
assert brush_default.get_id() == "2. Hardness 050"
assert brush_default.is_generated()
assert not brush_default.is_editable()
"""
Test getters of default, stock (not editable), parametric brush
"""
success, width, height, mask_bpp, color_bpp = brush_default.get_info()
print( width, height, mask_bpp, color_bpp )
assert success
assert width == 51
assert height == 51
assert mask_bpp == 1
# !!! Since parametric, not pixmap and color_bpp is zero
assert color_bpp == 0
success, width, height, mask_bpp, mask, color_bpp, pixels = brush_default.get_pixels()
print( width, height, mask_bpp, len(mask), color_bpp, len(pixels) )
assert success
assert width == 51
assert height == 51
assert mask_bpp == 1
# !!! Since parametric, not pixmap and color_bpp is zero
assert color_bpp == 0
assert len(mask) == 2601
assert len(pixels) == 0
# !!! spacing does not return success, only a value
spacing = brush_default.get_spacing()
print(spacing)
assert spacing == 10
# shape, radius, spikes, hardness, aspect_ratio, angle
success, shape = brush_default.get_shape()
assert success
print(shape)
assert shape == Pika.BrushGeneratedShape.CIRCLE
success, radius = brush_default.get_radius()
assert success
print(radius)
assert radius == 25.0
success, spikes = brush_default.get_spikes()
assert success
print(spikes)
assert spikes == 2
success, hardness = brush_default.get_hardness()
assert success
print(hardness)
assert hardness == 0.5
success, aspect_ratio = brush_default.get_aspect_ratio()
assert success
print(aspect_ratio)
assert aspect_ratio == 1.0
success, angle = brush_default.get_angle()
assert success
print(angle)
assert angle == 0.0
"""
Test set_spacing fails on not editable brush.
"""
success = brush_default.set_spacing(1)
assert not success
# spacing did not change
assert brush_default.get_spacing() == 10
"""
Test setters fail on non-editable brush.
When they fail, they return a value that is nullish
"""
success, returned_shape = brush_default.set_shape(Pika.BrushGeneratedShape.DIAMOND)
print(success, returned_shape)
assert not success
assert returned_shape is Pika.BrushGeneratedShape.CIRCLE
success, returned_radius = brush_default.set_radius(1.0)
print(success, returned_radius)
assert not success
assert returned_radius == 0.0
success, returned_spikes = brush_default.set_spikes(1.0)
print(success, returned_spikes)
assert not success
assert returned_spikes == 0
success, returned_hardness = brush_default.set_hardness(1.0)
print(success, returned_hardness)
assert not success
assert returned_hardness == 0.0
success, returned_aspect_ratio = brush_default.set_aspect_ratio(1.0)
print(success, returned_aspect_ratio)
assert not success
success, returned_angle = brush_default.set_angle(1.0)
assert not success
"""
Test non-parametric brush i.e. raster, and stock i.e. not editable
********************************************************************************
No method exists to get a specific brush.
But we can set the ID of a new brush.
"""
brush_raster = Pika.Brush()
brush_raster.set_property("id", "Acrylic 01")
assert brush_raster.get_id() == "Acrylic 01"
assert brush_raster.is_valid()
# raster brush is not parametric
assert not brush_raster.is_generated()
# This brush is not editable
assert not brush_raster.is_editable()
# Raster brush has spacing
spacing = brush_raster.get_spacing()
print(spacing)
assert spacing == 25
# Getters of attributes of parametric brush fail for raster brush
success, shape = brush_raster.get_shape()
assert not success
success, radius = brush_raster.get_radius()
assert not success
success, spikes = brush_raster.get_spikes()
assert not success
success, hardness = brush_raster.get_hardness()
assert not success
success, aspect_ratio = brush_raster.get_aspect_ratio()
assert not success
success, angle = brush_raster.get_angle()
assert not success
"""
Test raster brush of kind color has pixel data.
"""
brush_color = Pika.Brush()
brush_color.set_property("id", "z Pepper")
assert brush_color.is_valid()
success, width, height, mask_bpp, mask, color_bpp, pixels = brush_color.get_pixels()
print( width, height, mask_bpp, len(mask), color_bpp, len(pixels) )
assert success
# !!! Since is color and raster, has pixmap and color_bpp is non-zero
assert color_bpp == 3
assert len(pixels) > 0
"""
Test setting spacing on not editable, raster brush
"""
success = brush_raster.set_spacing(1)
assert not success
# spacing did not change
assert brush_raster.get_spacing() == 25
"""
Test setters fails on raster brush.
"""
success, returned_shape = brush_raster.set_shape(Pika.BrushGeneratedShape.DIAMOND)
print(success, returned_shape)
assert not success
# Returned value is integer 0, corresponding to CIRCLE enum
assert returned_shape is Pika.BrushGeneratedShape.CIRCLE
success, returned_radius = brush_raster.set_radius(1.0)
assert not success
success, returned_spikes = brush_raster.set_spikes(1.0)
assert not success
success, returned_hardness = brush_raster.set_hardness(1.0)
assert not success
success, returned_aspect_ratio = brush_raster.set_aspect_ratio(1.0)
assert not success
success, returned_angle = brush_raster.set_angle(1.0)
assert not success
"""
Test new brush
********************************************************************************
Parametric and editable
"""
brush_new = Pika.Brush.new("New Brush")
# is generated and editable
assert brush_new.is_generated()
assert brush_new.is_editable()
"""
Test setters on editable brush.
"""
success = brush_new.set_spacing(20)
assert success == True
spacing = brush_new.get_spacing()
assert spacing == 20
success, returned_shape = brush_new.set_shape(Pika.BrushGeneratedShape.DIAMOND)
print(success)
assert success == True
assert returned_shape == Pika.BrushGeneratedShape.DIAMOND
success, shape = brush_new.get_shape()
assert success == True
assert shape == Pika.BrushGeneratedShape.DIAMOND
success, returned_radius = brush_new.set_radius(4.0)
assert success == True
print(returned_radius)
assert returned_radius == 4.0
success, radius = brush_new.get_radius()
assert success == True
assert radius == 4.0
success, returned_spikes = brush_new.set_spikes(2)
assert success == True
assert returned_spikes == 2
success, spikes = brush_new.get_spikes()
assert success == True
assert spikes == 2
success, returned_hardness = brush_new.set_hardness(0.5)
assert success == True
print(returned_hardness)
assert returned_hardness == 0.5
success, hardness = brush_new.get_hardness()
assert success == True
assert hardness == 0.5
success, returned_aspect_ratio = brush_new.set_aspect_ratio(5.0)
assert success == True
print(returned_aspect_ratio)
assert returned_aspect_ratio == 5.0
success, aspect_ratio = brush_new.get_aspect_ratio()
assert success == True
assert aspect_ratio == 5.0
success, returned_angle = brush_new.set_angle(90.0)
assert success == True
print(returned_angle)
assert returned_angle == 90.0
success, angle = brush_new.get_angle()
assert success == True
assert angle == 90.0
"""
Test invalid enum for shape.
"""
# TODO it yields a TypeError
"""
Test clamping at upper limits.
"""
'''
Omitting this test for now. Possible bug.
The symptoms are that it takes a long time,
and then PIKA crashes.
success, returned_radius = brush_new.set_radius(40000)
assert success == True
# upper clamp
assert returned_radius == 32767.0
success, radius = brush_new.get_radius()
assert success == True
assert radius == 32767.0
'''
success, returned_spikes = brush_new.set_spikes(22)
assert success == True
assert returned_spikes == 20
success, spikes = brush_new.get_spikes()
assert success == True
assert spikes == 20
success, returned_hardness = brush_new.set_hardness(2.0)
assert success == True
assert returned_hardness == 1.0
success, hardness = brush_new.get_hardness()
assert success == True
assert hardness == 1.0
success, returned_aspect_ratio = brush_new.set_aspect_ratio(2000)
assert success == True
assert returned_aspect_ratio == 1000.0
success, aspect_ratio = brush_new.get_aspect_ratio()
assert success == True
assert aspect_ratio == 1000.0
success, returned_angle = brush_new.set_angle(270)
assert success == True
assert returned_angle == 90
success, angle = brush_new.get_angle()
assert success == True
assert angle == 90
"""
Cleanup
"""
success = brush_new.delete()
assert success == True

19
libpika/test/test_font.py Normal file
View File

@ -0,0 +1,19 @@
"""
Python scripts to test PikaFont class of libpika API.
See comments about usage and setup in test-resource-class.py
Setup: "Sans-serif" font is selected.
There are few methods on font.
Font has no other getter/setters and is not editable.
Tests of context_set_font and a few other class methods
are in test_resource_class.py
"""
"""
Test a known, stock font "Sans-serif"
"""
font_sans_serif = Pika.context_get_font()
assert font_sans_serif.get_id() == "Sans-serif"

View File

@ -0,0 +1,314 @@
"""
Python scripts to test PikaGradient class of libpika API.
See comments about usage and setup in test-resource-class.py
Setup: "FG to BG (RGB)" gradient is selected.
Testing context_get_foo is in test_resource_class.py
"""
"""
Create artifacts needed for testing.
"""
success, foreground_color = Pika.context_get_foreground()
assert success
success, background_color = Pika.context_get_background()
assert success
"""
Test a known, stock resource named 'FG to BG (RGB)'
!!! Test numeric literals must change if PIKA developers change the stock gradient.
"""
gradient_FG_BG = Pika.context_get_gradient()
print(gradient_FG_BG.get_id())
assert gradient_FG_BG.get_id() == 'FG to BG (RGB)'
assert gradient_FG_BG.is_valid() == True
print( gradient_FG_BG.get_number_of_segments() )
assert gradient_FG_BG.get_number_of_segments() == 1
print( gradient_FG_BG.segment_get_blending_function(0) )
success, blending_function = gradient_FG_BG.segment_get_blending_function(0)
assert success
assert blending_function == Pika.GradientSegmentType.LINEAR
print( gradient_FG_BG.segment_get_coloring_type(0) )
success, coloring_type = gradient_FG_BG.segment_get_coloring_type(0)
assert success
assert coloring_type == Pika.GradientSegmentColor.RGB
"""
Test stock gradient permissions:
is not editable
setters fail
delete fails
"""
# Stock gradient is not editable
assert not gradient_FG_BG.is_editable()
# Stock gradient is not editable so set color fails and has no effect
# The initial opacity is 100.0 percent
success, left_color, opacity = gradient_FG_BG.segment_get_left_color(0)
assert opacity == 100.0
# Attempt to change opacity to 0 fails
assert not gradient_FG_BG.segment_set_left_color(0, background_color, 0.0)
# opacity is unchanged
success, left_color, opacity = gradient_FG_BG.segment_get_left_color(0)
assert opacity == 100.0
# all other setters return error
assert not gradient_FG_BG.segment_set_right_color(0, background_color, 0.0)
success, position = gradient_FG_BG.segment_set_left_pos(0, 0.0)
assert not success
success, position = gradient_FG_BG.segment_set_right_pos(0, 0.0)
assert not success
success, position = gradient_FG_BG.segment_set_middle_pos(0, 0.0)
assert not success
# There is no setter for a single segment, use a range
assert not gradient_FG_BG.segment_range_set_coloring_type (0, 0, Pika.GradientSegmentColor.RGB)
assert not gradient_FG_BG.segment_range_set_blending_function(0, 0, Pika.GradientSegmentType.LINEAR)
# Cannot delete stock gradient
assert not gradient_FG_BG.delete()
"""
Test sampling of gradient.
"""
success, samples = gradient_FG_BG.get_uniform_samples(3, False)
print(samples)
assert success
# Each sample is a color quadruple RGBA
assert len(samples) == 12
# list of 3 sample positions, reverse the gradient
success, samples = gradient_FG_BG.get_custom_samples( [0.0, 0.5, 1.0], True)
assert success
# Each sample is a color quadruple RGBA
assert len(samples) == 12
"""
Test segment methods getters.
"""
success, left_color, opacity = gradient_FG_BG.segment_get_left_color(0)
print(left_color.r, left_color.g, left_color.b, left_color.a)
assert success
# Not supported: assert left_color == foreground_color
# black 0,0,0,1 opacity 1.0
assert left_color.r == 0.0
assert left_color.g == 0.0
assert left_color.b == 0.0
assert left_color.a == 1.0
assert opacity == 100.0
success, right_color, opacity = gradient_FG_BG.segment_get_right_color(0)
print(right_color.r, right_color.g, right_color.b, right_color.a)
assert success
# white 1,1,1,1 opacity 1.0
assert right_color.r == 1.0
assert right_color.g == 1.0
assert right_color.b == 1.0
assert right_color.a == 1.0
assert opacity == 100.0
success, left_pos = gradient_FG_BG.segment_get_left_pos(0)
print(left_pos)
assert left_pos == 0.0
success, right_pos = gradient_FG_BG.segment_get_right_pos(0)
print(right_pos)
assert right_pos == 1.0
success, middle_pos = gradient_FG_BG.segment_get_middle_pos(0)
print(middle_pos)
assert middle_pos == 0.5
success, blending_function = gradient_FG_BG.segment_get_blending_function(0)
assert success
assert blending_function == Pika.GradientSegmentType.LINEAR
success, coloring_type = gradient_FG_BG.segment_get_coloring_type (0)
assert success
assert coloring_type == Pika.GradientSegmentColor.RGB
"""
Test new gradient.
"""
gradient_new = Pika.Gradient.new("New Gradient")
# A new gradient has the requested name.
# Assuming it does not exist already because the tester followed setup correctly
assert gradient_new.get_id() == "New Gradient"
# a new gradient is editable
assert gradient_new.is_editable() == True
# a new gradient has one segment
assert gradient_new.get_number_of_segments() == 1
# attributes of a new gradient are defaulted.
success, left_color, opacity = gradient_FG_BG.segment_get_left_color(0)
print(left_color.r, left_color.g, left_color.b, left_color.a, opacity)
success, right_color, opacity = gradient_FG_BG.segment_get_right_color(0)
print(right_color.r, right_color.g, right_color.b, right_color.a, opacity)
"""
Test segment methods setters.
On the new gradient, which is editable.
"""
# setter succeeds
assert gradient_new.segment_set_left_color(0, background_color, 50.0)
# it changed the color from foreground black to background white
success, left_color, opacity = gradient_new.segment_get_left_color(0)
print(left_color.r, left_color.g, left_color.b, left_color.a)
assert left_color.r == 1.0
assert left_color.g == 1.0
assert left_color.b == 1.0
assert left_color.a == 0.5 # !!! opacity changes the alpha
assert opacity == 50.0
assert gradient_new.segment_set_right_color(0, foreground_color, 50.0)
success, right_color, opacity = gradient_new.segment_get_right_color(0)
print(right_color.r, right_color.g, right_color.b, right_color.a)
assert right_color.r == 0.0
assert right_color.g == 0.0
assert right_color.b == 0.0
assert right_color.a == 0.5
assert opacity == 50.0
success, left_pos = gradient_new.segment_set_left_pos(0, 0.01)
print(left_pos)
assert left_pos == 0.0
success, right_pos = gradient_new.segment_set_right_pos(0, 0.99)
print(right_pos)
assert right_pos == 1.0
success, middle_pos = gradient_new.segment_set_middle_pos(0, 0.49)
print(middle_pos)
assert middle_pos == 0.49
# There is no setter for a single segment, use a range
# Change to a different enum from existing
assert gradient_new.segment_range_set_coloring_type (0, 0, Pika.GradientSegmentColor.HSV_CW)
success, coloring_type = gradient_new.segment_get_coloring_type (0)
assert success
assert coloring_type == Pika.GradientSegmentColor.HSV_CW
assert gradient_new.segment_range_set_blending_function(0, 0, Pika.GradientSegmentType.CURVED)
success, blending_function = gradient_new.segment_get_blending_function (0)
assert success
assert blending_function == Pika.GradientSegmentType.CURVED
"""
Test segment range methods for splitting and flipping.
!!! Not fully testing the effect, whether segments where shuffled properly.
TODO test a range was flipped by testing for different color types
"""
assert gradient_new.segment_range_split_midpoint(0, 0)
# split one into two
assert gradient_new.get_number_of_segments() == 2
assert gradient_new.segment_range_flip(0, 1)
# flipping does not change count of segments
assert gradient_new.get_number_of_segments() == 2
assert gradient_new.segment_range_replicate(0, 1, 2)
# replicate two segments, first and second.
# replicate each into two, into four total
print( gradient_new.get_number_of_segments() )
assert gradient_new.get_number_of_segments() == 4
assert gradient_new.segment_range_split_midpoint(3, 3)
# We split last one of four, now have 5
assert gradient_new.get_number_of_segments() == 5
assert gradient_new.segment_range_split_midpoint(0, 0)
# We split first one of five, now have 6
assert gradient_new.get_number_of_segments() == 6
assert gradient_new.segment_range_split_uniform(1, 1, 3)
# We split second one of six into 3 parts, now have 8
assert gradient_new.get_number_of_segments() == 8
assert gradient_new.segment_range_delete(6, 6)
# We deleted seventh one of 8, not have seven
assert gradient_new.get_number_of_segments() == 7
# Move segment one left (minus), not compressing
actual_delta = gradient_new.segment_range_move(1, 1, -1.0, False)
print(actual_delta)
assert actual_delta == -0.0637499999
# Moving does not change the count of segments
assert gradient_new.get_number_of_segments() == 7
"""
Test the segment range functions that operate across the range.
TODO test the effect
"""
assert gradient_new.segment_range_redistribute_handles(0, 5)
assert gradient_new.segment_range_blend_colors(1, 4)
assert gradient_new.segment_range_blend_opacity(2, 3)
"""
Test segment methods setters: out of range.
"""
# Segment indexes that do not exist.
assert not gradient_new.segment_set_left_color(9, background_color, 50.0)
# This yields pika message, so don't always test
# assert not gradient_new.segment_set_left_color(-1, background_color, 50.0)
"""
Test delete gradient
Also cleans up artifacts of testing: the new gradient.
Otherwise it persists and interferes with repeated testing.
"""
gradient_new.delete()
assert not gradient_new.is_valid()
"""
Test that an instance that we created in previous test runs persists.
"""
# TODO fix this by creating it for the next test ession
#gradient_persisted = Pika.Gradient()
#gradient_persisted.set_property("id", "My Persisted Gradient")
#assert gradient_persisted.get_color_count() == 1
"""
Test invalid gradient
Rare. We do not envision plugins to be constructing Pika.Gradient using the class constructor.
(Only using the new() method.)
We only envision plugins getting a Pika.Gradient from a dialog or from Pika.Context.
Expect error dialog "A plugin is trying to use a resource that is no longer installed."
Commented out because it is tested in test-resource-class.py
and it requires interaction during testing.
"""
#gradient_invalid = Pika.Gradient() # class constructor in Python
#gradient_invalid.set_property("id", "invalid name")
#assert gradient_invalid.is_editable() == True

View File

@ -0,0 +1,156 @@
"""
Python scripts to test PikaPalette class of libpika API.
See comments about usage and setup in test-resource-class.py
Setup: "Bears" palette is selected.
Testing context_get_palette is in test_resource_class.py
"""
"""
Create artifacts needed for testing.
"""
success, foreground_color = Pika.context_get_foreground()
assert success
"""
Ensure setup was done.
"""
if Pika.context_get_palette().get_id() != "Bears" :
print("\n\n Setup improper: test requires palette Bears selected \n\n")
"""
Test a known, stock palette "Bears"
!!! Test numeric literals must change if the palette is changed.
"""
palette_bears = Pika.context_get_palette()
assert palette_bears.get_id() == "Bears"
assert palette_bears.get_color_count() == 256
assert palette_bears.get_columns() == 0
colors = palette_bears.get_colors()
assert len(colors) == 256
# color of first entry is as expected
success, color = palette_bears.entry_get_color(0)
assert success
assert color.r == 0.03137254901960784
assert color.g == 0.03137254901960784
assert color.b == 0.03137254901960784
"""
Test permissions on stock palette.
"""
# Stock palette is not editable
assert not palette_bears.is_editable()
# Stock palette cannot set columns
assert not palette_bears.set_columns(4)
# Stock palette cannot add entry
success, index = palette_bears.add_entry("foo", foreground_color)
assert success == False
# Stock palette cannot delete entry
assert not palette_bears.delete_entry(0)
"""
Test new palette.
"""
palette_new = Pika.Palette.new("Test New Palette")
# A new palette has the requested name.
# Assuming it does not exist already because the tester followed setup correctly
assert palette_new.get_id() == "Test New Palette"
# a new palette is editable
assert palette_new.is_editable() == True
# a new palette has zero colors
assert palette_new.get_color_count() == 0
# a new palette has an empty array of colors
colors = palette_new.get_colors()
assert len(colors) == 0
# a new palette displays with the default count of columns
assert palette_new.get_columns() == 0
# You can set columns even when empty of colors
success = palette_new.set_columns(12)
assert success == True
assert palette_new.get_columns() == 12
# Cannot set columns > 64
# Expect a pika error dialog "Calling error...out of range"
success = palette_new.set_columns(65)
assert not success
# after out of range, still has the original value
assert palette_new.get_columns() == 12
# Cannot set columns to negative number
# TODO
"""
Test add entry
"""
# You can add entries to a new palette
success, index = palette_new.add_entry("", foreground_color)
assert success == True
# The first index is 0
assert index == 0
# After adding an entry, color count is incremented
assert palette_new.get_color_count() == 1
# The color of the new entry equals what we passed
success, color = palette_new.entry_get_color(0)
assert success == True
assert foreground_color.r == color.r
# You can add the same color twice
success, index = palette_new.add_entry("", foreground_color)
assert success == True
assert index == 1
assert palette_new.get_color_count() == 2
# An added entry for which the provided name was empty string is empty string
# TODO C code seems to suggest that it should be named "Untitled"
success, name = palette_new.entry_get_name(1)
assert success
assert name == ""
"""
Test delete entry
"""
assert palette_new.delete_entry(1) == True
# Delete entry decrements the color count
assert palette_new.get_color_count() == 1
# A deleted entry no longer is gettable
success, name = palette_new.entry_get_name(1)
assert not success
assert name == None
"""
Test that an instance that we created in previous test runs persists.
"""
palette_persisted = Pika.Palette()
palette_persisted.set_property("id", "Test New Palette")
assert palette_persisted.get_color_count() == 1
"""
Test invalid palette
Rare. We do not envision plugins to be constructing Pika.Palette using the class constructor.
(Only using the new() method.)
We only envision plugins getting a Pika.Palette from a dialog or from Pika.Context.
Expect error dialog "A plugin is trying to use a resource that is no longer installed."
"""
palette_invalid = Pika.Palette() # class constructor in Python
palette_invalid.set_property("id", "invalid name")
assert palette_invalid.is_editable() == True

View File

@ -0,0 +1,42 @@
"""
Python to test PikaPattern class of libpika API.
See comments about usage and setup in test-resource-class.py
Paste into the Python Console and expect no exceptions.
Setup: "Pine" Pattern is selected.
Testing some class methods is in test_resource_class.py
Class has no setters.
Object is not editable.
Class has no is_editable() method.
"""
"""
Test a known, stock pattern "Pine"
!!! Test numeric literals must change if the pattern is changed.
"""
pattern_pine = Pika.context_get_pattern()
assert pattern_pine.get_id() == "Pine"
"""
Test getters
"""
success, width, height, bpp = pattern_pine.get_info()
print( width, height, bpp)
assert success
assert width == 64
assert height == 56
assert bpp == 3
success, width, height, bpp, pixels = pattern_pine.get_pixels()
print( len(pixels) )
assert success
assert width == 64
assert height == 56
assert bpp == 3
assert len(pixels) == 10752
# Not testing the pixels content