1require "cgi"
2require "rexml/document"
3
4require "rss-testcase"
5
6require "rss/1.0"
7require "rss/image"
8
9module RSS
10  class TestImage < TestCase
11
12    def setup
13      @prefix = "image"
14      @uri = "http://purl.org/rss/1.0/modules/image/"
15
16      @favicon_attrs = {
17        "rdf:about" => "http://www.kuro5hin.org/favicon.ico",
18        "#{@prefix}:size" => "small",
19      }
20      @favicon_contents = {"dc:title" => "Kuro5hin",}
21      @items = [
22        [
23          {
24            "rdf:about" => "http://www.example.org/item.png",
25            "rdf:resource" => "http://www.example.org/item",
26          },
27          {
28            "dc:title" => "Example Image",
29            "#{@prefix}:width" => "100",
30            "#{@prefix}:height" => "65",
31          },
32        ],
33        [
34          {
35            "rdf:about" => "http://www.kuro5hin.org/images/topics/culture.jpg",
36          },
37          {
38            "dc:title" => "Culture",
39            "#{@prefix}:width" => "80",
40            "#{@prefix}:height" => "50",
41          },
42        ]
43      ]
44
45
46      @channel_nodes = make_element("#{@prefix}:favicon",
47                                    @favicon_attrs,
48                                    @favicon_contents)
49      items = ""
50      @items.each do |attrs, contents|
51        image_item = make_element("#{@prefix}:item", attrs, contents)
52        items << make_item(image_item)
53      end
54
55      @ns = {
56        @prefix => @uri,
57        DC_PREFIX => DC_URI,
58      }
59      @rss_source = make_RDF(<<-EOR, @ns)
60#{make_channel(@channel_nodes)}
61#{make_image}
62#{items}
63#{make_textinput}
64EOR
65
66      @rss = Parser.parse(@rss_source)
67    end
68
69    def test_parser
70      assert_nothing_raised do
71        Parser.parse(@rss_source)
72      end
73
74      assert_too_much_tag("favicon", "channel") do
75        Parser.parse(make_RDF(<<-EOR, @ns))
76#{make_channel(@channel_nodes * 2)}
77#{make_item}
78EOR
79      end
80
81      attrs = {"rdf:about" => "http://www.example.org/item.png"}
82      contents = [["#{@prefix}:width", "80"]] * 5
83      image_item = make_element("#{@prefix}:item", attrs, contents)
84      assert_too_much_tag("width", "item") do
85        Parser.parse(make_RDF(<<-EOR, @ns))
86#{make_channel}
87#{make_item(image_item)}
88EOR
89      end
90    end
91
92    def test_favicon_accessor
93      favicon = @rss.channel.image_favicon
94      [
95        %w(about rdf:about http://example.com/favicon.ico),
96        %w(size image:size large),
97        %w(image_size image:size medium),
98      ].each do |name, full_name, new_value|
99        assert_equal(@favicon_attrs[full_name], favicon.__send__(name))
100        favicon.__send__("#{name}=", new_value)
101        assert_equal(new_value, favicon.__send__(name))
102        favicon.__send__("#{name}=", @favicon_attrs[full_name])
103        assert_equal(@favicon_attrs[full_name], favicon.__send__(name))
104      end
105
106      %w(small medium large).each do |value|
107        assert_nothing_raised do
108          favicon.size = value
109          favicon.image_size = value
110        end
111      end
112
113      %w(aaa AAA SMALL MEDIUM LARGE).each do |value|
114        args = ["#{@prefix}:favicon", value, "#{@prefix}:size"]
115        assert_not_available_value(*args) do
116          favicon.size = value
117        end
118        assert_not_available_value(*args) do
119          favicon.image_size = value
120        end
121      end
122
123      [
124        %w(dc_title dc:title sample-favicon),
125      ].each do |name, full_name, new_value|
126        assert_equal(@favicon_contents[full_name], favicon.__send__(name))
127        favicon.__send__("#{name}=", new_value)
128        assert_equal(new_value, favicon.__send__(name))
129        favicon.__send__("#{name}=", @favicon_contents[full_name])
130        assert_equal(@favicon_contents[full_name], favicon.__send__(name))
131      end
132    end
133
134    def test_item_accessor
135      @rss.items.each_with_index do |item, i|
136        image_item = item.image_item
137        attrs, contents = @items[i]
138        [
139          %w(about rdf:about http://example.com/image.png),
140          %w(resource rdf:resource http://example.com/),
141        ].each do |name, full_name, new_value|
142          assert_equal(attrs[full_name], image_item.__send__(name))
143          image_item.__send__("#{name}=", new_value)
144          assert_equal(new_value, image_item.__send__(name))
145          image_item.__send__("#{name}=", attrs[full_name])
146          assert_equal(attrs[full_name], image_item.__send__(name))
147        end
148
149        [
150          ["width", "image:width", "111"],
151          ["image_width", "image:width", "44"],
152          ["height", "image:height", "222"],
153          ["image_height", "image:height", "88"],
154        ].each do |name, full_name, new_value|
155          assert_equal(contents[full_name].to_i, image_item.__send__(name))
156          image_item.__send__("#{name}=", new_value)
157          assert_equal(new_value.to_i, image_item.__send__(name))
158          image_item.__send__("#{name}=", contents[full_name])
159          assert_equal(contents[full_name].to_i, image_item.__send__(name))
160        end
161
162        [
163          ["dc_title", "dc:title", "sample-image"],
164        ].each do |name, full_name, new_value|
165          assert_equal(contents[full_name], image_item.__send__(name))
166          image_item.__send__("#{name}=", new_value)
167          assert_equal(new_value, image_item.__send__(name))
168          image_item.__send__("#{name}=", contents[full_name])
169          assert_equal(contents[full_name], image_item.__send__(name))
170        end
171      end
172    end
173
174    def test_favicon_to_s
175      favicon = @rss.channel.image_favicon
176      expected_xml = image_xmlns_container(make_element("#{@prefix}:favicon",
177                                                        @favicon_attrs,
178                                                        @favicon_contents))
179      expected = REXML::Document.new(expected_xml)
180      actual_xml = image_xmlns_container(favicon.to_s(false, ""))
181      actual = REXML::Document.new(actual_xml)
182      assert_equal(expected.to_s, actual.to_s)
183    end
184
185    def test_item_to_s
186      @rss.items.each_with_index do |item, i|
187        attrs, contents = @items[i]
188        expected_xml = make_element("#{@prefix}:item", attrs, contents)
189        expected_xml = image_xmlns_container(expected_xml)
190        expected = REXML::Document.new(expected_xml)
191        actual_xml = image_xmlns_container(item.image_item.to_s(false, ""))
192        actual = REXML::Document.new(actual_xml)
193
194        assert_equal(expected[0].attributes, actual[0].attributes)
195
196        %w(image:height image:width dc:title).each do |name|
197          actual_target = actual.elements["//#{name}"]
198          expected_target = expected.elements["//#{name}"]
199          assert_equal(expected_target.to_s, actual_target.to_s)
200        end
201      end
202    end
203
204    private
205    def image_xmlns_container(content)
206      xmlns_container({
207                        @prefix => @uri,
208                        "dc" => "http://purl.org/dc/elements/1.1/",
209                        "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
210                      },
211                      content)
212    end
213  end
214end
215