diff --git a/code/+matbox/+setup/+internal/installFexPackage.m b/code/+matbox/+setup/+internal/installFexPackage.m index eef2f47..4730ebc 100644 --- a/code/+matbox/+setup/+internal/installFexPackage.m +++ b/code/+matbox/+setup/+internal/installFexPackage.m @@ -105,8 +105,17 @@ end elseif endsWith(addonUrl, '/mltbx') - [tempFilepath, C] = matbox.setup.internal.utility.tempsave(addonUrl, [toolboxIdentifier, '_temp.mltbx']); isLicenseOptionSupported = isMATLABReleaseOlderThan("R2026b"); + if isLicenseOptionSupported && ~isLicenseAgreed && ~canShowLicenseDialog() + fprintf(newline) + error("MatBox:Setup:LicenseDialogUnavailable", ... + ['Installing "%s" shows its license agreement in a dialog, and ', ... + 'this MATLAB session cannot display one. Pass AgreeToLicense=true ', ... + '(AgreeToLicenses=true in installRequirements) to accept the ', ... + 'license without the dialog.'], toolboxName) + end + + [tempFilepath, C] = matbox.setup.internal.utility.tempsave(addonUrl, [toolboxIdentifier, '_temp.mltbx']); if isLicenseOptionSupported installedAddon = matlab.addons.install(tempFilepath, isLicenseAgreed); else @@ -142,6 +151,17 @@ end end +function tf = canShowLicenseDialog() +% canShowLicenseDialog - Whether this session can display the add-on license dialog +% +% Before R2026b, matlab.addons.install shows the license agreement of an +% .mltbx add-on in a Java Swing dialog unless agreeToLicense is true. +% Java is headless when there is no display, as on a CI runner, and +% creating the dialog then throws java.awt.HeadlessException. + + tf = usejava("jvm") && ~java.awt.GraphicsEnvironment.isHeadless(); +end + function toolboxName = retrieveToolboxName(toolboxIdentifier) fex = matlab.addons.repositories.FileExchangeRepository(); diff --git a/tools/tests/+matboxtools/+unittest/SetupTest.m b/tools/tests/+matboxtools/+unittest/SetupTest.m index aca8cbe..a8996d9 100644 --- a/tools/tests/+matboxtools/+unittest/SetupTest.m +++ b/tools/tests/+matboxtools/+unittest/SetupTest.m @@ -64,6 +64,18 @@ function testOmittedAgreeToLicenseDoesNotWarn(testCase) [~, lastWarningId] = lastwarn(); testCase.verifyNotEqual(lastWarningId, 'MatBox:Setup:AgreeToLicenseIgnored') end + + function testOmittedAgreeToLicenseWithoutDisplayErrors(testCase) + testCase.assumeTrue(isMATLABReleaseOlderThan("R2026b"), ... + "From R2026b MATLAB installs an add-on without a license dialog.") + testCase.assumeTrue(usejava("jvm") && java.awt.GraphicsEnvironment.isHeadless(), ... + "This session can display the license dialog.") + testCase.uninstallFexAddon(testCase.FexMltbxUri) + + testCase.verifyError( ... + @() matbox.setup.installFromSourceUri(testCase.FexMltbxUri, "InstallationLocation", pwd), ... + "MatBox:Setup:LicenseDialogUnavailable") + end end methods @@ -88,11 +100,16 @@ function installRequirements(testCase, options) pathStr = matboxtools.projectdir(); requirementsPath = fullfile(pathStr, "tools", "tests", "test_resources"); + % The requirements include an .mltbx add-on. Before R2026b, + % installing one without accepting its license shows a dialog, + % which a session without a display, such as CI, cannot show. try if options.Update - matbox.installRequirements(requirementsPath, "u", "InstallationLocation", pwd) + matbox.installRequirements(requirementsPath, "u", ... + "InstallationLocation", pwd, "AgreeToLicenses", true) else - matbox.installRequirements(requirementsPath, "InstallationLocation", pwd) + matbox.installRequirements(requirementsPath, ... + "InstallationLocation", pwd, "AgreeToLicenses", true) end catch ME testCase.verifyFail(ME.message)