-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
HighLevel.cs
1090 lines (879 loc) · 45.1 KB
/
HighLevel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ATL.AudioData;
using System.IO;
using System.Drawing;
using ATL.test.IO.MetaData;
using System.Collections.Generic;
using static ATL.Logging.Log;
using System;
namespace ATL.test.IO
{
[TestClass]
public class HighLevel
{
[TestMethod]
public void TagIO_R_Single_ID3v1()
{
bool crossreadingDefault = MetaDataIOFactory.GetInstance().CrossReading;
MetaDataIOFactory.TagType[] tagPriorityDefault = new MetaDataIOFactory.TagType[MetaDataIOFactory.TAG_TYPE_COUNT];
MetaDataIOFactory.GetInstance().TagPriority.CopyTo(tagPriorityDefault, 0);
/* Set options for Metadata reader behaviour - this only needs to be done once, or not at all if relying on default settings */
MetaDataIOFactory.GetInstance().CrossReading = false; // default behaviour anyway
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.APE, 0); // No APEtag on sample file => should be ignored
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.ID3V1, 1); // Should be entirely read
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.ID3V2, 2); // Should not be read, since behaviour is single tag reading
/* end set options */
try
{
Track theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/01 - Title Screen.mp3");
Assert.AreEqual("Nintendo Sound Scream", theTrack.Artist); // Specifically tagged like this on the ID3v1 tag
Assert.AreEqual(0, theTrack.Year); // Specifically tagged as empty on the ID3v1 tag
}
finally
{
// Set back default settings
MetaDataIOFactory.GetInstance().CrossReading = crossreadingDefault;
MetaDataIOFactory.GetInstance().TagPriority = tagPriorityDefault;
}
}
[TestMethod]
public void TagIO_R_Multi()
{
bool crossreadingDefault = MetaDataIOFactory.GetInstance().CrossReading;
MetaDataIOFactory.TagType[] tagPriorityDefault = new MetaDataIOFactory.TagType[MetaDataIOFactory.TAG_TYPE_COUNT];
MetaDataIOFactory.GetInstance().TagPriority.CopyTo(tagPriorityDefault, 0);
/* Set options for Metadata reader behaviour - this only needs to be done once, or not at all if relying on default settings */
MetaDataIOFactory.GetInstance().CrossReading = true;
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.APE, 0); // No APEtag on sample file => should be ignored
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.ID3V1, 1); // Should be the main source except for the Year field (empty on ID3v1)
MetaDataIOFactory.GetInstance().SetTagPriority(MetaDataIOFactory.TagType.ID3V2, 2); // Should be used for the Year field (valuated on ID3v2)
/* end set options */
try
{
Track theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/01 - Title Screen.mp3");
Assert.AreEqual("Nintendo Sound Scream", theTrack.Artist); // Specifically tagged like this on the ID3v1 tag
Assert.AreEqual(1984, theTrack.Year); // Empty on the ID3v1 tag => cross-reading should read it on ID3v2
}
finally
{
// Set back default settings
MetaDataIOFactory.GetInstance().CrossReading = crossreadingDefault;
MetaDataIOFactory.GetInstance().TagPriority = tagPriorityDefault;
}
}
[TestMethod]
public void TagIO_R_MultiplePictures()
{
Track theTrack = new Track(TestUtils.GetResourceLocationRoot() + "OGG/bigPicture.ogg");
// Check if _all_ embedded pictures are accessible from Track
Assert.AreEqual(3, theTrack.EmbeddedPictures.Count);
}
[TestMethod]
public void TagIO_R_MetaFormat()
{
Track theTrack = new Track(TestUtils.GetResourceLocationRoot() + "AA/aa.aa");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("Native / AA", theTrack.MetadataFormats[0].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/APE.mp3");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("APEtag v2", theTrack.MetadataFormats[0].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/ID3v2.2 3 pictures.mp3");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("ID3v2.2", theTrack.MetadataFormats[0].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/id3v1.mp3");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("ID3v1.1", theTrack.MetadataFormats[0].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "MP3/01 - Title Screen.mp3");
Assert.AreEqual(2, theTrack.MetadataFormats.Count);
Assert.AreEqual("ID3v2.3", theTrack.MetadataFormats[0].Name);
Assert.AreEqual("ID3v1.1", theTrack.MetadataFormats[1].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "OGG/ogg.ogg");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("Native / Vorbis (OGG)", theTrack.MetadataFormats[0].Name);
theTrack = new Track(TestUtils.GetResourceLocationRoot() + "FLAC/flac.flac");
Assert.AreEqual(1, theTrack.MetadataFormats.Count);
Assert.AreEqual("Native / Vorbis (FLAC)", theTrack.MetadataFormats[0].Name);
}
[TestMethod]
public void TagIO_RW_DeleteTagAll()
{
testRemove("AA/aa.aa");
testRemove("MP3/APE.mp3");
testRemove("MP3/ID3v2.2 3 pictures.mp3");
testRemove("MP3/id3v1.mp3");
testRemove("MP3/01 - Title Screen.mp3");
testRemove("MP4/mp4.m4a");
testRemove("OGG/ogg.ogg");
testRemove("FLAC/flac.flac");
testRemove("WMA/wma.wma");
testRemove("SPC/spc.spc");
testRemove("PSF/psf.psf");
testRemove("VQF/vqf.vqf");
}
private void testRemove(string fileName)
{
string testFileLocation = TestUtils.CopyAsTempTestFile(fileName);
bool defaultTitleSetting = ATL.Settings.UseFileNameWhenNoTitle;
ATL.Settings.UseFileNameWhenNoTitle = false;
try
{
Track theTrack = new Track(testFileLocation);
Assert.IsTrue(theTrack.Title.Length > 0);
Assert.IsTrue(theTrack.RemoveAsync().Result);
Assert.AreEqual("", theTrack.Title);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.UseFileNameWhenNoTitle = defaultTitleSetting;
}
}
[TestMethod]
public void TagIO_RW_DeleteTagMultiple()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/01 - Title Screen.mp3");
Track theTrack = new Track(testFileLocation);
Assert.IsTrue(theTrack.Remove(MetaDataIOFactory.TagType.ID3V2));
Assert.AreEqual("Nintendo Sound Scream", theTrack.Artist); // Specifically tagged like this on the ID3v1 tag
Assert.AreEqual(0, theTrack.Year); // Empty on the ID3v1 tag => should really come empty since ID3v2 tag has been removed
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
/// <summary>
/// Check if the given file keeps its integrity after a no-op/neutral update
/// </summary>
/// <param name="resource"></param>
private void tagIO_RW_UpdateNeutral(string resource)
{
string location = TestUtils.GetResourceLocationRoot() + resource;
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
Assert.IsTrue(theTrack.Save());
// Check that the resulting file (working copy that has been processed) remains identical to the original file (i.e. no byte lost nor added)
// 1- File length should be the same
FileInfo originalFileInfo = new FileInfo(location);
FileInfo testFileInfo = new FileInfo(testFileLocation);
Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
// 2- File contents should be the same
// NB : Due to field order differences, MD5 comparison is not possible yet
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateNeutral()
{
ATL.Settings.MP4_createNeroChapters = false;
ATL.Settings.MP4_createQuicktimeChapters = false;
try
{
tagIO_RW_UpdateNeutral("MP3/id3v2.4_UTF8.mp3"); // ID3v2
tagIO_RW_UpdateNeutral("DSF/dsf.dsf"); // ID3v2 in DSF
tagIO_RW_UpdateNeutral("FLAC/flac.flac"); // Vorbis-FLAC
tagIO_RW_UpdateNeutral("OGG/ogg.ogg"); // Vorbis-OGG
tagIO_RW_UpdateNeutral("OPUS/opus.opus"); // OPUS
tagIO_RW_UpdateNeutral("MP3/APE.mp3"); // APE
// Native formats
tagIO_RW_UpdateNeutral("VQF/vqf.vqf");
tagIO_RW_UpdateNeutral("VGM/vgm.vgm");
tagIO_RW_UpdateNeutral("SPC/spc.spc");
tagIO_RW_UpdateNeutral("MP4/mp4.m4a");
tagIO_RW_UpdateNeutral("WMA/wma.wma");
}
finally
{
ATL.Settings.MP4_createNeroChapters = true;
ATL.Settings.MP4_createQuicktimeChapters = true;
}
}
private void tagIO_RW_UpdateEmpty(string resource, bool supportsTrack = true)
{
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
IProgress<float> progress = new Progress<float>(p => Console.WriteLine(p));
Track theTrack = new Track(testFileLocation);
// Simple field
theTrack.Artist = "Hey ho";
// Tricky fields that aren't managed with a 1-to-1 mapping
theTrack.Year = 1944;
theTrack.TrackNumber = 10;
Assert.IsTrue(theTrack.SaveAsync(progress).Result); // Hack to include async methods in test coverage
theTrack = new Track(testFileLocation);
Assert.AreEqual("Hey ho", theTrack.Artist);
Assert.AreEqual(1944, theTrack.Year);
if (supportsTrack) Assert.AreEqual(10, theTrack.TrackNumber);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateEmpty()
{
// Settings.DefaultTagsWhenNoMetadata = new int[2] { AudioData.MetaDataIOFactory.TagType.NATIVE, AudioData.MetaDataIOFactory.TagType.ID3V2 };
try
{
tagIO_RW_UpdateEmpty("MP3/empty.mp3"); // ID3v2
tagIO_RW_UpdateEmpty("DSF/empty.dsf"); // ID3v2 in DSF
tagIO_RW_UpdateEmpty("FLAC/empty.flac"); // Vorbis-FLAC
tagIO_RW_UpdateEmpty("OGG/empty.ogg"); // Vorbis-OGG
tagIO_RW_UpdateEmpty("OPUS/opus.opus"); // Opus
tagIO_RW_UpdateEmpty("MP3/empty.mp3"); // APE
// Native formats
tagIO_RW_UpdateEmpty("VQF/empty.vqf");
tagIO_RW_UpdateEmpty("VGM/empty.vgm", false);
tagIO_RW_UpdateEmpty("SPC/empty.spc");
tagIO_RW_UpdateEmpty("MP4/empty.m4a");
tagIO_RW_UpdateEmpty("WMA/empty_full.wma");
}
finally
{
ATL.Settings.DefaultTagsWhenNoMetadata = new MetaDataIOFactory.TagType[2] { MetaDataIOFactory.TagType.ID3V2, MetaDataIOFactory.TagType.NATIVE };
}
}
private void tagIO_RW_UpdateTagBaseField(string resource, bool supportsDisc = true, bool supportsTotalTracksDiscs = true, bool supportsTrack = true)
{
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
// Simple field
theTrack.Artist = "Hey ho";
// Tricky fields that aren't managed with a 1-to-1 mapping
theTrack.Year = 1944;
theTrack.TrackNumber = 10;
theTrack.TrackTotal = 20;
theTrack.DiscNumber = 30;
theTrack.DiscTotal = 40;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("Hey ho", theTrack.Artist);
Assert.AreEqual(1944, theTrack.Year);
if (supportsTrack) Assert.AreEqual(10, theTrack.TrackNumber);
if (supportsTotalTracksDiscs) Assert.AreEqual(20, theTrack.TrackTotal);
if (supportsDisc) Assert.AreEqual(30, theTrack.DiscNumber);
if (supportsTotalTracksDiscs) Assert.AreEqual(40, theTrack.DiscTotal);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateTagBaseField()
{
ATL.Settings.MP4_createNeroChapters = false;
ATL.Settings.MP4_createQuicktimeChapters = false;
try
{
tagIO_RW_UpdateTagBaseField("MP3/id3v2.4_UTF8.mp3"); // ID3v2
tagIO_RW_UpdateTagBaseField("DSF/dsf.dsf"); // ID3v2 in DSF
tagIO_RW_UpdateTagBaseField("FLAC/flac.flac"); // Vorbis-FLAC
tagIO_RW_UpdateTagBaseField("OGG/ogg.ogg"); // Vorbis-OGG
tagIO_RW_UpdateTagBaseField("OPUS/opus.opus"); // Opus
tagIO_RW_UpdateTagBaseField("MP3/APE.mp3"); // APE
// Specific formats
tagIO_RW_UpdateTagBaseField("VQF/vqf.vqf", false, false);
tagIO_RW_UpdateTagBaseField("VGM/vgm.vgm", false, false, false);
tagIO_RW_UpdateTagBaseField("SPC/spc.spc", false, false);
tagIO_RW_UpdateTagBaseField("MP4/mp4.m4a");
tagIO_RW_UpdateTagBaseField("WMA/wma.wma");
}
finally
{
ATL.Settings.MP4_createNeroChapters = true;
ATL.Settings.MP4_createQuicktimeChapters = true;
}
}
private void tagIO_RW_UpdatePadding(string resource, int paddingSize = 2048)
{
ATL.Settings.PaddingSize = paddingSize;
try
{
ArrayLogger log = new ArrayLogger();
string location = TestUtils.GetResourceLocationRoot() + resource;
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
string originalTitle = theTrack.Title;
// Test the use of padding without padding option on
// A1- Check that the resulting file (working copy that has been processed) keeps the same quantity of bytes when adding data
theTrack.Title = originalTitle + "1234567890";
Assert.IsTrue(theTrack.Save());
// A11- File length should be the same
FileInfo originalFileInfo = new FileInfo(location);
FileInfo testFileInfo = new FileInfo(testFileLocation);
Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
// A12- No stream manipulation should have occurred
IList<LogItem> logItems = log.GetAllItems(LV_DEBUG);
foreach (LogItem item in logItems)
{
if (item.Message.StartsWith("Disk stream operation : "))
{
Assert.Fail(item.Message);
}
}
// A2- Check that the resulting file (working copy that has been processed) keeps the same quantity of bytes when removing data
theTrack.Title = originalTitle;
Assert.IsTrue(theTrack.Save());
// A21- File length should be the same
originalFileInfo = new FileInfo(location);
testFileInfo = new FileInfo(testFileLocation);
Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
// A22- No stream manipulation should have occurred
logItems = log.GetAllItems(LV_DEBUG);
foreach (LogItem item in logItems)
{
if (item.Message.StartsWith("Disk stream operation : "))
{
Assert.Fail(item.Message);
}
}
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.PaddingSize = 2048;
}
}
private void tagIO_RW_AddPadding(string resource, int extraBytes = 0)
{
bool initEnablePadding = ATL.Settings.AddNewPadding;
ATL.Settings.AddNewPadding = false;
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
theTrack.Title = "a";
Assert.IsTrue(theTrack.Save());
long initialLength = new FileInfo(testFileLocation).Length;
ATL.Settings.AddNewPadding = true;
try
{
theTrack.Title = "b";
Assert.IsTrue(theTrack.Save());
// B1- Check that the resulting file size has been increased by the size of the padding
Assert.AreEqual(initialLength + ATL.Settings.PaddingSize + extraBytes, new FileInfo(testFileLocation).Length);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.AddNewPadding = initEnablePadding;
}
}
[TestMethod]
public void TagIO_RW_Padding()
{
ATL.Settings.MP4_createNeroChapters = false;
ATL.Settings.MP4_createQuicktimeChapters = false;
try
{
tagIO_RW_UpdatePadding("MP3/id3v2.4_UTF8.mp3"); // padded ID3v2
tagIO_RW_UpdatePadding("OGG/ogg.ogg"); // padded Vorbis-OGG
tagIO_RW_UpdatePadding("MP4/mp4.m4a", 17100); // padded MP4
tagIO_RW_UpdatePadding("FLAC/flac.flac", 4063); // padded Vorbis-FLAC
tagIO_RW_AddPadding("MP3/empty.mp3");
tagIO_RW_AddPadding("OGG/empty.ogg", 8); // 8 extra bytes for the segments table extension
tagIO_RW_AddPadding("MP4/chapters_NERO.mp4");
tagIO_RW_AddPadding("FLAC/empty.flac", ATL.Settings.PaddingSize + 4); // Additional padding for the ID3v2 tag + 4 bytes for VorbisComment's PADDING block header
}
finally
{
ATL.Settings.MP4_createNeroChapters = true;
ATL.Settings.MP4_createQuicktimeChapters = true;
}
}
private void tagIO_RW_doImageType(string resource, PictureInfo picture, PictureInfo.PIC_TYPE type)
{
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
if (theTrack.EmbeddedPictures.Count > 0) theTrack.EmbeddedPictures[0] = picture;
else theTrack.EmbeddedPictures.Add(picture);
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
// Check that the picture type can be read properly
Assert.AreEqual(type, theTrack.EmbeddedPictures[0].PicType);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
private void tagIO_RW_ImageType(string resource)
{
PictureInfo.PIC_TYPE[] types = (PictureInfo.PIC_TYPE[])Enum.GetValues(typeof(PictureInfo.PIC_TYPE));
foreach (PictureInfo.PIC_TYPE type in types)
{
if (type == PictureInfo.PIC_TYPE.Unsupported) continue;
PictureInfo newPicture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.gif"), type);
tagIO_RW_doImageType(resource, newPicture, type);
}
}
[TestMethod]
public void tagIO_RW_ImageType()
{
tagIO_RW_ImageType("MP3/id3v2.4_UTF8.mp3");
tagIO_RW_ImageType("MP3/APE.mp3");
}
[TestMethod]
public void TagIO_RW_AddRemoveTagRegularNotNullable()
{
bool initialSettings = ATL.Settings.NullAbsentValues;
ATL.Settings.NullAbsentValues = false;
try
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/empty.mp3");
Track theTrack = new Track(testFileLocation);
DateTime now = DateTime.Now;
// === FIRST TEST WITH REGULAR FIELDS
theTrack.Artist = "aaa"; // String data
theTrack.DiscNumber = 1; // Int data
theTrack.Year = 1998; // Int data
theTrack.Popularity = 0.2f; // Float data
theTrack.PublishingDate = now; // Date data
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("aaa", theTrack.Artist);
Assert.AreEqual(1, theTrack.DiscNumber);
Assert.AreEqual(1998, theTrack.Year);
Assert.AreEqual(0.2f, theTrack.Popularity);
Assert.AreEqual(now.ToString(), theTrack.PublishingDate.ToString());
theTrack.Artist = "";
theTrack.DiscNumber = 0;
theTrack.Year = 0;
theTrack.Popularity = 0;
theTrack.PublishingDate = DateTime.MinValue;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("", theTrack.Artist);
Assert.AreEqual(0, theTrack.DiscNumber);
Assert.AreEqual(0, theTrack.Year);
Assert.AreEqual(0, theTrack.Popularity);
Assert.AreEqual(DateTime.MinValue.ToString(), theTrack.PublishingDate.ToString());
// === SECOND TEST WITH YEAR/DATE DUAL PROPERTIES
theTrack.Date = now;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(now.Year, theTrack.Year);
Assert.AreEqual(now.ToString(), theTrack.Date.ToString());
theTrack.Date = DateTime.MinValue;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(0, theTrack.Year);
Assert.AreEqual(DateTime.MinValue.ToString(), theTrack.Date.ToString());
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.NullAbsentValues = initialSettings;
}
}
[TestMethod]
public void TagIO_RW_AddRemoveTagRegularNullable()
{
bool initialSettings = ATL.Settings.NullAbsentValues;
ATL.Settings.NullAbsentValues = true;
try
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/empty.mp3");
Track theTrack = new Track(testFileLocation);
DateTime now = DateTime.Now;
// === FIRST TEST WITH REGULAR FIELDS
theTrack.Artist = "aaa"; // String data
theTrack.DiscNumber = 1; // Int data
theTrack.Year = 1998; // Int data
theTrack.Popularity = 0.2f; // Float data
theTrack.PublishingDate = now; // Date data
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("aaa", theTrack.Artist);
Assert.AreEqual(1, theTrack.DiscNumber);
Assert.AreEqual(1998, theTrack.Year);
Assert.AreEqual(0.2f, theTrack.Popularity);
Assert.AreEqual(now.ToString(), theTrack.PublishingDate.ToString());
// Change value
theTrack.Popularity = 0;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(0, theTrack.Popularity);
// Remove value
theTrack.Artist = "";
theTrack.DiscNumber = null;
theTrack.Year = null;
theTrack.Popularity = null;
theTrack.PublishingDate = null;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("", theTrack.Artist);
Assert.IsFalse(theTrack.DiscNumber.HasValue);
Assert.IsFalse(theTrack.Year.HasValue);
Assert.IsFalse(theTrack.Popularity.HasValue);
Assert.IsFalse(theTrack.PublishingDate.HasValue);
// === SECOND TEST WITH YEAR/DATE DUAL PROPERTIES
theTrack.Date = now;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(now.Year, theTrack.Year);
Assert.AreEqual(now.ToString(), theTrack.Date.ToString());
theTrack.Date = null;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.IsFalse(theTrack.Year.HasValue);
Assert.IsFalse(theTrack.Date.HasValue);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.NullAbsentValues = initialSettings;
}
}
[TestMethod]
public void TagIO_RW_AddRemoveTagAdditionalField()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/01 - Title Screen.mp3");
Track theTrack = new Track(testFileLocation);
theTrack.AdditionalFields.Add("ABCD", "efgh");
theTrack.AdditionalFields.Remove("TENC");
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(1, theTrack.AdditionalFields.Count); // TENC should have been removed
Assert.IsTrue(theTrack.AdditionalFields.ContainsKey("ABCD"));
Assert.AreEqual("efgh", theTrack.AdditionalFields["ABCD"]);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateTagAdditionalField()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/01 - Title Screen.mp3");
Track theTrack = new Track(testFileLocation);
theTrack.AdditionalFields["TENC"] = "update test";
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(1, theTrack.AdditionalFields.Count);
Assert.IsTrue(theTrack.AdditionalFields.ContainsKey("TENC"));
Assert.AreEqual("update test", theTrack.AdditionalFields["TENC"]);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_AddRemoveTagPictures()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/id3v2.4_UTF8.mp3");
Track theTrack = new Track(testFileLocation);
theTrack.EmbeddedPictures.RemoveAt(1); // Remove Conductor; Front Cover remains
// Add CD
PictureInfo newPicture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.gif"), PictureInfo.PIC_TYPE.CD);
theTrack.EmbeddedPictures.Add(newPicture);
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(2, theTrack.EmbeddedPictures.Count); // Front Cover, CD
bool foundFront = false;
bool foundCD = false;
foreach (PictureInfo pic in theTrack.EmbeddedPictures)
{
if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Front)) foundFront = true;
if (pic.PicType.Equals(PictureInfo.PIC_TYPE.CD)) foundCD = true;
}
Assert.IsTrue(foundFront);
Assert.IsTrue(foundCD);
// Remove 1st and add it back on 1st position -> should still be listed 1st
theTrack.EmbeddedPictures.RemoveAt(0);
newPicture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.gif"), PictureInfo.PIC_TYPE.Back);
theTrack.EmbeddedPictures.Insert(0, newPicture);
Assert.IsTrue(theTrack.Save());
newPicture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.gif"), PictureInfo.PIC_TYPE.Front);
theTrack.EmbeddedPictures.Insert(0, newPicture);
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(3, theTrack.EmbeddedPictures.Count);
Assert.AreEqual(PictureInfo.PIC_TYPE.Front, theTrack.EmbeddedPictures[0].PicType);
Assert.AreEqual(PictureInfo.PIC_TYPE.Back, theTrack.EmbeddedPictures[1].PicType);
Assert.AreEqual(PictureInfo.PIC_TYPE.CD, theTrack.EmbeddedPictures[2].PicType);
// Remove all
theTrack.EmbeddedPictures.Clear();
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(0, theTrack.EmbeddedPictures.Count);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateTagPictures()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/id3v2.4_UTF8.mp3");
Track theTrack = new Track(testFileLocation);
// 1- Update Front picture field picture data
PictureInfo newPicture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic2.jpg"), PictureInfo.PIC_TYPE.Front);
theTrack.EmbeddedPictures.Add(newPicture);
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(2, theTrack.EmbeddedPictures.Count); // Front Cover, Icon
bool foundFront = false;
bool foundIcon = false;
foreach (PictureInfo pic in theTrack.EmbeddedPictures)
{
if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Front))
{
foundFront = true;
using (Image picture = Image.FromStream(new MemoryStream(pic.PictureData)))
{
// Properties of updated picture (pic2.jpg)
Assert.AreEqual(picture.RawFormat, System.Drawing.Imaging.ImageFormat.Jpeg);
Assert.AreEqual(900, picture.Width);
Assert.AreEqual(290, picture.Height);
}
}
if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Icon)) foundIcon = true;
// TODO test reading an unsupported pic code ?
}
Assert.IsTrue(foundFront);
Assert.IsTrue(foundIcon);
// 2- Update Front picture field description
theTrack.EmbeddedPictures[0].Description = "aaa";
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(2, theTrack.EmbeddedPictures.Count); // Front Cover, Icon
Assert.AreEqual("aaa", theTrack.EmbeddedPictures[0].Description);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_AddRemoveChapters()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/chapters.mp3");
Track theTrack = new Track(testFileLocation);
theTrack.ChaptersTableDescription = "Content";
theTrack.Chapters.RemoveAt(2);
// Add new chapter
ChapterInfo chapter = new ChapterInfo();
chapter.StartTime = 440;
chapter.StartOffset = 4400;
chapter.EndTime = 880;
chapter.EndOffset = 8800;
chapter.UniqueID = "849849";
chapter.Picture = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.jpeg"));
theTrack.Chapters.Add(chapter);
Assert.IsTrue(theTrack.Save());
IList<ChapterInfo> chaptersSave = new List<ChapterInfo>(theTrack.Chapters);
theTrack = new Track(testFileLocation);
IList<PictureInfo> pics = theTrack.EmbeddedPictures; // Hack to load chapter pictures
Assert.AreEqual("Content", theTrack.ChaptersTableDescription);
Assert.AreEqual(chaptersSave.Count, theTrack.Chapters.Count);
ChapterInfo readChapter;
for (int i = 0; i < theTrack.Chapters.Count; i++)
{
readChapter = theTrack.Chapters[i];
Assert.AreEqual(chaptersSave[i].StartOffset, readChapter.StartOffset);
Assert.AreEqual(chaptersSave[i].StartTime, readChapter.StartTime);
Assert.AreEqual(chaptersSave[i].EndOffset, readChapter.EndOffset);
if (i < theTrack.Chapters.Count - 1) Assert.AreEqual(chaptersSave[i + 1].StartTime, readChapter.EndTime);
Assert.AreEqual(chaptersSave[i].Title, readChapter.Title);
Assert.AreEqual(chaptersSave[i].Subtitle, readChapter.Subtitle);
Assert.AreEqual(chaptersSave[i].UniqueID, readChapter.UniqueID);
if (chaptersSave[i].Url != null)
{
Assert.AreEqual(chaptersSave[i].Url.Description, readChapter.Url.Description);
Assert.AreEqual(chaptersSave[i].Url.Url, readChapter.Url.Url);
}
if (chaptersSave[i].Picture != null)
{
Assert.IsNotNull(readChapter.Picture);
Assert.AreEqual(chaptersSave[i].Picture.ComputePicHash(), readChapter.Picture.ComputePicHash());
}
}
// Delete all
theTrack.Chapters.Clear();
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(0, theTrack.Chapters.Count);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateTagChapters()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/chapters.mp3");
Track theTrack = new Track(testFileLocation);
// Update 3rd chapter
ChapterInfo chapter = new ChapterInfo(theTrack.Chapters[2]);
chapter.Title = "updated title";
chapter.Subtitle = "updated subtitle";
chapter.Url = new ChapterInfo.UrlInfo("updated url");
theTrack.Chapters[2] = chapter;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual("toplevel toc", theTrack.ChaptersTableDescription);
Assert.AreEqual(chapter.Title, theTrack.Chapters[2].Title);
Assert.AreEqual(chapter.Subtitle, theTrack.Chapters[2].Subtitle);
Assert.AreEqual(chapter.Url.Url, theTrack.Chapters[2].Url.Url);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_ResetTrackDisc()
{
string testFileLocation = TestUtils.CopyAsTempTestFile("MP3/id3v2.4_UTF8.mp3");
Track theTrack = new Track(testFileLocation);
theTrack.TrackNumber = 1;
theTrack.TrackTotal = 2;
theTrack.DiscNumber = 3;
theTrack.DiscTotal = 4;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(1, theTrack.TrackNumber);
Assert.AreEqual(2, theTrack.TrackTotal);
Assert.AreEqual(3, theTrack.DiscNumber);
Assert.AreEqual(4, theTrack.DiscTotal);
theTrack.TrackNumber = 0;
theTrack.TrackTotal = 0;
theTrack.DiscNumber = 0;
theTrack.DiscTotal = 0;
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
Assert.AreEqual(0, theTrack.TrackNumber);
Assert.AreEqual(0, theTrack.TrackTotal);
Assert.AreEqual(0, theTrack.DiscNumber);
Assert.AreEqual(0, theTrack.DiscTotal);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_RW_UpdateKeepDataIntegrity()
{
ATL.Settings.AddNewPadding = true;
try
{
string resource = "OGG/ogg.ogg";
string location = TestUtils.GetResourceLocationRoot() + resource;
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
string initialArtist = theTrack.Artist; // '֎FATHER֎'
theTrack.Artist = "Hey ho";
Assert.IsTrue(theTrack.Save());
theTrack = new Track(testFileLocation);
theTrack.Artist = initialArtist;
Assert.IsTrue(theTrack.Save());
// Check that the resulting file (working copy that has been processed) remains identical to the original file (i.e. no byte lost nor added)
FileInfo originalFileInfo = new FileInfo(location);
FileInfo testFileInfo = new FileInfo(testFileLocation);
Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
/* Not possible due to field order being changed
string originalMD5 = TestUtils.GetFileMD5Hash(location);
string testMD5 = TestUtils.GetFileMD5Hash(testFileLocation);
Assert.IsTrue(originalMD5.Equals(testMD5));
*/
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
finally
{
ATL.Settings.AddNewPadding = false;
}
}
[TestMethod]
public void StreamedIO_R_Audio()
{
string resource = "OGG/ogg.ogg";
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
using (FileStream fs = new FileStream(testFileLocation, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Track theTrack = new Track(fs, "audio/ogg");
Assert.AreEqual(33, theTrack.Duration);
Assert.AreEqual(69, theTrack.Bitrate);
Assert.AreEqual(-1, theTrack.BitDepth);
Assert.AreEqual(22050, theTrack.SampleRate);
Assert.AreEqual(true, theTrack.IsVBR);
Assert.AreEqual(23125, theTrack.TechnicalInformation.AudioDataOffset);
Assert.AreEqual(278029, theTrack.TechnicalInformation.AudioDataSize);
Assert.AreEqual(AudioDataIOFactory.CF_LOSSY, theTrack.CodecFamily);
}
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void TagIO_R_VorbisFLAC_multipleArtists()
{
string resource = "FLAC/multiple_artists.flac";
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);
Track theTrack = new Track(testFileLocation);
// Read
Assert.AreEqual("Kamome Sano" + ATL.Settings.DisplayValueSeparator + "Punipuni Denki", theTrack.Artist);
// Write
theTrack.Artist = "aaa" + ATL.Settings.DisplayValueSeparator + "bbb" + ATL.Settings.DisplayValueSeparator + "ccc";
theTrack.Save();
Track theTrack2 = new Track(testFileLocation);
Assert.AreEqual("aaa" + ATL.Settings.DisplayValueSeparator + "bbb" + ATL.Settings.DisplayValueSeparator + "ccc", theTrack2.Artist);
// Get rid of the working copy
if (Settings.DeleteAfterSuccess) File.Delete(testFileLocation);
}
[TestMethod]
public void StreamedIO_R_Meta()
{
string resource = "OGG/ogg.ogg";
string testFileLocation = TestUtils.CopyAsTempTestFile(resource);