2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <boost/test/unit_test.hpp>
21 #include <dcp/subtitle_content.h>
22 #include "lib/subrip.h"
23 #include "lib/subrip_content.h"
24 #include "lib/subrip_decoder.h"
25 #include "lib/render_subtitles.h"
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
34 /** Test SubRip::convert_time */
35 BOOST_AUTO_TEST_CASE (subrip_time_test)
37 BOOST_CHECK_EQUAL (SubRip::convert_time ("00:03:10,500"), ContentTime::from_seconds ((3 * 60) + 10 + 0.5));
38 BOOST_CHECK_EQUAL (SubRip::convert_time ("04:19:51,782"), ContentTime::from_seconds ((4 * 3600) + (19 * 60) + 51 + 0.782));
41 /** Test SubRip::convert_coordinate */
42 BOOST_AUTO_TEST_CASE (subrip_coordinate_test)
44 BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("foo:42"), 42);
45 BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("X1:999"), 999);
48 /** Test SubRip::convert_content */
49 BOOST_AUTO_TEST_CASE (subrip_content_test)
52 list<SubRipSubtitlePiece> p;
54 c.push_back ("Hello world");
55 p = SubRip::convert_content (c);
56 BOOST_CHECK_EQUAL (p.size(), 1);
57 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
60 c.push_back ("<b>Hello world</b>");
61 p = SubRip::convert_content (c);
62 BOOST_CHECK_EQUAL (p.size(), 1);
63 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
64 BOOST_CHECK_EQUAL (p.front().bold, true);
67 c.push_back ("<i>Hello world</i>");
68 p = SubRip::convert_content (c);
69 BOOST_CHECK_EQUAL (p.size(), 1);
70 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
71 BOOST_CHECK_EQUAL (p.front().italic, true);
74 c.push_back ("<u>Hello world</u>");
75 p = SubRip::convert_content (c);
76 BOOST_CHECK_EQUAL (p.size(), 1);
77 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
78 BOOST_CHECK_EQUAL (p.front().underline, true);
81 c.push_back ("{b}Hello world{/b}");
82 p = SubRip::convert_content (c);
83 BOOST_CHECK_EQUAL (p.size(), 1);
84 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
85 BOOST_CHECK_EQUAL (p.front().bold, true);
88 c.push_back ("{i}Hello world{/i}");
89 p = SubRip::convert_content (c);
90 BOOST_CHECK_EQUAL (p.size(), 1);
91 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
92 BOOST_CHECK_EQUAL (p.front().italic, true);
95 c.push_back ("{u}Hello world{/u}");
96 p = SubRip::convert_content (c);
97 BOOST_CHECK_EQUAL (p.size(), 1);
98 BOOST_CHECK_EQUAL (p.front().text, "Hello world");
99 BOOST_CHECK_EQUAL (p.front().underline, true);
102 c.push_back ("<b>This is <i>nesting</i> of subtitles</b>");
103 p = SubRip::convert_content (c);
104 BOOST_CHECK_EQUAL (p.size(), 3);
105 list<SubRipSubtitlePiece>::iterator i = p.begin ();
106 BOOST_CHECK_EQUAL (i->text, "This is ");
107 BOOST_CHECK_EQUAL (i->bold, true);
108 BOOST_CHECK_EQUAL (i->italic, false);
110 BOOST_CHECK_EQUAL (i->text, "nesting");
111 BOOST_CHECK_EQUAL (i->bold, true);
112 BOOST_CHECK_EQUAL (i->italic, true);
114 BOOST_CHECK_EQUAL (i->text, " of subtitles");
115 BOOST_CHECK_EQUAL (i->bold, true);
116 BOOST_CHECK_EQUAL (i->italic, false);
121 /** Test parsing of full SubRip file content */
122 BOOST_AUTO_TEST_CASE (subrip_parse_test)
124 shared_ptr<SubRipContent> content (new SubRipContent (shared_ptr<Film> (), "test/data/subrip.srt"));
125 content->examine (shared_ptr<Job> ());
126 BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
130 vector<SubRipSubtitle>::const_iterator i = s._subtitles.begin();
132 BOOST_CHECK (i != s._subtitles.end ());
133 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((1 * 60) + 49.200));
134 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((1 * 60) + 52.351));
135 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
136 BOOST_CHECK_EQUAL (i->pieces.front().text, "This is a subtitle, and it goes over two lines.");
139 BOOST_CHECK (i != s._subtitles.end ());
140 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((1 * 60) + 52.440));
141 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((1 * 60) + 54.351));
142 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
143 BOOST_CHECK_EQUAL (i->pieces.front().text, "We have emboldened this");
144 BOOST_CHECK_EQUAL (i->pieces.front().bold, true);
147 BOOST_CHECK (i != s._subtitles.end ());
148 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((1 * 60) + 54.440));
149 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((1 * 60) + 56.590));
150 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
151 BOOST_CHECK_EQUAL (i->pieces.front().text, "And italicised this.");
152 BOOST_CHECK_EQUAL (i->pieces.front().italic, true);
155 BOOST_CHECK (i != s._subtitles.end ());
156 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((1 * 60) + 56.680));
157 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((1 * 60) + 58.955));
158 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
159 BOOST_CHECK_EQUAL (i->pieces.front().text, "Shall I compare thee to a summers' day?");
162 BOOST_CHECK (i != s._subtitles.end ());
163 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((2 * 60) + 0.840));
164 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((2 * 60) + 3.400));
165 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
166 BOOST_CHECK_EQUAL (i->pieces.front().text, "Is this a dagger I see before me?");
169 BOOST_CHECK (i != s._subtitles.end ());
170 BOOST_CHECK_EQUAL (i->from, ContentTime::from_seconds ((3 * 60) + 54.560));
171 BOOST_CHECK_EQUAL (i->to, ContentTime::from_seconds ((3 * 60) + 56.471));
172 BOOST_CHECK_EQUAL (i->pieces.size(), 1);
173 BOOST_CHECK_EQUAL (i->pieces.front().text, "Hello world.");
176 BOOST_CHECK (i == s._subtitles.end ());
179 /** Test rendering of a SubRip subtitle */
180 BOOST_AUTO_TEST_CASE (subrip_render_test)
182 shared_ptr<SubRipContent> content (new SubRipContent (shared_ptr<Film> (), "test/data/subrip.srt"));
183 content->examine (shared_ptr<Job> ());
184 BOOST_CHECK_EQUAL (content->full_length(), DCPTime::from_seconds ((3 * 60) + 56.471));
186 shared_ptr<Film> film = new_test_film ("subrip_render_test");
188 shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
189 shared_ptr<DecodedTextSubtitle> dts = dynamic_pointer_cast<DecodedTextSubtitle> (decoder->peek ());
191 shared_ptr<Image> image;
192 Position<int> position;
193 render_subtitles (dts->subs, dcp::Size (1998, 1080), image, position);
194 write_image (image, "build/test/subrip_render_test.png");
195 check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");