-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HK] Use MapPart in 'ActiveMapTracker' (#478)
* [HK] Use MapPart in 'ActiveMapTracker' * Add JUnit test of class 'ActiveMapTracker' * uses ConcurrentLinkedDeque for openMaps Signed-off-by: Sebastian Schulz [email protected] Co-authored-by: Frank Gasdorf <[email protected]>
- Loading branch information
Showing
2 changed files
with
242 additions
and
32 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
....project.ui.tests/src/org/locationtech/udig/project/ui/internal/ActiveMapTrackerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* uDig - User Friendly Desktop Internet GIS client | ||
* https://locationtech.org/projects/technology.udig | ||
* (C) 2021, Eclipse Foundation | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution | ||
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). | ||
*/ | ||
package org.locationtech.udig.project.ui.internal; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.atLeastOnce; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.eclipse.ui.IEditorReference; | ||
import org.eclipse.ui.IViewPart; | ||
import org.eclipse.ui.IViewReference; | ||
import org.eclipse.ui.IWorkbenchPage; | ||
import org.eclipse.ui.IWorkbenchPartReference; | ||
import org.eclipse.ui.IWorkbenchWindow; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.locationtech.udig.project.internal.Map; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ActiveMapTrackerTest { | ||
|
||
@Mock | ||
private IWorkbenchWindow workbenchWindow; | ||
|
||
@Mock | ||
private IWorkbenchPage workbenchPage; | ||
|
||
@Mock | ||
private IEditorReference editorReference; | ||
|
||
@Mock | ||
private IWorkbenchPartReference partRef; | ||
|
||
@Test | ||
public void registersPageListenerOnWindowOpen() { | ||
final ActiveMapTracker instance = new ActiveMapTracker(); | ||
when(workbenchWindow.getPages()).thenReturn(new IWorkbenchPage[] {}); | ||
|
||
workbenchWindow.addPageListener(instance); | ||
|
||
instance.windowOpened(workbenchWindow); | ||
verify(workbenchWindow, atLeastOnce()).addPageListener(instance); | ||
} | ||
|
||
@Test | ||
public void removesPageListenerOnWindowClose() { | ||
final ActiveMapTracker instance = new ActiveMapTracker(); | ||
when(workbenchWindow.getPages()).thenReturn(new IWorkbenchPage[] {}); | ||
|
||
workbenchWindow.removePageListener(instance); | ||
|
||
instance.windowClosed(workbenchWindow); | ||
verify(workbenchWindow, atLeastOnce()).removePageListener(instance); | ||
} | ||
|
||
@Test | ||
public void registersOpenPartOnWindowOpen() { | ||
final ActiveMapTracker instance = new ActiveMapTracker(); | ||
|
||
final Map map = mock(Map.class); | ||
|
||
final MapEditorPart mEditor = mock(MapEditorPart.class); | ||
|
||
when(mEditor.getMap()).thenReturn(map); | ||
|
||
when(editorReference.getPart(false)).thenReturn(mEditor); | ||
|
||
when(workbenchWindow.getPages()).thenReturn(new IWorkbenchPage[] { workbenchPage }); | ||
|
||
when(workbenchPage.getEditorReferences()) | ||
.thenReturn(new IEditorReference[] { editorReference }); | ||
|
||
when(workbenchPage.getViewReferences()).thenReturn(new IViewReference[] {}); | ||
|
||
instance.windowOpened(workbenchWindow); | ||
|
||
assertTrue(instance.getOpenMapParts().contains(mEditor)); | ||
assertTrue(instance.getOpenMaps().contains(map)); | ||
|
||
verify(mEditor, times(1)).getMap(); | ||
verify(editorReference, times(1)).getPart(false); | ||
verify(workbenchWindow, times(1)).getPages(); | ||
verify(workbenchPage, times(1)).getEditorReferences(); | ||
verify(workbenchPage, times(1)).getViewReferences(); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void openMapsCollectionIsReadOnly() { | ||
final Map map = mock(Map.class); | ||
new ActiveMapTracker().getOpenMaps().remove(map); | ||
} | ||
|
||
@Test | ||
public void trackMapPartOnlyOnPartOpened() { | ||
final ActiveMapTracker instance = new ActiveMapTracker(); | ||
final MapEditorPart mapPart = mock(MapEditorPart.class); | ||
final IViewPart anotherPart = mock(IViewPart.class); | ||
|
||
when(partRef.getPart(false)).thenReturn(mapPart); | ||
|
||
assertEquals(0, instance.getOpenMapParts().size()); | ||
instance.partOpened(partRef); | ||
|
||
assertTrue(instance.getOpenMapParts().contains(mapPart)); | ||
assertEquals(1, instance.getOpenMapParts().size()); | ||
|
||
// another View | ||
when(partRef.getPart(false)).thenReturn(anotherPart); | ||
|
||
instance.partOpened(partRef); | ||
|
||
assertTrue(instance.getOpenMapParts().contains(mapPart)); | ||
assertEquals(1, instance.getOpenMapParts().size()); | ||
assertEquals(mapPart, instance.getMostRecentOpenedPart()); | ||
|
||
verify(partRef, times(2)).getPart(false); | ||
} | ||
|
||
@Test | ||
public void mostRecentOpenedMapFirst() { | ||
final ActiveMapTracker instance = new ActiveMapTracker(); | ||
IWorkbenchPartReference partRef = mock(IWorkbenchPartReference.class); | ||
final MapEditorPart mapPart1 = mock(MapEditorPart.class); | ||
final MapEditorPart mapPart2 = mock(MapEditorPart.class); | ||
|
||
when(partRef.getPart(false)).thenReturn(mapPart1); | ||
|
||
assertEquals(0, instance.getOpenMapParts().size()); | ||
instance.partOpened(partRef); | ||
|
||
assertTrue(instance.getOpenMapParts().contains(mapPart1)); | ||
assertEquals(mapPart1, instance.getMostRecentOpenedPart()); | ||
assertEquals(1, instance.getOpenMapParts().size()); | ||
|
||
// second View | ||
when(partRef.getPart(false)).thenReturn(mapPart2); | ||
|
||
instance.partOpened(partRef); | ||
assertTrue(instance.getOpenMapParts().contains(mapPart1)); | ||
assertTrue(instance.getOpenMapParts().contains(mapPart2)); | ||
|
||
assertEquals(2, instance.getOpenMapParts().size()); | ||
assertEquals(mapPart2, instance.getMostRecentOpenedPart()); | ||
verify(partRef, times(2)).getPart(false); | ||
} | ||
} |
Oops, something went wrong.