Contenuto principale

dismissDialog

Class: matlab.uitest.TestCase
Namespace: matlab.uitest

Dismiss dialog box

Since R2024b. Recommended over dismissAlertDialog.

Description

dismissDialog dismisses an app or system dialog box in a test. When you dismiss a dialog box, you close it without making a selection in the dialog box.

For more information about app and system dialog boxes and their blocking behavior, see Summary of Dialog Boxes.

App Dialog Boxes

dismissDialog(testCase,dialogType,fig) dismisses the frontmost nonblocking app dialog box of the specified type in the figure window.

example

dismissDialog(testCase,dialogType,fig,fcn) dismisses the frontmost blocking app dialog box of the specified type. Because a blocking dialog box prevents additional commands from running while it is open, it must be created as part of the method call by using the fcn function handle.

When you use this syntax, the method first creates a blocking app dialog box by invoking the specified function handle. The method then dismisses the dialog box, which allows code execution to continue.

example

System Dialog Boxes

dialogData = dismissDialog(testCase,dialogType,fcn) dismisses the system dialog box of the specified type and returns information about the interaction with the dialog box. Because a system dialog box is blocking, it must be created as part of the method call by using the fcn function handle. (since R2026a)

example

Input Arguments

expand all

Test case, specified as a matlab.uitest.TestCase object.

Type of the app or system dialog box, specified as one of the values in this table. Specify the value that corresponds to the name of the function used to create the dialog box.

Dialog Box TypeCreation FunctionDescriptionCategoryBlocking Behavior
"uialert"uialertAlert dialog boxAppNonblocking
"uiconfirm"uiconfirmConfirmation dialog boxApp
  • Nonblocking if created by a call to uiconfirm with no output

  • Blocking if created by a call to uiconfirm with an output

"uigetdir" (since R2026a)

uigetdirFolder selection dialog boxSystemBlocking

"uigetfile" (since R2026a)

uigetfileFile selection dialog boxSystemBlocking

"uiputfile" (since R2026a)

uiputfileDialog box for saving filesSystemBlocking

For more information about app and system dialog boxes, see Summary of Dialog Boxes.

Data Types: string | char

Target figure for the app dialog box, specified as a matlab.ui.Figure object. The figure must be created with the uifigure function.

Code that creates the blocking dialog box, specified as a function handle. The function handle must create a blocking dialog box of type dialogType. For an app dialog box, the function handle must create the dialog box in the target figure fig.

Example: @() createBlockingAppDialog(fig)

Example: @uigetdir

Output Arguments

expand all

Since R2026a

Information about the system dialog box after programmatic interaction, returned as a GetDirDialogData, GetFileDialogData, or PutFileDialogData object. The type of returned object and its contents depend on the type of system dialog box.

Unlike framework interactions with app dialog boxes, which provide visual feedback, interactions with system dialog boxes do not display any graphical output. To get information about the system dialog box under test, inspect the properties of dialogData.

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Programmatically dismiss a nonblocking alert dialog box in front of a figure.

Create a UI figure with a push button that creates a modal alert dialog box when clicked. To program the button behavior, specify its ButtonPushedFcn callback property. See the code of the callback function buttonCallback, which creates an alert dialog box, at the end of this example.

fig = uifigure;
b = uibutton(fig,Text="Close",ButtonPushedFcn=@buttonCallback);

Create a test case for interactive testing.

testCase = matlab.uitest.TestCase.forInteractiveUse;

Test a press gesture on the button. The gesture executes the callback, which creates an alert dialog box in the figure. The alert dialog box is nonblocking and lets you run additional commands while it is open.

testCase.press(b)

Programmatically dismiss the alert dialog box by using the dismissDialog method. The dialog box closes. Because the dialog box in this example specifies the CloseFcn callback, the figure behind the dialog box also closes when the dialog box closes.

testCase.dismissDialog("uialert",fig)

Callback Function

This code shows the callback function used in this example. The buttonCallback function executes when the button in the figure is clicked. The function creates an alert dialog box in the figure. When the dialog box is dismissed, the CloseFcn callback closes the figure.

function buttonCallback(src,~)
uialert(src.Parent,"The figure will close.","Warning", ...
    Icon="warning", ...
    CloseFcn=@(~,~)close(src.Parent))
end

Programmatically dismiss a blocking confirmation dialog box in front of a figure.

Create a UI figure with a push button that creates a modal confirmation dialog box when clicked. To program the button behavior, specify its ButtonPushedFcn callback property. See the code of the callback function buttonCallback, which creates a confirmation dialog box, at the end of this example.

fig = uifigure;
b = uibutton(fig,Text="Close",ButtonPushedFcn=@buttonCallback);

Create a test case for interactive testing.

testCase = matlab.uitest.TestCase.forInteractiveUse;

Testing a press gesture on the button executes the callback, which creates a blocking confirmation dialog box in the figure. Programmatically dismiss the dialog box by using the dismissDialog method. This code first creates the blocking dialog box by invoking the specified function handle, which presses the button in the figure. The method then dismisses the dialog box, which allows code execution to continue.

testCase.dismissDialog("uiconfirm",fig,@() testCase.press(b))

