Main Content

scroll

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

Perform scroll gesture on UI component

Since R2024a

Description

example

scroll(testCase,comp,direction) performs a scroll gesture, such as a scroll wheel movement, with the pointer located at the center of the specified component comp. The method mimics a user scrolling in the specified direction.

Input Arguments

expand all

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

Component to scroll on, specified as a UI component object. This table lists the supported components.

Supported ComponentTypical Creation Function
Axesaxes
UI Axesuiaxes

Example: axes(Position=[0.1 0.1 0.6 0.6])

Direction of scroll, specified as "up" or "down" to scroll vertically, and "left" or "right" to scroll horizontally.

Data Types: string | char

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Test a vertical scroll gesture on axes whose background color changes based on the scroll direction.

Create a UI figure with axes whose background color changes based on the direction of a vertical scroll. To program the axes behavior, create a window scroll wheel callback for the figure by specifying its WindowScrollWheelFcn callback property. See the code of the callback function changeColor, which is used to change the axes background color based on the scroll direction, at the end of this example.

fig = uifigure(WindowScrollWheelFcn=@changeColor);
ax = axes(fig);
plot(ax,1:10)

Create an interactive test case, and use it to verify that the axes background color is white, or [1 1 1].

testCase = matlab.uitest.TestCase.forInteractiveUse;
testCase.verifyEqual(ax.Color,[1 1 1])
Verification passed.

Test a scroll gesture on the axes in the down direction. The gesture executes the window scroll wheel callback, which sets the axes background color based on the scroll direction.

testCase.scroll(ax,"down")

Figure contains an axes object. The axes object contains an object of type line.

Test if the axes background color is now green, or [0 1 0]. The test passes.

testCase.verifyEqual(ax.Color,[0 1 0])
Verification passed.

Callback Function

This code shows the callback function used in this example. The function queries the VerticalScrollCount property of event to identify the scroll direction.

function changeColor(src,event)
if event.VerticalScrollCount < 0    % scrolling up
    src.Children.Color = "red";
elseif event.VerticalScrollCount > 0    % scrolling down
    src.Children.Color = "green";
end
end

Version History

Introduced in R2024a