1be36193a96374fe6a7584d0e3abe476d515fddc
[dcpomatic.git] / test / hints_test.cc
1 /*
2     Copyright (C) 2020 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::string;
35 using std::vector;
36 using boost::optional;
37 using std::shared_ptr;
38
39
40 vector<string> current_hints;
41
42
43 static
44 void
45 collect_hint (string hint)
46 {
47         current_hints.push_back (hint);
48 }
49
50
51 static
52 vector<string>
53 get_hints (shared_ptr<Film> film)
54 {
55         current_hints.clear ();
56         Hints hints (film);
57         hints.Hint.connect (collect_hint);
58         hints.start ();
59         hints.join ();
60         while (signal_manager->ui_idle()) {}
61         return current_hints;
62 }
63
64
65 static
66 void
67 check (TextType type, string name, optional<string> expected_hint = optional<string>())
68 {
69         shared_ptr<Film> film = new_test_film2 (name);
70         shared_ptr<Content> content = content_factory("test/data/" + name + ".srt").front();
71         content->text.front()->set_type (type);
72         film->examine_and_add_content (content);
73         BOOST_REQUIRE (!wait_for_jobs());
74         vector<string> hints = get_hints (film);
75
76         if (expected_hint) {
77                 BOOST_REQUIRE_EQUAL (hints.size(), 1U);
78                 BOOST_CHECK_EQUAL (hints[0], *expected_hint);
79         } else {
80                 BOOST_CHECK (hints.empty());
81         }
82 }
83
84
85 BOOST_AUTO_TEST_CASE (hint_closed_caption_too_long)
86 {
87         check (
88                 TEXT_CLOSED_CAPTION,
89                 "hint_closed_caption_too_long",
90                 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)
91               );
92 }
93
94
95 BOOST_AUTO_TEST_CASE (hint_many_closed_caption_lines)
96 {
97         check (
98                 TEXT_CLOSED_CAPTION,
99                 "hint_many_closed_caption_lines",
100                 String::compose("Some of your closed captions span more than %1 lines, so they will be truncated.", MAX_CLOSED_CAPTION_LINES)
101               );
102 }
103
104
105 BOOST_AUTO_TEST_CASE (hint_subtitle_too_early)
106 {
107         check (
108                 TEXT_OPEN_SUBTITLE,
109                 "hint_subtitle_too_early",
110                 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.")
111                 );
112 }
113
114
115 BOOST_AUTO_TEST_CASE (hint_short_subtitles)
116 {
117         check (
118                 TEXT_OPEN_SUBTITLE,
119                 "hint_short_subtitles",
120                 string("At least one of your subtitles lasts less than 15 frames.  It is advisable to make each subtitle at least 15 frames long.")
121                 );
122 }
123
124
125 BOOST_AUTO_TEST_CASE (hint_subtitles_too_close)
126 {
127         check (
128                 TEXT_OPEN_SUBTITLE,
129                 "hint_subtitles_too_close",
130                 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.")
131               );
132 }
133
134
135 BOOST_AUTO_TEST_CASE (hint_many_subtitle_lines)
136 {
137         check (
138                 TEXT_OPEN_SUBTITLE,
139                 "hint_many_subtitle_lines",
140                 string("At least one of your subtitles has more than 3 lines.  It is advisable to use no more than 3 lines.")
141               );
142 }
143
144
145 BOOST_AUTO_TEST_CASE (hint_subtitle_too_long)
146 {
147         check (
148                 TEXT_OPEN_SUBTITLE,
149                 "hint_subtitle_too_long",
150                 string("At least one of your subtitle lines has more than 52 characters.  It is advisable to make each line 52 characters at most in length.")
151               );
152 }
153
154
155 BOOST_AUTO_TEST_CASE (hint_subtitle_mxf_too_big)
156 {
157         string const name = "hint_subtitle_mxf_too_big";
158
159         shared_ptr<Film> film = new_test_film2 (name);
160         shared_ptr<Content> content = content_factory("test/data/" + name + ".srt").front();
161         content->text.front()->set_type (TEXT_OPEN_SUBTITLE);
162         for (int i = 1; i < 512; ++i) {
163                 shared_ptr<dcpomatic::Font> font(new dcpomatic::Font(String::compose("font_%1", i)));
164                 font->set_file ("test/data/LiberationSans-Regular.ttf");
165                 content->text.front()->add_font(font);
166         }
167         film->examine_and_add_content (content);
168         BOOST_REQUIRE (!wait_for_jobs());
169         vector<string> hints = get_hints (film);
170
171         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
172         BOOST_CHECK_EQUAL (
173                 hints[0],
174                 "At least one of your subtitle files is larger than " MAX_TEXT_MXF_SIZE_TEXT " in total.  "
175                 "You should divide the DCP into shorter reels."
176                 );
177 }
178
179
180 BOOST_AUTO_TEST_CASE (hint_closed_caption_xml_too_big)
181 {
182         string const name = "hint_closed_caption_xml_too_big";
183
184         shared_ptr<Film> film = new_test_film2 (name);
185
186         FILE* ccap = fopen_boost (String::compose("build/test/%1.srt", name), "w");
187         BOOST_REQUIRE (ccap);
188         for (int i = 0; i < 2048; ++i) {
189                 fprintf(ccap, "%d\n", i + 1);
190                 int second = i * 2;
191                 int minute = second % 60;
192                 fprintf(ccap, "00:%02d:%02d,000 --> 00:%02d:%02d,000\n", minute, second, minute, second + 1);
193                 fprintf(ccap, "Here are some closed captions.\n\n");
194         }
195         fclose (ccap);
196
197         shared_ptr<Content> content = content_factory("build/test/" + name + ".srt").front();
198         content->text.front()->set_type (TEXT_CLOSED_CAPTION);
199         film->examine_and_add_content (content);
200         BOOST_REQUIRE (!wait_for_jobs());
201         vector<string> hints = get_hints (film);
202
203         BOOST_REQUIRE_EQUAL (hints.size(), 1U);
204         BOOST_CHECK_EQUAL (
205                 hints[0],
206                 "At least one of your closed caption files' XML part is larger than " MAX_CLOSED_CAPTION_XML_SIZE_TEXT ".  "
207                 "You should divide the DCP into shorter reels."
208                 );
209 }
210