3 #include "../aspectratio.h" 4 #include "../backuphelper.h" 5 #include "../exceptions.h" 7 #include "../mediafileinfo.h" 8 #include "../mediaformat.h" 9 #include "../signature.h" 11 #include "../tagtarget.h" 13 #include <c++utilities/conversion/stringbuilder.h> 14 #include <c++utilities/io/catchiofailure.h> 15 #include <c++utilities/tests/testutils.h> 18 #include <cppunit/TestFixture.h> 19 #include <cppunit/extensions/HelperMacros.h> 36 CPPUNIT_TEST(testSize);
37 CPPUNIT_TEST(testTagTarget);
38 CPPUNIT_TEST(testSignature);
39 CPPUNIT_TEST(testMargin);
40 CPPUNIT_TEST(testAspectRatio);
41 CPPUNIT_TEST(testMediaFormat);
43 CPPUNIT_TEST(testBackupFile);
45 CPPUNIT_TEST_SUITE_END();
52 void testStatusProvider();
56 void testAspectRatio();
57 void testMediaFormat();
59 void testBackupFile();
75 static_assert(
Size().isNull(),
"Size::isNull()");
76 static_assert(!
Size(3, 4).isNull(),
"Size::isNull()");
77 static_assert(
Size(3, 4).resolution() == 12,
"Size::resolution");
79 Size size(1920, 1080);
80 CPPUNIT_ASSERT_EQUAL(
"width: 1920, height: 1080"s, size.
toString());
81 CPPUNIT_ASSERT_EQUAL(
"1080p"s,
string(size.
abbreviation()));
84 CPPUNIT_ASSERT_EQUAL(
"720p"s,
string(size.
abbreviation()));
90 CPPUNIT_ASSERT(target.
isEmpty());
91 CPPUNIT_ASSERT_EQUAL_MESSAGE(
"default level is 50", 50ul, target.
level());
92 CPPUNIT_ASSERT_EQUAL(
"level 50"s, target.
toString(TagTargetLevel::Unspecified));
93 target =
TagTarget(30, { 1, 2, 3 }, { 4 }, { 5, 6 }, { 7, 8, 9 });
94 CPPUNIT_ASSERT(!target.isEmpty());
95 const auto mapping = [](uint64 level) {
return level == 30 ?
TagTargetLevel::Track : TagTargetLevel::Unspecified; };
97 "level 30 'track, song, chapter', track 1, track 2, track 3, chapter 4, edition 5, edition 6, attachment 7, attachment 8, attachment 9"s,
98 target.toString(mapping));
100 CPPUNIT_ASSERT_EQUAL(
"level 40, track 1, track 2, track 3, chapter 4, edition 5, edition 6, attachment 7, attachment 8, attachment 9"s,
101 target.toString(mapping));
102 target.setLevelName(
"test");
103 CPPUNIT_ASSERT_EQUAL(
"level 40 'test', track 1, track 2, track 3, chapter 4, edition 5, edition 6, attachment 7, attachment 8, attachment 9"s,
104 target.toString(mapping));
105 CPPUNIT_ASSERT(target ==
TagTarget(40, { 1, 2, 3 }, { 4 }, { 5, 6 }, { 7, 8, 9 }));
107 CPPUNIT_ASSERT(target.isEmpty());
112 const unsigned char xzHead[12] = { 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x04, 0xe6, 0xd6, 0xb4, 0x46 };
115 CPPUNIT_ASSERT_EQUAL(ContainerFormat::Unknown,
parseSignature(reinterpret_cast<const char *>(xzHead), 3));
116 CPPUNIT_ASSERT_EQUAL(ContainerFormat::Unknown,
parseSignature(reinterpret_cast<const char *>(xzHead), 2));
117 CPPUNIT_ASSERT_EQUAL(ContainerFormat::Unknown,
parseSignature(reinterpret_cast<const char *>(xzHead), 0));
119 const auto containerFormat =
parseSignature(reinterpret_cast<const char *>(xzHead),
sizeof(xzHead));
128 static_assert(
Margin().isNull(),
"empty margin");
129 static_assert(!
Margin(0, 2).isNull(),
"non-empty margin");
131 CPPUNIT_ASSERT_EQUAL(
"top: 1; left: 2; bottom: 3; right: 4"s,
Margin(1, 2, 3, 4).toString());
136 static_assert(!
AspectRatio().isValid(),
"invalid aspect ratio");
137 static_assert(
AspectRatio(16, 9).isValid(),
"valid aspect ratio");
138 static_assert(
AspectRatio(16, 9).isExtended(),
"extended aspect ratio");
141 CPPUNIT_ASSERT_EQUAL(static_cast<uint16>(16), ratio.
numerator);
142 CPPUNIT_ASSERT_EQUAL(static_cast<uint16>(11), ratio.
denominator);
144 CPPUNIT_ASSERT_EQUAL(static_cast<uint16>(0), ratio2.
numerator);
145 CPPUNIT_ASSERT_EQUAL(static_cast<uint16>(0), ratio2.
denominator);
152 CPPUNIT_ASSERT_EQUAL(
"Advanced Audio Coding"s,
string(aac.
name()));
153 CPPUNIT_ASSERT_EQUAL(
"AAC"s,
string(aac.
abbreviation()));
160 CPPUNIT_ASSERT_EQUAL(
"Advanced Audio Coding Low Complexity Profile"s,
string(aac.
name()));
161 CPPUNIT_ASSERT_EQUAL(
"MPEG-4 AAC-LC"s,
string(aac.
abbreviation()));
163 CPPUNIT_ASSERT_EQUAL(
"Spectral Band Replication / HE-AAC"s,
string(aac.
extensionName()));
167 void UtilitiesTests::testBackupFile()
169 using namespace BackupHelper;
177 const auto workingDir(file.containingDirectory());
181 string backupPath1, backupPath2;
182 NativeFileStream backupStream1, backupStream2;
184 CPPUNIT_ASSERT_EQUAL(workingDir +
"/unsupported.bin.bak", backupPath1);
187 file.stream().open(file.path(), ios_base::out);
188 file.stream() <<
"test1" << endl;
192 CPPUNIT_ASSERT_EQUAL(workingDir +
"/unsupported.bin.1.bak", backupPath2);
195 backupStream2.close();
196 remove(backupPath2.data());
197 file.stream().open(file.path(), ios_base::out);
198 file.stream() <<
"test2" << endl;
204 CPPUNIT_FAIL(
"renaming failed because backup dir does not exist");
206 const char *what = catchIoFailure();
207 CPPUNIT_ASSERT(strstr(what,
"Unable to rename original file before rewriting it."));
209 backupStream2.clear();
210 workingCopyPathMode(
"bak/unsupported.bin", WorkingCopyMode::NoCopy);
212 CPPUNIT_ASSERT_EQUAL(workingDir +
"/bak/unsupported.bin", backupPath2);
215 backupStream2.close();
216 CPPUNIT_ASSERT_EQUAL(0,
remove(backupPath2.data()));
217 CPPUNIT_ASSERT_EQUAL(0,
remove(argsToString(workingDir %
'/' +
backupDirectory()).data()));
220 backupStream1.seekg(0, ios_base::end);
221 CPPUNIT_ASSERT_EQUAL(41_st, static_cast<size_t>(backupStream1.tellg()));
228 file.stream().seekg(0x1D);
229 CPPUNIT_ASSERT_EQUAL(0x34_st, static_cast<size_t>(file.stream().get()));
241 CPPUNIT_ASSERT_THROW(
243 CPPUNIT_ASSERT(diag.
level() < DiagLevel::Critical);
244 CPPUNIT_ASSERT(!diag.empty());
245 CPPUNIT_ASSERT_EQUAL(
"Rewriting the file to apply changed tag information has been aborted."s, diag.front().message());
246 CPPUNIT_ASSERT_EQUAL(
"The original file has been restored."s, diag.back().message());
256 CPPUNIT_ASSERT(diag.
level() >= DiagLevel::Critical);
257 CPPUNIT_ASSERT_EQUAL(
"Rewriting the file to apply changed tag information failed."s, diag.front().message());
258 CPPUNIT_ASSERT_EQUAL(
"The original file has been restored."s, diag.back().message());
264 throwIoFailure(
"simulated IO failure");
269 CPPUNIT_FAIL(
"IO failure not rethrown");
273 CPPUNIT_ASSERT(diag.
level() >= DiagLevel::Critical);
274 CPPUNIT_ASSERT_EQUAL(
"An IO error occured when rewriting the file to apply changed tag information."s, diag.front().message());
275 CPPUNIT_ASSERT_EQUAL(
"The original file has been restored."s, diag.back().message());
278 CPPUNIT_ASSERT_EQUAL(0,
remove(file.path().data()));
The Margin class defines the four margins of a rectangle.
std::string toString(const std::function< TagTargetLevel(uint64)> &tagTargetMapping) const
Returns the string representation of the current instance.
const char * abbreviation() const
Returns an abbreviation for the current instance, eg.
void setWidth(uint32 value)
Sets the width.
uint64 level() const
Returns the level.
TAG_PARSER_EXPORT std::string & backupDirectory()
Returns the directory used to store backup files.
The UtilitiesTests class tests various utility classes and functions of the tagparser library...
void setHeight(uint32 value)
Sets the height.
The Size class defines the size of a two-dimensional object using integer point precision.
Contains utility classes helping to read and write streams.
TAG_PARSER_EXPORT const char * containerFormatName(ContainerFormat containerFormat)
Returns the name of the specified container format as C-style string.
bool isEmpty() const
Returns an indication whether the target is empty.
TAG_PARSER_EXPORT void restoreOriginalFileFromBackupFile(const std::string &originalPath, const std::string &backupPath, IoUtilities::NativeFileStream &originalStream, IoUtilities::NativeFileStream &backupStream)
TAG_PARSER_EXPORT const char * containerFormatAbbreviation(ContainerFormat containerFormat, MediaType mediaType=MediaType::Unknown, unsigned int version=0)
Returns the abbreviation of the container format as C-style string considering the specified media ty...
CPPUNIT_TEST_SUITE_REGISTRATION(UtilitiesTests)
TAG_PARSER_EXPORT void handleFailureAfterFileModified(MediaFileInfo &mediaFileInfo, const std::string &backupPath, IoUtilities::NativeFileStream &outputStream, IoUtilities::NativeFileStream &backupStream, Diagnostics &diag, const std::string &context="making file")
std::string toString() const
Returns the string representation of the current size.
TAG_PARSER_EXPORT ContainerFormat parseSignature(const char *buffer, int bufferSize)
Parses the signature read from the specified buffer.
TAG_PARSER_EXPORT void createBackupFile(const std::string &originalPath, std::string &backupPath, IoUtilities::NativeFileStream &originalStream, IoUtilities::NativeFileStream &backupStream)
TAG_PARSER_EXPORT const char * containerFormatSubversion(ContainerFormat containerFormat)
Returns the subversion of the container format as C-style string.