C++11 tidying.
[dcpomatic.git] / test / kdm_naming_test.cc
1 /*
2     Copyright (C) 2020-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/cinema.h"
23 #include "lib/config.h"
24 #include "lib/content_factory.h"
25 #include "lib/film.h"
26 #include "lib/kdm_with_metadata.h"
27 #include "lib/screen.h"
28 #include "test.h"
29 #include <boost/test/unit_test.hpp>
30
31
32 using std::dynamic_pointer_cast;
33 using std::list;
34 using std::make_shared;
35 using std::shared_ptr;
36 using std::string;
37 using std::vector;
38 using boost::optional;
39
40
41 static
42 bool
43 confirm_overwrite (boost::filesystem::path)
44 {
45         return true;
46 }
47
48
49 static shared_ptr<dcpomatic::Screen> cinema_a_screen_1;
50 static shared_ptr<dcpomatic::Screen> cinema_a_screen_2;
51 static shared_ptr<dcpomatic::Screen> cinema_b_screen_x;
52 static shared_ptr<dcpomatic::Screen> cinema_b_screen_y;
53 static shared_ptr<dcpomatic::Screen> cinema_b_screen_z;
54
55
56 BOOST_AUTO_TEST_CASE (single_kdm_naming_test)
57 {
58         auto c = Config::instance();
59
60         auto cert = c->decryption_chain()->leaf();
61
62         /* Cinema A: UTC +4:30 */
63         auto cinema_a = make_shared<Cinema>("Cinema A", list<string>(), "", 4, 30);
64         cinema_a_screen_1 = make_shared<dcpomatic::Screen>("Screen 1", "", cert, vector<TrustedDevice>());
65         cinema_a->add_screen (cinema_a_screen_1);
66         cinema_a_screen_2 = make_shared<dcpomatic::Screen>("Screen 2", "", cert, vector<TrustedDevice>());
67         cinema_a->add_screen (cinema_a_screen_2);
68         c->add_cinema (cinema_a);
69
70         /* Cinema B: UTC -1:00 */
71         auto cinema_b = make_shared<Cinema>("Cinema B", list<string>(), "", -1, 0);
72         cinema_b_screen_x = make_shared<dcpomatic::Screen>("Screen X", "", cert, vector<TrustedDevice>());
73         cinema_b->add_screen (cinema_b_screen_x);
74         cinema_b_screen_y = make_shared<dcpomatic::Screen>("Screen Y", "", cert, vector<TrustedDevice>());
75         cinema_b->add_screen (cinema_b_screen_y);
76         cinema_b_screen_z = make_shared<dcpomatic::Screen>("Screen Z", "", cert, vector<TrustedDevice>());
77         cinema_b->add_screen (cinema_b_screen_z);
78         c->add_cinema (cinema_a);
79
80         /* Film */
81         boost::filesystem::remove_all ("build/test/single_kdm_naming_test");
82         auto film = new_test_film2 ("single_kdm_naming_test");
83         film->set_name ("my_great_film");
84         film->examine_and_add_content (content_factory("test/data/flat_black.png").front());
85         BOOST_REQUIRE (!wait_for_jobs());
86         film->set_encrypted (true);
87         make_and_verify_dcp (film);
88         auto cpls = film->cpls ();
89         BOOST_REQUIRE(cpls.size() == 1);
90
91         dcp::LocalTime from (cert.not_before());
92         from.add_months (2);
93         dcp::LocalTime until (cert.not_after());
94         until.add_months (-2);
95
96         auto const from_string = from.date() + " " + from.time_of_day(true, false);
97         auto const until_string = until.date() + " " + until.time_of_day(true, false);
98
99         auto cpl = cpls.front().cpl_file;
100         auto kdm = kdm_for_screen (
101                         film,
102                         cpls.front().cpl_file,
103                         cinema_a_screen_1,
104                         boost::posix_time::time_from_string(from_string),
105                         boost::posix_time::time_from_string(until_string),
106                         dcp::Formulation::MODIFIED_TRANSITIONAL_1,
107                         false,
108                         optional<int>()
109                         );
110
111         write_files (
112                 { kdm },
113                 boost::filesystem::path("build/test/single_kdm_naming_test"),
114                 dcp::NameFormat("KDM %c - %s - %f - %b - %e"),
115                 &confirm_overwrite
116                 );
117
118         auto from_time = from.time_of_day (true, false);
119         boost::algorithm::replace_all (from_time, ":", "-");
120         auto until_time = until.time_of_day (true, false);
121         boost::algorithm::replace_all (until_time, ":", "-");
122
123         auto const ref = String::compose("KDM_Cinema_A_-_Screen_1_-_my_great_film_-_%1_%2_-_%3_%4.xml", from.date(), from_time, until.date(), until_time);
124         BOOST_CHECK_MESSAGE (boost::filesystem::exists("build/test/single_kdm_naming_test/" + ref), "File " << ref << " not found");
125 }
126
127
128 BOOST_AUTO_TEST_CASE (directory_kdm_naming_test, * boost::unit_test::depends_on("single_kdm_naming_test"))
129 {
130         using boost::filesystem::path;
131
132         auto cert = Config::instance()->decryption_chain()->leaf();
133
134         /* Film */
135         boost::filesystem::remove_all ("build/test/directory_kdm_naming_test");
136         auto film = new_test_film2 (
137                 "directory_kdm_naming_test",
138                 { content_factory("test/data/flat_black.png").front() }
139                 );
140
141         film->set_name ("my_great_film");
142         film->set_encrypted (true);
143         make_and_verify_dcp (film);
144         auto cpls = film->cpls ();
145         BOOST_REQUIRE(cpls.size() == 1);
146
147         dcp::LocalTime from (cert.not_before());
148         from.add_months (2);
149         dcp::LocalTime until (cert.not_after());
150         until.add_months (-2);
151
152         string const from_string = from.date() + " " + from.time_of_day(true, false);
153         string const until_string = until.date() + " " + until.time_of_day(true, false);
154
155         list<shared_ptr<dcpomatic::Screen>> screens = {
156                 cinema_a_screen_2, cinema_b_screen_x, cinema_a_screen_1, (cinema_b_screen_z)
157         };
158
159         auto const cpl = cpls.front().cpl_file;
160         auto const cpl_id = cpls.front().cpl_id;
161
162         list<KDMWithMetadataPtr> kdms;
163         for (auto i: screens) {
164                 auto kdm = kdm_for_screen (
165                                 film,
166                                 cpls.front().cpl_file,
167                                 i,
168                                 boost::posix_time::time_from_string(from_string),
169                                 boost::posix_time::time_from_string(until_string),
170                                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
171                                 false,
172                                 optional<int>()
173                                 );
174
175                 kdms.push_back (kdm);
176         }
177
178         write_directories (
179                 collect(kdms),
180                 path("build/test/directory_kdm_naming_test"),
181                 dcp::NameFormat("%c - %s - %f - %b - %e"),
182 #ifdef DCPOMATIC_WINDOWS
183                 /* With %i in the format the path is too long for Windows */
184                 dcp::NameFormat("KDM %c - %s - %f - %b - %e"),
185 #else
186                 dcp::NameFormat("KDM %c - %s - %f - %b - %e - %i"),
187 #endif
188                 &confirm_overwrite
189                 );
190
191         auto from_time = from.time_of_day (true, false);
192         boost::algorithm::replace_all (from_time, ":", "-");
193         auto until_time = until.time_of_day (true, false);
194         boost::algorithm::replace_all (until_time, ":", "-");
195
196         path const base = "build/test/directory_kdm_naming_test";
197
198         path dir_a = String::compose("Cinema_A_-_%s_-_my_great_film_-_%1_%2_-_%3_%4", from.date(), from_time, until.date(), until_time);
199         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a), "Directory " << dir_a << " not found");
200         path dir_b = String::compose("Cinema_B_-_%s_-_my_great_film_-_%1_%2_-_%3_%4", from.date(), from_time, until.date(), until_time);
201         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b), "Directory " << dir_b << " not found");
202
203 #ifdef DCPOMATIC_WINDOWS
204         path ref = String::compose("KDM_Cinema_A_-_Screen_2_-_my_great_film_-_%1_%2_-_%3_%4.xml", from.date(), from_time, until.date(), until_time);
205 #else
206         path ref = String::compose("KDM_Cinema_A_-_Screen_2_-_my_great_film_-_%1_%2_-_%3_%4_-_%5.xml", from.date(), from_time, until.date(), until_time, cpl_id);
207 #endif
208         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
209
210 #ifdef DCPOMATIC_WINDOWS
211         ref = String::compose("KDM_Cinema_B_-_Screen_X_-_my_great_film_-_%1_%2_-_%3_%4.xml", from.date(), from_time, until.date(), until_time);
212 #else
213         ref = String::compose("KDM_Cinema_B_-_Screen_X_-_my_great_film_-_%1_%2_-_%3_%4_-_%5.xml", from.date(), from_time, until.date(), until_time, cpl_id);
214 #endif
215         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
216
217 #ifdef DCPOMATIC_WINDOWS
218         ref = String::compose("KDM_Cinema_A_-_Screen_1_-_my_great_film_-_%1_%2_-_%3_%4.xml", from.date(), from_time, until.date(), until_time);
219 #else
220         ref = String::compose("KDM_Cinema_A_-_Screen_1_-_my_great_film_-_%1_%2_-_%3_%4_-_%5.xml", from.date(), from_time, until.date(), until_time, cpl_id);
221 #endif
222         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
223
224 #ifdef DCPOMATIC_WINDOWS
225         ref = String::compose("KDM_Cinema_B_-_Screen_Z_-_my_great_film_-_%1_%2_-_%3_%4.xml", from.date(), from_time, until.date(), until_time);
226 #else
227         ref = String::compose("KDM_Cinema_B_-_Screen_Z_-_my_great_film_-_%1_%2_-_%3_%4_-_%5.xml", from.date(), from_time, until.date(), until_time, cpl_id);
228 #endif
229         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
230 }
231