File: ffi-gobject_test.rb

package info (click to toggle)
ruby-gir-ffi 0.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,824 kB
  • sloc: ruby: 20,895; makefile: 90; sh: 1
file content (151 lines) | stat: -rw-r--r-- 4,238 bytes parent folder | download
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
# frozen_string_literal: true

require 'gir_ffi_test_helper'

describe GObject do
  before do
    GirFFI.setup :Regress
  end

  describe '::signal_emit' do
    it 'emits a signal' do
      a = 1
      o = Regress::TestSubObj.new
      prc = proc { a = 2 }
      callback = GObject::Callback.from prc
      ::GObject::Lib.g_signal_connect_data o, 'test', callback, nil, nil, 0
      GObject.signal_emit o, 'test'
      assert_equal 2, a
    end

    it 'handles return values' do
      s = Gio::SocketService.new

      argtypes = [:pointer, :pointer, :pointer, :pointer]
      callback = FFI::Function.new(:bool, argtypes) { |_a, _b, _c, _d| true }
      ::GObject::Lib.g_signal_connect_data s, 'incoming', callback, nil, nil, 0
      rv = GObject.signal_emit s, 'incoming'
      assert_equal true, rv
    end

    it 'passes in extra arguments' do
      o = Regress::TestSubObj.new
      sb = Regress::TestSimpleBoxedA.new
      sb.some_int8 = 31
      sb.some_double = 2.42
      sb.some_enum = :value2
      b2 = nil

      argtypes = [:pointer, :pointer, :pointer]
      callback = FFI::Function.new(:void, argtypes) do |_a, b, _c|
        b2 = b
      end
      ::GObject::Lib.g_signal_connect_data o, 'test-with-static-scope-arg', callback, nil, nil, 0
      GObject.signal_emit o, 'test-with-static-scope-arg', sb

      sb2 = Regress::TestSimpleBoxedA.wrap b2
      assert sb.equals(sb2)
    end

    it 'allows specifying signal detail' do
      a = 1
      o = Regress::TestSubObj.new

      callback = FFI::Function.new(:void, [:pointer, :pointer, :pointer]) { a = 2 }
      ::GObject::Lib.g_signal_connect_data o, 'notify::detail', callback, nil, nil, 0

      GObject.signal_emit o, 'notify::detail'

      a.must_equal 2
    end
  end

  describe '::signal_connect' do
    it 'installs a signal handler' do
      a = 1
      o = Regress::TestSubObj.new
      GObject.signal_connect(o, 'test') { a = 2 }
      GObject.signal_emit o, 'test'
      assert_equal 2, a
    end

    it 'passes user data to handler' do
      a = 1
      o = Regress::TestSubObj.new
      GObject.signal_connect(o, 'test', 2) { |_i, d| a = d }
      GObject.signal_emit o, 'test'
      assert_equal 2, a
    end

    it 'passes object to handler' do
      o = Regress::TestSubObj.new
      o2 = nil
      GObject.signal_connect(o, 'test') { |i, _d| o2 = i }
      GObject.signal_emit o, 'test'
      assert_instance_of Regress::TestSubObj, o2
      assert_equal o.to_ptr, o2.to_ptr
    end

    it 'does not allow connecting an invalid signal' do
      o = Regress::TestSubObj.new
      proc { GObject.signal_connect(o, 'not-really-a-signal') {} }.
        must_raise GirFFI::SignalNotFoundError
    end

    it 'handles return values' do
      s = Gio::SocketService.new
      GObject.signal_connect(s, 'incoming') { true }
      rv = GObject.signal_emit s, 'incoming'
      assert_equal true, rv
    end

    it 'requires a block' do
      o = Regress::TestSubObj.new
      proc { GObject.signal_connect o, 'test' }.must_raise ArgumentError
    end

    it 'allows specifying signal detail' do
      a = 1
      o = Regress::TestSubObj.new
      GObject.signal_connect(o, 'notify::detail', 2) { |_i, _, d| a = d }
      GObject.signal_emit o, 'notify::detail'
      assert_equal 2, a
    end

    describe 'connecting a signal with extra arguments' do
      before do
        @a = nil
        @b = 2

        o = Regress::TestSubObj.new
        sb = Regress::TestSimpleBoxedA.new
        sb.some_int = 23

        GObject.signal_connect(o, 'test-with-static-scope-arg', 2) do |_instance, object, user_data|
          @a = user_data
          @b = object
        end
        GObject.signal_emit o, 'test-with-static-scope-arg', sb
      end

      it 'passes on the user data argument' do
        assert_equal 2, @a
      end

      it 'passes on the extra arguments' do
        assert_instance_of Regress::TestSimpleBoxedA, @b
        assert_equal 23, @b.some_int
      end
    end
  end

  describe '::signal_connect_after' do
    it 'installs a signal handler' do
      a = 1
      o = Regress::TestSubObj.new
      GObject.signal_connect_after(o, 'test') { a = 2 }
      GObject.signal_emit o, 'test'
      assert_equal 2, a
    end
  end
end