File: test-binding.rb

package info (click to toggle)
ruby-gnome 4.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,648 kB
  • sloc: ruby: 67,701; ansic: 67,431; xml: 350; sh: 201; cpp: 45; makefile: 42
file content (178 lines) | stat: -rw-r--r-- 5,723 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
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
# Copyright (C) 2015-2021  Ruby-GNOME Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class TestGLibBinding < Test::Unit::TestCase
  include GLibTestUtils
  sub_test_case "bind_property" do
    class DataObjectDefault < GLib::Object
      type_register

      install_property(GLib::Param::Int.new("source", # name
                                            "Source", # nick
                                            "The source data", # blurb
                                            0,     # min
                                            100,   # max
                                            0,     # default
                                            GLib::Param::READABLE |
                                            GLib::Param::WRITABLE))
      install_property(GLib::Param::Int.new("target", # name
                                            "Target", # nick
                                            "The target data", # blurb
                                            0,     # min
                                            100,   # max
                                            0,     # default
                                            GLib::Param::READABLE |
                                            GLib::Param::WRITABLE))

      attr_reader :source, :target
      def initialize
        @source = 0
        @target = 0
        super
      end

      def source=(value)
        @source = value
        notify("source")
      end

      def target=(value)
        @target = value
        notify("target")
      end
    end

    setup do
      @source = DataObjectDefault.new
      @target = DataObjectDefault.new
      @binding = @source.bind_property("source", @target, "target", :default)
    end

    test "#source" do
      assert_equal(@source, @binding.source)
    end

    test "#source_property" do
      assert_equal("source", @binding.source_property)
    end

    test "#target" do
      assert_equal(@target, @binding.target)
    end

    test "#target_property" do
      assert_equal("target", @binding.target_property)
    end

    test "#flags" do
      assert_equal(GLib::BindingFlags::DEFAULT, @binding.flags)
    end

    test "#unbind" do
      assert_equal(0, @target.target)
      @source.source = 10
      assert_equal(10, @target.target)
      @binding.unbind
      @source.source = 20
      assert_equal(10, @target.target)
    end
  end
  sub_test_case "bind_property_full" do
    class DataObjectBidir < GLib::Object
      type_register

      install_property(GLib::Param::Int.new("source", # name
                                            "Source", # nick
                                            "The source data", # blurb
                                            0,     # min
                                            100,   # max
                                            0,     # default
                                            GLib::Param::READABLE |
                                            GLib::Param::WRITABLE))
      install_property(GLib::Param::String.new("target", # name
                                               "Target", # nick
                                               "The target data", # blurb
                                               "",     # default
                                               GLib::Param::READABLE |
                                               GLib::Param::WRITABLE))

      attr_reader :source, :target
      def initialize
        @source = 0
        @target = "nan"
        super
      end

      def source=(value)
        @source = value
        notify("source")
      end

      def target=(value)
        @target = value
        notify("target")
      end
    end

    setup do
      @source = DataObjectBidir.new
      @target = DataObjectBidir.new
      transform_to_callback = proc do |source_value|
        source_value.to_s
      end

      transform_from_callback = proc do |target_value|
        target_value.to_i
      end

      @binding = @source.bind_property("source", @target, "target",
                                       :bidirectional,
                                       :transform_to => transform_to_callback,
                                       :transform_from => transform_from_callback)
    end

    test "#source" do
      assert_equal(@source, @binding.source)
    end

    test "#source_property" do
      assert_equal("source", @binding.source_property)
    end

    test "#target" do
      assert_equal(@target, @binding.target)
    end

    test "#target_property" do
      assert_equal("target", @binding.target_property)
    end

    test "#flags" do
      assert_equal(GLib::BindingFlags::BIDIRECTIONAL, @binding.flags)
    end

    test "#unbind" do
      assert_equal("nan", @target.target)
      @source.source = 10
      assert_equal("10", @target.target)
      @target.target = "30"
      assert_equal(30, @source.source)
      @binding.unbind
      @source.source = 20
      assert_equal("30", @target.target)
    end
  end
end