Speed up hints tests by not running a pointless audio analysis.
[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/audio_content.h"
23 #include "lib/content.h"
24 #include "lib/content_factory.h"
25 #include "lib/cross.h"
26 #include "lib/film.h"
27 #include "lib/font.h"
28 #include "lib/hints.h"
29 #include "lib/text_content.h"
30 #include "lib/util.h"
31 #include "test.h"
32 #include <boost/test/unit_test.hpp>
33
34
35 using std::make_shared;
36 using std::shared_ptr;
37 using std::string;
38 using std::vector;
39 using boost::optional;
40
41
42 vector<string> current_hints;
43
44
45 static
46 void
47 collect_hint (string hint)
48 {
49         current_hints.push_back (hint);
50 }
51
52
53 static
54 vector<string>
55 get_hints (shared_ptr<Film> film)
56 {
57         current_hints.clear ();
58         Hints hints (film);
59         /* None of our tests need the audio analysis, and it is quite time-consuming */
60         hints.disable_audio_analysis ();
61         hints.Hint.connect (collect_hint);
62         hints.start ();
63         hints.join ();
64         while (signal_manager->ui_idle()) {}
65         return current_hints;
66 }
67
68
69 static
70 void
71 check (TextType type, string name, optional<string> expected_hint = optional<string>())
72 {
73         auto film = new_test_film2 (name);
74         auto content = content_factory("test/data/" + name + ".srt").front();
75         content->text.front()->set_type (type);
76         content->text.front()->set_language (dcp::LanguageTag("en-US"));
77         film->examine_and_add_content (content);
78         BOOST_REQUIRE (!wait_for_jobs());
79         auto hints = get_hints (film);
80
81         if (expected_hint) {
82                 BOOST_REQUIRE_EQUAL (hints.size(), 1U);
83                 BOOST_CHECK_EQUAL (hints[0], *expected_hint);
84         } else {
85                 BOOST_CHECK (hints.empty());
86         }
87 }
88
89
90 BOOST_AUTO_TEST_CASE (hint_closed_caption_too_long)
91 {
92         check (
93                 TextType::CLOSED_CAPTION,
94                 "hint_closed_caption_too_long",
95                 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)
96               );
97 }
98
99
100 BOOST_AUTO_TEST_CASE (hint_many_closed_caption_lines)
101 {
102         check (
103                 TextType::CLOSED_CAPTION,
104                 "hint_many_closed_caption_lines",
105                 String::compose("Some of your closed captions span more than %1 lines, so they will be truncated.", MAX_CLOSED_CAPTION_LINES)
106               );
107 }
108
109
110 BOOST_AUTO_TEST_CASE (hint_subtitle_too_early)
111 {
112         check (
113                 TextType::OPEN_SUBTITLE,
114                 "hint_subtitle_too_early",
115                 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.")
116                 );
117 }
118
119
120 BOOST_AUTO_TEST_CASE (hint_short_subtitles)
121 {
122         check (
123                 TextType::OPEN_SUBTITLE,
124                 "hint_short_subtitles",
125                 string("At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long.")
126                 );
127 }
128
129
130 BOOST_AUTO_TEST_CASE (hint_subtitles_too_close)
131 {
132         check (
133                 TextType::OPEN_SUBTITLE,
134                 "hint_subtitles_too_close",
135                 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.")
136               );
137 }
138
139
140 BOOST_AUTO_TEST_CASE (hint_many_subtitle_lines)
141 {
142         check (
143                 TextType::OPEN_SUBTITLE,
144                 "hint_many_subtitle_lines",
145                 string("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines.")
146               );
147 }
148
149
150 BOOST_AUTO_TEST_CASE (hint_subtitle_too_long)
151 {
152         check (
153                 TextType::OPEN_SUBTITLE,
154                 "hint_subtitle_too_long",
155                 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.")
156               );
157 }
158
159
160 BOOST_AUTO_TEST_CASE (hint_subtitle_much_too_long)
161 {
162         check (
163                 TextType::OPEN_SUBTITLE,
164                 "hint_subtitle_much_too_long",
165                 string("At least one of your subtitle lines has more than 79 characters.  You should make each line 79 characters at most in length.")
166               );
167 }
168
169
170 BOOST_AUTO_TEST_CASE (hint_subtitle_mxf_too_big)
171 {
172         string const name = "hint_subtitle_mxf_too_big";
173
174         auto film = new_test_film2 (name);
175         auto content = content_factory("test/data/" + name + ".srt").front();
176         content->text.front()->set_type (TextType::OPEN_SUBTITLE);
177         content->text.front()->set_language (dcp::LanguageTag("en-US"));
178         for (int i = 1; i < 512; ++i) {
179                 auto font = make_shared<dcpomatic::Font>(String::compose("font_%1", i));
180                 font->set_file ("test/data/LiberationSans-Regular.ttf");
181                 content->text.front()->add_font(font);
182         }
183         film->examine_and_add_content (content);
184         BOOST_REQUIRE (!wait_for_jobs());
185         auto hints = get_hints (film);
186
187         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
188         BOOST_CHECK_EQUAL (
189                 hints[0],
190                 "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
191                 "You should divide the DCP into shorter reels."
192                 );
193 }
194
195
196 BOOST_AUTO_TEST_CASE (hint_closed_caption_xml_too_big)
197 {
198         string const name = "hint_closed_caption_xml_too_big";
199
200         auto film = new_test_film2 (name);
201
202         auto ccap = fopen_boost (String::compose("build/test/%1.srt", name), "w");
203         BOOST_REQUIRE (ccap);
204         for (int i = 0; i < 2048; ++i) {
205                 fprintf(ccap, "%d\n", i + 1);
206                 int second = i * 2;
207                 int minute = second % 60;
208                 fprintf(ccap, "00:%02d:%02d,000 --> 00:%02d:%02d,000\n", minute, second, minute, second + 1);
209                 fprintf(ccap, "Here are some closed captions.\n\n");
210         }
211         fclose (ccap);
212
213         auto content = content_factory("build/test/" + name + ".srt").front();
214         content->text.front()->set_type (TextType::CLOSED_CAPTION);
215         content->text.front()->set_language (dcp::LanguageTag("en-US"));
216         film->examine_and_add_content (content);
217         BOOST_REQUIRE (!wait_for_jobs());
218         auto hints = get_hints (film);
219
220         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
221         BOOST_CHECK_EQUAL (
222                 hints[0],
223                 "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT ".  "
224                 "You should divide the DCP into shorter reels."
225                 );
226 }
227
228
229 BOOST_AUTO_TEST_CASE (hints_destroyed_while_running)
230 {
231         auto film = new_test_film2 ("hints_destroyed_while_running");
232         auto content = content_factory(TestPaths::private_data() / "boon_telly.mkv").front();
233         film->examine_and_add_content (content);
234         BOOST_REQUIRE (!wait_for_jobs());
235
236         auto hints = make_shared<Hints>(film);
237         hints->start ();
238         dcpomatic_sleep_seconds (1);
239         hints.reset ();
240         dcpomatic_sleep_seconds (1);
241 }
242
243
244 BOOST_AUTO_TEST_CASE (hints_audio_with_no_language)
245 {
246         auto content = content_factory("test/data/sine_440.wav").front();
247         auto film = new_test_film2 ("hints_audio_with_no_language", { content });
248         content->audio->set_gain (-6);
249
250         auto hints = get_hints (film);
251         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
252         BOOST_CHECK_EQUAL (
253                 hints[0],
254                 "Some of your content has audio but you have not set the audio language.  It is advisable to set the audio language "
255                 "in the \"DCP\" tab unless your audio has no spoken parts."
256                 );
257
258 }
259