Add --twod option to create CLI and stop 2D from being forced over
[dcpomatic.git] / test / create_cli_test.cc
1 /*
2     Copyright (C) 2019-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/config.h"
23 #include "lib/create_cli.h"
24 #include "lib/film.h"
25 #include "lib/ratio.h"
26 #include "lib/dcp_content_type.h"
27 #include "test.h"
28 #include <boost/test/unit_test.hpp>
29 #include <boost/tokenizer.hpp>
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <iostream>
32
33
34 using std::string;
35
36
37 static CreateCLI
38 run (string cmd)
39 {
40         /* This approximates the logic which splits command lines up into argc/argv */
41
42         boost::escaped_list_separator<char> els ("", " ", "\"\'");
43         boost::tokenizer<boost::escaped_list_separator<char> > tok (cmd, els);
44
45         std::vector<char*> argv(256);
46         int argc = 0;
47
48         for (boost::tokenizer<boost::escaped_list_separator<char> >::iterator i = tok.begin(); i != tok.end(); ++i) {
49                 argv[argc++] = strdup (i->c_str());
50         }
51
52         CreateCLI cc (argc, argv.data());
53
54         for (int i = 0; i < argc; ++i) {
55                 free (argv[i]);
56         }
57
58         return cc;
59 }
60
61 BOOST_AUTO_TEST_CASE (create_cli_test)
62 {
63         CreateCLI cc = run ("dcpomatic2_create --version");
64         BOOST_CHECK (!cc.error);
65         BOOST_CHECK (cc.version);
66
67         cc = run ("dcpomatic2_create --versionX");
68         BOOST_REQUIRE (cc.error);
69         BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: unrecognised option '--versionX'"));
70
71         cc = run ("dcpomatic2_create --help");
72         BOOST_REQUIRE (cc.error);
73
74         cc = run ("dcpomatic2_create -h");
75         BOOST_REQUIRE (cc.error);
76
77         cc = run ("dcpomatic2_create x --name frobozz --template bar");
78         BOOST_CHECK (!cc.error);
79         BOOST_CHECK_EQUAL(cc._name, "frobozz");
80         BOOST_REQUIRE(cc._template_name);
81         BOOST_CHECK_EQUAL(*cc._template_name, "bar");
82
83         cc = run ("dcpomatic2_create x --dcp-content-type FTR");
84         BOOST_CHECK (!cc.error);
85         BOOST_CHECK_EQUAL(cc._dcp_content_type, DCPContentType::from_isdcf_name("FTR"));
86
87         cc = run ("dcpomatic2_create x --dcp-frame-rate 30");
88         BOOST_CHECK (!cc.error);
89         BOOST_REQUIRE (cc.dcp_frame_rate);
90         BOOST_CHECK_EQUAL (*cc.dcp_frame_rate, 30);
91
92         cc = run ("dcpomatic2_create x --container-ratio 185");
93         BOOST_CHECK (!cc.error);
94         BOOST_CHECK_EQUAL(cc._container_ratio, Ratio::from_id("185"));
95
96         cc = run ("dcpomatic2_create x --container-ratio XXX");
97         BOOST_CHECK (cc.error);
98
99         cc = run ("dcpomatic2_create x --still-length 42");
100         BOOST_CHECK (!cc.error);
101         BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);
102
103         cc = run ("dcpomatic2_create x --standard SMPTE");
104         BOOST_CHECK (!cc.error);
105         BOOST_CHECK_EQUAL(cc._standard, dcp::Standard::SMPTE);
106
107         cc = run ("dcpomatic2_create x --standard interop");
108         BOOST_CHECK (!cc.error);
109         BOOST_CHECK_EQUAL(cc._standard, dcp::Standard::INTEROP);
110
111         cc = run ("dcpomatic2_create x --standard SMPTEX");
112         BOOST_CHECK (cc.error);
113
114         cc = run("dcpomatic2_create x --twod");
115         BOOST_CHECK(cc._twod);
116
117         cc = run("dcpomatic2_create x --threed");
118         BOOST_CHECK(cc._threed);
119
120         cc = run("dcpomatic2_create x --twod --threed");
121         BOOST_CHECK(cc.error);
122
123         cc = run ("dcpomatic2_create x --config foo/bar");
124         BOOST_CHECK (!cc.error);
125         BOOST_REQUIRE (cc.config_dir);
126         BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");
127
128         cc = run ("dcpomatic2_create x --output fred/jim");
129         BOOST_CHECK (!cc.error);
130         BOOST_REQUIRE (cc.output_dir);
131         BOOST_CHECK_EQUAL (*cc.output_dir, "fred/jim");
132
133         cc = run ("dcpomatic2_create x --outputX fred/jim");
134         BOOST_CHECK (cc.error);
135
136         cc = run ("dcpomatic2_create --config foo/bar --still-length 42 --output flaps fred jim sheila");
137         BOOST_CHECK (!cc.error);
138         BOOST_REQUIRE (cc.config_dir);
139         BOOST_CHECK_EQUAL (*cc.config_dir, "foo/bar");
140         BOOST_CHECK_EQUAL(cc.still_length.get_value_or(0), 42);
141         BOOST_REQUIRE (cc.output_dir);
142         BOOST_CHECK_EQUAL (*cc.output_dir, "flaps");
143         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
144         BOOST_CHECK_EQUAL (cc.content[0].path, "fred");
145         BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::TWO_D);
146         BOOST_CHECK_EQUAL (cc.content[1].path, "jim");
147         BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::TWO_D);
148         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila");
149         BOOST_CHECK_EQUAL (cc.content[2].frame_type, VideoFrameType::TWO_D);
150
151         cc = run ("dcpomatic2_create --left-eye left.mp4 --right-eye right.mp4");
152         BOOST_REQUIRE_EQUAL (cc.content.size(), 2U);
153         BOOST_CHECK_EQUAL (cc.content[0].path, "left.mp4");
154         BOOST_CHECK_EQUAL (cc.content[0].frame_type, VideoFrameType::THREE_D_LEFT);
155         BOOST_CHECK_EQUAL (cc.content[1].path, "right.mp4");
156         BOOST_CHECK_EQUAL (cc.content[1].frame_type, VideoFrameType::THREE_D_RIGHT);
157         BOOST_CHECK_EQUAL(cc._fourk, false);
158
159         cc = run ("dcpomatic2_create --twok foo.mp4");
160         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
161         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
162         BOOST_CHECK_EQUAL(cc._twok, true);
163         BOOST_CHECK (!cc.error);
164
165         cc = run ("dcpomatic2_create --fourk foo.mp4");
166         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
167         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
168         BOOST_CHECK_EQUAL(cc._fourk, true);
169         BOOST_CHECK (!cc.error);
170
171         cc = run ("dcpomatic2_create --j2k-bandwidth 120 foo.mp4");
172         BOOST_REQUIRE_EQUAL (cc.content.size(), 1U);
173         BOOST_CHECK_EQUAL (cc.content[0].path, "foo.mp4");
174         BOOST_REQUIRE(cc._j2k_bandwidth);
175         BOOST_CHECK_EQUAL(*cc._j2k_bandwidth, 120000000);
176         BOOST_CHECK (!cc.error);
177
178         cc = run ("dcpomatic2_create --channel L fred.wav --channel R jim.wav sheila.wav");
179         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
180         BOOST_CHECK_EQUAL (cc.content[0].path, "fred.wav");
181         BOOST_CHECK (cc.content[0].channel);
182         BOOST_CHECK (*cc.content[0].channel == dcp::Channel::LEFT);
183         BOOST_CHECK_EQUAL (cc.content[1].path, "jim.wav");
184         BOOST_CHECK (cc.content[1].channel);
185         BOOST_CHECK (*cc.content[1].channel == dcp::Channel::RIGHT);
186         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila.wav");
187         BOOST_CHECK (!cc.content[2].channel);
188
189         cc = run ("dcpomatic2_create --channel foo fred.wav");
190         BOOST_REQUIRE (cc.error);
191         BOOST_CHECK (boost::algorithm::starts_with(*cc.error, "dcpomatic2_create: foo is not valid for --channel"));
192
193         cc = run ("dcpomatic2_create fred.wav --gain -6 jim.wav --gain 2 sheila.wav");
194         BOOST_REQUIRE_EQUAL (cc.content.size(), 3U);
195         BOOST_CHECK_EQUAL (cc.content[0].path, "fred.wav");
196         BOOST_CHECK (!cc.content[0].gain);
197         BOOST_CHECK_EQUAL (cc.content[1].path, "jim.wav");
198         BOOST_CHECK_CLOSE (*cc.content[1].gain, -6, 0.001);
199         BOOST_CHECK_EQUAL (cc.content[2].path, "sheila.wav");
200         BOOST_CHECK_CLOSE (*cc.content[2].gain, 2, 0.001);
201
202         cc = run("dcpomatic2_create --cpl 123456-789-0 dcp");
203         BOOST_REQUIRE_EQUAL(cc.content.size(), 1U);
204         BOOST_CHECK_EQUAL(cc.content[0].path, "dcp");
205         BOOST_REQUIRE(static_cast<bool>(cc.content[0].cpl));
206         BOOST_CHECK_EQUAL(*cc.content[0].cpl, "123456-789-0");
207
208         cc = run("dcpomatic2_create -s SMPTE sheila.wav");
209         BOOST_CHECK(!cc.still_length);
210         BOOST_CHECK(cc.error);
211 }
212
213
214 BOOST_AUTO_TEST_CASE(create_cli_template_test)
215 {
216         ConfigRestorer cr;
217
218         Config::override_path = "test/data";
219
220         auto cc = run("dcpomatic2_create test/data/flat_red.png --template 2d");
221         auto film = cc.make_film();
222         BOOST_CHECK(!film->three_d());
223
224         cc = run("dcpomatic2_create test/data/flat_red.png --template 2d --threed");
225         film = cc.make_film();
226         BOOST_CHECK(film->three_d());
227
228         cc = run("dcpomatic2_create test/data/flat_red.png --template 3d");
229         film = cc.make_film();
230         BOOST_CHECK(film->three_d());
231
232         cc = run("dcpomatic2_create test/data/flat_red.png --template 3d --twod");
233         film = cc.make_film();
234         BOOST_CHECK(!film->three_d());
235 }
236