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
|
"""
Python scripts to test GimpPalette class of libgimp 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 = Gimp.context_get_foreground()
assert success
"""
Ensure setup was done.
"""
if Gimp.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 = Gimp.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.get_entry_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 = Gimp.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 gimp 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.get_entry_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.get_entry_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.get_entry_name(1)
assert not success
assert name == None
"""
Test that an instance that we created in previous test runs persists.
"""
palette_persisted = Gimp.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 Gimp.Palette using the class constructor.
(Only using the new() method.)
We only envision plugins getting a Gimp.Palette from a dialog or from Gimp.Context.
Expect error dialog "A plugin is trying to use a resource that is no longer installed."
"""
palette_invalid = Gimp.Palette() # class constructor in Python
palette_invalid.set_property("id", "invalid name")
assert palette_invalid.is_editable() == True
|