Fill test disk partitions with random noise to expose more bugs.
[dcpomatic.git] / test / subtitle_font_id_change_test.cc
1 /*
2     Copyright (C) 2022 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 /** @file  test/subtitle_font_id_change_test.cc
23  *  @brief Check that old projects can still be used after the changes in 5a820bb8fae34591be5ac6d19a73461b9dab532a
24  */
25
26
27 #include "lib/check_content_job.h"
28 #include "lib/content.h"
29 #include "lib/film.h"
30 #include "lib/font.h"
31 #include "lib/text_content.h"
32 #include "test.h"
33 #include <dcp/verify.h>
34 #include <boost/filesystem.hpp>
35 #include <boost/test/unit_test.hpp>
36
37
38 using std::string;
39
40
41 class Editor
42 {
43 public:
44         Editor (boost::filesystem::path path)
45                 : _path(path)
46                 , _content(dcp::file_to_string(path))
47         {
48
49         }
50
51         ~Editor ()
52         {
53                 auto f = fopen(_path.string().c_str(), "w");
54                 BOOST_REQUIRE(f);
55                 fwrite(_content.c_str(), _content.length(), 1, f);
56                 fclose(f);
57         }
58
59         void replace (string a, string b)
60         {
61                 auto old_content = _content;
62                 boost::algorithm::replace_all (_content, a, b);
63                 BOOST_REQUIRE (_content != old_content);
64         }
65
66 private:
67         boost::filesystem::path _path;
68         std::string _content;
69 };
70
71
72 BOOST_AUTO_TEST_CASE(subtitle_font_id_change_test1)
73 {
74         auto film = new_test_film2("subtitle_font_id_change_test1");
75         boost::filesystem::remove(film->file("metadata.xml"));
76         boost::filesystem::copy_file("test/data/subtitle_font_id_change_test1.xml", film->file("metadata.xml"));
77         film->read_metadata();
78
79         auto content = film->content();
80         BOOST_REQUIRE_EQUAL(content.size(), 1U);
81         BOOST_REQUIRE_EQUAL(content[0]->text.size(), 1U);
82
83         content[0]->set_paths({"test/data/short.srt"});
84
85         CheckContentJob check(film);
86         check.run();
87
88         make_and_verify_dcp(film, { dcp::VerificationNote::Code::INVALID_STANDARD });
89 }
90
91
92 BOOST_AUTO_TEST_CASE(subtitle_font_id_change_test2)
93 {
94         auto film = new_test_film2("subtitle_font_id_change_test2");
95         boost::filesystem::remove(film->file("metadata.xml"));
96         boost::filesystem::copy_file("test/data/subtitle_font_id_change_test2.xml", film->file("metadata.xml"));
97         {
98                 Editor editor(film->file("metadata.xml"));
99                 editor.replace("/usr/share/fonts/truetype/inconsolata/Inconsolata.otf", "test/data/Inconsolata-VF.ttf");
100         }
101         film->read_metadata();
102
103         auto content = film->content();
104         BOOST_REQUIRE_EQUAL(content.size(), 1U);
105         BOOST_REQUIRE_EQUAL(content[0]->text.size(), 1U);
106
107         content[0]->set_paths({"test/data/short.srt"});
108
109         CheckContentJob check(film);
110         check.run();
111
112         auto font = content[0]->text.front()->get_font("");
113         BOOST_REQUIRE(font->file());
114         BOOST_CHECK_EQUAL(*font->file(), "test/data/Inconsolata-VF.ttf");
115
116         make_and_verify_dcp(film, { dcp::VerificationNote::Code::INVALID_STANDARD });
117 }
118
119
120 BOOST_AUTO_TEST_CASE(subtitle_font_id_change_test3)
121 {
122         auto film = new_test_film2("subtitle_font_id_change_test3");
123         boost::filesystem::remove(film->file("metadata.xml"));
124         boost::filesystem::copy_file("test/data/subtitle_font_id_change_test3.xml", film->file("metadata.xml"));
125         {
126                 Editor editor(film->file("metadata.xml"));
127                 editor.replace("/usr/share/fonts/truetype/inconsolata/Inconsolata.otf", "test/data/Inconsolata-VF.ttf");
128         }
129         film->read_metadata();
130
131         auto content = film->content();
132         BOOST_REQUIRE_EQUAL(content.size(), 1U);
133         BOOST_REQUIRE_EQUAL(content[0]->text.size(), 1U);
134
135         content[0]->set_paths({"test/data/fonts.ass"});
136
137         CheckContentJob check(film);
138         check.run();
139
140         auto font = content[0]->text.front()->get_font("Arial Black");
141         BOOST_REQUIRE(font->file());
142         BOOST_CHECK_EQUAL(*font->file(), "test/data/Inconsolata-VF.ttf");
143
144         font = content[0]->text.front()->get_font("Helvetica Neue");
145         BOOST_REQUIRE(font->file());
146         BOOST_CHECK_EQUAL(*font->file(), "test/data/Inconsolata-VF.ttf");
147
148         make_and_verify_dcp(film, { dcp::VerificationNote::Code::INVALID_STANDARD });
149 }