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