Merge master.
[dcpomatic.git] / test / subrip_test.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
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"
26 #include "test.h"
27
28 using std::list;
29 using std::vector;
30 using std::string;
31 using boost::shared_ptr;
32 using boost::dynamic_pointer_cast;
33
34 /** Test SubRip::convert_time */
35 BOOST_AUTO_TEST_CASE (subrip_time_test)
36 {
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));
39 }
40
41 /** Test SubRip::convert_coordinate */
42 BOOST_AUTO_TEST_CASE (subrip_coordinate_test)
43 {
44         BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("foo:42"), 42);
45         BOOST_CHECK_EQUAL (SubRip::convert_coordinate ("X1:999"), 999);
46 }
47
48 /** Test SubRip::convert_content */
49 BOOST_AUTO_TEST_CASE (subrip_content_test)
50 {
51         list<string> c;
52         list<SubRipSubtitlePiece> p;
53         
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");
58         c.clear ();
59
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);
65         c.clear ();
66
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);
72         c.clear ();
73
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);
79         c.clear ();
80
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);
86         c.clear ();
87
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);
93         c.clear ();
94
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);
100         c.clear ();
101
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);
109         ++i;
110         BOOST_CHECK_EQUAL (i->text, "nesting");
111         BOOST_CHECK_EQUAL (i->bold, true);
112         BOOST_CHECK_EQUAL (i->italic, true);
113         ++i;
114         BOOST_CHECK_EQUAL (i->text, " of subtitles");
115         BOOST_CHECK_EQUAL (i->bold, true);
116         BOOST_CHECK_EQUAL (i->italic, false);
117         ++i;
118         c.clear ();
119 }
120
121 /** Test parsing of full SubRip file content */
122 BOOST_AUTO_TEST_CASE (subrip_parse_test)
123 {
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));
127
128         SubRip s (content);
129
130         vector<SubRipSubtitle>::const_iterator i = s._subtitles.begin();
131
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.");
137
138         ++i;
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);
145
146         ++i;
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);
153
154         ++i;
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?");
160
161         ++i;
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?");
167
168         ++i;
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.");
174
175         ++i;
176         BOOST_CHECK (i == s._subtitles.end ());
177 }
178
179 /** Test rendering of a SubRip subtitle */
180 BOOST_AUTO_TEST_CASE (subrip_render_test)
181 {
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));
185
186         shared_ptr<Film> film = new_test_film ("subrip_render_test");
187
188         shared_ptr<SubRipDecoder> decoder (new SubRipDecoder (content));
189         list<shared_ptr<ContentTextSubtitle> > cts = decoder->get_text_subtitles (ContentTime::from_seconds (109), ContentTime::from_seconds (110));
190         BOOST_CHECK_EQUAL (cts.size(), 1);
191
192         PositionImage image = render_subtitles (cts.front()->subs, dcp::Size (1998, 1080));
193         write_image (image.image, "build/test/subrip_render_test.png");
194         check_file ("build/test/subrip_render_test.png", "test/data/subrip_render_test.png");
195 }