1
0

themingUtils.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. import { colord } from 'colord'
  6. export const defaultPrimary = '#00679e'
  7. export const defaultBackground = 'jenna-kim-the-globe.webp'
  8. /**
  9. * Check if a CSS variable is set to a specific color
  10. * @param variable Variable to check
  11. * @param expectedColor Color that is expected
  12. */
  13. export function validateCSSVariable(variable: string, expectedColor: string) {
  14. const value = window.getComputedStyle(Cypress.$('body').get(0)).getPropertyValue(variable)
  15. console.debug(`${variable}, is: ${colord(value).toHex()} expected: ${expectedColor}`)
  16. return colord(value).isEqual(expectedColor)
  17. }
  18. /**
  19. * Validate the current page body css variables
  20. *
  21. * @param {string} expectedColor the expected primary color
  22. * @param {string|null} expectedBackground the expected background
  23. * @param {string|null} expectedBackgroundColor the expected background color (null to ignore)
  24. */
  25. export function validateBodyThemingCss(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground, expectedBackgroundColor: string|null = defaultPrimary) {
  26. // We must use `Cypress.$` here as any assertions (get is an assertion) is not allowed in wait-until's check function, see documentation
  27. const guestBackgroundColor = Cypress.$('body').css('background-color')
  28. const guestBackgroundImage = Cypress.$('body').css('background-image')
  29. const isValidBackgroundColor = expectedBackgroundColor === null || colord(guestBackgroundColor).isEqual(expectedBackgroundColor)
  30. const isValidBackgroundImage = !expectedBackground
  31. ? guestBackgroundImage === 'none'
  32. : guestBackgroundImage.includes(expectedBackground)
  33. console.debug({
  34. isValidBackgroundColor,
  35. isValidBackgroundImage,
  36. guestBackgroundColor: colord(guestBackgroundColor).toHex(),
  37. guestBackgroundImage,
  38. })
  39. return isValidBackgroundColor && isValidBackgroundImage && validateCSSVariable('--color-primary', expectedColor)
  40. }
  41. /**
  42. * Check background color of element
  43. * @param element JQuery element to check
  44. * @param color expected color
  45. */
  46. export function expectBackgroundColor(element: JQuery<HTMLElement>, color: string) {
  47. expect(colord(element.css('background-color')).toHex()).equal(colord(color).toHex())
  48. }
  49. /**
  50. * Validate the user theming default select option css
  51. *
  52. * @param {string} expectedColor the expected color
  53. * @param {string} expectedBackground the expected background
  54. */
  55. export const validateUserThemingDefaultCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
  56. const defaultSelectButton = Cypress.$('[data-user-theming-background-default]')
  57. if (defaultSelectButton.length === 0) {
  58. return false
  59. }
  60. const backgroundImage = defaultSelectButton.css('background-image')
  61. const backgroundColor = defaultSelectButton.css('background-color')
  62. const isValidBackgroundImage = !expectedBackground
  63. ? (backgroundImage === 'none' || Cypress.$('body').css('background-image') === 'none')
  64. : backgroundImage.includes(expectedBackground)
  65. console.debug({
  66. colorPickerOptionColor: colord(backgroundColor).toHex(),
  67. expectedColor,
  68. isValidBackgroundImage,
  69. backgroundImage,
  70. })
  71. return isValidBackgroundImage && colord(backgroundColor).isEqual(expectedColor)
  72. }
  73. export const pickRandomColor = function(context: string, index?: number): Cypress.Chainable<string> {
  74. // Pick one of the first 8 options
  75. const randColour = index ?? Math.floor(Math.random() * 8)
  76. const colorPreviewSelector = `${context} [data-admin-theming-setting-color]`
  77. let oldColor = ''
  78. cy.get(colorPreviewSelector).then(($el) => {
  79. oldColor = $el.css('background-color')
  80. })
  81. // Open picker
  82. cy.get(`${context} [data-admin-theming-setting-color-picker]`).scrollIntoView()
  83. cy.get(`${context} [data-admin-theming-setting-color-picker]`).click({ force: true })
  84. // Click on random color
  85. cy.get('.color-picker__simple-color-circle').eq(randColour).click()
  86. // Wait for color change
  87. cy.waitUntil(() => Cypress.$(colorPreviewSelector).css('background-color') !== oldColor)
  88. // Get the selected color from the color preview block
  89. return cy.get(colorPreviewSelector).then(($el) => $el.css('background-color'))
  90. }