From 4dd1092b29dc33f1615fa78959535a523d24f0af Mon Sep 17 00:00:00 2001 From: Enda O Brien Date: Fri, 20 Sep 2024 17:52:36 +0100 Subject: [PATCH] 2 failing tests to demonstrate issue #345 ExtendedMarkersView .. Limit #346 Squashed commit of the following: commit 03b85b8753361c7678753a1972360351249524f6 Merge: 693a6f14de c40b7a177e Author: eobrienPilz <58333578+eobrienPilz@users.noreply.github.com> Date: Mon Mar 25 12:31:05 2024 +0000 Merge branch 'master' into 345_ViewerFilterWithLimit commit 693a6f14de1c59da072a4fd515b98996535a3d9a Author: Enda O Brien Date: Tue Sep 20 13:01:09 2022 +0100 2 failing tests to demonstrate issue #345 ExtendedMarkersView .. Limit --- .../ui/tests/internal/InternalTestSuite.java | 2 + .../markers/LimitAndViewerFilterTest.java | 232 ++++++++++++++++++ .../markers/RedProblemMarkerViewView.java | 91 +++++++ tests/org.eclipse.ui.tests/plugin.xml | 5 + 4 files changed, 330 insertions(+) create mode 100644 tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/LimitAndViewerFilterTest.java create mode 100644 tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/RedProblemMarkerViewView.java diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/InternalTestSuite.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/InternalTestSuite.java index d0b5f349c67..056a88fbe20 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/InternalTestSuite.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/InternalTestSuite.java @@ -18,6 +18,7 @@ import org.eclipse.ui.tests.markers.Bug75909Test; import org.eclipse.ui.tests.markers.DeclarativeFilterActivityTest; import org.eclipse.ui.tests.markers.DeclarativeFilterDeclarationTest; +import org.eclipse.ui.tests.markers.LimitAndViewerFilterTest; import org.eclipse.ui.tests.markers.MarkerHelpRegistryReaderTest; import org.eclipse.ui.tests.markers.MarkerHelpRegistryTest; import org.eclipse.ui.tests.markers.MarkerQueryTest; @@ -68,5 +69,6 @@ LargeFileLimitsPreferenceHandlerTest.class, WorkbookEditorsHandlerTest.class, ScopeAreaTest.class, + LimitAndViewerFilterTest.class, }) public class InternalTestSuite {} diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/LimitAndViewerFilterTest.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/LimitAndViewerFilterTest.java new file mode 100644 index 00000000000..e9cc233afc3 --- /dev/null +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/LimitAndViewerFilterTest.java @@ -0,0 +1,232 @@ +/******************************************************************************* + * Copyright (c) 2022 Enda O'Brien and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which accompanies this distribution, + * and is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: IBM Corporation - initial API and implementation + *******************************************************************************/ + +package org.eclipse.ui.tests.markers; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.widgets.TreeItem; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.internal.ide.IDEInternalPreferences; +import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; +import org.eclipse.ui.tests.harness.util.UITestCase; +import org.eclipse.ui.views.markers.MarkerItem; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * + * @since 3.5 + * + * Test that both the Content provider and TreeViewer for + * ExtendedMarkersView are giving consistent results when applying a + * ViewerFilter and marker limit. + * + */ +@RunWith(JUnit4.class) +public class LimitAndViewerFilterTest extends UITestCase { + + /** + * @param testName + */ + public LimitAndViewerFilterTest() { + super(LimitAndViewerFilterTest.class.getSimpleName()); + } + + final static int MARKER_LIMIT = 5, RED_MARKER_COUNT = 4, BLUE_MARKER_COUNT = 4; + public static final String MARKER_COLOR_BLUE = "BLUE"; + + /** + * Test set up with - A marker limit set to 5. - creates 4 red and 4 blue + * markers in the workspace. + * + */ + @Before + public void before() { + // Enable Marker limit and set to 5 + IPreferenceStore preferenceStore = IDEWorkbenchPlugin.getDefault().getPreferenceStore(); + preferenceStore.setValue(IDEInternalPreferences.USE_MARKER_LIMITS, true); + preferenceStore.setValue(IDEInternalPreferences.MARKER_LIMITS_VALUE, MARKER_LIMIT); + + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + + // create 4 red and 4 blue markers + try { + createMarkers(workspace, RED_MARKER_COUNT, RedProblemMarkerViewView.MARKER_COLOR_RED); + createMarkers(workspace, BLUE_MARKER_COUNT, MARKER_COLOR_BLUE); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + @After + public void after() { + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + deleteMarkers(workspace); + } + + /** + * + * Test using a view that only displays red markers. The expected result is that + * the TreeViewer should contain the 4 red markers and no blue markers. + * + * @throws Exception + */ + + @Test + public void expectTreeViewerToContain4Markers() throws Exception { + + // Open the Red marker view and expand the view + openView(); + expandView(); + // As the limit is 5, Expect that 4 red markers makes it into the tree + // viewer + validateViewerMarkerCounts(RED_MARKER_COUNT); + } + + /** + * Test using a view that only displays red markers. The expected result is that + * the Content provider should display the 4 red markers and no blue markers. + * + * @throws Exception + */ + + @Test + public void expectContentProviderToContainOnlyRedMarkers() throws Exception { + // Open the Red marker view and expand the view + openView(); + expandView(); + // Expect that only red markers makes it into the tree viewer + validateContentProviderMarkerColors(RedProblemMarkerViewView.MARKER_COLOR_RED); + } + + void openView() { + IWorkbenchWindow ww = fWorkbench.getActiveWorkbenchWindow(); + // Get current page + + try { + IWorkbenchPage wp = ww.getActivePage(); + + wp.showView(RedProblemMarkerViewView.ID); + + processEventsUntil(() -> ww.getShell() != null, 10000); + } catch (Exception e) { + + } + } + + void expandView() { + + // Get current page + IWorkbenchPage wp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + + RedProblemMarkerViewView view; + try { + view = (RedProblemMarkerViewView) wp.showView(RedProblemMarkerViewView.ID); + + ISelectionProvider provider = view.getSite().getSelectionProvider(); + TreeViewer viewer = (TreeViewer) provider; + + viewer.expandAll(); + processEventsUntil(() -> viewer.getExpandedElements().length > 0, 10000); + waitForJobs(100, 1000); + } catch (Exception e) { + throw new RuntimeException(); + } + } + + void validateViewerMarkerCounts(int expectedCount) { + // Get current page + IWorkbenchPage wp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + + RedProblemMarkerViewView view; + + view = (RedProblemMarkerViewView) wp.findView(RedProblemMarkerViewView.ID); + + ISelectionProvider provider = view.getSite().getSelectionProvider(); + TreeViewer viewer = (TreeViewer) provider; + + TreeItem[] viewerGroupRows = viewer.getTree().getItems(); + TreeItem[] viewerMarkers = viewerGroupRows[0].getItems(); + + assertEquals(expectedCount, viewerMarkers.length); + } + + void validateContentProviderMarkerColors(String expectedColor) { + + // Get current page + IWorkbenchPage wp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + + RedProblemMarkerViewView view; + + view = (RedProblemMarkerViewView) wp.findView(RedProblemMarkerViewView.ID); + + ISelectionProvider provider = view.getSite().getSelectionProvider(); + TreeViewer viewer = (TreeViewer) provider; + + ITreeContentProvider contentProvider = (ITreeContentProvider) viewer.getContentProvider(); + Object[] contentProvGroupRows = contentProvider.getElements(null); + Object[] contentProvMarkers = contentProvider.getChildren(contentProvGroupRows[0]); + + for (Object item : contentProvMarkers) { + MarkerItem marker = (MarkerItem) item; + String markerColor = marker.getAttributeValue(RedProblemMarkerViewView.MARKER_COLOR_ATTRIBUTE, ""); + assertEquals(expectedColor, markerColor); + } + + } + + /** + * Create the given number of problem markers in the workspace and set the color + * attribute using the color argument. + * + * @param workspace + * @param count + * @param color + * @throws Exception + */ + void createMarkers(IWorkspace workspace, int count, String color) throws Exception { + for (int i = 0; i < count; i++) { + IMarker marker = workspace.getRoot().createMarker(IMarker.PROBLEM); + marker.setAttribute(RedProblemMarkerViewView.MARKER_COLOR_ATTRIBUTE, color); + marker.setAttribute(IMarker.MESSAGE, color + ": " + i); + marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); + } + } + + /** + * Delete all problem markers from the workspace. + * + * @param workspace + */ + + void deleteMarkers(IWorkspace workspace) { + try { + workspace.getRoot().deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); + } catch (CoreException e) { + e.printStackTrace(); + } + } +} diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/RedProblemMarkerViewView.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/RedProblemMarkerViewView.java new file mode 100644 index 00000000000..3ae00c97089 --- /dev/null +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/markers/RedProblemMarkerViewView.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2022 Enda O'Brien and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which accompanies this distribution, + * and is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.ui.tests.markers; + +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerFilter; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.views.markers.MarkerItem; +import org.eclipse.ui.views.markers.MarkerSupportView; +import org.eclipse.ui.views.markers.internal.MarkerSupportRegistry; + +/** + * @since 3.5 + * + * A problem view that uses a VierweFilter to only show red markers. A + * red marker is a problem marker with a color attribute set to red. + */ +public class RedProblemMarkerViewView extends MarkerSupportView { + public static final String ID = "org.eclipse.ui.tests.LimitAndViewerFilterView"; + + public static final String MARKER_COLOR_ATTRIBUTE = "COLOR"; + public static final String MARKER_COLOR_RED = "RED"; + + /** + * create the view using the problem marker generator + */ + + public RedProblemMarkerViewView() { + super(MarkerSupportRegistry.PROBLEMS_GENERATOR); + } + + @Override + public void createPartControl(Composite parent) { + super.createPartControl(parent); + addViewerFilter(); + } + + /** + * Add a filter that removes all markers other than red markers. i.e. markers + * where the value of MARKER_COLOR_ATTRIBUTE is MARKER_COLOR_RED + */ + + void addViewerFilter() { + + // Get current page + IWorkbenchPage wp = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + + RedProblemMarkerViewView view; + try { + view = (RedProblemMarkerViewView) wp.showView(org.eclipse.ui.tests.markers.RedProblemMarkerViewView.ID); + + ISelectionProvider provider = view.getSite().getSelectionProvider(); + + TreeViewer viewer = (TreeViewer) provider; + + viewer.addFilter(new ViewerFilter() { + + @Override + public boolean select(Viewer viewer, Object parentElement, Object element) { + if (element instanceof MarkerItem) { + MarkerItem item = (MarkerItem) element; + + // keep the group row + if (item.getMarker() == null) { + return true; + } + return MARKER_COLOR_RED.equals(item.getAttributeValue(MARKER_COLOR_ATTRIBUTE, "")); + } + return false; + } + }); + } catch (Exception e) { + throw new RuntimeException(); + } + + } + +} diff --git a/tests/org.eclipse.ui.tests/plugin.xml b/tests/org.eclipse.ui.tests/plugin.xml index 3c15b5d245a..3a42538bd94 100644 --- a/tests/org.eclipse.ui.tests/plugin.xml +++ b/tests/org.eclipse.ui.tests/plugin.xml @@ -3932,6 +3932,11 @@ id="org.eclipse.ui.tests.customScopeTestView" name="Custom Scope Area View"> + +