|
| 1 | +// Copyright 2026 The Chromium Authors |
| 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 * as Platform from '../../core/platform/platform.js'; |
| 6 | +import {renderElementIntoDOM} from '../../testing/DOMHelpers.js'; |
| 7 | +import {describeWithEnvironment} from '../../testing/EnvironmentHelpers.js'; |
| 8 | +import * as Components from '../../ui/legacy/components/utils/utils.js'; |
| 9 | +import * as UI from '../../ui/legacy/legacy.js'; |
| 10 | + |
| 11 | +import * as Elements from './elements.js'; |
| 12 | + |
| 13 | +const {urlString} = Platform.DevToolsPath; |
| 14 | + |
| 15 | +describeWithEnvironment('ImagePreviewPopover', () => { |
| 16 | + let clock: sinon.SinonFakeTimers; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // UI.PopoverHelper.PopoverHelper uses setTimeout internally to manage popover |
| 20 | + // show and hide timing. Using fake timers allows us to control these |
| 21 | + // timeouts synchronously in the test without real-world delays or flakiness. |
| 22 | + clock = sinon.useFakeTimers(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + clock.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + /** |
| 30 | + * Periodically ticks the fake clock and flushes microtasks until the given |
| 31 | + * condition is met. This is used to wait for asynchronous work that is |
| 32 | + * triggered by timers (like PopoverHelper) or multiple await points. |
| 33 | + */ |
| 34 | + async function poll(condition: () => boolean): Promise<void> { |
| 35 | + for (let i = 0; i < 20; i++) { |
| 36 | + if (condition()) { |
| 37 | + return; |
| 38 | + } |
| 39 | + clock.tick(1); |
| 40 | + await Promise.resolve(); |
| 41 | + } |
| 42 | + throw new Error('Condition not met'); |
| 43 | + } |
| 44 | + |
| 45 | + it('shows an image preview when hovering over an element with an image URL', async () => { |
| 46 | + const container = document.createElement('div'); |
| 47 | + renderElementIntoDOM(container); |
| 48 | + |
| 49 | + const getLinkElement = (event: Event) => event.target as Element; |
| 50 | + const getNodeFeatures = async () => undefined; |
| 51 | + new Elements.ImagePreviewPopover.ImagePreviewPopover(container, getLinkElement, getNodeFeatures); |
| 52 | + |
| 53 | + const element = document.createElement('span'); |
| 54 | + element.textContent = 'hover me'; |
| 55 | + element.boxInWindow = () => new AnchorBox(0, 0, 10, 10); |
| 56 | + container.appendChild(element); |
| 57 | + |
| 58 | + const imageUrl = urlString`http://example.com/image.png`; |
| 59 | + Elements.ImagePreviewPopover.ImagePreviewPopover.setImageUrl(element, imageUrl); |
| 60 | + |
| 61 | + const buildStub = sinon.stub(Components.ImagePreview.ImagePreview, 'build').resolves(document.createElement('div')); |
| 62 | + |
| 63 | + const event = new MouseEvent('mousemove', { |
| 64 | + bubbles: true, |
| 65 | + cancelable: true, |
| 66 | + clientX: 5, |
| 67 | + clientY: 5, |
| 68 | + }); |
| 69 | + element.dispatchEvent(event); |
| 70 | + |
| 71 | + // Wait for the build stub to be called, which happens inside the async show() method |
| 72 | + // triggered after PopoverHelper's internal timer. |
| 73 | + await poll(() => buildStub.called); |
| 74 | + |
| 75 | + sinon.assert.calledWith(buildStub, imageUrl, true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not show a preview when hovering over an element without an image URL', async () => { |
| 79 | + const container = document.createElement('div'); |
| 80 | + renderElementIntoDOM(container); |
| 81 | + |
| 82 | + const getLinkElement = (event: Event) => event.target as Element; |
| 83 | + const getNodeFeatures = async () => undefined; |
| 84 | + new Elements.ImagePreviewPopover.ImagePreviewPopover(container, getLinkElement, getNodeFeatures); |
| 85 | + |
| 86 | + const element = document.createElement('span'); |
| 87 | + element.textContent = 'hover me'; |
| 88 | + element.boxInWindow = () => new AnchorBox(0, 0, 10, 10); |
| 89 | + container.appendChild(element); |
| 90 | + |
| 91 | + const buildStub = sinon.stub(Components.ImagePreview.ImagePreview, 'build').resolves(document.createElement('div')); |
| 92 | + |
| 93 | + const event = new MouseEvent('mousemove', { |
| 94 | + bubbles: true, |
| 95 | + cancelable: true, |
| 96 | + clientX: 5, |
| 97 | + clientY: 5, |
| 98 | + }); |
| 99 | + element.dispatchEvent(event); |
| 100 | + |
| 101 | + // Tick the clock and flush microtasks to ensure any scheduled work has a chance to run. |
| 102 | + clock.tick(1); |
| 103 | + await Promise.resolve(); |
| 104 | + |
| 105 | + sinon.assert.notCalled(buildStub); |
| 106 | + }); |
| 107 | + |
| 108 | + it('hides the popover when hide() is called', async () => { |
| 109 | + const container = document.createElement('div'); |
| 110 | + renderElementIntoDOM(container); |
| 111 | + |
| 112 | + const getLinkElement = (event: Event) => event.target as Element; |
| 113 | + const getNodeFeatures = async () => undefined; |
| 114 | + const imagePreviewPopover = |
| 115 | + new Elements.ImagePreviewPopover.ImagePreviewPopover(container, getLinkElement, getNodeFeatures); |
| 116 | + |
| 117 | + const element = document.createElement('span'); |
| 118 | + element.boxInWindow = () => new AnchorBox(0, 0, 10, 10); |
| 119 | + container.appendChild(element); |
| 120 | + |
| 121 | + const imageUrl = urlString`http://example.com/image.png`; |
| 122 | + Elements.ImagePreviewPopover.ImagePreviewPopover.setImageUrl(element, imageUrl); |
| 123 | + |
| 124 | + sinon.stub(Components.ImagePreview.ImagePreview, 'build').resolves(document.createElement('div')); |
| 125 | + const glassPaneShowStub = sinon.stub(UI.GlassPane.GlassPane.prototype, 'show'); |
| 126 | + const glassPaneHideStub = sinon.stub(UI.GlassPane.GlassPane.prototype, 'hide'); |
| 127 | + |
| 128 | + const event = new MouseEvent('mousemove', { |
| 129 | + bubbles: true, |
| 130 | + clientX: 5, |
| 131 | + clientY: 5, |
| 132 | + }); |
| 133 | + element.dispatchEvent(event); |
| 134 | + |
| 135 | + // Wait until the popover's show() method has been called. This ensures the |
| 136 | + // async chain in PopoverHelper has finished and the hidePopoverCallback is set. |
| 137 | + await poll(() => glassPaneShowStub.called); |
| 138 | + |
| 139 | + imagePreviewPopover.hide(); |
| 140 | + sinon.assert.called(glassPaneHideStub); |
| 141 | + }); |
| 142 | +}); |
0 commit comments