Change video content scaling so that it either:
[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 #include <boost/shared_ptr.hpp>
30
31 using std::list;
32 using std::string;
33 using std::vector;
34 using boost::shared_ptr;
35 using boost::optional;
36 using boost::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         film->make_dcp ();
83         BOOST_REQUIRE (!wait_for_jobs());
84         film->write_metadata ();
85         vector<CPLSummary> cpls = film->cpls ();
86         BOOST_REQUIRE(cpls.size() == 1);
87
88         dcp::LocalTime from (cert.not_before());
89         from.add_months (2);
90         dcp::LocalTime until (cert.not_after());
91         until.add_months (-2);
92
93         string const from_string = from.date() + " " + from.time_of_day(true, false);
94         string const until_string = until.date() + " " + until.time_of_day(true, false);
95
96         boost::filesystem::path cpl = cpls.front().cpl_file;
97         KDMWithMetadataPtr kdm = kdm_for_screen (
98                         film,
99                         cpls.front().cpl_file,
100                         cinema_a_screen_1,
101                         boost::posix_time::time_from_string(from_string),
102                         boost::posix_time::time_from_string(until_string),
103                         dcp::MODIFIED_TRANSITIONAL_1,
104                         false,
105                         optional<int>()
106                         );
107
108         list<KDMWithMetadataPtr> kdms;
109         kdms.push_back (kdm);
110
111         write_files (
112                 kdms,
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         string from_time = from.time_of_day (true, false);
119         boost::algorithm::replace_all (from_time, ":", "-");
120         string until_time = until.time_of_day (true, false);
121         boost::algorithm::replace_all (until_time, ":", "-");
122
123         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);
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         dcp::Certificate cert = Config::instance()->decryption_chain()->leaf();
133
134         /* Film */
135         boost::filesystem::remove_all ("build/test/directory_kdm_naming_test");
136         shared_ptr<Film> film = new_test_film2 ("directory_kdm_naming_test");
137         film->set_name ("my_great_film");
138         film->examine_and_add_content (content_factory("test/data/flat_black.png").front());
139         BOOST_REQUIRE (!wait_for_jobs());
140         film->set_encrypted (true);
141         film->make_dcp ();
142         BOOST_REQUIRE (!wait_for_jobs());
143         film->write_metadata ();
144         vector<CPLSummary> 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         screens.push_back (cinema_a_screen_2);
157         screens.push_back (cinema_b_screen_x);
158         screens.push_back (cinema_a_screen_1);
159         screens.push_back (cinema_b_screen_z);
160
161         path const cpl = cpls.front().cpl_file;
162         string const cpl_id = cpls.front().cpl_id;
163
164         list<KDMWithMetadataPtr> kdms;
165         BOOST_FOREACH (shared_ptr<dcpomatic::Screen> i, screens) {
166                 KDMWithMetadataPtr kdm = kdm_for_screen (
167                                 film,
168                                 cpls.front().cpl_file,
169                                 i,
170                                 boost::posix_time::time_from_string(from_string),
171                                 boost::posix_time::time_from_string(until_string),
172                                 dcp::MODIFIED_TRANSITIONAL_1,
173                                 false,
174                                 optional<int>()
175                                 );
176
177                 kdms.push_back (kdm);
178         }
179
180         write_directories (
181                 collect(kdms),
182                 path("build/test/directory_kdm_naming_test"),
183                 dcp::NameFormat("%c - %s - %f - %b - %e"),
184                 dcp::NameFormat("KDM %c - %s - %f - %b - %e - %i"),
185                 &confirm_overwrite
186                 );
187
188         string from_time = from.time_of_day (true, false);
189         boost::algorithm::replace_all (from_time, ":", "-");
190         string until_time = until.time_of_day (true, false);
191         boost::algorithm::replace_all (until_time, ":", "-");
192
193         path const base = "build/test/directory_kdm_naming_test";
194
195         path dir_a = String::compose("Cinema_A_-_%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_a), "Directory " << dir_a << " not found");
197         path dir_b = String::compose("Cinema_B_-_%s_-_my_great_film_-_%1_%2_-_%3_%4", from.date(), from_time, until.date(), until_time);
198         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b), "Directory " << dir_b << " not found");
199
200         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);
201         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
202
203         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);
204         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
205
206         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);
207         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_a / ref), "File " << ref << " not found");
208
209         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);
210         BOOST_CHECK_MESSAGE (boost::filesystem::exists(base / dir_b / ref), "File " << ref << " not found");
211 }
212