Main Content

chooseContextMenu

Class: matlab.uitest.TestCase
Package: matlab.uitest

Perform choose gesture on context menu item

Description

example

chooseContextMenu(testcase,comp,menuitem) performs a right-click at the center of the UI component comp to open a context menu. The method then chooses the specified menuitem.

example

chooseContextMenu(testcase,comp,menuitem,location) specifies the location to open the context menu within the component. You can only specify location with five of the available components: axes, UI axes, polar axes, panel, and UI figure.

example

chooseContextMenu(testcase,uit,menuitem,indices) right-clicks the cell specified by indices within the table UI component uit. The method then chooses the specified menuitem.

Input Arguments

expand all

Instance of the test case, specified as a matlab.uitest.TestCase object.

UI component with the context menu, specified as a UI component object. The context menu must include the menu item specified by menuitem. Components that support a context menu include images, buttons, switches, axes, and figures.

Supported ComponentTypical Creation Function
Axesaxes
Buttonuibutton
Check Boxuicheckbox
Date Pickeruidatepicker
Discrete Knobuiknob
Drop Downuidropdown
Edit Field (Numeric, Text)uieditfield
Imageuiimage
Knobuiknob
List Boxuilistbox
Paneluipanel
Polar Axespolaraxes
Radio Buttonuiradiobutton
Slideruislider
Spinneruispinner
State Buttonuibutton
Switch (Rocker, Slider, Toggle)uiswitch
Text Areauitextarea
Toggle Buttonuitogglebutton
Tree Nodeuitreenode
UI Axesuiaxes
UI Figureuifigure

Example: uifigure

Example: axes('Position',[0.1 0.1 .6 .6])

Example: uispinner('Limits',[0 10],'Value',5)

Context menu item to choose during the test, specified as a matlab.ui.container.Menu object. Menu items are created with the uimenu function.

Location to open the context menu within the UI component, specified as the coordinates of the point. The form of location depends on the UI component:

  • Axes and UI Axes — A 1-by-2 or 1-by-3 numeric array containing x-, y-, and optionally z-coordinates.

  • Polar Axes — A 1-by-2 numeric array containing θ- and r-coordinates.

  • Panel and UI Figure — A 1-by-2 numeric array containing x- and y-coordinates. Specify the coordinates of the point to right-click measured in pixels from the lower-left corner of the component.

Example: [32.5 13 0.25] (UI axes)

Example: [pi/2 0.5] (Polar axes)

Example: [100 200] (UI figure)

Target table UI component, specified as a matlab.ui.control.Table object. Table UI components are created with the uitable function.

Indices of the table cell to right-click, specified as a 1-by-2 vector with the row index appearing before the column index.

Example: [2 3]

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Create a context menu with two menu items in a UI figure. Assign the context menu to the figure by setting the ContextMenu property of the figure to the ContextMenu object. To view the context menu, right-click anywhere in the figure window.

fig = uifigure;

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Menu1');
m2 = uimenu(cm,'Text','Menu2');

fig.ContextMenu = cm;

Create an interactive test case and choose the menu item m1. The context menu and a blue dot appear at the center of the figure. Then, a second blue dot representing the programmatic choose gesture appears and disappears at the center of the selected menu item.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.chooseContextMenu(fig,m1)

Create an Axes object in a UI figure. Assign a context menu with two menu items to the Axes object.

fig = uifigure;
ax = axes(fig);

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Menu1');
m2 = uimenu(cm,'Text','Menu2');

ax.ContextMenu = cm;

Create an interactive test case and choose the menu item m2 by opening the context menu for the axes at the coordinates (0.85,0.2). The context menu and a blue dot appear at the specified axes coordinates. Then, a second blue dot representing the programmatic choose gesture appears and disappears at the center of the second menu item.

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.chooseContextMenu(ax,m2,[0.85 0.2]);

Create a table UI component that contains a mixture of different data types. Then, assign a context menu with two items to the table.

fig = uifigure;
uit = uitable(fig);
d = {'Male',52,true;'Male',40,true;'Female',25,false};
uit.Data = d;

cm = uicontextmenu(fig);
m1 = uimenu(cm,'Text','Menu1');
m2 = uimenu(cm,'Text','Menu2');
uit.ContextMenu = cm;

Create an interactive test case and choose the menu item m2 by opening the context menu within the table cell with indices (1,1).

tc = matlab.uitest.TestCase.forInteractiveUse;
tc.chooseContextMenu(uit,m2,[1 1])

Introduced in R2020b