File: test_raster_source_pattern.rb

package info (click to toggle)
ruby-cairo 1.17.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,532 kB
  • sloc: ruby: 11,997; ansic: 10,183; sh: 48; makefile: 4
file content (162 lines) | stat: -rw-r--r-- 4,691 bytes parent folder | download | duplicates (4)
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
require "cairo"
require "tempfile"

class RasterSourcePatternTest < Test::Unit::TestCase
  include Helper

  def setup
    only_pattern("RasterSource")
  end

  def test_acquire_and_release
    Cairo::ImageSurface.create(100, 100) do |surface|
      Cairo::Context.create(surface) do |context|
        context.set_source(1, 1, 1)
        context.paint

        called = []
        red_pattern = Cairo::RasterSourcePattern.new(100, 100)
        red_pattern.acquire do |pattern, target, extents|
          called << :acquire
          red_image = target.create_similar_image(extents.width, extents.height)
          red_image.set_device_offset(extents.x, extents.y)
          Cairo::Context.create(red_image) do |red_image_context|
            red_image_context.set_source(1, 0, 0)
            red_image_context.paint
          end
          red_image
        end
        red_pattern.release do |pattern, surface|
          called << :release
          surface.finish
        end
        context.set_source(red_pattern)
        context.rectangle(40, 40, 20, 20)
        context.fill
        assert_equal([:acquire, :release], called)
      end
      show_result(surface)
    end
  end

  class SnapshotTest < self
    def setup
      super
      only_surface("Recording")
    end

    def test_success
      Cairo::RecordingSurface.create(0, 0, 100, 100) do |surface|
        Cairo::Context.create(surface) do |context|
          called = []
          raster_source = Cairo::RasterSourcePattern.new(100, 100)
          raster_source.snapshot do |pattern|
            called << :snapshot
          end
          context.set_source(raster_source)
          context.rectangle(40, 40, 20, 20)
          context.fill
          assert_equal([:snapshot], called)
        end
        show_result(surface)
      end
    end

    def test_error
      Cairo::RecordingSurface.create(0, 0, 100, 100) do |surface|
        Cairo::Context.create(surface) do |context|
          called = []
          raster_source = Cairo::RasterSourcePattern.new(100, 100)
          raster_source.snapshot do |pattern|
            called << :snapshot
            raise NoMemoryError
          end
          context.set_source(raster_source)
          context.rectangle(40, 40, 20, 20)
          assert_raise(NoMemoryError) do
            context.fill
          end
          assert_equal([:snapshot], called)
        end
        show_result(surface)
      end
    end
  end

  class CopyTest < self
    def setup
      super
      only_surface("Script")
    end

    def test_success
      output = StringIO.new
      device = Cairo::ScriptDevice.new(output)
      Cairo::ScriptSurface.create(device, 100, 200) do |surface|
        Cairo::Context.create(surface) do |context|
          called = []

          raster_source = Cairo::RasterSourcePattern.new(100, 100)
          raster_source.acquire do |pattern, target, extents|
            called << :acquire
            red_image = target.create_similar_image(extents.width,
                                                    extents.height)
            red_image.set_device_offset(extents.x, extents.y)
            Cairo::Context.create(red_image) do |red_image_context|
              red_image_context.set_source(1, 0, 0)
              red_image_context.paint
            end
            red_image
          end
          raster_source.release do |pattern, surface|
            called << :release
            surface.finish
          end
          raster_source.copy do |pattern|
            called << :copy
          end

          context.set_source(raster_source)
          context.rectangle(40, 40, 20, 20)
          context.fill

          assert_equal([:copy, :acquire, :release], called)
        end
        show_result(surface)
      end
    end

    def test_error
      output = StringIO.new
      device = Cairo::ScriptDevice.new(output)
      Cairo::ScriptSurface.create(device, 100, 200) do |surface|
        Cairo::Context.create(surface) do |context|
          called = []

          raster_source = Cairo::RasterSourcePattern.new(100, 100)
          raster_source.copy do |pattern|
            called << :copy
            raise NoMemoryError
          end

          context.set_source(raster_source)
          context.rectangle(40, 40, 20, 20)
          assert_raise(NoMemoryError) do
            context.fill
          end

          assert_equal([:copy], called)
        end
        show_result(surface)
      end
    end
  end

  private
  def show_result(image_surface)
    return if ENV["RCAIRO_TEST_SHOW_RESULT"] != "yes"
    result = Tempfile.new("result")
    image_surface.write_to_png(result.path)
    system("display", result.path)
  end
end