|
| 1 | +// Copyright 2023 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import {assert} from 'chai'; |
| 6 | + |
| 7 | +import { |
| 8 | + ELEMENTS_PANEL_SELECTOR, |
| 9 | + getStyleRule, |
| 10 | + goToResourceAndWaitForStyleSection, |
| 11 | + SECTION_SUBTITLE_SELECTOR, |
| 12 | + STYLE_PROPERTIES_SELECTOR, |
| 13 | +} from '../../e2e/helpers/elements-helpers.js'; |
| 14 | +import type {DevToolsPage} from '../../e2e_non_hosted/shared/frontend-helper.js'; |
| 15 | + |
| 16 | +async function getStyleRuleProperties(selector: string, count: number, devToolsPage: DevToolsPage) { |
| 17 | + const rule = await getStyleRule(selector, devToolsPage); |
| 18 | + const propertyElements = await devToolsPage.waitForMany(STYLE_PROPERTIES_SELECTOR, count, rule); |
| 19 | + const properties = await Promise.all(propertyElements.map(e => e.evaluate(e => e.textContent))); |
| 20 | + properties.sort(); |
| 21 | + const subtitle = await (await devToolsPage.waitFor(SECTION_SUBTITLE_SELECTOR, rule)).evaluate(e => e.textContent); |
| 22 | + |
| 23 | + return {properties, subtitle}; |
| 24 | +} |
| 25 | + |
| 26 | +describe('The styles pane', () => { |
| 27 | + it('shows syntax mismatches as invalid properties', async ({devToolsPage, inspectedPage}) => { |
| 28 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 29 | + await devToolsPage.waitFor('.invalid-property-value:has(> [aria-label="CSS property name: --my-color"])'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('shows a parser error message popover on syntax mismatches', async ({devToolsPage, inspectedPage}) => { |
| 33 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 34 | + await devToolsPage.hover( |
| 35 | + '.invalid-property-value:has(> [aria-label="CSS property name: --my-color"]) .exclamation-mark'); |
| 36 | + |
| 37 | + const popover = await devToolsPage.waitFor(':popover-open devtools-css-variable-parser-error'); |
| 38 | + const firstSection = await devToolsPage.waitFor('.variable-value-popup-wrapper', popover); |
| 39 | + const popoverContents = await firstSection.evaluate(e => e.deepInnerText()); |
| 40 | + |
| 41 | + assert.deepEqual(popoverContents, 'Invalid property value, expected type "<color>"\nView registered property'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('shows registered properties', async ({devToolsPage, inspectedPage}) => { |
| 45 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 46 | + assert.deepEqual(await getStyleRuleProperties('--my-color', 3, devToolsPage), { |
| 47 | + properties: [' inherits: false;', ' initial-value: red;', ' syntax: "<color>";'], |
| 48 | + subtitle: '<style>', |
| 49 | + }); |
| 50 | + assert.deepEqual(await getStyleRuleProperties('--my-color2', 3, devToolsPage), { |
| 51 | + properties: [' inherits: false;', ' initial-value: #c0ffee;', ' syntax: "<color>";'], |
| 52 | + subtitle: '<style>', |
| 53 | + }); |
| 54 | + assert.deepEqual(await getStyleRuleProperties('--my-cssom-color', 3, devToolsPage), { |
| 55 | + properties: [' inherits: false;', ' initial-value: orange;', ' syntax: "<color>";'], |
| 56 | + subtitle: 'CSS.registerProperty', |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('shows a foldable @property section when there are 5 or less registered properties', |
| 61 | + async ({devToolsPage, inspectedPage}) => { |
| 62 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 63 | + |
| 64 | + const stylesPane = await devToolsPage.waitFor('div.styles-pane'); |
| 65 | + { |
| 66 | + const section = await devToolsPage.waitForElementWithTextContent('@property', stylesPane); |
| 67 | + assert.deepEqual(await section.evaluate(e => e.ariaExpanded), 'true'); |
| 68 | + const rule = await getStyleRule('--my-color', devToolsPage); |
| 69 | + assert.isTrue(await rule.evaluate(e => !e.classList.contains('hidden'))); |
| 70 | + } |
| 71 | + |
| 72 | + { |
| 73 | + const section = await devToolsPage.click('pierceShadowText/@property', {root: stylesPane}); |
| 74 | + await devToolsPage.waitForFunction(async () => 'false' === await section.evaluate(e => e.ariaExpanded)); |
| 75 | + const rule = await getStyleRule('--my-color', devToolsPage); |
| 76 | + await devToolsPage.waitForFunction(() => rule.evaluate(e => e.classList.contains('hidden'))); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + it('shows a collapsed @property section when there are more than 5 registered properties', |
| 81 | + async ({devToolsPage, inspectedPage}) => { |
| 82 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 83 | + |
| 84 | + // Add some properties to go above the threshold |
| 85 | + await inspectedPage.evaluate(() => { |
| 86 | + for (let n = 0; n < 5; ++n) { |
| 87 | + CSS.registerProperty({name: `--custom-prop-${n}`, inherits: false, syntax: '<length>', initialValue: '0px'}); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + await devToolsPage.reload(); |
| 92 | + |
| 93 | + const stylesPane = await devToolsPage.waitFor('div.styles-pane'); |
| 94 | + { |
| 95 | + const section = await devToolsPage.waitForElementWithTextContent('@property', stylesPane); |
| 96 | + assert.deepEqual(await section.evaluate(e => e.ariaExpanded), 'false'); |
| 97 | + // Pick the style rule added last to ensure the sections are fully drawn |
| 98 | + const rule = await getStyleRule('--custom-prop-4', devToolsPage); |
| 99 | + assert.isTrue(await rule.evaluate(e => e.classList.contains('hidden'))); |
| 100 | + } |
| 101 | + |
| 102 | + await devToolsPage.waitForFunction(async () => { |
| 103 | + const section = await devToolsPage.click('pierceShadowText/@property', {root: stylesPane}); |
| 104 | + await devToolsPage.waitForFunction(async () => 'true' === await section.evaluate(e => e.ariaExpanded)); |
| 105 | + const rule = await getStyleRule('--custom-prop-4', devToolsPage); |
| 106 | + return await rule.evaluate(e => !e.classList.contains('hidden')); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('shows registration information in a variable popover', async ({devToolsPage, inspectedPage}) => { |
| 111 | + async function hoverVariable(label: string, devToolsPage: DevToolsPage) { |
| 112 | + const isValue = label.startsWith('var('); |
| 113 | + if (isValue) { |
| 114 | + const prop = await devToolsPage.waitForAria(`CSS property value: ${label}`); |
| 115 | + await devToolsPage.hover('.link-swatch-link', {root: prop}); |
| 116 | + } else { |
| 117 | + await devToolsPage.hover(`aria/CSS property name: ${label}`); |
| 118 | + } |
| 119 | + |
| 120 | + const popover = await devToolsPage.waitFor(':popover-open devtools-css-variable-value-view'); |
| 121 | + const popoverContents = await popover.evaluate(e => { |
| 122 | + return e.deepInnerText(); |
| 123 | + }); |
| 124 | + |
| 125 | + await devToolsPage.hover(ELEMENTS_PANEL_SELECTOR); |
| 126 | + await devToolsPage.waitForNone(':popover-open devtools-css-variable-value-view'); |
| 127 | + |
| 128 | + return popoverContents; |
| 129 | + } |
| 130 | + await goToResourceAndWaitForStyleSection('elements/at-property.html', devToolsPage, inspectedPage); |
| 131 | + |
| 132 | + assert.strictEqual( |
| 133 | + await hoverVariable('var(--my-cssom-color)', devToolsPage), |
| 134 | + 'orange\nsyntax: "<color>"\ninherits: false\ninitial-value: orange\nView registered property'); |
| 135 | + |
| 136 | + assert.strictEqual( |
| 137 | + await hoverVariable('--my-color', devToolsPage), |
| 138 | + 'red\nsyntax: "<color>"\ninherits: false\ninitial-value: red\nView registered property'); |
| 139 | + assert.strictEqual( |
| 140 | + await hoverVariable('var(--my-color)', devToolsPage), |
| 141 | + 'red\nsyntax: "<color>"\ninherits: false\ninitial-value: red\nView registered property'); |
| 142 | + |
| 143 | + assert.strictEqual( |
| 144 | + await hoverVariable('--my-color2', devToolsPage), |
| 145 | + 'gray\nsyntax: "<color>"\ninherits: false\ninitial-value: #c0ffee\nView registered property'); |
| 146 | + assert.strictEqual( |
| 147 | + await hoverVariable('var(--my-color2)', devToolsPage), |
| 148 | + 'gray\nsyntax: "<color>"\ninherits: false\ninitial-value: #c0ffee\nView registered property'); |
| 149 | + |
| 150 | + assert.strictEqual(await hoverVariable('--my-other-color', devToolsPage), 'green'); |
| 151 | + assert.strictEqual(await hoverVariable('var(--my-other-color)', devToolsPage), 'green'); |
| 152 | + }); |
| 153 | +}); |
0 commit comments