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