summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rss/test_image.rb
blob: d8f0b26103d768bfb1281c382eb8c0372046922d (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
require "cgi"
require "rexml/document"

require_relative "rss-testcase"

require "rss/1.0"
require "rss/image"

module RSS
  class TestImage < TestCase

    def setup
      @prefix = "image"
      @uri = "http://purl.org/rss/1.0/modules/image/"

      @favicon_attrs = {
        "rdf:about" => "http://www.kuro5hin.org/favicon.ico",
        "#{@prefix}:size" => "small",
      }
      @favicon_contents = {"dc:title" => "Kuro5hin",}
      @items = [
        [
          {
            "rdf:about" => "http://www.example.org/item.png",
            "rdf:resource" => "http://www.example.org/item",
          },
          {
            "dc:title" => "Example Image",
            "#{@prefix}:width" => "100",
            "#{@prefix}:height" => "65",
          },
        ],
        [
          {
            "rdf:about" => "http://www.kuro5hin.org/images/topics/culture.jpg",
          },
          {
            "dc:title" => "Culture",
            "#{@prefix}:width" => "80",
            "#{@prefix}:height" => "50",
          },
        ]
      ]


      @channel_nodes = make_element("#{@prefix}:favicon",
                                    @favicon_attrs,
                                    @favicon_contents)
      items = ""
      @items.each do |attrs, contents|
        image_item = make_element("#{@prefix}:item", attrs, contents)
        items << make_item(image_item)
      end

      @ns = {
        @prefix => @uri,
        DC_PREFIX => DC_URI,
      }
      @rss_source = make_RDF(<<-EOR, @ns)
#{make_channel(@channel_nodes)}
#{make_image}
#{items}
#{make_textinput}
EOR

      @rss = Parser.parse(@rss_source)
    end

    def test_parser
      assert_nothing_raised do
        Parser.parse(@rss_source)
      end

      assert_too_much_tag("favicon", "channel") do
        Parser.parse(make_RDF(<<-EOR, @ns))
#{make_channel(@channel_nodes * 2)}
#{make_item}
EOR
      end

      attrs = {"rdf:about" => "http://www.example.org/item.png"}
      contents = [["#{@prefix}:width", "80"]] * 5
      image_item = make_element("#{@prefix}:item", attrs, contents)
      assert_too_much_tag("width", "item") do
        Parser.parse(make_RDF(<<-EOR, @ns))
#{make_channel}
#{make_item(image_item)}
EOR
      end
    end

    def test_favicon_accessor
      favicon = @rss.channel.image_favicon
      [
        %w(about rdf:about http://example.com/favicon.ico),
        %w(size image:size large),
        %w(image_size image:size medium),
      ].each do |name, full_name, new_value|
        assert_equal(@favicon_attrs[full_name], favicon.__send__(name))
        favicon.__send__("#{name}=", new_value)
        assert_equal(new_value, favicon.__send__(name))
        favicon.__send__("#{name}=", @favicon_attrs[full_name])
        assert_equal(@favicon_attrs[full_name], favicon.__send__(name))
      end

      %w(small medium large).each do |value|
        assert_nothing_raised do
          favicon.size = value
          favicon.image_size = value
        end
      end

      %w(aaa AAA SMALL MEDIUM LARGE).each do |value|
        args = ["#{@prefix}:favicon", value, "#{@prefix}:size"]
        assert_not_available_value(*args) do
          favicon.size = value
        end
        assert_not_available_value(*args) do
          favicon.image_size = value
        end
      end

      [
        %w(dc_title dc:title sample-favicon),
      ].each do |name, full_name, new_value|
        assert_equal(@favicon_contents[full_name], favicon.__send__(name))
        favicon.__send__("#{name}=", new_value)
        assert_equal(new_value, favicon.__send__(name))
        favicon.__send__("#{name}=", @favicon_contents[full_name])
        assert_equal(@favicon_contents[full_name], favicon.__send__(name))
      end
    end

    def test_item_accessor
      @rss.items.each_with_index do |item, i|
        image_item = item.image_item
        attrs, contents = @items[i]
        [
          %w(about rdf:about http://example.com/image.png),
          %w(resource rdf:resource http://example.com/),
        ].each do |name, full_name, new_value|
          assert_equal(attrs[full_name], image_item.__send__(name))
          image_item.__send__("#{name}=", new_value)
          assert_equal(new_value, image_item.__send__(name))
          image_item.__send__("#{name}=", attrs[full_name])
          assert_equal(attrs[full_name], image_item.__send__(name))
        end

        [
          ["width", "image:width", "111"],
          ["image_width", "image:width", "44"],
          ["height", "image:height", "222"],
          ["image_height", "image:height", "88"],
        ].each do |name, full_name, new_value|
          assert_equal(contents[full_name].to_i, image_item.__send__(name))
          image_item.__send__("#{name}=", new_value)
          assert_equal(new_value.to_i, image_item.__send__(name))
          image_item.__send__("#{name}=", contents[full_name])
          assert_equal(contents[full_name].to_i, image_item.__send__(name))
        end

        [
          ["dc_title", "dc:title", "sample-image"],
        ].each do |name, full_name, new_value|
          assert_equal(contents[full_name], image_item.__send__(name))
          image_item.__send__("#{name}=", new_value)
          assert_equal(new_value, image_item.__send__(name))
          image_item.__send__("#{name}=", contents[full_name])
          assert_equal(contents[full_name], image_item.__send__(name))
        end
      end
    end

    def test_favicon_to_s
      favicon = @rss.channel.image_favicon
      expected_xml = image_xmlns_container(make_element("#{@prefix}:favicon",
                                                        @favicon_attrs,
                                                        @favicon_contents))
      expected = REXML::Document.new(expected_xml)
      actual_xml = image_xmlns_container(favicon.to_s(false, ""))
      actual = REXML::Document.new(actual_xml)
      assert_equal(expected.to_s, actual.to_s)
    end

    def test_item_to_s
      @rss.items.each_with_index do |item, i|
        attrs, contents = @items[i]
        expected_xml = make_element("#{@prefix}:item", attrs, contents)
        expected_xml = image_xmlns_container(expected_xml)
        expected = REXML::Document.new(expected_xml)
        actual_xml = image_xmlns_container(item.image_item.to_s(false, ""))
        actual = REXML::Document.new(actual_xml)

        assert_equal(expected[0].attributes, actual[0].attributes)

        %w(image:height image:width dc:title).each do |name|
          actual_target = actual.elements["//#{name}"]
          expected_target = expected.elements["//#{name}"]
          assert_equal(expected_target.to_s, actual_target.to_s)
        end
      end
    end

    private
    def image_xmlns_container(content)
      xmlns_container({
                        @prefix => @uri,
                        "dc" => "http://purl.org/dc/elements/1.1/",
                        "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                      },
                      content)
    end
  end
end