f929da9c46f993f64b1596c27ad1493a6de376cf
[dcpomatic.git] / test / hints_test.cc
1 /*
2     Copyright (C) 2020-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "lib/content.h"
23 #include "lib/content_factory.h"
24 #include "lib/cross.h"
25 #include "lib/film.h"
26 #include "lib/font.h"
27 #include "lib/hints.h"
28 #include "lib/text_content.h"
29 #include "lib/util.h"
30 #include "test.h"
31 #include <boost/test/unit_test.hpp>
32
33
34 using std::make_shared;
35 using std::shared_ptr;
36 using std::string;
37 using std::vector;
38 using boost::optional;
39
40
41 vector<string> current_hints;
42
43
44 static
45 void
46 collect_hint (string hint)
47 {
48         current_hints.push_back (hint);
49 }
50
51
52 static
53 vector<string>
54 get_hints (shared_ptr<Film> film)
55 {
56         current_hints.clear ();
57         Hints hints (film);
58         hints.Hint.connect (collect_hint);
59         hints.start ();
60         hints.join ();
61         while (signal_manager->ui_idle()) {}
62         return current_hints;
63 }
64
65
66 static
67 void
68 check (TextType type, string name, optional<string> expected_hint = optional<string>())
69 {
70         auto film = new_test_film2 (name);
71         auto content = content_factory("test/data/" + name + ".srt").front();
72         content->text.front()->set_type (type);
73         content->text.front()->set_language (dcp::LanguageTag("en-US"));
74         film->examine_and_add_content (content);
75         BOOST_REQUIRE (!wait_for_jobs());
76         auto hints = get_hints (film);
77
78         if (expected_hint) {
79                 BOOST_REQUIRE_EQUAL (hints.size(), 1U);
80                 BOOST_CHECK_EQUAL (hints[0], *expected_hint);
81         } else {
82                 BOOST_CHECK (hints.empty());
83         }
84 }
85
86
87 BOOST_AUTO_TEST_CASE (hint_closed_caption_too_long)
88 {
89         check (
90                 TextType::CLOSED_CAPTION,
91                 "hint_closed_caption_too_long",
92                 String::compose("At least one of your closed caption lines has more than %1 characters.  It is advisable to make each line %1 characters at most in length.", MAX_CLOSED_CAPTION_LENGTH, MAX_CLOSED_CAPTION_LENGTH)
93               );
94 }
95
96
97 BOOST_AUTO_TEST_CASE (hint_many_closed_caption_lines)
98 {
99         check (
100                 TextType::CLOSED_CAPTION,
101                 "hint_many_closed_caption_lines",
102                 String::compose("Some of your closed captions span more than %1 lines, so they will be truncated.", MAX_CLOSED_CAPTION_LINES)
103               );
104 }
105
106
107 BOOST_AUTO_TEST_CASE (hint_subtitle_too_early)
108 {
109         check (
110                 TextType::OPEN_SUBTITLE,
111                 "hint_subtitle_too_early",
112                 string("It is advisable to put your first subtitle at least 4 seconds after the start of the DCP to make sure it is seen.")
113                 );
114 }
115
116
117 BOOST_AUTO_TEST_CASE (hint_short_subtitles)
118 {
119         check (
120                 TextType::OPEN_SUBTITLE,
121                 "hint_short_subtitles",
122                 string("At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long.")
123                 );
124 }
125
126
127 BOOST_AUTO_TEST_CASE (hint_subtitles_too_close)
128 {
129         check (
130                 TextType::OPEN_SUBTITLE,
131                 "hint_subtitles_too_close",
132                 string("At least one of your subtitles starts less than 2 frames after the previous one.  It is advisable to make the gap between subtitles at least 2 frames.")
133               );
134 }
135
136
137 BOOST_AUTO_TEST_CASE (hint_many_subtitle_lines)
138 {
139         check (
140                 TextType::OPEN_SUBTITLE,
141                 "hint_many_subtitle_lines",
142                 string("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines.")
143               );
144 }
145
146
147 BOOST_AUTO_TEST_CASE (hint_subtitle_too_long)
148 {
149         check (
150                 TextType::OPEN_SUBTITLE,
151                 "hint_subtitle_too_long",
152                 string("At least one of your subtitle lines has more than 52 characters.  It is recommended to make each line 52 characters at most in length.")
153               );
154 }
155
156
157 BOOST_AUTO_TEST_CASE (hint_subtitle_much_too_long)
158 {
159         check (
160                 TextType::OPEN_SUBTITLE,
161                 "hint_subtitle_much_too_long",
162                 string("At least one of your subtitle lines has more than 79 characters.  You should make each line 79 characters at most in length.")
163               );
164 }
165
166
167 BOOST_AUTO_TEST_CASE (hint_subtitle_mxf_too_big)
168 {
169         string const name = "hint_subtitle_mxf_too_big";
170
171         auto film = new_test_film2 (name);
172         auto content = content_factory("test/data/" + name + ".srt").front();
173         content->text.front()->set_type (TextType::OPEN_SUBTITLE);
174         content->text.front()->set_language (dcp::LanguageTag("en-US"));
175         for (int i = 1; i < 512; ++i) {
176                 auto font = make_shared<dcpomatic::Font>(String::compose("font_%1", i));
177                 font->set_file ("test/data/LiberationSans-Regular.ttf");
178                 content->text.front()->add_font(font);
179         }
180         film->examine_and_add_content (content);
181         BOOST_REQUIRE (!wait_for_jobs());
182         auto hints = get_hints (film);
183
184         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
185         BOOST_CHECK_EQUAL (
186                 hints[0],
187                 "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
188                 "You should divide the DCP into shorter reels."
189                 );
190 }
191
192
193 BOOST_AUTO_TEST_CASE (hint_closed_caption_xml_too_big)
194 {
195         string const name = "hint_closed_caption_xml_too_big";
196
197         auto film = new_test_film2 (name);
198
199         auto ccap = fopen_boost (String::compose("build/test/%1.srt", name), "w");
200         BOOST_REQUIRE (ccap);
201         for (int i = 0; i < 2048; ++i) {
202                 fprintf(ccap, "%d\n", i + 1);
203                 int second = i * 2;
204                 int minute = second % 60;
205                 fprintf(ccap, "00:%02d:%02d,000 --> 00:%02d:%02d,000\n", minute, second, minute, second + 1);
206                 fprintf(ccap, "Here are some closed captions.\n\n");
207         }
208         fclose (ccap);
209
210         auto content = content_factory("build/test/" + name + ".srt").front();
211         content->text.front()->set_type (TextType::CLOSED_CAPTION);
212         content->text.front()->set_language (dcp::LanguageTag("en-US"));
213         film->examine_and_add_content (content);
214         BOOST_REQUIRE (!wait_for_jobs());
215         auto hints = get_hints (film);
216
217         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
218         BOOST_CHECK_EQUAL (
219                 hints[0],
220                 "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT ".  "
221                 "You should divide the DCP into shorter reels."
222                 );
223 }
224