def test_runJavaScript(self): w = WebviewWidget() w.runJavaScript('2;') retvals = [] w.runJavaScript('3;', lambda retval: retvals.append(retval)) wait(until=lambda: retvals) self.assertEqual(retvals[0], 3)
def test_escape_hides(self): window = QDialog() w = WebviewWidget(window) window.show() w.setFocus(Qt.OtherFocusReason) self.assertFalse(window.isHidden()) QTest.keyClick(w, Qt.Key_Escape) self.assertTrue(window.isHidden())
def _setup_ui_(self): self.table_model = ReportItemModel(0, len(Column.__members__)) self.table = ReportTable(self.controlArea) self.table.setModel(self.table_model) self.table.setShowGrid(False) self.table.setSelectionBehavior(QTableView.SelectRows) self.table.setSelectionMode(QTableView.SingleSelection) self.table.setWordWrap(False) self.table.setMouseTracking(True) self.table.verticalHeader().setSectionResizeMode(QHeaderView.Fixed) self.table.verticalHeader().setDefaultSectionSize(20) self.table.verticalHeader().setVisible(False) self.table.horizontalHeader().setVisible(False) self.table.setFixedWidth(250) self.table.setColumnWidth(Column.item, 200) self.table.setColumnWidth(Column.remove, 23) self.table.setColumnWidth(Column.scheme, 25) self.table.clicked.connect(self._table_clicked) self.table.selectionModel().selectionChanged.connect( self._table_selection_changed) self.controlArea.layout().addWidget(self.table) self.last_scheme = None self.scheme_button = gui.button( self.controlArea, self, "Back to Last Scheme", callback=self._show_last_scheme, ) box = gui.hBox(self.controlArea) box.setContentsMargins(-6, 0, -6, 0) self.save_button = gui.button(box, self, "Save", callback=self.save_report) self.print_button = gui.button(box, self, "Print", callback=self._print_report) class PyBridge(QObject): @pyqtSlot(str) def _select_item(myself, item_id): item = self.table_model.get_item_by_id(item_id) self.table.selectRow( self.table_model.indexFromItem(item).row()) self._change_selected_item(item) @pyqtSlot(str, str) def _add_comment(myself, item_id, value): item = self.table_model.get_item_by_id(item_id) item.comment = value self.report_changed = True self.report_view = WebviewWidget(self.mainArea, bridge=PyBridge(self)) self.mainArea.layout().addWidget(self.report_view)
def test_escape_hides(self): # NOTE: This test doesn't work as it is supposed to. window = QDialog() w = WebviewWidget(window) window.show() w.setFocus(Qt.OtherFocusReason) self.assertFalse(window.isHidden()) # This event is sent to the wrong widget. Should be sent to the # inner HTML view as focused, but no amount of clicking/ focusing # helped, neither did invoking JS handler directly. I'll live with it. QTest.keyClick(w, Qt.Key_Escape) self.assertTrue(window.isHidden())
def test_base(self): w = WebviewWidget() w.evalJS('document.write("foo");') SVG = '<svg xmlns:dc="...">asd</svg>' w.onloadJS('''document.write('{}');'''.format(SVG)) w.setUrl(SOME_URL) svg = self.process_events(lambda: w.svg()) self.assertEqual(svg, SVG) self.assertEqual( w.html(), '<html><head></head><body>foo<svg xmlns:dc="...">asd</svg></body></html>' )
def test_base(self): w = WebviewWidget() w.evalJS('document.write("foo");') SVG = '<svg xmlns:dc="...">asd</svg>' w.onloadJS('''document.write('{}');'''.format(SVG)) w.setUrl(SOME_URL) svg = None while svg is None: try: svg = w.svg() break except ValueError: qApp.processEvents() self.assertEqual(svg, SVG) self.assertEqual( w.html(), '<html><head></head><body>foo<svg xmlns:dc="...">asd</svg></body></html>')
def test_base(self): w = WebviewWidget() w.evalJS('document.write("foo");') SVG = '<svg xmlns:dc="...">asd</svg>' w.onloadJS('''document.write('{}');'''.format(SVG)) w.setUrl(SOME_URL) svg = self.process_events(lambda: w.svg()) self.assertEqual(svg, SVG) self.process_events(until=lambda: 'foo' in w.html()) html = '<svg xmlns:dc="...">asd</svg>' self.assertEqual( w.html(), '<html><head></head><body>{}</body></html>'.format( # WebKit evaluates first document.write first, whereas # WebEngine evaluates onloadJS first 'foo' + html if HAVE_WEBKIT else html + 'foo'))
def test_exposeObject(self): test = self OBJ = dict(a=[1, 2], b='c') done = False class Bridge(QObject): @pyqtSlot('QVariantMap') def check_object(self, obj): nonlocal test, done, OBJ done = True test.assertEqual(obj, OBJ) w = WebviewWidget(bridge=Bridge()) w.setUrl(SOME_URL) w.exposeObject('obj', OBJ) w.evalJS('''pybridge.check_object(window.obj);''') self.process_events(lambda: done) self.assertRaises(ValueError, w.exposeObject, 'obj', QDialog())
def __init__(self): super().__init__() self.table = None self._html = None def _loadFinished(is_ok): if is_ok: QTimer.singleShot( 1, lambda: setattr(self, '_html', self.webview.html())) self.webview = WebviewWidget(loadFinished=_loadFinished) vb = gui.vBox(self.controlArea, 'Import Data') hb = gui.hBox(vb) self.combo = combo = URLComboBox( hb, self.recent, editable=True, minimumWidth=400, insertPolicy=QComboBox.InsertAtTop, toolTip='Format: ' + VALID_URL_HELP, editTextChanged=self.is_valid_url, # Indirect via QTimer because calling wait() -> processEvents, # while our currentIndexChanged event hadn't yet finished. # Avoids calling handler twice. currentIndexChanged=lambda: QTimer.singleShot(1, self.load_url)) hb.layout().addWidget(QLabel('Public link URL:', hb)) hb.layout().addWidget(combo) hb.layout().setStretch(1, 2) RELOAD_TIMES = ( ('No reload', ), ('5 s', 5000), ('10 s', 10000), ('30 s', 30000), ('1 min', 60 * 1000), ('2 min', 2 * 60 * 1000), ('5 min', 5 * 60 * 1000), ) reload_timer = QTimer(self, timeout=lambda: self.load_url(from_reload=True)) def _on_reload_changed(): if self.reload_idx == 0: reload_timer.stop() return reload_timer.start(RELOAD_TIMES[self.reload_idx][1]) gui.comboBox(vb, self, 'reload_idx', label='Reload every:', orientation=Qt.Horizontal, items=[i[0] for i in RELOAD_TIMES], callback=_on_reload_changed) box = gui.widgetBox(self.controlArea, "Columns (Double-click to edit)") self.domain_editor = DomainEditor(self) editor_model = self.domain_editor.model() def editorDataChanged(): self.apply_domain_edit() self.commit() editor_model.dataChanged.connect(editorDataChanged) box.layout().addWidget(self.domain_editor) box = gui.widgetBox(self.controlArea, "Info", addSpace=True) info = self.data_info = gui.widgetLabel(box, '') info.setWordWrap(True) self.controlArea.layout().addStretch(1) gui.auto_commit(self.controlArea, self, 'autocommit', label='Commit') self.set_info()