std::shared_ptr
[dcpomatic.git] / test / file_naming_test.cc
1 /*
2     Copyright (C) 2016-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 /** @file  test/file_naming_test.cc
22  *  @brief Test how files in DCPs are named.
23  *  @ingroup feature
24  */
25
26 #include "test.h"
27 #include "lib/config.h"
28 #include "lib/film.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/dcp_content_type.h"
31 #include "lib/video_content.h"
32 #ifdef DCPOMATIC_WINDOWS
33 #include <boost/locale.hpp>
34 #endif
35 #include <boost/test/unit_test.hpp>
36 #include <boost/regex.hpp>
37
38 using std::string;
39 using std::shared_ptr;
40
41 class Keep
42 {
43 public:
44         Keep ()
45         {
46                 _format = Config::instance()->dcp_asset_filename_format ();
47         }
48
49         ~Keep ()
50         {
51                 Config::instance()->set_dcp_asset_filename_format (_format);
52         }
53
54 private:
55         dcp::NameFormat _format;
56 };
57
58 BOOST_AUTO_TEST_CASE (file_naming_test)
59 {
60         Keep k;
61         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
62
63         shared_ptr<Film> film = new_test_film ("file_naming_test");
64         film->set_name ("file_naming_test");
65         film->set_video_frame_rate (24);
66         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
67         shared_ptr<FFmpegContent> r (new FFmpegContent("test/data/flat_red.png"));
68         film->examine_and_add_content (r);
69         shared_ptr<FFmpegContent> g (new FFmpegContent("test/data/flat_green.png"));
70         film->examine_and_add_content (g);
71         shared_ptr<FFmpegContent> b (new FFmpegContent("test/data/flat_blue.png"));
72         film->examine_and_add_content (b);
73         BOOST_REQUIRE (!wait_for_jobs());
74
75         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
76         r->set_video_frame_rate (24);
77         r->video->set_length (24);
78         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
79         g->set_video_frame_rate (24);
80         g->video->set_length (24);
81         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
82         b->set_video_frame_rate (24);
83         b->video->set_length (24);
84
85         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
86         film->write_metadata ();
87         film->make_dcp ();
88         BOOST_REQUIRE (!wait_for_jobs());
89
90         int got[3] = { 0, 0, 0 };
91         for (
92                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->file(film->dcp_name()));
93                 i != boost::filesystem::directory_iterator();
94                 ++i) {
95                 if (boost::regex_match(i->path().string(), boost::regex(".*flat_red\\.png_.*\\.mxf"))) {
96                         ++got[0];
97                 } else if (boost::regex_match(i->path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
98                         ++got[1];
99                 } else if (boost::regex_match(i->path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
100                         ++got[2];
101                 }
102         }
103
104         for (int i = 0; i < 3; ++i) {
105                 BOOST_CHECK (got[i] == 2);
106         }
107 }
108
109 BOOST_AUTO_TEST_CASE (file_naming_test2)
110 {
111         Keep k;
112         Config::instance()->set_dcp_asset_filename_format (dcp::NameFormat ("%c"));
113
114         shared_ptr<Film> film = new_test_film ("file_naming_test2");
115         film->set_name ("file_naming_test2");
116         film->set_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"));
117
118 #ifdef DCPOMATIC_WINDOWS
119         /* This is necessary so that the UTF8 string constant below gets converted properly */
120         std::locale::global(boost::locale::generator().generate(""));
121         boost::filesystem::path::imbue(std::locale());
122 #endif
123
124         shared_ptr<FFmpegContent> r (new FFmpegContent("test/data/flät_red.png"));
125         film->examine_and_add_content (r);
126         shared_ptr<FFmpegContent> g (new FFmpegContent("test/data/flat_green.png"));
127         film->examine_and_add_content (g);
128         shared_ptr<FFmpegContent> b (new FFmpegContent("test/data/flat_blue.png"));
129         film->examine_and_add_content (b);
130         BOOST_REQUIRE (!wait_for_jobs());
131
132         r->set_position (film, dcpomatic::DCPTime::from_seconds(0));
133         r->set_video_frame_rate (24);
134         r->video->set_length (24);
135         g->set_position (film, dcpomatic::DCPTime::from_seconds(1));
136         g->set_video_frame_rate (24);
137         g->video->set_length (24);
138         b->set_position (film, dcpomatic::DCPTime::from_seconds(2));
139         b->set_video_frame_rate (24);
140         b->video->set_length (24);
141
142         film->set_reel_type (REELTYPE_BY_VIDEO_CONTENT);
143         film->make_dcp ();
144         BOOST_REQUIRE (!wait_for_jobs());
145
146         int got[3] = { 0, 0, 0 };
147         for (
148                 boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator (film->file(film->dcp_name()));
149                 i != boost::filesystem::directory_iterator();
150                 ++i) {
151                 if (boost::regex_match(i->path().string(), boost::regex(".*flt_red\\.png_.*\\.mxf"))) {
152                         ++got[0];
153                 } else if (boost::regex_match(i->path().string(), boost::regex(".*flat_green\\.png_.*\\.mxf"))) {
154                         ++got[1];
155                 } else if (boost::regex_match(i->path().string(), boost::regex(".*flat_blue\\.png_.*\\.mxf"))) {
156                         ++got[2];
157                 }
158         }
159
160         for (int i = 0; i < 3; ++i) {
161                 BOOST_CHECK (got[i] == 2);
162         }
163 }