Fix typo if -> of (thanks to Uwe Dittes)
[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/film.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/dcp_content_type.h"
33 #include "lib/video_content.h"
34 #ifdef DCPOMATIC_WINDOWS
35 #include <boost/locale.hpp>
36 #endif
37 #include <boost/test/unit_test.hpp>
38 #include <boost/regex.hpp>
39
40
41 using std::string;
42 using std::shared_ptr;
43 using std::make_shared;
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 BOOST_AUTO_TEST_CASE (file_naming_test)
65 {
66         Keep k;
67         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat("%c"));
68
69         auto film = new_test_film ("file_naming_test");
70         film->set_name ("file_naming_test");
71         film->set_video_frame_rate (24);
72         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
73         auto r = make_shared<FFmpegContent>("test/data/flat_red.png");
74         film->examine_and_add_content (r);
75         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
76         film->examine_and_add_content (g);
77         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
78         film->examine_and_add_content (b);
79         BOOST_REQUIRE (!wait_for_jobs());
80
81         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
82         r->set_video_frame_rate (24);
83         r->video->set_length (24);
84         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
85         g->set_video_frame_rate (24);
86         g->video->set_length (24);
87         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
88         b->set_video_frame_rate (24);
89         b->video->set_length (24);
90
91         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
92         film->write_metadata ();
93         make_and_verify_dcp (
94                 film,
95                 {
96                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
97                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
98                 });
99
100         int got[3] = { 0, 0, 0 };
101         for (auto i: boost::filesystem::directory_iterator(film->file(film->dcp_name()))) {
102                 if (boost::regex_match(i.path().string(), boost::regex(".*flat_red\\.png_.*\\.mxf"))) {
103                         ++got[0];
104                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
105                         ++got[1];
106                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
107                         ++got[2];
108                 }
109         }
110
111         for (int i = 0; i < 3; ++i) {
112                 BOOST_CHECK (got[i] == 2);
113         }
114 }
115
116
117 BOOST_AUTO_TEST_CASE (file_naming_test2)
118 {
119         Keep k;
120         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
121
122         auto film = new_test_film ("file_naming_test2");
123         film->set_name ("file_naming_test2");
124         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
125
126 #ifdef DCPOMATIC_WINDOWS
127         /* This is necessary so that the UTF8 string constant below gets converted properly */
128         std::locale::global(boost::locale::generator().generate(""));
129         boost::filesystem::path::imbue(std::locale());
130 #endif
131
132         auto r = make_shared<FFmpegContent>("test/data/flät_red.png");
133         film->examine_and_add_content (r);
134         auto g = make_shared<FFmpegContent>("test/data/flat_green.png");
135         film->examine_and_add_content (g);
136         auto b = make_shared<FFmpegContent>("test/data/flat_blue.png");
137         film->examine_and_add_content (b);
138         BOOST_REQUIRE (!wait_for_jobs());
139
140         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
141         r->set_video_frame_rate (24);
142         r->video->set_length (24);
143         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
144         g->set_video_frame_rate (24);
145         g->video->set_length (24);
146         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
147         b->set_video_frame_rate (24);
148         b->video->set_length (24);
149
150         film->set_reel_type (ReelType::BY_VIDEO_CONTENT);
151         make_and_verify_dcp (
152                 film,
153                 {
154                         dcp::VerificationNote::Code::MISSING_FFMC_IN_FEATURE,
155                         dcp::VerificationNote::Code::MISSING_FFEC_IN_FEATURE
156                 });
157
158         int got[3] = { 0, 0, 0 };
159         for (auto i: boost::filesystem::directory_iterator (film->file(film->dcp_name()))) {
160                 if (boost::regex_match(i.path().string(), boost::regex(".*flat_red\\.png_.*\\.mxf"))) {
161                         ++got[0];
162                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
163                         ++got[1];
164                 } else if (boost::regex_match(i.path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
165                         ++got[2];
166                 }
167         }
168
169         for (int i = 0; i < 3; ++i) {
170                 BOOST_CHECK (got[i] == 2);
171         }
172 }