1
0

themingUtils.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { colord } from 'colord'
  23. const defaultNextcloudBlue = '#0082c9'
  24. export const defaultPrimary = '#00679e'
  25. export const defaultBackground = 'kamil-porembinski-clouds.jpg'
  26. /**
  27. * Validate the current page body css variables
  28. *
  29. * @param {string} expectedColor the expected color
  30. * @param {string|null} expectedBackground the expected background
  31. */
  32. export const validateBodyThemingCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
  33. // We must use `Cypress.$` here as any assertions (get is an assertion) is not allowed in wait-until's check function, see documentation
  34. const guestBackgroundColor = Cypress.$('body').css('background-color')
  35. const guestBackgroundImage = Cypress.$('body').css('background-image')
  36. const isValidBackgroundColor = colord(guestBackgroundColor).isEqual(expectedColor)
  37. const isValidBackgroundImage = !expectedBackground
  38. ? guestBackgroundImage === 'none'
  39. : guestBackgroundImage.includes(expectedBackground)
  40. console.debug({ guestBackgroundColor: colord(guestBackgroundColor).toHex(), guestBackgroundImage, expectedColor, expectedBackground, isValidBackgroundColor, isValidBackgroundImage })
  41. return isValidBackgroundColor && isValidBackgroundImage
  42. }
  43. /**
  44. * Validate the user theming default select option css
  45. *
  46. * @param {string} expectedColor the expected color
  47. * @param {string} expectedBackground the expected background
  48. */
  49. export const validateUserThemingDefaultCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
  50. const defaultSelectButton = Cypress.$('[data-user-theming-background-default]')
  51. if (defaultSelectButton.length === 0) {
  52. return false
  53. }
  54. const defaultOptionBackground = defaultSelectButton.css('background-image')
  55. const colorPickerOptionColor = defaultSelectButton.css('background-color')
  56. const isNextcloudBlue = colord(colorPickerOptionColor).isEqual('#0082c9')
  57. const isValidBackgroundImage = !expectedBackground
  58. ? defaultOptionBackground === 'none'
  59. : defaultOptionBackground.includes(expectedBackground)
  60. console.debug({ colorPickerOptionColor: colord(colorPickerOptionColor).toHex(), expectedColor, isValidBackgroundImage, isNextcloudBlue })
  61. return isValidBackgroundImage && (
  62. colord(colorPickerOptionColor).isEqual(expectedColor)
  63. // we replace nextcloud blue with the the default rpimary (apps/theming/lib/Themes/DefaultTheme.php line 76)
  64. || (isNextcloudBlue && colord(expectedColor).isEqual(defaultPrimary))
  65. )
  66. }
  67. export const pickRandomColor = function(): Cypress.Chainable<string> {
  68. // Pick one of the first 8 options
  69. const randColour = Math.floor(Math.random() * 8)
  70. const colorPreviewSelector = '[data-user-theming-background-color],[data-admin-theming-setting-primary-color]'
  71. let oldColor = ''
  72. cy.get(colorPreviewSelector).then(($el) => {
  73. oldColor = $el.css('background-color')
  74. })
  75. // Open picker
  76. cy.contains('button', 'Change color').click()
  77. // Click on random color
  78. cy.get('.color-picker__simple-color-circle').eq(randColour).click()
  79. // Wait for color change
  80. cy.waitUntil(() => Cypress.$(colorPreviewSelector).css('background-color') !== oldColor)
  81. // Get the selected color from the color preview block
  82. return cy.get(colorPreviewSelector).then(($el) => $el.css('background-color'))
  83. }