De-hackify subtitle handling a bit.
[libdcp.git] / test / tests.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/filesystem.hpp>
21 #include "KM_prng.h"
22 #include "dcp.h"
23 #include "util.h"
24 #include "metadata.h"
25 #include "types.h"
26 #include "exceptions.h"
27 #include "subtitle_asset.h"
28 #include "picture_asset.h"
29 #include "sound_asset.h"
30 #include "reel.h"
31
32 #define BOOST_TEST_DYN_LINK
33 #define BOOST_TEST_MODULE libdcp_test
34 #include <boost/test/unit_test.hpp>
35
36 using namespace std;
37 using namespace boost;
38
39 string
40 j2c (int)
41 {
42         return "test/data/32x32_red_square.j2c";
43 }
44
45 string
46 wav (libdcp::Channel)
47 {
48         return "test/data/1s_24-bit_48k_silence.wav";
49 }
50                 
51
52 BOOST_AUTO_TEST_CASE (dcp_test)
53 {
54         Kumu::libdcp_test = true;
55         
56         libdcp::Metadata* t = libdcp::Metadata::instance ();
57         t->issuer = "OpenDCP 0.0.25";
58         t->creator = "OpenDCP 0.0.25";
59         t->company_name = "OpenDCP";
60         t->product_name = "OpenDCP";
61         t->product_version = "0.0.25";
62         t->issue_date = "2012-07-17T04:45:18+00:00";
63         filesystem::remove_all ("build/test/foo");
64         filesystem::create_directories ("build/test/foo");
65         libdcp::DCP d ("build/test/foo", "A Test DCP", libdcp::FEATURE, 24, 24);
66
67         shared_ptr<libdcp::PictureAsset> mp (new libdcp::PictureAsset (
68                                                     sigc::ptr_fun (&j2c),
69                                                     "build/test/foo",
70                                                     "video.mxf",
71                                                     &d.Progress,
72                                                     24,
73                                                     24,
74                                                     32,
75                                                     32
76                                                     ));
77
78         shared_ptr<libdcp::SoundAsset> ms (new libdcp::SoundAsset (
79                                                   sigc::ptr_fun (&wav),
80                                                   "build/test/foo",
81                                                   "audio.mxf",
82                                                   &(d.Progress),
83                                                   24,
84                                                   24,
85                                                   2
86                                                   ));
87
88         d.add_reel (shared_ptr<libdcp::Reel> (new libdcp::Reel (mp, ms, shared_ptr<libdcp::SubtitleAsset> ())));
89
90         d.write_xml ();
91 }
92
93 BOOST_AUTO_TEST_CASE (error_test)
94 {
95         libdcp::DCP d ("build/test/bar", "A Test DCP", libdcp::TEST, 24, 24);
96         vector<string> p;
97         p.push_back ("frobozz");
98
99         BOOST_CHECK_THROW (new libdcp::PictureAsset (p, "build/test/bar", "video.mxf", &d.Progress, 24, 24, 32, 32), libdcp::FileError);
100         BOOST_CHECK_THROW (new libdcp::SoundAsset (p, "build/test/bar", "audio.mxf", &d.Progress, 24, 24), libdcp::FileError);
101 }
102
103 BOOST_AUTO_TEST_CASE (read_dcp)
104 {
105         libdcp::DCP d ("test/ref/DCP");
106
107         BOOST_CHECK_EQUAL (d.name(), "A Test DCP");
108         BOOST_CHECK_EQUAL (d.content_kind(), libdcp::FEATURE);
109         BOOST_CHECK_EQUAL (d.frames_per_second(), 24);
110         BOOST_CHECK_EQUAL (d.length(), 24);
111 }
112         
113 BOOST_AUTO_TEST_CASE (subtitles)
114 {
115         libdcp::SubtitleAsset subs ("test/ref", "subs.xml");
116
117         BOOST_CHECK_EQUAL (subs.language(), "French");
118         BOOST_CHECK_EQUAL (subs.font_nodes().size(), 1);
119         BOOST_CHECK_EQUAL (subs.font_nodes().front()->subtitle_nodes.size(), 4);
120
121         list<shared_ptr<libdcp::SubtitleNode> >::const_iterator i = subs.font_nodes().front()->subtitle_nodes.begin ();
122
123         BOOST_CHECK_EQUAL ((*i)->in, libdcp::Time (0, 0, 5, 198));
124         BOOST_CHECK_EQUAL ((*i)->out, libdcp::Time (0, 0, 7, 115));
125         BOOST_CHECK_EQUAL ((*i)->text_nodes.size(), 1);
126         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->v_position, 15);
127         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->text, "My jacket was Idi Amin's");
128         ++i;
129
130         BOOST_CHECK_EQUAL ((*i)->in, libdcp::Time (0, 0, 7, 177));
131         BOOST_CHECK_EQUAL ((*i)->out, libdcp::Time (0, 0, 11, 31));
132         BOOST_CHECK_EQUAL ((*i)->text_nodes.size(), 2);
133         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->v_position, 21);
134         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->text, "My corset was H.M. The Queen's");
135         BOOST_CHECK_EQUAL ((*i)->text_nodes.back()->v_position, 15);
136         BOOST_CHECK_EQUAL ((*i)->text_nodes.back()->text, "My large wonderbra");
137         ++i;
138
139         BOOST_CHECK_EQUAL ((*i)->in, libdcp::Time (0, 0, 11, 94));
140         BOOST_CHECK_EQUAL ((*i)->out, libdcp::Time (0, 0, 13, 63));
141         BOOST_CHECK_EQUAL ((*i)->text_nodes.size(), 1);
142         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->v_position, 15);
143         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->text, "Once belonged to the Shah");
144         ++i;
145
146         BOOST_CHECK_EQUAL ((*i)->in, libdcp::Time (0, 0, 13, 104));
147         BOOST_CHECK_EQUAL ((*i)->out, libdcp::Time (0, 0, 15, 177));
148         BOOST_CHECK_EQUAL ((*i)->text_nodes.size(), 1);
149         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->v_position, 15);
150         BOOST_CHECK_EQUAL ((*i)->text_nodes.front()->text, "And these are Roy Hattersley's jeans");
151
152         BOOST_CHECK_EQUAL (subs.subtitles_at (libdcp::Time (0, 0, 14, 42)).size(), 1);
153         shared_ptr<libdcp::Subtitle> s = subs.subtitles_at (libdcp::Time (0, 0, 14, 42)).front ();
154         BOOST_CHECK_EQUAL (s->text(), "And these are Roy Hattersley's jeans");
155         BOOST_CHECK_EQUAL (s->v_position(), 15);
156         BOOST_CHECK_EQUAL (s->in(), libdcp::Time (0, 0, 13, 104));
157         BOOST_CHECK_EQUAL (s->out(), libdcp::Time (0, 0, 15, 177));
158         BOOST_CHECK_EQUAL (s->font(), "Arial");
159         BOOST_CHECK_EQUAL (s->size_in_pixels(1080), 53);
160 }
161
162 BOOST_AUTO_TEST_CASE (dcp_time)
163 {
164         libdcp::Time t (977143, 24);
165
166         BOOST_CHECK_EQUAL (t.t, 73);
167         BOOST_CHECK_EQUAL (t.s, 34);
168         BOOST_CHECK_EQUAL (t.m, 18);
169         BOOST_CHECK_EQUAL (t.h, 11);
170 }