forked from Vocab-Apps/anki-hyper-tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_components.py
2398 lines (1778 loc) · 94.6 KB
/
test_components.py
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
import sys
from calendar import c
import aqt.qt
import os
import copy
import pprint
import component_batch_preview
import component_configuration
import config_models
import servicemanager
import testing_utils
import hypertts
import constants
import languages
import time
import component_voiceselection
import component_source
import component_target
import component_batch
import component_text_processing
import component_realtime_source
import component_realtime_side
import component_realtime
import component_hyperttspro
import component_shortcuts
import component_preferences
logging_utils = __import__('logging_utils', globals(), locals(), [], sys._addon_import_level_base)
logger = logging_utils.get_test_child_logger(__name__)
class EmptyDialog(aqt.qt.QDialog):
def __init__(self):
super(aqt.qt.QDialog, self).__init__()
self.closed = None
def setupUi(self):
self.main_layout = aqt.qt.QVBoxLayout(self)
def getLayout(self):
return self.main_layout
def setLayout(self, layout):
self.main_layout = layout
def addChildLayout(self, layout):
self.main_layout.addLayout(layout)
def addChildWidget(self, widget):
self.main_layout.addWidget(widget)
def close(self):
self.closed = True
class MockModelChangeCallback():
def __init__(self):
self.model = None
def model_updated(self, model):
logger.info('MockModelChangeCallback.model_updated')
self.model = copy.deepcopy(model)
class MockBatchPreviewCallback():
def __init__(self):
self.sample_text = None
self.batch_start_called = None
self.batch_end_called = None
def sample_selected(self, note_id, text):
self.note_id = note_id
self.sample_text = text
def batch_start(self):
self.batch_start_called = True
def batch_end(self, completed):
self.batch_end_called = True
def get_hypertts_instance():
# return hypertts_instance
config_gen = testing_utils.TestConfigGenerator()
return config_gen.build_hypertts_instance_test_servicemanager('default')
def test_voice_selection_defaults_single(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
voiceselection.voices_combobox.setCurrentIndex(1) # pick second voice
expected_output = {
'voice_selection_mode': 'single',
'voice': {
'voice': {
'gender': 'Male',
'language': 'fr_FR',
'name': 'voice_a_1',
'service': 'ServiceA',
'voice_key': {'name': 'voice_1'}
},
'options': {
}
}
}
assert voiceselection.serialize() == expected_output
# dialog.exec()
def test_voice_selection_manual(qtbot):
# HYPERTTS_VOICE_SELECTION_DIALOG_DEBUG=yes pytest test_components.py -k test_voice_selection_manual -s -rPP
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
if os.environ.get('HYPERTTS_VOICE_SELECTION_DIALOG_DEBUG', 'no') == 'yes':
dialog.exec()
def test_voice_selection_single_1(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
voiceselection.voices_combobox.setCurrentIndex(0) # pick second voice
# dialog.exec()
expected_output = {
'voice_selection_mode': 'single',
'voice': {
'voice': {
'gender': 'Female',
'language': 'en_US',
'name': 'voice_a_2',
'service': 'ServiceA',
'voice_key': {'name': 'voice_2'}
},
'options': {
}
}
}
assert voiceselection.serialize() == expected_output
# change options
speaking_rate_widget = dialog.findChild(aqt.qt.QDoubleSpinBox, "voice_option_speaking_rate")
speaking_rate_widget.setValue(0.25)
expected_output = {
'voice_selection_mode': 'single',
'voice': {
'voice': {
'gender': 'Female',
'language': 'en_US',
'name': 'voice_a_2',
'service': 'ServiceA',
'voice_key': {'name': 'voice_2'}
},
'options': {
'speaking_rate': 0.25
}
}
}
assert voiceselection.serialize() == expected_output
def test_voice_selection_format_ogg(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
voiceselection.voices_combobox.setCurrentIndex(0) # pick second voice
# change options
format_widget = dialog.findChild(aqt.qt.QComboBox, "voice_option_format")
# default should be mp3
assert format_widget.currentText() == 'mp3'
format_widget.setCurrentText('ogg_opus')
expected_output = {
'voice_selection_mode': 'single',
'voice': {
'voice': {
'gender': 'Female',
'language': 'en_US',
'name': 'voice_a_2',
'service': 'ServiceA',
'voice_key': {'name': 'voice_2'}
},
'options': {
'format': 'ogg_opus'
}
}
}
assert voiceselection.serialize() == expected_output
def test_voice_selection_random_1(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# dialog.exec()
# choose random mode
# qtbot.t(voiceselection.radio_button_random, aqt.qt.Qt.MouseButton.LeftButton)
voiceselection.radio_button_random.setChecked(True)
# pick second voice and add it
voiceselection.voices_combobox.setCurrentIndex(0) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# pick third voice and add it
voiceselection.voices_combobox.setCurrentIndex(2) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
expected_output = {
'voice_selection_mode': 'random',
'voice_list': [
{
'voice': {
'gender': 'Female',
'language': 'en_US',
'name': 'voice_a_2',
'service': 'ServiceA',
'voice_key': {'name': 'voice_2'}
},
'options': {
},
'weight': 1
},
{
'voice': {
'gender': 'Female',
'language': 'ja_JP',
'name': 'voice_a_3',
'service': 'ServiceA',
'voice_key': {'name': 'voice_3'}
},
'options': {
},
'weight': 1
}
]
}
assert voiceselection.serialize() == expected_output
def test_voice_selection_random_to_single(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# choose random mode
# qtbot.mouseClick(voiceselection.radio_button_random, aqt.qt.Qt.MouseButton.LeftButton)
voiceselection.radio_button_random.setChecked(True)
# pick second voice and add it
voiceselection.voices_combobox.setCurrentIndex(1) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# pick third voice and add it
voiceselection.voices_combobox.setCurrentIndex(2) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# check model change callback
assert model_change_callback.model.selection_mode == constants.VoiceSelectionMode.random
assert len(model_change_callback.model.get_voice_list()) == 2
# go back to single
voiceselection.radio_button_single.setChecked(True)
# check model change callback
assert model_change_callback.model.selection_mode == constants.VoiceSelectionMode.single
# verify that the selected voices grid is empty
assert voiceselection.voice_list_grid_layout.count() == 0
def test_voice_selection_random_remove_voices(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# choose random mode
# qtbot.mouseClick(voiceselection.radio_button_random, aqt.qt.Qt.MouseButton.LeftButton)
voiceselection.radio_button_random.setChecked(True)
# pick second voice and add it
voiceselection.voices_combobox.setCurrentIndex(1) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# pick third voice and add it
voiceselection.voices_combobox.setCurrentIndex(2) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# check model change callback
assert model_change_callback.model.selection_mode == constants.VoiceSelectionMode.random
assert len(model_change_callback.model.get_voice_list()) == 2
# now remove one of the voices
logger.info('removing voice_row_1')
remove_voice_button = dialog.findChild(aqt.qt.QPushButton, 'remove_voice_row_1')
qtbot.mouseClick(remove_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# check model change callback
assert model_change_callback.model.selection_mode == constants.VoiceSelectionMode.random
assert len(model_change_callback.model.get_voice_list()) == 1
def test_voice_selection_random_2(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# choose random mode
voiceselection.radio_button_random.setChecked(True)
# add the first voice twice, but with different options
voiceselection.voices_combobox.setCurrentIndex(1)
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# change options
speaking_rate_widget = dialog.findChild(aqt.qt.QDoubleSpinBox, "voice_option_speaking_rate")
speaking_rate_widget.setValue(0.25)
# add again
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# build expected voice selection model
expected_model = config_models.VoiceSelectionRandom()
voice = [x for x in hypertts_instance.service_manager.full_voice_list() if x.service.name == 'ServiceA' and x.name == 'voice_a_1'][0]
expected_model.add_voice(config_models.VoiceWithOptionsRandom(voice, {}))
expected_model.add_voice(config_models.VoiceWithOptionsRandom(voice, {'speaking_rate': 0.25}))
assert voiceselection.voice_selection_model.serialize() == expected_model.serialize()
def test_voice_selection_priority_1(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# choose random mode
# qtbot.mouseClick(voiceselection.radio_button_random, aqt.qt.Qt.MouseButton.LeftButton)
voiceselection.radio_button_priority.setChecked(True)
# dialog.exec()
# pick second voice and add it
voiceselection.voices_combobox.setCurrentIndex(0) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
# pick third voice and add it
voiceselection.voices_combobox.setCurrentIndex(2) # pick second voice
qtbot.mouseClick(voiceselection.add_voice_button, aqt.qt.Qt.MouseButton.LeftButton)
expected_model = config_models.VoiceSelectionPriority()
voice_2 = [x for x in hypertts_instance.service_manager.full_voice_list() if x.service.name == 'ServiceA' and x.name == 'voice_a_2'][0]
voice_3 = [x for x in hypertts_instance.service_manager.full_voice_list() if x.service.name == 'ServiceA' and x.name == 'voice_a_3'][0]
expected_model.add_voice(config_models.VoiceWithOptionsPriority(voice_2, {}))
expected_model.add_voice(config_models.VoiceWithOptionsPriority(voice_3, {}))
assert voiceselection.voice_selection_model.serialize() == expected_model.serialize()
# dialog.exec()
def test_voice_selection_filters(qtbot):
manager = servicemanager.ServiceManager(testing_utils.get_test_services_dir(), 'test_services', True)
manager.init_services()
manager.get_service('ServiceA').enabled = True
manager.get_service('ServiceB').enabled = True
anki_utils = testing_utils.MockAnkiUtils({})
hypertts_instance = hypertts.HyperTTS(anki_utils, manager)
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
# ensure filters are set to All by default
assert voiceselection.audio_languages_combobox.currentText() == constants.LABEL_FILTER_ALL
assert voiceselection.languages_combobox.currentText() == constants.LABEL_FILTER_ALL
assert voiceselection.services_combobox.currentText() == constants.LABEL_FILTER_ALL
assert voiceselection.genders_combobox.currentText() == constants.LABEL_FILTER_ALL
# filter language to Japanese
voiceselection.languages_combobox.setCurrentText('Japanese')
# ensure japanese filter is enabled
assert len(voiceselection.filtered_voice_list) < len(voiceselection.voice_list)
assert len(voiceselection.filtered_voice_list) == voiceselection.voices_combobox.count()
for voice in voiceselection.filtered_voice_list:
assert voice.language.lang == languages.Language.ja
# reset filters
qtbot.mouseClick(voiceselection.reset_filters_button, aqt.qt.Qt.MouseButton.LeftButton)
# ensure all voices are available now
assert len(voiceselection.filtered_voice_list) == len(voiceselection.voice_list)
# filter gender to male
voiceselection.genders_combobox.setCurrentText('Male')
# ensure only male voices appear
assert len(voiceselection.filtered_voice_list) < len(voiceselection.voice_list)
assert len(voiceselection.filtered_voice_list) == voiceselection.voices_combobox.count()
for voice in voiceselection.filtered_voice_list:
assert voice.gender == constants.Gender.Male
# reset filters again
qtbot.mouseClick(voiceselection.reset_filters_button, aqt.qt.Qt.MouseButton.LeftButton)
# ensure all voices are available now
assert len(voiceselection.filtered_voice_list) == len(voiceselection.voice_list)
# filter with two different conditions
voiceselection.languages_combobox.setCurrentText('Japanese')
voiceselection.genders_combobox.setCurrentText('Female')
# ensure only female japanese voices are present
assert len(voiceselection.filtered_voice_list) < len(voiceselection.voice_list)
assert len(voiceselection.filtered_voice_list) == voiceselection.voices_combobox.count()
for voice in voiceselection.filtered_voice_list:
assert voice.gender == constants.Gender.Female
assert voice.language.lang == languages.Language.ja
# reset filters again
qtbot.mouseClick(voiceselection.reset_filters_button, aqt.qt.Qt.MouseButton.LeftButton)
# select random mode and add some voices
# ======================================
# dialog.exec()
def test_voice_selection_samples(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
voiceselection.voices_combobox.setCurrentIndex(1)
# simulate selection from the preview grid
voiceselection.sample_text_selected('Bonjour')
qtbot.mouseClick(voiceselection.play_sample_button, aqt.qt.Qt.MouseButton.LeftButton)
assert hypertts_instance.anki_utils.played_sound == {
'source_text': 'Bonjour',
'voice': {
'gender': 'Male',
'language': 'fr_FR',
'name': 'voice_a_1',
'service': 'ServiceA',
'voice_key': {'name': 'voice_1'}
},
'options': {}
}
# dialog.exec()
def test_voice_selection_load_model(qtbot):
hypertts_instance = get_hypertts_instance()
dialog = EmptyDialog()
dialog.setupUi()
model_change_callback = MockModelChangeCallback()
voiceselection = component_voiceselection.VoiceSelection(hypertts_instance, dialog, model_change_callback.model_updated)
dialog.addChildWidget(voiceselection.draw())
voice_list = hypertts_instance.service_manager.full_voice_list()
voice_a_2 = [x for x in voice_list if x.name == 'voice_a_2'][0]
voice_a_3 = [x for x in voice_list if x.name == 'voice_a_3'][0]
# single voice
# ============
model = config_models.VoiceSelectionSingle()
model.voice = config_models.VoiceWithOptions(voice_a_2, {'speaking_rate': 3.5})
voiceselection.load_model(model)
assert voiceselection.radio_button_single.isChecked()
assert voiceselection.voices_combobox.currentText() == str(voice_a_2)
speaking_rate_widget = voiceselection.voice_options_widgets['voice_option_speaking_rate']
assert speaking_rate_widget != None
assert speaking_rate_widget.value() == 3.5
# single voice, ogg format
# ========================
model = config_models.VoiceSelectionSingle()
model.voice = config_models.VoiceWithOptions(voice_a_2, {'format': 'ogg_opus'})
voiceselection.load_model(model)
assert voiceselection.radio_button_single.isChecked()
assert voiceselection.voices_combobox.currentText() == str(voice_a_2)
format_widget = voiceselection.voice_options_widgets['voice_option_format']
assert format_widget != None
assert format_widget.currentText() == 'ogg_opus'
# single voice, mp3 format
# ========================
model = config_models.VoiceSelectionSingle()
model.voice = config_models.VoiceWithOptions(voice_a_2, {'format': 'mp3'})
voiceselection.load_model(model)
assert voiceselection.radio_button_single.isChecked()
assert voiceselection.voices_combobox.currentText() == str(voice_a_2)
format_widget = voiceselection.voice_options_widgets['voice_option_format']
assert format_widget != None
assert format_widget.currentText() == 'mp3'
# random
# =======
model = config_models.VoiceSelectionRandom()
model.add_voice(config_models.VoiceWithOptionsRandom(voice_a_2, {'speaking_rate': 2.5}))
model.add_voice(config_models.VoiceWithOptionsRandom(voice_a_3, {}))
voiceselection.load_model(model)
assert voiceselection.radio_button_random.isChecked()
assert voiceselection.voice_list_grid_layout.itemAt(0).widget().text() == str(voice_a_2) + ' (speaking_rate: 2.5)'
assert voiceselection.voice_list_grid_layout.itemAt(3).widget().text() == str(voice_a_3)
# priority
# ========
model = config_models.VoiceSelectionPriority()
model.add_voice(config_models.VoiceWithOptionsPriority(voice_a_2, {'speaking_rate': 2.5}))
model.add_voice(config_models.VoiceWithOptionsPriority(voice_a_3, {}))
voiceselection.load_model(model)
assert voiceselection.radio_button_priority.isChecked()
assert voiceselection.voice_list_grid_layout.itemAt(0).widget().text() == str(voice_a_2) + ' (speaking_rate: 2.5)'
assert voiceselection.voice_list_grid_layout.itemAt(4).widget().text() == str(voice_a_3)
# dialog.exec()
def test_batch_source_1(qtbot):
config_gen = testing_utils.TestConfigGenerator()
hypertts_instance = config_gen.build_hypertts_instance_test_servicemanager('default')
dialog = EmptyDialog()
dialog.setupUi()
note_id_list = [config_gen.note_id_1, config_gen.note_id_2]
model_change_callback = MockModelChangeCallback()
field_list = hypertts_instance.get_all_fields_from_notes(note_id_list)
batch_source = component_source.BatchSource(hypertts_instance, field_list, model_change_callback.model_updated)
dialog.addChildWidget(batch_source.draw())
# the field selected should be "Chinese"
expected_source_model = config_models.BatchSourceSimple('Chinese')
# the simple stack item should be selected
assert batch_source.source_config_stack.currentIndex() == batch_source.SOURCE_CONFIG_STACK_SIMPLE
assert batch_source.batch_source_model.serialize() == expected_source_model.serialize()
# select another field, 'English'
batch_source.source_field_combobox.setCurrentText('English')
expected_source_model.source_field = 'English'
assert batch_source.batch_source_model.serialize() == expected_source_model.serialize()
# select template mode
batch_source.batch_mode_combobox.setCurrentText('template')
assert batch_source.source_config_stack.currentIndex() == batch_source.SOURCE_CONFIG_STACK_TEMPLATE
# enter template format
qtbot.keyClicks(batch_source.simple_template_input, '{Chinese}')
expected_source_model = config_models.BatchSourceTemplate(constants.BatchMode.template,
'{Chinese}', constants.TemplateFormatVersion.v1)
assert batch_source.batch_source_model.serialize() == expected_source_model.serialize()
# load model tests
# ================
# the field selected should be "English"
model = config_models.BatchSourceSimple('English')
batch_source.load_model(model)
assert batch_source.batch_mode_combobox.currentText() == 'simple'
assert batch_source.source_field_combobox.currentText() == 'English'
model.source_field = 'Chinese'
batch_source.load_model(model)
assert batch_source.batch_mode_combobox.currentText() == 'simple'
assert batch_source.source_field_combobox.currentText() == 'Chinese'
model.mode = constants.BatchMode.template
model.source_template = '{English}'
model.template_format_version = constants.TemplateFormatVersion.v1
batch_source.load_model(model)
assert batch_source.batch_mode_combobox.currentText() == 'template'
assert batch_source.simple_template_input.text() == '{English}'
# load advanced template
model.mode = constants.BatchMode.advanced_template
model.source_template = f"""result = 'yoyo'"""
model.template_format_version = constants.TemplateFormatVersion.v1
batch_source.load_model(model)
assert batch_source.advanced_template_input.toPlainText() == f"""result = 'yoyo'"""
# dialog.exec()
def test_target(qtbot):
config_gen = testing_utils.TestConfigGenerator()
hypertts_instance = config_gen.build_hypertts_instance_test_servicemanager('default')
dialog = EmptyDialog()
dialog.setupUi()
note_id_list = [config_gen.note_id_1, config_gen.note_id_2]
model_change_callback = MockModelChangeCallback()
field_list = hypertts_instance.get_all_fields_from_notes(note_id_list)
batch_target = component_target.BatchTarget(hypertts_instance, field_list, model_change_callback.model_updated)
dialog.addChildWidget(batch_target.draw())
# pick the Sound field
batch_target.target_field_combobox.setCurrentText('Sound')
assert batch_target.batch_target_model.target_field == 'Sound'
assert batch_target.batch_target_model.text_and_sound_tag == False
assert batch_target.batch_target_model.remove_sound_tag == True
# change some options
batch_target.radio_button_text_sound.setChecked(True)
assert batch_target.batch_target_model.text_and_sound_tag == True
batch_target.radio_button_keep_sound.setChecked(True)
assert batch_target.batch_target_model.remove_sound_tag == False
# load model tests
# ================
target_field = 'Chinese'
text_and_sound_tag = False
remove_sound_tag = True
model = config_models.BatchTarget(target_field, text_and_sound_tag, remove_sound_tag)
batch_target.load_model(model)
assert batch_target.target_field_combobox.currentText() == 'Chinese'
assert batch_target.radio_button_sound_only.isChecked() == True
assert batch_target.radio_button_text_sound.isChecked() == False
assert batch_target.radio_button_keep_sound.isChecked() == False
assert batch_target.radio_button_remove_sound.isChecked() == True
target_field = 'Pinyin'
text_and_sound_tag = True
remove_sound_tag = True
model = config_models.BatchTarget(target_field, text_and_sound_tag, remove_sound_tag)
batch_target.load_model(model)
assert batch_target.target_field_combobox.currentText() == 'Pinyin'
assert batch_target.radio_button_sound_only.isChecked() == False
assert batch_target.radio_button_text_sound.isChecked() == True
assert batch_target.radio_button_keep_sound.isChecked() == False
assert batch_target.radio_button_remove_sound.isChecked() == True
target_field = 'Sound'
text_and_sound_tag = True
remove_sound_tag = False
model = config_models.BatchTarget(target_field, text_and_sound_tag, remove_sound_tag)
batch_target.load_model(model)
assert batch_target.target_field_combobox.currentText() == 'Sound'
assert batch_target.radio_button_sound_only.isChecked() == False
assert batch_target.radio_button_text_sound.isChecked() == True
assert batch_target.radio_button_keep_sound.isChecked() == True
assert batch_target.radio_button_remove_sound.isChecked() == False
target_field = 'Chinese'
text_and_sound_tag = False
remove_sound_tag = False
model = config_models.BatchTarget(target_field, text_and_sound_tag, remove_sound_tag)
batch_target.load_model(model)
assert batch_target.target_field_combobox.currentText() == 'Chinese'
assert batch_target.radio_button_sound_only.isChecked() == True
assert batch_target.radio_button_text_sound.isChecked() == False
assert batch_target.radio_button_keep_sound.isChecked() == True
assert batch_target.radio_button_remove_sound.isChecked() == False
def test_batch_preview(qtbot):
config_gen = testing_utils.TestConfigGenerator()
hypertts_instance = config_gen.build_hypertts_instance_test_servicemanager('default')
# configure delay on service A
# hypertts_instance.service_manager.get_service('ServiceA').configure({'delay': 1.0})
dialog = EmptyDialog()
dialog.setupUi()
note_id_list = [config_gen.note_id_1, config_gen.note_id_2]
voice_list = hypertts_instance.service_manager.full_voice_list()
voice_a_1 = [x for x in voice_list if x.name == 'voice_a_1'][0]
voice_selection = config_models.VoiceSelectionSingle()
voice_selection.set_voice(config_models.VoiceWithOptions(voice_a_1, {}))
batch_config = config_models.BatchConfig()
source = config_models.BatchSourceSimple('Chinese')
target = config_models.BatchTarget('Sound', False, True)
batch_config.set_source(source)
batch_config.set_target(target)
batch_config.set_voice_selection(voice_selection)
batch_preview_callback = MockBatchPreviewCallback()
batch_preview = component_batch_preview.BatchPreview(hypertts_instance, note_id_list,
batch_preview_callback.sample_selected,
batch_preview_callback.batch_start,
batch_preview_callback.batch_end)
batch_preview.load_model(batch_config)
dialog.addChildLayout(batch_preview.draw())
# dialog.exec()
# return
def test_batch_dialog(qtbot):
config_gen = testing_utils.TestConfigGenerator()
hypertts_instance = config_gen.build_hypertts_instance_test_servicemanager('default')
dialog = EmptyDialog()
dialog.setupUi()
note_id_list = [config_gen.note_id_1, config_gen.note_id_2]
# test saving of config
# =====================
batch = component_batch.ComponentBatch(hypertts_instance, dialog)
batch.configure_browser(note_id_list)
batch.draw(dialog.getLayout())
# select a source field and target field
batch.source.source_field_combobox.setCurrentText('English')
batch.target.target_field_combobox.setCurrentText('Sound')
# set profile name
batch.profile_name_combobox.setCurrentText('batch profile 1')
# save button should be enabled
assert batch.profile_save_button.isEnabled() == True
# save
qtbot.mouseClick(batch.profile_save_button, aqt.qt.Qt.MouseButton.LeftButton)
# should be disabled after saving
assert batch.profile_save_button.isEnabled() == False
assert batch.profile_load_button.isEnabled() == False
assert batch.profile_save_button.text() == 'Preset Saved'
print(hypertts_instance.anki_utils.written_config)
assert 'batch profile 1' in hypertts_instance.anki_utils.written_config[constants.CONFIG_BATCH_CONFIG]
# try to deserialize that config, it should have the English field selected
deserialized_model = hypertts_instance.load_batch_config('batch profile 1')
assert deserialized_model.source.source_field == 'English'
assert deserialized_model.target.target_field == 'Sound'
# test loading of config
# ======================
dialog = EmptyDialog()
dialog.setupUi()
batch = component_batch.ComponentBatch(hypertts_instance, dialog)
batch.configure_browser(note_id_list)
batch.draw(dialog.getLayout())
# dialog.exec()
assert batch.profile_load_button.isEnabled() == False
# select preset
batch.profile_name_combobox.setCurrentText('batch profile 1')
# should be enabled now
assert batch.profile_load_button.isEnabled() == True
assert batch.profile_load_button.text() == 'Load'
# open
qtbot.mouseClick(batch.profile_load_button, aqt.qt.Qt.MouseButton.LeftButton)
# button should go back to disabled
assert batch.profile_load_button.isEnabled() == False
assert batch.profile_save_button.isEnabled() == False
assert batch.profile_load_button.text() == 'Preset Loaded'
# assertions on GUI
assert batch.source.source_field_combobox.currentText() == 'English'
assert batch.target.target_field_combobox.currentText() == 'Sound'
# dialog.exec()
# test launching with a particular preset
# =======================================
dialog = EmptyDialog()
dialog.setupUi()
batch = component_batch.ComponentBatch(hypertts_instance, dialog)
batch.configure_browser(note_id_list)
batch.draw(dialog.getLayout())
batch.load_batch('batch profile 1')
# assertions on GUI
assert batch.source.source_field_combobox.currentText() == 'English'
assert batch.target.target_field_combobox.currentText() == 'Sound'
# dialog.exec()
assert batch.profile_load_button.isEnabled() == False
assert batch.profile_save_button.isEnabled() == False
# play sound preview
# ==================
# select second row
index_second_row = batch.preview.batch_preview_table_model.createIndex(1, 0)
batch.preview.table_view.selectionModel().select(index_second_row, aqt.qt.QItemSelectionModel.SelectionFlag.Select)
# select voice
batch.voice_selection.voices_combobox.setCurrentIndex(1)
# press preview button
qtbot.mouseClick(batch.preview_sound_button, aqt.qt.Qt.MouseButton.LeftButton)
# dialog.exec()
assert hypertts_instance.anki_utils.played_sound == {
'source_text': 'hello',
'voice': {
'gender': 'Male',
'language': 'fr_FR',
'name': 'voice_a_1',
'service': 'ServiceA',
'voice_key': {'name': 'voice_1'}
},
'options': {}
}
# load audio
# ==========
batch.source.source_field_combobox.setCurrentText('Chinese')
qtbot.mouseClick(batch.apply_button, aqt.qt.Qt.MouseButton.LeftButton)
# make sure notes were updated
note_1 = hypertts_instance.anki_utils.get_note_by_id(config_gen.note_id_1)
assert 'Sound' in note_1.set_values
sound_tag = note_1.set_values['Sound']
audio_full_path = hypertts_instance.anki_utils.extract_sound_tag_audio_full_path(sound_tag)
audio_data = hypertts_instance.anki_utils.extract_mock_tts_audio(audio_full_path)
assert audio_data['source_text'] == '老人家'
note_2 = hypertts_instance.anki_utils.get_note_by_id(config_gen.note_id_2)
assert 'Sound' in note_2.set_values
sound_tag = note_2.set_values['Sound']
audio_full_path = hypertts_instance.anki_utils.extract_sound_tag_audio_full_path(sound_tag)
audio_data = hypertts_instance.anki_utils.extract_mock_tts_audio(audio_full_path)
assert audio_data['source_text'] == '你好'
# button state
assert batch.apply_button.isEnabled() == False
assert batch.cancel_button.isEnabled() == True
assert batch.cancel_button.text() == 'Close'
# delete profile
# ==============
assert batch.profile_name_combobox.count() == 2
assert batch.profile_name_combobox.currentText() == 'batch profile 1'
qtbot.mouseClick(batch.profile_delete_button, aqt.qt.Qt.MouseButton.LeftButton)
# make sure the profile was deleted
assert 'batch profile 1' not in hypertts_instance.anki_utils.written_config[constants.CONFIG_BATCH_CONFIG]
# make sure the combobox was updated
assert batch.profile_name_combobox.count() == 1
assert batch.profile_name_combobox.currentText() == 'Preset 1'
# dialog.exec()
def test_batch_dialog_sound_preview_error(qtbot):
config_gen = testing_utils.TestConfigGenerator()
hypertts_instance = config_gen.build_hypertts_instance_test_servicemanager('default')
dialog = EmptyDialog()
dialog.setupUi()
note_id_list = [config_gen.note_id_1, config_gen.note_id_2]
# test saving of config
# =====================
batch = component_batch.ComponentBatch(hypertts_instance, dialog)
batch.configure_browser(note_id_list)
batch.draw(dialog.getLayout())
# select English source
batch.source.source_field_combobox.setCurrentText('English')
# play sound preview with error voice
# ===================================
# dialog.exec()
# select error voice
batch.voice_selection.voices_combobox.setCurrentIndex(5)