Don't write fonts to closed caption assets, even if we have some (re-fixing #2505).
[dcpomatic.git] / test / closed_caption_test.cc
1 /*
2     Copyright (C) 2018-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/dcp_content.h"
23 #include "lib/film.h"
24 #include "lib/string_text_file_content.h"
25 #include "lib/text_content.h"
26 #include "test.h"
27 #include <dcp/cpl.h>
28 #include <dcp/dcp.h>
29 #include <dcp/reel.h>
30 #include <dcp/reel_closed_caption_asset.h>
31 #include <boost/test/unit_test.hpp>
32
33
34 using std::list;
35 using std::make_shared;
36
37
38 /** Basic test that Interop closed captions are written */
39 BOOST_AUTO_TEST_CASE (closed_caption_test1)
40 {
41         Cleanup cl;
42
43         auto content = make_shared<StringTextFileContent>("test/data/subrip.srt");
44         auto film = new_test_film2 ("closed_caption_test1", { content }, &cl);
45
46         content->only_text()->set_type (TextType::CLOSED_CAPTION);
47
48         make_and_verify_dcp (
49                 film,
50                 {
51                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
52                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
53                         dcp::VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_LENGTH,
54                         dcp::VerificationNote::Code::MISSING_CPL_METADATA
55                 });
56
57         /* Just check to see that there's a CCAP in the CPL: this
58            check could be better!
59         */
60
61         dcp::DCP check (film->dir(film->dcp_name()));
62         check.read ();
63
64         BOOST_REQUIRE_EQUAL (check.cpls().size(), 1U);
65         BOOST_REQUIRE_EQUAL (check.cpls().front()->reels().size(), 1U);
66         BOOST_REQUIRE (!check.cpls().front()->reels().front()->closed_captions().empty());
67
68         cl.run ();
69 }
70
71 /** Test multiple closed captions */
72 BOOST_AUTO_TEST_CASE (closed_caption_test2)
73 {
74         Cleanup cl;
75         auto content1 = make_shared<StringTextFileContent>("test/data/subrip.srt");
76         auto content2 = make_shared<StringTextFileContent>("test/data/subrip2.srt");
77         auto content3 = make_shared<StringTextFileContent>("test/data/subrip3.srt");
78         auto film = new_test_film2 ("closed_caption_test2", { content1, content2, content3 }, &cl);
79
80         content1->only_text()->set_type (TextType::CLOSED_CAPTION);
81         content1->only_text()->set_dcp_track (DCPTextTrack("First track", dcp::LanguageTag("fr-FR")));
82         content2->only_text()->set_type (TextType::CLOSED_CAPTION);
83         content2->only_text()->set_dcp_track (DCPTextTrack("Second track", dcp::LanguageTag("de-DE")));
84         content3->only_text()->set_type (TextType::CLOSED_CAPTION);
85         content3->only_text()->set_dcp_track (DCPTextTrack("Third track", dcp::LanguageTag("it-IT")));
86
87         make_and_verify_dcp (
88                 film,
89                 {
90                         dcp::VerificationNote::Code::INVALID_SUBTITLE_DURATION,
91                         dcp::VerificationNote::Code::INVALID_CLOSED_CAPTION_LINE_LENGTH,
92                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
93                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
94                 }
95                 );
96
97         dcp::DCP check (film->dir(film->dcp_name()));
98         check.read ();
99
100         BOOST_REQUIRE_EQUAL (check.cpls().size(), 1U);
101         BOOST_REQUIRE_EQUAL (check.cpls().front()->reels().size(), 1U);
102         auto ccaps = check.cpls().front()->reels().front()->closed_captions();
103         BOOST_REQUIRE_EQUAL (ccaps.size(), 3U);
104
105         auto i = ccaps.begin ();
106         BOOST_CHECK_EQUAL ((*i)->annotation_text().get_value_or(""), "First track");
107         BOOST_REQUIRE (static_cast<bool>((*i)->language()));
108         BOOST_CHECK_EQUAL ((*i)->language().get(), "fr-FR");
109         ++i;
110         BOOST_CHECK_EQUAL ((*i)->annotation_text().get_value_or(""), "Second track");
111         BOOST_REQUIRE (static_cast<bool>((*i)->language()));
112         BOOST_CHECK_EQUAL ((*i)->language().get(), "de-DE");
113         ++i;
114         BOOST_CHECK_EQUAL ((*i)->annotation_text().get_value_or(""), "Third track");
115         BOOST_REQUIRE (static_cast<bool>((*i)->language()));
116         BOOST_CHECK_EQUAL ((*i)->language().get(), "it-IT");
117
118         cl.run ();
119 }
120
121
122
123 BOOST_AUTO_TEST_CASE(fonts_in_inputs_removed_for_ccaps)
124 {
125         auto dcp_with_ccaps = make_shared<DCPContent>("test/data/xml_subtitle_test2");
126         auto film = new_test_film2("fonts_in_inputs_removed_for_ccaps", { dcp_with_ccaps });
127         for (auto i: dcp_with_ccaps->text) {
128                 i->set_use(true);
129                 i->set_type(TextType::CLOSED_CAPTION);
130         }
131         film->set_interop(true);
132         make_and_verify_dcp(film, { dcp::VerificationNote::Code::INVALID_STANDARD, dcp::VerificationNote::Code::INVALID_SUBTITLE_SPACING });
133
134         auto sub = dcp::file_to_string(find_file(film->dir(film->dcp_name()), "sub_"));
135         BOOST_CHECK(sub.find("Font") == std::string::npos);
136 }
137