chooseContextMenu
Class: matlab.uitest.TestCase
Namespace: matlab.uitest
Syntax
Description
Input Arguments
testCase
— Test case
matlab.uitest.TestCase
object
Test case, specified as a matlab.uitest.TestCase
object.
comp
— UI component with context menu
UI component 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 Component | Typical Creation Function |
---|---|
Axes | axes |
Button | uibutton |
Check Box | uicheckbox |
Date Picker | uidatepicker |
Discrete Knob | uiknob |
Drop Down | uidropdown |
Edit Field (Numeric, Text) | uieditfield |
Hyperlink | uihyperlink |
Image | uiimage |
Knob | uiknob |
Label | uilabel |
List Box | uilistbox |
Panel | uipanel |
Polar Axes | polaraxes |
Radio Button | uiradiobutton |
Slider | uislider |
Spinner | uispinner |
State Button | uibutton |
Switch (Rocker, Slider, Toggle) | uiswitch |
Text Area | uitextarea |
Toggle Button | uitogglebutton |
Tree Node | uitreenode |
UI Axes | uiaxes |
UI Figure | uifigure |
Example: uifigure
Example: axes('Position',[0.1 0.1 .6 .6])
Example: uispinner('Limits',[0 10],'Value',5)
menuitem
— Context menu item to choose
matlab.ui.container.Menu
object
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
— Location to open context menu
1-by-2 or 1-by-3 numeric array
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 — A 1-by-2 numeric array containing x- and y-coordinates. Specify the coordinates of the point measured in pixels from the lower-left corner of the component.
UI Figure — A 1-by-2 numeric array containing x- and y-coordinates. Specify the coordinates of the point 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)
uit
— Target table UI component
matlab.ui.control.Table
object
Target table UI component, specified as a matlab.ui.control.Table
object. Table UI components are created with the uitable
function.
indices
— Indices of table cell
1-by-2 vector
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]
Examples
Choose Context Menu Item for UI Figure
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)
Choose Context Menu Item at Specified Location
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]);
Choose Context Menu Item for Specified Table Cell
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])
Version History
Introduced in R2020bR2024b: Specify location of gesture on figures that use nonpixel units
You can specify the location of the gesture on a UI figure that uses any unit of measurement.
In previous releases, the method lets you specify coordinates only for figures whose
Units
property is set to "pixels"
.
R2024b: Specifying location of gesture on axes or UI axes with active right y-axis is not supported
Specifying the location of the gesture on axes or UI axes with an active right y-axis is no longer supported. If your chart has two y-axes, activate the side associated with the left y-axis before performing the gesture. In previous releases, when you specify the location on axes or UI axes with an active right side, the app testing framework interprets that location with respect to the left y-axis, which can cause the gesture to occur at an unexpected location or fail.
R2024a: Node expanded callback executes when gestures programmatically expand tree nodes
To better mimic a user who must expand tree nodes to interact with a nested node, the
node expanded callback executes when you perform a gesture on a collapsed tree node by using
the chooseContextMenu
method. You can specify the callback by setting the
NodeExpandedFcn
property of the tree.
For example, programmatically choose a menu item for a nested tree node, and display the text of any programmatically expanded nodes.
fig = uifigure; t = uitree(fig); t.NodeExpandedFcn = @(src,event) disp(event.Node.Text); parent = uitreenode(t,"Text","Runners"); child1 = uitreenode(parent,"Text","Joe"); child2 = uitreenode(parent,"Text","Linda"); cm = uicontextmenu(fig); m1 = uimenu(cm,"Text","Menu1"); m2 = uimenu(cm,"Text","Menu2"); child2.ContextMenu = cm; testCase = matlab.uitest.TestCase.forInteractiveUse; testCase.chooseContextMenu(child2,m1)
If you do not want the callback to execute, preserving the behavior in R2023b and earlier, expand the tree node before performing the gesture. In this code, the callback does not execute.
fig = uifigure; t = uitree(fig); t.NodeExpandedFcn = @(src,event) disp(event.Node.Text); parent = uitreenode(t,"Text","Runners"); child1 = uitreenode(parent,"Text","Joe"); child2 = uitreenode(parent,"Text","Linda"); cm = uicontextmenu(fig); m1 = uimenu(cm,"Text","Menu1"); m2 = uimenu(cm,"Text","Menu2"); child2.ContextMenu = cm; testCase = matlab.uitest.TestCase.forInteractiveUse; expand(parent) testCase.chooseContextMenu(child2,m1)
R2024a: Perform gestures on hyperlinks
You can test a right-click to open a context menu within hyperlink components.
R2023a: Perform gestures on labels
You can test a right-click to open a context menu within label components.
R2021a: Perform gestures on panels and tables
You can test a right-click to open a context menu within panels and table UI components.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)