Ejemplo n.º 1
0
def widget_test(model_test, monkeypatch, qtbot):
    # Fake registry.
    def list_plugins(plugin_type):
        assert plugin_type == 'dictionary'
        for name, readonly in (
            ('bad', False),
            ('json', False),
            ('ro', True),
        ):
            obj = SimpleNamespace(readonly=readonly)
            yield SimpleNamespace(name=name, obj=obj)

    registry = mock.MagicMock(spec=['list_plugins'])
    registry.list_plugins.side_effect = list_plugins
    monkeypatch.setattr('plover.gui_qt.dictionaries_widget.registry', registry)
    # Fake file dialog.
    file_dialog = mock.MagicMock(spec='''
                                 getOpenFileNames
                                 getSaveFileName
                                 '''.split())
    monkeypatch.setattr('plover.gui_qt.dictionaries_widget.QFileDialog',
                        file_dialog)

    # Fake `create_dictionary`.
    def create_dictionary(filename, threaded_save=True):
        pass

    steno_dict = mock.create_autospec(StenoDictionary)
    create_dictionary = mock.create_autospec(create_dictionary,
                                             return_value=steno_dict)
    monkeypatch.setattr('plover.gui_qt.dictionaries_widget.create_dictionary',
                        create_dictionary)
    # Patch `DictionariesModel` constructor to use our own instance.
    monkeypatch.setattr('plover.gui_qt.dictionaries_widget.DictionariesModel',
                        lambda engine, icons: model_test.model)
    widget = DictionariesWidget()
    widget.setup(model_test.engine)
    qtbot.addWidget(widget)
    test = WidgetTest(registry, qtbot, widget, file_dialog, create_dictionary,
                      model_test)
    return test