|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Lars Vogel and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Lars Vogel - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.e4.ui.workbench.renderers.swt; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import jakarta.annotation.PostConstruct; |
| 23 | +import jakarta.inject.Inject; |
| 24 | + |
| 25 | +import org.eclipse.e4.ui.model.application.MApplication; |
| 26 | +import org.eclipse.e4.ui.model.application.ui.basic.MTrimBar; |
| 27 | +import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow; |
| 28 | +import org.eclipse.e4.ui.model.application.ui.menu.MToolControl; |
| 29 | +import org.eclipse.e4.ui.tests.rules.WorkbenchContextExtension; |
| 30 | +import org.eclipse.e4.ui.workbench.IPresentationEngine; |
| 31 | +import org.eclipse.e4.ui.workbench.modeling.EModelService; |
| 32 | +import org.eclipse.swt.SWT; |
| 33 | +import org.eclipse.swt.widgets.Composite; |
| 34 | +import org.eclipse.swt.widgets.Control; |
| 35 | +import org.eclipse.swt.widgets.Label; |
| 36 | +import org.junit.jupiter.api.BeforeEach; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 39 | + |
| 40 | +public class ToolControlRendererTest { |
| 41 | + |
| 42 | + private static final String BUNDLE_URI = "bundleclass://org.eclipse.e4.ui.tests/" |
| 43 | + + ToolControlRendererTest.class.getName() + "$"; |
| 44 | + |
| 45 | + @RegisterExtension |
| 46 | + public WorkbenchContextExtension contextRule = new WorkbenchContextExtension(); |
| 47 | + |
| 48 | + @Inject |
| 49 | + private EModelService ems; |
| 50 | + |
| 51 | + @Inject |
| 52 | + private MApplication application; |
| 53 | + |
| 54 | + private MTrimmedWindow window; |
| 55 | + private MTrimBar trimBar; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setUp() { |
| 59 | + window = ems.createModelElement(MTrimmedWindow.class); |
| 60 | + application.getChildren().add(window); |
| 61 | + |
| 62 | + trimBar = ems.createModelElement(MTrimBar.class); |
| 63 | + window.getTrimBars().add(trimBar); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void testWidgetCreated() { |
| 68 | + MToolControl toolControl = createToolControl("SimpleControl"); |
| 69 | + trimBar.getChildren().add(toolControl); |
| 70 | + |
| 71 | + contextRule.createAndRunWorkbench(window); |
| 72 | + |
| 73 | + assertNotNull(toolControl.getWidget(), "Widget must be created for a MToolControl"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void testNoChildren_widgetIsNull() { |
| 78 | + MToolControl toolControl = createToolControl("EmptyControl"); |
| 79 | + trimBar.getChildren().add(toolControl); |
| 80 | + |
| 81 | + contextRule.createAndRunWorkbench(window); |
| 82 | + |
| 83 | + assertNull(toolControl.getWidget(), |
| 84 | + "Widget must be null when the contribution creates no SWT children"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testHiddenExplicitly_hidesControl() { |
| 89 | + MToolControl toolControl = createToolControl("SimpleControl"); |
| 90 | + trimBar.getChildren().add(toolControl); |
| 91 | + |
| 92 | + contextRule.createAndRunWorkbench(window); |
| 93 | + |
| 94 | + assertTrue(toolControl.isVisible(), "Control should be visible initially"); |
| 95 | + |
| 96 | + toolControl.getTags().add(IPresentationEngine.HIDDEN_EXPLICITLY); |
| 97 | + contextRule.spinEventLoop(); |
| 98 | + |
| 99 | + assertFalse(toolControl.isVisible(), "Control should be hidden after HIDDEN_EXPLICITLY tag is added"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testHiddenExplicitly_removeTag_restoresVisibility() { |
| 104 | + MToolControl toolControl = createToolControl("SimpleControl"); |
| 105 | + trimBar.getChildren().add(toolControl); |
| 106 | + |
| 107 | + contextRule.createAndRunWorkbench(window); |
| 108 | + |
| 109 | + toolControl.getTags().add(IPresentationEngine.HIDDEN_EXPLICITLY); |
| 110 | + contextRule.spinEventLoop(); |
| 111 | + assertFalse(toolControl.isVisible()); |
| 112 | + |
| 113 | + toolControl.getTags().remove(IPresentationEngine.HIDDEN_EXPLICITLY); |
| 114 | + contextRule.spinEventLoop(); |
| 115 | + |
| 116 | + assertTrue(toolControl.isVisible(), "Visibility must be restored after HIDDEN_EXPLICITLY tag is removed"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void testHideableControls_haveIndependentMenus() { |
| 121 | + MToolControl tc1 = createToolControl("SimpleControl"); |
| 122 | + tc1.getTags().add("HIDEABLE"); |
| 123 | + trimBar.getChildren().add(tc1); |
| 124 | + |
| 125 | + MToolControl tc2 = createToolControl("SimpleControl"); |
| 126 | + tc2.getTags().add("HIDEABLE"); |
| 127 | + trimBar.getChildren().add(tc2); |
| 128 | + |
| 129 | + contextRule.createAndRunWorkbench(window); |
| 130 | + |
| 131 | + Control widget1 = (Control) tc1.getWidget(); |
| 132 | + Control widget2 = (Control) tc2.getWidget(); |
| 133 | + |
| 134 | + assertNotNull(widget1.getMenu(), "First HIDEABLE control must have a context menu"); |
| 135 | + assertNotNull(widget2.getMenu(), "Second HIDEABLE control must have a context menu"); |
| 136 | + assertNotSame(widget1.getMenu(), widget2.getMenu(), |
| 137 | + "Each HIDEABLE control must have its own independent menu instance"); |
| 138 | + assertFalse(widget1.getMenu().isDisposed(), |
| 139 | + "First control's menu must not be disposed after second control is rendered"); |
| 140 | + } |
| 141 | + |
| 142 | + private MToolControl createToolControl(String innerClassName) { |
| 143 | + MToolControl tc = ems.createModelElement(MToolControl.class); |
| 144 | + tc.setContributionURI(BUNDLE_URI + innerClassName); |
| 145 | + return tc; |
| 146 | + } |
| 147 | + |
| 148 | + // --- Contribution classes used by the tests --- |
| 149 | + |
| 150 | + public static class SimpleControl { |
| 151 | + @PostConstruct |
| 152 | + public void create(Composite parent) { |
| 153 | + new Label(parent, SWT.NONE).setText("test"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public static class EmptyControl { |
| 158 | + @PostConstruct |
| 159 | + public void create(@SuppressWarnings("unused") Composite parent) { |
| 160 | + // intentionally creates no SWT children |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments