-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
dart_tags_test.dart
314 lines (262 loc) · 7.79 KB
/
dart_tags_test.dart
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
@TestOn('vm')
import 'dart:io';
import 'package:dart_tags/dart_tags.dart';
import 'package:dart_tags/src/frames/id3v2/comm_frame.dart';
import 'package:dart_tags/src/model/comment.dart';
import 'package:dart_tags/src/readers/id3v1.dart';
import 'package:dart_tags/src/readers/id3v2.dart';
import 'package:dart_tags/src/writers/id3v1.dart';
import 'package:dart_tags/src/writers/id3v2.dart';
import 'package:test/test.dart';
void main() {
File file1;
File file2;
File picture;
const outputDir = 'test/output';
setUp(() {
file1 = File('test/test_assets/id3v1.mp3');
file2 = File('test/test_assets/id3v24.mp3');
picture = File('test/test_assets/mink-mingle-109837-unsplash.jpg');
});
group('V2 Frame Tests', () {
test('COMM encode', () {
final expected = [
0x43,
0x4F,
0x4D,
0x4D,
0x00,
0x00,
0x00,
0x15,
0x00,
0x00,
0x03,
0x65,
0x6E,
0x67,
0x64,
0x65,
0x73,
0x73,
0x75,
0x00,
0x63,
0x6F,
0x6D,
0x6D,
0x65,
0x6E,
0x74,
0x61,
0x64,
0x6F,
0x72
];
final frame = COMMFrame();
final lst = frame.encode(Comment('eng', 'dessu', 'commentador'));
expect(lst, equals(expected));
});
test('COMM decode', () {
//COMM.......engdessu.commentador
final data = [
0x43,
0x4F,
0x4D,
0x4D,
0x00,
0x00,
0x00,
0x15,
0x00,
0x00,
0x03,
0x65,
0x6E,
0x67,
0x64,
0x65,
0x73,
0x73,
0x75,
0x00,
0x63,
0x6F,
0x6D,
0x6D,
0x65,
0x6E,
0x74,
0x61,
0x64,
0x6F,
0x72
];
final decodeList = COMMFrame().decode(data);
expect(decodeList.value.comment, equals('commentador'));
expect(decodeList.value.description, equals('dessu'));
expect(decodeList.value.lang, equals('eng'));
});
});
group('Writer Tests', () {
test('generate tag block v2.4', () async {
final tag = Tag()
..tags = {
'title': 'foo',
'artist': 'bar',
'album': 'baz',
'year': '2010',
'comment': Comment('eng', 'desc_here', 'lol it is a comment'),
'track': '6',
'genre': 'Dream',
'custom': 'Just a tag',
'picture': AttachedPicture()
..imageData = picture.readAsBytesSync()
..imageTypeCode = 0x03
..mime = 'image/jpeg'
..description = 'foo.jpg'
}
..type = 'ID3'
..version = '2.4';
final writer = ID3V2Writer();
final blocks = writer.write(await file2.readAsBytes(), tag);
final r = ID3V2Reader();
final f = await r.read(blocks);
expect(f.tags, equals(tag.tags));
});
test('generate tag block v1.1', () async {
final tag = Tag()
..tags = {
'title': 'foo',
'artist': 'bar',
'album': 'baz',
'year': '2010',
'comment': 'lol it is a comment',
'track': '6',
'genre': 'Dream'
}
..type = 'ID3'
..version = '1.1';
final writer = ID3V1Writer();
final blocks = writer.write(await file1.readAsBytes(), tag);
final r = ID3V1Reader();
final f = await r.read(blocks);
expect(f, equals(tag));
});
test('put blocks', () async {
final tag1 = Tag()
..tags = {
'title': 'foo',
'artist': 'bar',
'album': 'baz',
'year': '2010',
'comment': 'lol it is a comment',
'track': '6',
'genre': 'Dream'
}
..type = 'ID3'
..version = '1.1';
final tag2 = Tag()
..tags = {
'title': 'foo',
'artist': 'bar',
'album': 'baz',
'year': '2010',
'comment': Comment('eng', 'desc', 'lol it is a comment'),
'track': '6',
'genre': 'Dream',
'Custom': 'Just tag'
}
..type = 'ID3'
..version = '2.4';
final _av = () async =>
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
final foo = await TagProcessor().putTagsToByteArray(_av(), [tag1, tag2]);
final _fr = () async => foo;
final rdr1 = ID3V1Reader();
final t1 = await rdr1.read(_fr());
expect(t1.tags, equals(tag1.tags));
final rdr2 = ID3V2Reader();
final t2 = await rdr2.read(_fr());
expect(t2.tags, equals(tag2.tags));
});
});
group('Reader Tests', () {
test('Test with file 1.1', () async {
final foo = await TagProcessor()
.getTagsFromByteArray(file1.readAsBytes(), [TagType.id3v1]);
expect(foo.length, equals(1));
expect(foo[0].type, equals('ID3'));
expect(foo[0].version, equals('1.1'));
});
test('Test with file 2.4', () async {
final foo = await TagProcessor()
.getTagsFromByteArray(file2.readAsBytes(), [TagType.id3v2]);
expect(foo.length, equals(1));
expect(foo[0].type, equals('ID3'));
expect(foo[0].version, equals('2.4.0'));
});
test('Test on Failure', () async {
expect(
() async => await TagProcessor().getTagsFromByteArray(null),
throwsA(predicate<Exception>((e) =>
e is ParsingException &&
e.cause == ParsingException.byteArrayNull)));
expect(
() async => await TagProcessor().getTagsFromByteData(null),
throwsA(predicate<Exception>((e) =>
e is ParsingException &&
e.cause == ParsingException.byteDataNull)));
});
});
group('Issues test', () {
//https://github.com/NiKoTron/dart-tags/issues/4
test(' Artist tag restriction on characters [#4]', () async {
final artistName = 'Ilaiyaraaja, K. S. Chithra, S. P. BalasubrahmanyamT';
final tag2 = Tag()
..tags = {
'artist': artistName,
}
..type = 'ID3'
..version = '2.4';
final _av = () async =>
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
final foo = await TagProcessor().putTagsToByteArray(_av(), [tag2]);
final _fr = () async => foo;
final rdr2 = ID3V2Reader();
final t2 = await rdr2.read(_fr());
expect(t2.tags, equals(tag2.tags));
});
//https://github.com/NiKoTron/dart-tags/issues/3
test(' Example for writing APIC tags [#3] ', () async {
final pic1 = AttachedPicture()
..imageData = picture.readAsBytesSync()
..imageTypeCode = 0x03
..mime = 'image/jpeg'
..description = 'foo.jpg';
final tag = Tag()
..tags = {'picture': pic1}
..type = 'ID3'
..version = '2.4';
final writer = ID3V2Writer();
final blocks = writer.write(await file2.readAsBytes(), tag);
File('$outputDir/result.mp3')
..createSync(recursive: true)
..writeAsBytesSync(await blocks, mode: FileMode.write);
print('check the $outputDir/result.mp3');
final r = ID3V2Reader();
final f = await r.read(blocks);
final AttachedPicture pic = f.tags['picture'];
File('$outputDir/${pic.description}.jpg')
..createSync(recursive: true)
..writeAsBytesSync(pic.imageData);
final html = '<div><p>${pic.description}}</p><img src="data:${pic.mime};base64, ${pic.imageData64}" alt="Red dot" /></div>';
File('$outputDir/${pic.description}.html')
..createSync(recursive: true)
..writeAsStringSync(html);
print('check the $outputDir/${pic.description}.jpg');
print('check the $outputDir/${pic.description}.html');
expect(pic, equals(pic1));
});
});
}