| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2009 by Lukas Lalinsky |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include <string> |
| 27 | #include <cstdio> |
| 28 | |
| 29 | #include "tpropertymap.h" |
| 30 | #include "xiphcomment.h" |
| 31 | #include "vorbisfile.h" |
| 32 | #include <cppunit/extensions/HelperMacros.h> |
| 33 | #include "utils.h" |
| 34 | |
| 35 | using namespace std; |
| 36 | using namespace TagLib; |
| 37 | |
| 38 | class TestXiphComment : public CppUnit::TestFixture |
| 39 | { |
| 40 | CPPUNIT_TEST_SUITE(TestXiphComment); |
| 41 | CPPUNIT_TEST(testYear); |
| 42 | CPPUNIT_TEST(testSetYear); |
| 43 | CPPUNIT_TEST(testTrack); |
| 44 | CPPUNIT_TEST(testSetTrack); |
| 45 | CPPUNIT_TEST(testInvalidKeys1); |
| 46 | CPPUNIT_TEST(testInvalidKeys2); |
| 47 | CPPUNIT_TEST(testClearComment); |
| 48 | CPPUNIT_TEST(testRemoveFields); |
| 49 | CPPUNIT_TEST(testPicture); |
| 50 | CPPUNIT_TEST(testLowercaseFields); |
| 51 | CPPUNIT_TEST_SUITE_END(); |
| 52 | |
| 53 | public: |
| 54 | |
| 55 | void testYear() |
| 56 | { |
| 57 | Ogg::XiphComment cmt; |
| 58 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), cmt.year()); |
| 59 | cmt.addField("YEAR", "2009"); |
| 60 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2009), cmt.year()); |
| 61 | cmt.addField("DATE", "2008"); |
| 62 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2008), cmt.year()); |
| 63 | } |
| 64 | |
| 65 | void testSetYear() |
| 66 | { |
| 67 | Ogg::XiphComment cmt; |
| 68 | cmt.addField("YEAR", "2009"); |
| 69 | cmt.addField("DATE", "2008"); |
| 70 | cmt.setYear(1995); |
| 71 | CPPUNIT_ASSERT(cmt.fieldListMap()["YEAR"].isEmpty()); |
| 72 | CPPUNIT_ASSERT_EQUAL(String("1995"), cmt.fieldListMap()["DATE"].front()); |
| 73 | } |
| 74 | |
| 75 | void testTrack() |
| 76 | { |
| 77 | Ogg::XiphComment cmt; |
| 78 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), cmt.track()); |
| 79 | cmt.addField("TRACKNUM", "7"); |
| 80 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(7), cmt.track()); |
| 81 | cmt.addField("TRACKNUMBER", "8"); |
| 82 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(8), cmt.track()); |
| 83 | } |
| 84 | |
| 85 | void testSetTrack() |
| 86 | { |
| 87 | Ogg::XiphComment cmt; |
| 88 | cmt.addField("TRACKNUM", "7"); |
| 89 | cmt.addField("TRACKNUMBER", "8"); |
| 90 | cmt.setTrack(3); |
| 91 | CPPUNIT_ASSERT(cmt.fieldListMap()["TRACKNUM"].isEmpty()); |
| 92 | CPPUNIT_ASSERT_EQUAL(String("3"), cmt.fieldListMap()["TRACKNUMBER"].front()); |
| 93 | } |
| 94 | |
| 95 | void testInvalidKeys1() |
| 96 | { |
| 97 | PropertyMap map; |
| 98 | map[""] = String("invalid key: empty string"); |
| 99 | map["A=B"] = String("invalid key: contains '='"); |
| 100 | map["A~B"] = String("invalid key: contains '~'"); |
| 101 | map["A\x7F" "B"] = String("invalid key: contains '\x7F'"); |
| 102 | map[L"A\x3456" "B"] = String("invalid key: Unicode"); |
| 103 | |
| 104 | Ogg::XiphComment cmt; |
| 105 | PropertyMap unsuccessful = cmt.setProperties(map); |
| 106 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(5), unsuccessful.size()); |
| 107 | CPPUNIT_ASSERT(cmt.properties().isEmpty()); |
| 108 | } |
| 109 | |
| 110 | void testInvalidKeys2() |
| 111 | { |
| 112 | Ogg::XiphComment cmt; |
| 113 | cmt.addField("", "invalid key: empty string"); |
| 114 | cmt.addField("A=B", "invalid key: contains '='"); |
| 115 | cmt.addField("A~B", "invalid key: contains '~'"); |
| 116 | cmt.addField("A\x7F" "B", "invalid key: contains '\x7F'"); |
| 117 | cmt.addField(L"A\x3456" "B", "invalid key: Unicode"); |
| 118 | CPPUNIT_ASSERT_EQUAL(0U, cmt.fieldCount()); |
| 119 | } |
| 120 | |
| 121 | void testClearComment() |
| 122 | { |
| 123 | ScopedFileCopy copy("empty", ".ogg"); |
| 124 | |
| 125 | { |
| 126 | Ogg::Vorbis::File f(copy.fileName().c_str()); |
| 127 | f.tag()->addField("COMMENT", "Comment1"); |
| 128 | f.save(); |
| 129 | } |
| 130 | { |
| 131 | Ogg::Vorbis::File f(copy.fileName().c_str()); |
| 132 | f.tag()->setComment(""); |
| 133 | CPPUNIT_ASSERT_EQUAL(String(""), f.tag()->comment()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void testRemoveFields() |
| 138 | { |
| 139 | Ogg::Vorbis::File f(TEST_FILE_PATH_C("empty.ogg")); |
| 140 | f.tag()->addField("title", "Title1"); |
| 141 | f.tag()->addField("Title", "Title1", false); |
| 142 | f.tag()->addField("titlE", "Title2", false); |
| 143 | f.tag()->addField("TITLE", "Title3", false); |
| 144 | f.tag()->addField("artist", "Artist1"); |
| 145 | f.tag()->addField("ARTIST", "Artist2", false); |
| 146 | CPPUNIT_ASSERT_EQUAL(String("Title1 / Title1 / Title2 / Title3"), f.tag()->title()); |
| 147 | CPPUNIT_ASSERT_EQUAL(String("Artist1 / Artist2"), f.tag()->artist()); |
| 148 | |
| 149 | f.tag()->removeFields("title", "Title1"); |
| 150 | CPPUNIT_ASSERT_EQUAL(String("Title2 / Title3"), f.tag()->title()); |
| 151 | CPPUNIT_ASSERT_EQUAL(String("Artist1 / Artist2"), f.tag()->artist()); |
| 152 | |
| 153 | f.tag()->removeFields("Artist"); |
| 154 | CPPUNIT_ASSERT_EQUAL(String("Title2 / Title3"), f.tag()->title()); |
| 155 | CPPUNIT_ASSERT(f.tag()->artist().isEmpty()); |
| 156 | |
| 157 | f.tag()->removeAllFields(); |
| 158 | CPPUNIT_ASSERT(f.tag()->title().isEmpty()); |
| 159 | CPPUNIT_ASSERT(f.tag()->artist().isEmpty()); |
| 160 | CPPUNIT_ASSERT_EQUAL(String("Xiph.Org libVorbis I 20050304"), f.tag()->vendorID()); |
| 161 | } |
| 162 | |
| 163 | void testPicture() |
| 164 | { |
| 165 | ScopedFileCopy copy("empty", ".ogg"); |
| 166 | string newname = copy.fileName(); |
| 167 | |
| 168 | { |
| 169 | Vorbis::File f(newname.c_str()); |
| 170 | auto newpic = new FLAC::Picture(); |
| 171 | newpic->setType(FLAC::Picture::BackCover); |
| 172 | newpic->setWidth(5); |
| 173 | newpic->setHeight(6); |
| 174 | newpic->setColorDepth(16); |
| 175 | newpic->setNumColors(7); |
| 176 | newpic->setMimeType("image/jpeg"); |
| 177 | newpic->setDescription("new image"); |
| 178 | newpic->setData("JPEG data"); |
| 179 | f.tag()->addPicture(newpic); |
| 180 | f.save(); |
| 181 | } |
| 182 | { |
| 183 | Vorbis::File f(newname.c_str()); |
| 184 | List<FLAC::Picture *> lst = f.tag()->pictureList(); |
| 185 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size()); |
| 186 | CPPUNIT_ASSERT_EQUAL(5, lst[0]->width()); |
| 187 | CPPUNIT_ASSERT_EQUAL(6, lst[0]->height()); |
| 188 | CPPUNIT_ASSERT_EQUAL(16, lst[0]->colorDepth()); |
| 189 | CPPUNIT_ASSERT_EQUAL(7, lst[0]->numColors()); |
| 190 | CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), lst[0]->mimeType()); |
| 191 | CPPUNIT_ASSERT_EQUAL(String("new image"), lst[0]->description()); |
| 192 | CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG data"), lst[0]->data()); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void testLowercaseFields() |
| 197 | { |
| 198 | const ScopedFileCopy copy("lowercase-fields", ".ogg"); |
| 199 | { |
| 200 | Vorbis::File f(copy.fileName().c_str()); |
| 201 | List<FLAC::Picture *> lst = f.tag()->pictureList(); |
| 202 | CPPUNIT_ASSERT_EQUAL(String("TEST TITLE"), f.tag()->title()); |
| 203 | CPPUNIT_ASSERT_EQUAL(String("TEST ARTIST"), f.tag()->artist()); |
| 204 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size()); |
| 205 | f.save(); |
| 206 | } |
| 207 | { |
| 208 | Vorbis::File f(copy.fileName().c_str()); |
| 209 | CPPUNIT_ASSERT(f.find("METADATA_BLOCK_PICTURE") > 0); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | }; |
| 214 | |
| 215 | CPPUNIT_TEST_SUITE_REGISTRATION(TestXiphComment); |