Skip to content

Commit

Permalink
Add unit test for default button
Browse files Browse the repository at this point in the history
  • Loading branch information
DanicaSTFC committed Feb 15, 2024
1 parent 8e806c8 commit 78e9109
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
10 changes: 5 additions & 5 deletions eqt/ui/FormDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ def saveAllWidgetStates(self):
'''
self.formWidget.saveAllWidgetStates()

def getWidgetStates(self):
def getSavedWidgetStates(self):
'''Returns the saved widget states.'''
return self.formWidget.getWidgetStates()
return self.formWidget.getSavedWidgetStates()

def getDefaultWidgetStates(self):
'''Returns the saved default widget states.'''
Expand Down Expand Up @@ -375,7 +375,7 @@ def _onOk(self):
self.close()

if self.display_on_parent_dict:
if self.getWidgetStates() == self.getDefaultWidgetStates():
if self.getSavedWidgetStates() == self.getDefaultWidgetStates():
self._removeWidgetsFromParent()
else:
self._addOrUpdateWidgetsInParent()
Expand All @@ -390,9 +390,9 @@ def _addOrUpdateWidgetsInParent(self):
"""
for index, name in enumerate(self.display_on_parent_dict, start=1):
widget_row = self.parent_button_row + index if self.parent_button_row != -1 else -1
value = str(self.getWidgetStates()[f'{name}_field']['value'])
value = str(self.getSavedWidgetStates()[f'{name}_field']['value'])
if f'{name}_field' not in self.dialog_parent.getWidgets():
label = str(self.getWidgetStates()[f'{name}_label']['value'])
label = str(self.getSavedWidgetStates()[f'{name}_label']['value'])
self.dialog_parent.insertWidget(widget_row, name, QtWidgets.QLabel(value), label)
else:
self.dialog_parent.getWidget(name, 'field').setText(value)
Expand Down
6 changes: 3 additions & 3 deletions eqt/ui/UIFormWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def saveAllWidgetStates(self):
'''
self.widget_states = self.getAllWidgetStates()

def getWidgetStates(self):
def getSavedWidgetStates(self):
'''Returns the saved widget states.'''
return self.widget_states

Expand Down Expand Up @@ -616,9 +616,9 @@ def saveAllWidgetStates(self):
'''
self.widget().saveAllWidgetStates()

def getWidgetStates(self):
def getSavedWidgetStates(self):
'''Returns the saved widget states.'''
return self.widget().getWidgetStates()
return self.widget().getSavedWidgetStates()

def getDefaultWidgetStates(self):
'''Returns the saved default widget states.'''
Expand Down
49 changes: 31 additions & 18 deletions test/test__formUI_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,6 @@ def setUp(self):
self.simple_form = AdvancedFormDialog()
self.add_two_widgets()


def click_default_button(self):
"""
Clicks the default button on the form.
Expand All @@ -815,20 +814,20 @@ def test_position_default_button(self):

def test_dialog_ok_button_behaviour(self):
"""
Test the behavior of the Ok button on the advanced dialog.
Tests the behavior of the Ok button on the advanced dialog.
This test case verifies the button's behavior in different scenarios:
1. Click the Ok button without changing any states and verify that the parent widget states remain unchanged.
2. Change the states and click the OK button, then verify that the parent widget states are updated correctly.
3. Click the default button and then the OK button, and verify that the parent widget states are restored to their initial values.
1. Clicks the Ok button without changing any states and verifies that the parent widget states remain unchanged.
2. Changes the states and click the OK button, then verifies that the parent widget states are updated correctly.
3. Clicks the default button and then the OK button, and verifies that the parent widget states are restored to their initial values.
"""
# click ok first
# 1.
parent_initial_states = self.form_parent.getAllWidgetStates()
self.form.open()
self.click_Ok()
parent_states = self.form_parent.getAllWidgetStates()
self.assertEqual(parent_states, parent_initial_states)
# change states and click ok
# 2.
for i in [0, 1]:
self.set_state(i)
self.form.open()
Expand All @@ -838,7 +837,7 @@ def test_dialog_ok_button_behaviour(self):
self.assertEqual(parent_states[f'{name}_field']['value'],
str(self.exampleState[i][f'{name}_value']))
self.assertEqual(parent_states[f'{name}_label']['value'], name)
# click default and then ok
# 3.
self.form.open()
self.click_default_button()
self.click_Ok()
Expand All @@ -847,30 +846,30 @@ def test_dialog_ok_button_behaviour(self):

def test_dialog_cancel_button_behaviour(self):
"""
Test the behavior of the Cancel button on the advanced dialog.
Tests the behavior of the Cancel button on the advanced dialog.
This test case verifies the button's behavior in different scenarios:
1. Open the dialog, click the Cancel button without changing any states and verify that the
1. Opens the dialog, clicks the Cancel button without changing any states and verifies that the
parent-widget states remain unchanged.
2. Open the dialog, update the widgets and click the Cancel button. Verify that the parent-widget states remain unchanged.
3. Open the dialog, update the widgets and click the Ok button. Then reopen the dialog, change the states and click the Cancel button.
Verify that the parent widget states are those set before reopening the dialog.
4. Open the dialog, click the default button and then the Cancel button. Verify that the parent widget
2. Opens the dialog, update the widgets and clicks the Cancel button. Verifies that the parent-widget states remain unchanged.
3. Opens the dialog, updates the widgets and clicks the Ok button. Then reopens the dialog, changes the states and clicks the Cancel button.
Verifies that the parent widget states are those set before reopening the dialog.
4. Opens the dialog, clicks the default button and then the Cancel button. Verifies that the parent widget
states remain unchanged.
"""
# click cancel first
# 1.
parent_initial_states = self.form_parent.getAllWidgetStates()
self.form.open()
self.click_Cancel()
parent_states = self.form_parent.getAllWidgetStates()
self.assertEqual(parent_states, parent_initial_states)
# change states and click Cancel
# 2.
self.form.open()
self.set_state(0)
self.click_Cancel()
parent_states = self.form_parent.getAllWidgetStates()
self.assertEqual(parent_states, parent_initial_states)
# change states and click Ok then chance states and click cancel
# 3.
self.form.open()
self.set_state(0)
self.click_Ok()
Expand All @@ -882,7 +881,7 @@ def test_dialog_cancel_button_behaviour(self):
self.assertEqual(parent_states[f'{name}_field']['value'],
str(self.exampleState[0][f'{name}_value']))
self.assertEqual(parent_states[f'{name}_label']['value'], name)
# click default and then Cancel
# 4.
self.form.open()
self.click_default_button()
self.click_Cancel()
Expand All @@ -891,3 +890,17 @@ def test_dialog_cancel_button_behaviour(self):
self.assertEqual(parent_states[f'{key}_field']['value'],
str(self.exampleState[0][f'{key}_value']))
self.assertEqual(parent_states[f'{key}_label']['value'], key)

def test_dialog_default_button_behaviour(self):
"""
Tests the behavior of the default button on the advanced dialog.
This test case opens the dialog, updates the widgets, clicks the default
button and then the Ok button. Verifies that the widgets in the dialog
are set to their default states.
"""
self.form.open()
self.set_state(0)
self.click_default_button()
self.click_Ok()
self.assertEqual(self.form.getSavedWidgetStates(), self.form.getDefaultWidgetStates())

0 comments on commit 78e9109

Please sign in to comment.