def keyPress(self, widget, key, modifier=qt.Qt.NoModifier, delay=-1): """Sends a Qt key press event. See QTest.keyPress for details. """ QTest.keyPress(widget, key, modifier, delay) self.qWait(20)
def test_recipe_name(self): """ Tests the name method. """ widget = RecipeNameWidget() input_data = random_string() for char in input_data: QTest.keyPress(widget.widget, char) QTest.keyRelease(widget.widget, char) self.assertEqual(widget.name(), input_data)
def test_ingredient_name_and_mass_entered(self): """ Tests that after entering ingredient name and mass the first callback is called. """ input_data_name = self.PRODUCT_NAMES[1] input_data_mass = "331" default_mass = 100 # After the first call (after product name was entered) the mass will be equal to default_mass. ingredient_entered_callback = Callback((input_data_name, default_mass)) finalize_callback = Callback() widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback) for char in input_data_name: QTest.keyPress(widget._ingredient_line_edit, char) QTest.keyRelease(widget._ingredient_line_edit, char) QTest.keyPress(widget._ingredient_line_edit, Qt.Key.Key_Enter) QTest.keyRelease(widget._ingredient_line_edit, Qt.Key.Key_Enter) # Refresh the callback. ingredient_entered_callback.called = False ingredient_entered_callback.expected_args = (input_data_name, int(input_data_mass)) for char in input_data_mass: QTest.keyPress(widget._ingredient_mass_line_edit, char) QTest.keyRelease(widget._ingredient_mass_line_edit, char) QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) self.assertTrue(ingredient_entered_callback.called) self.assertFalse(finalize_callback.called)
def test_ingredient_name_and_mass_finalize(self): """ Tests that after clicking the finalize button the second callback is called. """ input_data_name = self.PRODUCT_NAMES[1] input_data_mass = "331" # After the first call (after product name was entered) the mass will be equal to 100. ingredient_entered_callback = Callback((input_data_name, 100)) finalize_callback = Callback((input_data_name, int(input_data_mass))) widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback) for char in input_data_name: QTest.keyPress(widget._ingredient_line_edit, char) QTest.keyRelease(widget._ingredient_line_edit, char) QTest.keyPress(widget._ingredient_line_edit, Qt.Key.Key_Enter) QTest.keyRelease(widget._ingredient_line_edit, Qt.Key.Key_Enter) # Refresh the callback. ingredient_entered_callback.called = False ingredient_entered_callback.expected_args = (input_data_name, int(input_data_mass)) for char in input_data_mass: QTest.keyPress(widget._ingredient_mass_line_edit, char) QTest.keyRelease(widget._ingredient_mass_line_edit, char) QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) QTest.mouseClick(widget._ingredient_add_button, Qt.MouseButton.LeftButton) self.assertTrue(finalize_callback.called)
def test_recipe_get_text(self): """ Tests the get text method. """ widget = RecipeTextWidget() input_data = [random_string(50) for _ in range(10)] for line in input_data: for char in line: QTest.keyPress(widget.widget, char) QTest.keyRelease(widget.widget, char) QTest.keyPress(widget.widget, Qt.Key.Key_Enter) QTest.keyRelease(widget.widget, Qt.Key.Key_Enter) self.assertEqual(widget.get_text(), "\n".join(input_data) + "\n")
def test_recipe_lookup_callback(self): """ Tests the get text method. """ product_names = ["test1", "test2", "test3"] callback = Callback(("test2",)) widget = RecipeLookupWidget(product_names, callback.callback) input_data = "test2" for char in input_data: QTest.keyPress(widget.widget, char) QTest.keyRelease(widget.widget, char) QTest.keyPress(widget.widget, Qt.Key.Key_Enter) QTest.keyRelease(widget.widget, Qt.Key.Key_Enter) self.assertTrue(callback.called)
def test_ingredient_mass_entered(self): """ Tests that after entering ingredient mass the first callback is called. """ input_data = "259" ingredient_entered_callback = Callback(("", int(input_data))) finalize_callback = Callback() widget = IngredientWidget(self.PRODUCT_NAMES, ingredient_entered_callback.callback, finalize_callback.callback) for char in input_data: QTest.keyPress(widget._ingredient_mass_line_edit, char) QTest.keyRelease(widget._ingredient_mass_line_edit, char) QTest.keyPress(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) QTest.keyRelease(widget._ingredient_mass_line_edit, Qt.Key.Key_Enter) self.assertTrue(ingredient_entered_callback.called) self.assertFalse(finalize_callback.called)
def test_serves(self): """ Tests the name method. """ widget = ServesAmountWidget(empty_callback) input_data = "5" QTest.keyPress(widget.widget, Qt.Key.Key_Backspace) QTest.keyRelease(widget.widget, Qt.Key.Key_Backspace) for char in input_data: QTest.keyPress(widget.widget, char) QTest.keyRelease(widget.widget, char) QTest.keyPress(widget.widget, Qt.Key.Key_Enter) QTest.keyRelease(widget.widget, Qt.Key.Key_Enter) self.assertEqual(widget.serves(), int(input_data))
def test_serves_callback(self): """ Tests that callback is called when value is entered. """ callback = Callback((5, )) widget = ServesAmountWidget(callback.callback) input_data = "5" QTest.keyPress(widget.widget, Qt.Key.Key_Backspace) QTest.keyRelease(widget.widget, Qt.Key.Key_Backspace) for char in input_data: QTest.keyPress(widget.widget, char) QTest.keyRelease(widget.widget, char) QTest.keyPress(widget.widget, Qt.Key.Key_Enter) QTest.keyRelease(widget.widget, Qt.Key.Key_Enter) self.assertTrue(callback.called)