Subtitles at method.
[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
29 #define BOOST_TEST_DYN_LINK
30 #define BOOST_TEST_MODULE libdcp_test
31 #include <boost/test/unit_test.hpp>
32
33 using namespace std;
34 using namespace boost;
35
36 string
37 j2c (int)
38 {
39         return "test/data/32x32_red_square.j2c";
40 }
41
42 string
43 wav (libdcp::Channel)
44 {
45         return "test/data/1s_24-bit_48k_silence.wav";
46 }
47                 
48
49 BOOST_AUTO_TEST_CASE (dcp_test)
50 {
51         Kumu::libdcp_test = true;
52         
53         libdcp::Metadata* t = libdcp::Metadata::instance ();
54         t->issuer = "OpenDCP 0.0.25";
55         t->creator = "OpenDCP 0.0.25";
56         t->company_name = "OpenDCP";
57         t->product_name = "OpenDCP";
58         t->product_version = "0.0.25";
59         t->issue_date = "2012-07-17T04:45:18+00:00";
60         filesystem::remove_all ("build/test/foo");
61         filesystem::create_directories ("build/test/foo");
62         libdcp::DCP d ("build/test/foo", "A Test DCP", libdcp::FEATURE, 24, 24);
63
64         d.add_picture_asset (sigc::ptr_fun (&j2c), 32, 32);
65         d.add_sound_asset (sigc::ptr_fun (&wav), 2);
66
67         d.write_xml ();
68 }
69
70 BOOST_AUTO_TEST_CASE (error_test)
71 {
72         libdcp::DCP d ("build/test/bar", "A Test DCP", libdcp::TEST, 24, 24);
73         vector<string> p;
74         p.push_back ("frobozz");
75         BOOST_CHECK_THROW (d.add_picture_asset (p, 32, 32), libdcp::FileError);
76         BOOST_CHECK_THROW (d.add_sound_asset (p), libdcp::FileError);
77 }
78
79 BOOST_AUTO_TEST_CASE (read_dcp)
80 {
81         libdcp::DCP d ("test/ref/DCP");
82
83         BOOST_CHECK_EQUAL (d.name(), "A Test DCP");
84         BOOST_CHECK_EQUAL (d.content_kind(), libdcp::FEATURE);
85         BOOST_CHECK_EQUAL (d.frames_per_second(), 24);
86         BOOST_CHECK_EQUAL (d.length(), 24);
87 }
88         
89 BOOST_AUTO_TEST_CASE (subtitles)
90 {
91         libdcp::SubtitleAsset subs ("test/ref", "subs.xml");
92
93         BOOST_CHECK_EQUAL (subs.language(), "French");
94         BOOST_CHECK_EQUAL (subs.fonts().size(), 1);
95         BOOST_CHECK_EQUAL (subs.fonts().front()->subtitles().size(), 4);
96
97         list<shared_ptr<libdcp::Subtitle> >::const_iterator i = subs.fonts().front()->subtitles().begin ();
98
99         BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 5, 198));
100         BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 7, 115));
101         BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
102         BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
103         BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "My jacket was Idi Amin's");
104         ++i;
105
106         BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 7, 177));
107         BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 11, 31));
108         BOOST_CHECK_EQUAL ((*i)->texts().size(), 2);
109         BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 21);
110         BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "My corset was H.M. The Queen's");
111         BOOST_CHECK_EQUAL ((*i)->texts().back()->v_position(), 15);
112         BOOST_CHECK_EQUAL ((*i)->texts().back()->text(), "My large wonderbra");
113         ++i;
114
115         BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 11, 94));
116         BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 13, 63));
117         BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
118         BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
119         BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "Once belonged to the Shah");
120         ++i;
121
122         BOOST_CHECK_EQUAL ((*i)->in(), libdcp::Time (0, 0, 13, 104));
123         BOOST_CHECK_EQUAL ((*i)->out(), libdcp::Time (0, 0, 15, 177));
124         BOOST_CHECK_EQUAL ((*i)->texts().size(), 1);
125         BOOST_CHECK_EQUAL ((*i)->texts().front()->v_position(), 15);
126         BOOST_CHECK_EQUAL ((*i)->texts().front()->text(), "And these are Roy Hattersley's jeans");
127
128         BOOST_CHECK_EQUAL (subs.subtitles_at (libdcp::Time (0, 0, 14, 042)).size(), 1);
129         BOOST_CHECK_EQUAL (subs.subtitles_at (libdcp::Time (0, 0, 14, 042)).front()->text(), "And these are Roy Hattersley's jeans");
130 }
131
132