Add a unit test.
[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
37 static
38 bool
39 confirm_overwrite (boost::filesystem::path)
40 {
41         return true;
42 }
43
44
45 BOOST_AUTO_TEST_CASE (single_kdm_name_test)
46 {
47         Config* c = Config::instance();
48
49         dcp::Certificate cert = c->decryption_chain()->leaf();
50
51         /* Cinema A: UTC +4:30 */
52         shared_ptr<Cinema> cinema_a (new Cinema("Cinema A", list<string>(), "", 4, 30));
53         shared_ptr<dcpomatic::Screen> cinema_a_screen_1(new dcpomatic::Screen("Screen 1", "", cert, vector<TrustedDevice>()));
54         cinema_a->add_screen (cinema_a_screen_1);
55         shared_ptr<dcpomatic::Screen> cinema_a_screen_2(new dcpomatic::Screen("Screen 2", "", cert, vector<TrustedDevice>()));
56         cinema_a->add_screen (cinema_a_screen_2);
57         c->add_cinema (cinema_a);
58
59         /* Cinema B: UTC -1:00 */
60         shared_ptr<Cinema> cinema_b (new Cinema("Cinema B", list<string>(), "", -1, 0));
61         shared_ptr<dcpomatic::Screen> cinema_b_screen_x(new dcpomatic::Screen("Screen X", "", cert, vector<TrustedDevice>()));
62         cinema_b->add_screen (cinema_b_screen_x);
63         shared_ptr<dcpomatic::Screen> cinema_b_screen_y(new dcpomatic::Screen("Screen Y", "", cert, vector<TrustedDevice>()));
64         cinema_b->add_screen (cinema_b_screen_y);
65         shared_ptr<dcpomatic::Screen> cinema_b_screen_z(new dcpomatic::Screen("Screen Z", "", cert, vector<TrustedDevice>()));
66         cinema_b->add_screen (cinema_b_screen_z);
67         c->add_cinema (cinema_a);
68
69         /* Film */
70         boost::filesystem::remove_all ("build/test/single_kdm_name_test");
71         shared_ptr<Film> film = new_test_film2 ("single_kdm_name_test");
72         film->set_name ("my_great_film");
73         film->examine_and_add_content (content_factory("test/data/flat_black.png").front());
74         BOOST_REQUIRE (!wait_for_jobs());
75         film->set_encrypted (true);
76         film->make_dcp ();
77         BOOST_REQUIRE (!wait_for_jobs());
78         film->write_metadata ();
79         vector<CPLSummary> cpls = film->cpls ();
80         BOOST_REQUIRE(cpls.size() == 1);
81
82         dcp::LocalTime from (cert.not_before());
83         from.add_months (2);
84         dcp::LocalTime until (cert.not_after());
85         until.add_months (-2);
86
87         string const from_string = from.date() + " " + from.time_of_day(true, false);
88         string const until_string = until.date() + " " + until.time_of_day(true, false);
89
90         boost::filesystem::path cpl = cpls.front().cpl_file;
91         KDMWithMetadataPtr kdm = kdm_for_screen (
92                         film,
93                         cpls.front().cpl_file,
94                         cinema_a_screen_1,
95                         boost::posix_time::time_from_string(from_string),
96                         boost::posix_time::time_from_string(until_string),
97                         dcp::MODIFIED_TRANSITIONAL_1,
98                         false,
99                         optional<int>()
100                         );
101
102         list<KDMWithMetadataPtr> kdms;
103         kdms.push_back (kdm);
104
105         write_files (
106                 kdms,
107                 boost::filesystem::path("build/test/single_kdm_name_test"),
108                 dcp::NameFormat("KDM %c - %s - %f - %b - %e"),
109                 &confirm_overwrite
110                 );
111
112         string from_time = from.time_of_day (true, false);
113         boost::algorithm::replace_all (from_time, ":", "-");
114         string until_time = until.time_of_day (true, false);
115         boost::algorithm::replace_all (until_time, ":", "-");
116
117         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);
118         BOOST_CHECK_MESSAGE (boost::filesystem::exists("build/test/single_kdm_name_test/" + ref), "File " << ref << " not found");
119 }
120