Supporters update.
[dcpomatic.git] / test / file_naming_test.cc
1 /*
2     Copyright (C) 2016-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 /** @file  test/file_naming_test.cc
23  *  @brief Test how files in DCPs are named.
24  *  @ingroup feature
25  */
26
27
28 #include "test.h"
29 #include "lib/config.h"
30 #include "lib/content_factory.h"
31 #include "lib/dcp_content_type.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/film.h"
34 #include "lib/video_content.h"
35 #ifdef DCPOMATIC_WINDOWS
36 #include <boost/locale.hpp>
37 #endif
38 #include <boost/test/unit_test.hpp>
39 #include <boost/regex.hpp>
40
41
42 using std::make_shared;
43 using std::string;
44
45
46 static
47 string
48 mxf_regex(string part) {
49 #ifdef DCPOMATIC_WINDOWS
50         /* Windows replaces . in filenames with _ */
51         return String::compose(".*flat_%1_png_.*\\.mxf", part);
52 #else
53         return String::compose(".*flat_%1\\.png_.*\\.mxf", part);
54 #endif
55 };
56
57
58
59 BOOST_AUTO_TEST_CASE (file_naming_test)
60 {
61         ConfigRestorer cr;
62         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat("%c"));
63
64         auto film = new_test_film ("file_naming_test");
65         film->set_name ("file_naming_test");
66         film->set_video_frame_rate (24);
67         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
68         auto r = make_shared<FFmpegContent>("test/data/flat_red.png");
69         film->examine_and_add_content (r);
70         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
71         film->examine_and_add_content (g);
72         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
73         film->examine_and_add_content (b);
74         BOOST_REQUIRE (!wait_for_jobs());
75
76         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
77         r->set_video_frame_rate(film, 24);
78         r->video->set_length (24);
79         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
80         g->set_video_frame_rate(film, 24);
81         g->video->set_length (24);
82         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
83         b->set_video_frame_rate(film, 24);
84         b->video->set_length (24);
85
86         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
87         film->write_metadata ();
88         make_and_verify_dcp (
89                 film,
90                 {
91                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
92                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
93                 });
94
95         int got[3] = { 0, 0, 0 };
96         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
97                 if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("red")))) {
98                         ++got[0];
99                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("green")))) {
100                         ++got[1];
101                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("blue")))) {
102                         ++got[2];
103                 }
104         }
105
106         for (int i = 0; i < 3; ++i) {
107                 BOOST_CHECK (got[i] == 2);
108         }
109 }
110
111
112 BOOST_AUTO_TEST_CASE (file_naming_test2)
113 {
114         ConfigRestorer cr;
115
116         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
117
118         auto film = new_test_film ("file_naming_test2");
119         film->set_name ("file_naming_test2");
120         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
121
122 #ifdef DCPOMATIC_WINDOWS
123         /* This is necessary so that the UTF8 string constant below gets converted properly */
124         std::locale::global(boost::locale::generator().generate(""));
125         boost::filesystem::path::imbue(std::locale());
126 #endif
127
128         auto r = make_shared<FFmpegContent>("test/data/flät_red.png");
129         film->examine_and_add_content (r);
130         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
131         film->examine_and_add_content (g);
132         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
133         film->examine_and_add_content (b);
134         BOOST_REQUIRE (!wait_for_jobs());
135
136         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
137         r->set_video_frame_rate(film, 24);
138         r->video->set_length (24);
139         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
140         g->set_video_frame_rate(film, 24);
141         g->video->set_length (24);
142         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
143         b->set_video_frame_rate(film, 24);
144         b->video->set_length (24);
145
146         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
147         make_and_verify_dcp (
148                 film,
149                 {
150                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
151                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
152                 });
153
154         int got[3] = { 0, 0, 0 };
155         for (auto i: boost::filesystem::directory_iterator (film->file(film->dcp_name()))) {
156                 if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("red")))) {
157                         ++got[0];
158                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("green")))) {
159                         ++got[1];
160                 } else if (boost::regex_match(i.path().string(), boost::regex(mxf_regex("blue")))) {
161                         ++got[2];
162                 }
163         }
164
165         for (int i = 0; i < 3; ++i) {
166                 BOOST_CHECK (got[i] == 2);
167         }
168 }
169
170
171 BOOST_AUTO_TEST_CASE (subtitle_file_naming)
172 {
173         ConfigRestorer cr;
174
175         Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("%t ostrabagalous %c"));
176
177         auto content = content_factory("test/data/15s.srt");
178         auto film = new_test_film2("subtitle_file_naming", content);
179         film->set_interop(false);
180
181         make_and_verify_dcp (
182                 film,
183                 {
184                         dcp::VerificationNote::Code::MISSING_CPL_METADATA,
185                         dcp::VerificationNote::Code::MISSING_SUBTITLE_LANGUAGE,
186                         dcp::VerificationNote::Code::INVALID_SUBTITLE_FIRST_TEXT_TIME,
187                 });
188
189         int got = 0;
190
191         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
192                 if (boost::regex_match(i.path().filename().string(), boost::regex("sub_ostrabagalous_15s.*\\.mxf"))) {
193                         ++got;
194                 }
195         }
196
197         BOOST_CHECK_EQUAL(got, 1);
198 }
199
200
201 BOOST_AUTO_TEST_CASE(remove_bad_characters_from_template)
202 {
203         ConfigRestorer cr;
204
205         /* %z is not recognised, so the % should be discarded so it won't trip
206          * an invalid URI check in make_and_verify_dcp
207          */
208         Config::instance()->set_dcp_asset_filename_format(dcp::NameFormat("%c%z"));
209
210         auto content = content_factory("test/data/flat_red.png");
211         auto film = new_test_film2("remove_bad_characters_from_template", content);
212         make_and_verify_dcp(
213                 film,
214                 {
215                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
216                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
217                 });
218 }
219