|
| 1 | +const themesToTest = ['light', 'dark', 'light-highcontrast', 'dark-highcontrast'] |
| 2 | + |
| 3 | +const testCases = { |
| 4 | + 'Main text': { |
| 5 | + foregroundColors: [ |
| 6 | + 'color-main-text', |
| 7 | + // 'color-text-light', deprecated |
| 8 | + // 'color-text-lighter', deprecated |
| 9 | + 'color-text-maxcontrast', |
| 10 | + ], |
| 11 | + backgroundColors: [ |
| 12 | + 'color-main-background', |
| 13 | + 'color-background-hover', |
| 14 | + 'color-background-dark', |
| 15 | + // 'color-background-darker', this should only be used for elements not for text |
| 16 | + ], |
| 17 | + }, |
| 18 | + 'blurred background': { |
| 19 | + foregroundColors: [ |
| 20 | + 'color-main-text', |
| 21 | + 'color-text-maxcontrast-blur', |
| 22 | + ], |
| 23 | + backgroundColors: [ |
| 24 | + 'color-main-background-blur', |
| 25 | + ], |
| 26 | + }, |
| 27 | + Primary: { |
| 28 | + foregroundColors: [ |
| 29 | + 'color-primary-text', |
| 30 | + ], |
| 31 | + backgroundColors: [ |
| 32 | + // 'color-primary-default', this should only be used for elements not for text! |
| 33 | + // 'color-primary-hover', this should only be used for elements and not for text! |
| 34 | + 'color-primary', |
| 35 | + ], |
| 36 | + }, |
| 37 | + 'Primary light': { |
| 38 | + foregroundColors: [ |
| 39 | + 'color-primary-light-text', |
| 40 | + ], |
| 41 | + backgroundColors: [ |
| 42 | + 'color-primary-light', |
| 43 | + 'color-primary-light-hover', |
| 44 | + ], |
| 45 | + }, |
| 46 | + 'Primary element': { |
| 47 | + foregroundColors: [ |
| 48 | + 'color-primary-element-text', |
| 49 | + 'color-primary-element-text-dark', |
| 50 | + ], |
| 51 | + backgroundColors: [ |
| 52 | + 'color-primary-element', |
| 53 | + 'color-primary-element-hover', |
| 54 | + ], |
| 55 | + }, |
| 56 | + 'Primary element light': { |
| 57 | + foregroundColors: [ |
| 58 | + 'color-primary-element-light-text', |
| 59 | + ], |
| 60 | + backgroundColors: [ |
| 61 | + 'color-primary-element-light', |
| 62 | + 'color-primary-element-light-hover', |
| 63 | + ], |
| 64 | + }, |
| 65 | + 'Servity information texts': { |
| 66 | + foregroundColors: [ |
| 67 | + 'color-error-text', |
| 68 | + 'color-warning-text', |
| 69 | + 'color-success-text', |
| 70 | + 'color-info-text', |
| 71 | + ], |
| 72 | + backgroundColors: [ |
| 73 | + 'color-main-background', |
| 74 | + 'color-background-hover', |
| 75 | + 'color-main-background-blur', |
| 76 | + ], |
| 77 | + }, |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Create a wrapper element with color and background set |
| 82 | + * |
| 83 | + * @param foreground The foreground color (css variable without leading --) |
| 84 | + * @param background The background color |
| 85 | + */ |
| 86 | +function createTestCase(foreground: string, background: string) { |
| 87 | + const wrapper = document.createElement('div') |
| 88 | + wrapper.style.padding = '14px' |
| 89 | + wrapper.style.color = `var(--${foreground})` |
| 90 | + wrapper.style.backgroundColor = `var(--${background})` |
| 91 | + if (background.includes('blur')) { |
| 92 | + wrapper.style.backdropFilter = 'var(--filter-background-blur)' |
| 93 | + } |
| 94 | + |
| 95 | + const testCase = document.createElement('div') |
| 96 | + testCase.innerText = `${foreground} ${background}` |
| 97 | + testCase.setAttribute('data-cy-testcase', '') |
| 98 | + |
| 99 | + wrapper.appendChild(testCase) |
| 100 | + return wrapper |
| 101 | +} |
| 102 | + |
| 103 | +describe('Accessibility of Nextcloud theming colors', () => { |
| 104 | + for (const theme of themesToTest) { |
| 105 | + context(`Theme: ${theme}`, () => { |
| 106 | + before(() => { |
| 107 | + cy.createRandomUser().then(($user) => { |
| 108 | + // set user theme |
| 109 | + cy.runOccCommand(`user:setting -- '${$user.userId}' theming enabled-themes '["${theme}"]'`) |
| 110 | + cy.login($user) |
| 111 | + cy.visit('/') |
| 112 | + cy.injectAxe({ axeCorePath: 'node_modules/axe-core/axe.min.js' }) |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + cy.document().then(doc => { |
| 118 | + // Unset background image and thus use background-color for testing blur background (images do not work with axe-core) |
| 119 | + doc.body.style.backgroundImage = 'unset' |
| 120 | + |
| 121 | + const root = doc.querySelector('main') |
| 122 | + if (root === null) { |
| 123 | + throw new Error('No test root found') |
| 124 | + } |
| 125 | + root.innerHTML = '' |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + for (const [name, { backgroundColors, foregroundColors }] of Object.entries(testCases)) { |
| 130 | + context(`Accessibility of CSS color variables for ${name}`, () => { |
| 131 | + for (const foreground of foregroundColors) { |
| 132 | + for (const background of backgroundColors) { |
| 133 | + it(`color contrast of ${foreground} on ${background}`, () => { |
| 134 | + cy.document().then(doc => { |
| 135 | + const element = createTestCase(foreground, background) |
| 136 | + const root = doc.querySelector('main') |
| 137 | + // eslint-disable-next-line no-unused-expressions |
| 138 | + expect(root).not.to.be.undefined |
| 139 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 140 | + root!.appendChild(element) |
| 141 | + |
| 142 | + cy.checkA11y('[data-cy-testcase]', { |
| 143 | + runOnly: ['color-contrast'], |
| 144 | + }) |
| 145 | + }) |
| 146 | + }) |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +}) |
0 commit comments