1+ classdef BaseTest < matlab .uitest .TestCase
2+ % Implements a unit test with some added helper methods
3+
4+ % Copyright 2020-2025 The MathWorks Inc.
5+
6+
7+ %% Protected Helper Methods
8+ methods (Access = protected )
9+
10+ function pos = findPreferredPosition(~)
11+ % Callback when a button is pressed
12+
13+ % Position it off the non-primary monitor if possible
14+ % This gets it away from the editor when writing tests
15+ monitorPositions = get(0 , ' MonitorPositions' );
16+
17+ if isempty(monitorPositions )
18+
19+ % Shouldn't happen, but just in case
20+ % Leave space for taskbar
21+ pos = [1 100 ];
22+
23+ elseif size(monitorPositions ,1 ) == 1
24+
25+ % Primary monitor
26+ monIdx = 1 ;
27+
28+ % Get position
29+ % Leave space for taskbar
30+ pos = monitorPositions(monIdx , 1 : 2 ) + [1 100 ];
31+
32+ else
33+ % Secondary monitors available
34+
35+ % Find the primary monitor
36+ isPrimary = all(monitorPositions(: ,1 : 2 ) == [1 1 ], 2 );
37+
38+ % If multiple non-primary, choose the last one
39+ monIdx = find(~isPrimary ,1 ,' last' );
40+
41+ % If none found, revert to the primary
42+ if isempty(monIdx )
43+ monIdx = 1 ;
44+ end
45+
46+ % Use the lower-left corner of the selected monitor
47+ % Leave space for taskbar
48+
49+ % Get position
50+ pos = monitorPositions(monIdx , 1 : 2 ) + [1 100 ];
51+
52+ end % if
53+
54+ end % function
55+
56+
57+ function assumeMinimumRelease(testCase , releaseName )
58+ % Callback when a button is pressed
59+
60+ isUnsupported = isMATLABReleaseOlderThan(releaseName );
61+ diag = " Release not supported." ;
62+ testCase .assumeFalse(isUnsupported , diag )
63+
64+ end % function
65+
66+
67+ function verifyEquality(testCase , actualValue , expValue )
68+
69+ if ischar(expValue ) || isStringScalar(expValue )
70+ testCase .verifyTrue( all(strcmp(actualValue , expValue )) );
71+ elseif isa(actualValue ,' matlab.lang.OnOffSwitchState' )
72+ testCase .verifyTrue( all(actualValue == expValue ) );
73+ else
74+ testCase .verifyEqual(actualValue , expValue );
75+ end
76+
77+ end % function
78+
79+
80+ function verifyVisible(testCase , component )
81+ % Verify the specified component is set to visible
82+
83+ arguments
84+ testCase matlab.uitest.TestCase
85+ component
86+ end
87+
88+ import matlab .unittest .constraints .Eventually
89+ import matlab .unittest .constraints .IsTrue
90+
91+ % Verify values
92+ testCase .verifyThat(...
93+ @()logical(component .Visible ),...
94+ Eventually(IsTrue , " WithTimeoutOf" , 5 ));
95+
96+ end % function
97+
98+
99+ function verifyNotVisible(testCase , component )
100+ % Verify the specified component is set to not visible
101+
102+ arguments
103+ testCase matlab.uitest.TestCase
104+ component
105+ end
106+
107+ import matlab .unittest .constraints .Eventually
108+ import matlab .unittest .constraints .IsFalse
109+
110+ % Verify values
111+ testCase .verifyThat(...
112+ @()logical(component .Visible ),...
113+ Eventually(IsFalse , " WithTimeoutOf" , 5 ));
114+
115+ end % function
116+
117+
118+ function verifyEnabled(testCase , component )
119+ % Verify the specified component is set to enabled
120+
121+ arguments
122+ testCase matlab.uitest.TestCase
123+ component
124+ end
125+
126+ import matlab .unittest .constraints .Eventually
127+ import matlab .unittest .constraints .IsTrue
128+
129+ % Verify values
130+ testCase .verifyThat(...
131+ @()logical(component .Enable ),...
132+ Eventually(IsTrue , " WithTimeoutOf" , 5 ));
133+
134+ end % function
135+
136+
137+ function verifyNotEnabled(testCase , component )
138+ % Verify the specified component is set to not enabled
139+
140+ arguments
141+ testCase matlab.uitest.TestCase
142+ component
143+ end
144+
145+ import matlab .unittest .constraints .Eventually
146+ import matlab .unittest .constraints .IsFalse
147+
148+ % Verify values
149+ testCase .verifyThat(...
150+ @()logical(component .Enable ),...
151+ Eventually(IsFalse , " WithTimeoutOf" , 5 ));
152+
153+ end % function
154+
155+
156+ function verifyPixelSize(testCase , component , expSize )
157+ % Verify the specified component has the specified size
158+
159+ arguments
160+ testCase matlab.uitest.TestCase
161+ component
162+ expSize (1 ,2 ) double
163+ end
164+
165+ import matlab .unittest .constraints .Eventually
166+ import matlab .unittest .constraints .IsEqualTo
167+
168+ % Verify values
169+ testCase .verifyThat(...
170+ @()getPixelSize(component ),...
171+ Eventually(IsEqualTo(expSize ), " WithTimeoutOf" , 5 ));
172+
173+ function sz = getPixelSize(comp )
174+
175+ pos = getpixelposition(comp );
176+ sz = pos(3 : 4 );
177+
178+ end
179+
180+ end % function
181+
182+
183+ function verifyPropertyValue(testCase , component , property , expValue )
184+ % Verify the specified component's property eventually is equal
185+ % to expValue
186+
187+ arguments
188+ testCase matlab.uitest.TestCase
189+ component (1 ,1 ) matlab.graphics.Graphics
190+ property (1 ,1 ) string
191+ expValue
192+ end
193+
194+ import matlab .unittest .constraints .Eventually
195+ import matlab .unittest .constraints .IsEqualTo
196+
197+ % Verify values
198+ testCase .verifyThat(...
199+ @()get(component , property ),...
200+ Eventually(IsEqualTo(expValue ), " WithTimeoutOf" , 5 ));
201+
202+ end % function
203+
204+ end % methods
205+
206+ end % classdef
0 commit comments