Close the figure used for testing.

close(fig)

Callback Function

This code shows the callback function used in this example. The buttonCallback function executes when the button in the figure is clicked. The function creates a blocking confirmation dialog box in the figure. Selecting the "Proceed" option in the dialog box closes the figure. Selecting the "Cancel" option in the dialog box does not affect the figure.

function buttonCallback(src,~)
selection = uiconfirm(src.Parent,"Close this figure?","Confirm Close", ...
    Icon="warning", ...
    Options=["Proceed" "Cancel"]);

switch selection
    case "Proceed"
        close(src.Parent)
    case "Cancel"
        return
end
end

Since R2026a

Programmatically dismiss a system dialog box using the dismissDialog method.

Create a UI figure with a push button that creates a folder selection dialog box when clicked. To program the button behavior, specify its ButtonPushedFcn callback property. The dialog box in this example opens to the current folder.

fig = uifigure;
b = uibutton(fig, ...
    Text="Select Folder", ...
    ButtonPushedFcn=@(~,~) uigetdir);

Create a test case for interactive testing.

testCase = matlab.uitest.TestCase.forInteractiveUse;

Testing a press gesture on the button executes the callback. Programmatically dismiss the resulting folder selection dialog box by using the dismissDialog method. This code first invokes the specified function handle, which presses the button in the figure. The method then mimics a user dismissing the dialog box, which allows code execution to continue. Because framework interactions with system dialog boxes do not display any graphical output, the method returns information about the dialog box as a GetDirDialogData object.

dialogData = testCase.dismissDialog("uigetdir",@() testCase.press(b))
dialogData = 
  GetDirDialogData with properties:

    SelectedFolder: []
     InitialFolder: "C:\work\ExampleManager\user.examples\matlab-ex18708834"
             Title: "Select Folder to Open"

Close the figure used for testing.

close(fig)

Programmatically close a sequence of blocking dialog boxes using the chooseDialog and dismissDialog methods. To close the dialog boxes, first embed all the dialog box creation logic into a function handle, and then perform the final gesture using that function handle.

In a UI figure, create a push button that can result in a sequence of blocking dialog boxes when clicked. To program the button behavior, specify its ButtonPushedFcn callback property.

fig = uifigure;
b = uibutton(fig,Text="Close",ButtonPushedFcn=@buttonCallback);

This code shows the callback function used to create the dialog boxes. The buttonCallback function executes when the button in the figure is clicked. The function creates a blocking confirmation dialog box in the figure with "Proceed" and "Cancel" options:

  • Selecting the "Proceed" option in the dialog box creates another blocking confirmation dialog box through the reconfirm function.

  • Selecting the "Cancel" option in the dialog box does not affect the figure.

The reconfirm function executes when the "Proceed" option is selected in the original dialog box. The function creates another blocking confirmation dialog box in the figure with "Yes" and "No" options:

  • Selecting the "Yes" option in the dialog box closes the figure.

  • Selecting the "No" option in the dialog box does not affect the figure.

function buttonCallback(src,~)
selection = uiconfirm(src.Parent,"Close this figure?","Confirm Close", ...
    Icon="warning", ...
    Options=["Proceed" "Cancel"]);

switch selection
    case "Proceed"
        reconfirm(src)   % Display another dialog box to reconfirm
    case "Cancel"
        return
end
end

function reconfirm(src)
selection = uiconfirm(src.Parent,"Are you sure?","Confirm Close", ...
    Icon="warning", ...
    Options=["Yes" "No"]);

switch selection
    case "Yes"
        close(src.Parent)   % Close the figure
    case "No"
        return
end
end

To test the app, create a test case for interactive testing. You use this test case first to test a press gesture on the button, which executes the callback and creates a blocking confirmation dialog box in the figure. Then, you programmatically select the "Proceed" option in the dialog box by using the chooseDialog method and dismiss the resulting dialog box by using the dismissDialog method.

testCase = matlab.uitest.TestCase.forInteractiveUse;

Because blocking dialog boxes prevent commands from running while they are open, you cannot call the press, chooseDialog, and dismissDialog methods independently. Instead, you must embed all the dialog box creation logic into a single function handle and then perform the final gesture using that function handle. In this code, fcn1 and fcn2, respectively, specify the code that must run for the first and second dialog boxes to be created. In particular, fcn2 represents all the dialog box creation logic in the test.

fcn1 = @() testCase.press(b);
fcn2 = @() testCase.chooseDialog("uiconfirm",fig,fcn1,"Proceed");

To programmatically close the expected dialog boxes, perform the final gesture using the dismissDialog method with the function handle fcn2 that includes all the dialog box creation logic. This call to the dismissDialog method results in these gestures:

  1. Press the button in the figure, which creates the first blocking dialog box.

  2. Select the "Proceed" option in the first blocking dialog box, which closes the dialog box and creates the second blocking dialog box.

  3. Close the second blocking dialog box by dismissing it.

testCase.dismissDialog("uiconfirm",fig,fcn2)

Closing the blocking dialog boxes allows code execution to continue. For example, close the figure used for testing.

close(fig)

More About

expand all

Version History

Introduced in R2024b

expand all