Changeset 10604 in josm for trunk/test


Ignore:
Timestamp:
2016-07-23T14:54:19+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #12478, fix #12565, fix #11114 - Use ​Swing Copy/Paste instead of CopyAction/PasteAction with custom buffer (patch by michael2402, modified) - gsoc-core

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/actions/CopyActionTest.java

    r8876 r10604  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertNotNull;
     7import static org.junit.Assert.assertTrue;
     8import static org.junit.Assert.fail;
    59
     10import java.awt.datatransfer.Clipboard;
     11import java.awt.datatransfer.DataFlavor;
     12import java.awt.datatransfer.StringSelection;
     13import java.awt.datatransfer.UnsupportedFlavorException;
     14import java.io.IOException;
    615import java.util.Arrays;
    7 import java.util.Collections;
    816
    9 import org.junit.BeforeClass;
     17import org.junit.Rule;
    1018import org.junit.Test;
    11 import org.openstreetmap.josm.JOSMFixture;
    12 import org.openstreetmap.josm.data.osm.Relation;
     19import org.openstreetmap.josm.Main;
     20import org.openstreetmap.josm.data.coor.LatLon;
     21import org.openstreetmap.josm.data.osm.DataSet;
     22import org.openstreetmap.josm.data.osm.Node;
    1323import org.openstreetmap.josm.data.osm.Way;
     24import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
     25import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
     26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     27import org.openstreetmap.josm.testutils.JOSMTestRules;
     28
     29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1430
    1531/**
     
    1733 */
    1834public class CopyActionTest {
     35    private static final class CapturingCopyAction extends CopyAction {
     36        private boolean warningShown;
    1937
    20     /**
    21      * Setup test.
    22      */
    23     @BeforeClass
    24     public static void setUpBeforeClass() {
    25         JOSMFixture.createUnitTestFixture().init();
     38        @Override
     39        protected void showEmptySelectionWarning() {
     40            warningShown = true;
     41        }
    2642    }
    2743
    2844    /**
    29      * Test of {@link CopyAction#getCopyString} method for a single way.
     45     * We need prefs for this.
     46     */
     47    @Rule
     48    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     49    public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI();
     50
     51    /**
     52     * Test that copy action copies the selected primitive
     53     * @throws IOException if an I/O error occurs
     54     * @throws UnsupportedFlavorException if the requested data flavor is not supported
    3055     */
    3156    @Test
    32     public void testCopyStringWay() {
    33         final Way way = new Way(123L);
    34         assertEquals("way 123", CopyAction.getCopyString(Collections.singleton(way)));
     57    public void testWarnOnEmpty() throws UnsupportedFlavorException, IOException {
     58        Clipboard clipboard = ClipboardUtils.getClipboard();
     59        clipboard.setContents(new StringSelection("test"), null);
     60
     61        CapturingCopyAction action = new CapturingCopyAction();
     62
     63        action.updateEnabledState();
     64        assertFalse(action.isEnabled());
     65        action.actionPerformed(null);
     66        assertTrue(action.warningShown);
     67
     68        Main.getLayerManager().addLayer(new OsmDataLayer(new DataSet(), "test", null));
     69        action.warningShown = false;
     70
     71        action.updateEnabledState();
     72        assertFalse(action.isEnabled());
     73        action.actionPerformed(null);
     74        assertTrue(action.warningShown);
     75
     76        assertEquals("test", clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor));
    3577    }
    3678
    3779    /**
    38      * Test of {@link CopyAction#getCopyString} method for a way and a relation.
     80     * Test that copy action copies the selected primitive
     81     * @throws Exception if an error occurs
    3982     */
    4083    @Test
    41     public void testCopyStringWayRelation() {
    42         final Way way = new Way(123L);
    43         final Relation relation = new Relation(456);
    44         assertEquals("way 123,relation 456", CopyAction.getCopyString(Arrays.asList(way, relation)));
    45         assertEquals("relation 456,way 123", CopyAction.getCopyString(Arrays.asList(relation, way)));
     84    public void testCopySinglePrimitive() throws Exception {
     85        DataSet data = new DataSet();
     86
     87        Node node1 = new Node();
     88        node1.setCoor(LatLon.ZERO);
     89        data.addPrimitive(node1);
     90
     91        Node node2 = new Node();
     92        node2.setCoor(LatLon.ZERO);
     93        data.addPrimitive(node2);
     94        Way way = new Way();
     95        way.setNodes(Arrays.asList(node1, node2));
     96        data.addPrimitive(way);
     97        data.setSelected(way);
     98
     99        Main.getLayerManager().addLayer(new OsmDataLayer(data, "test", null));
     100
     101        CopyAction action = new CopyAction() {
     102            @Override
     103            protected void showEmptySelectionWarning() {
     104                fail("Selection is not empty.");
     105            }
     106        };
     107        action.updateEnabledState();
     108        assertTrue(action.isEnabled());
     109        action.actionPerformed(null);
     110
     111        Object copied = ClipboardUtils.getClipboard().getContents(null).getTransferData(PrimitiveTransferData.DATA_FLAVOR);
     112        assertNotNull(copied);
     113        assertTrue(copied instanceof PrimitiveTransferData);
     114        PrimitiveTransferData ptd = (PrimitiveTransferData) copied;
     115        Object[] direct = ptd.getDirectlyAdded().toArray();
     116        assertEquals(1, direct.length);
     117        Object[] referenced = ptd.getReferenced().toArray();
     118        assertEquals(2, referenced.length);
    46119    }
    47120}
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/PrimitiveTransferableTest.java

    r9711 r10604  
    55import static org.junit.Assert.assertFalse;
    66import static org.junit.Assert.assertTrue;
    7 import static org.openstreetmap.josm.gui.datatransfer.PrimitiveTransferable.PRIMITIVE_DATA;
    87
    98import java.awt.datatransfer.DataFlavor;
    109import java.awt.datatransfer.UnsupportedFlavorException;
     10import java.util.Arrays;
    1111import java.util.Collection;
    1212import java.util.Collections;
     13import java.util.List;
    1314
    14 import org.junit.BeforeClass;
     15import org.junit.Rule;
    1516import org.junit.Test;
    16 import org.openstreetmap.josm.JOSMFixture;
    1717import org.openstreetmap.josm.data.osm.Node;
     18import org.openstreetmap.josm.data.osm.NodeData;
    1819import org.openstreetmap.josm.data.osm.PrimitiveData;
     20import org.openstreetmap.josm.gui.datatransfer.data.PrimitiveTransferData;
     21import org.openstreetmap.josm.gui.datatransfer.data.TagTransferData;
     22import org.openstreetmap.josm.testutils.JOSMTestRules;
     23
     24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1925
    2026/**
     
    2228 */
    2329public class PrimitiveTransferableTest {
     30    /**
     31     * Prefs to use OSM primitives
     32     */
     33    @Rule
     34    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     35    public JOSMTestRules test = new JOSMTestRules().preferences();
    2436
    2537    /**
    26      * Setup tests
    27      */
    28     @BeforeClass
    29     public static void setUpBeforeClass() {
    30         JOSMFixture.createUnitTestFixture().init();
    31     }
    32 
    33     /**
    34      * Test of {@link PrimitiveTransferable#getTransferDataFlavors()} method.
     38     * Test of {@link PrimitiveTransferable#getTransferDataFlavors()} method response order
    3539     */
    3640    @Test
    3741    public void testGetTransferDataFlavors() {
    38         DataFlavor[] flavors = new PrimitiveTransferable(null).getTransferDataFlavors();
    39         assertEquals(2, flavors.length);
    40         assertEquals(PRIMITIVE_DATA, flavors[0]);
    41         assertEquals(DataFlavor.stringFlavor, flavors[1]);
     42        List<DataFlavor> flavors = Arrays.asList(new PrimitiveTransferable(null).getTransferDataFlavors());
     43        int ptd = flavors.indexOf(PrimitiveTransferData.DATA_FLAVOR);
     44        int tags = flavors.indexOf(TagTransferData.FLAVOR);
     45        int string = flavors.indexOf(DataFlavor.stringFlavor);
     46
     47        assertTrue(ptd >= 0);
     48        assertTrue(tags >= 0);
     49        assertTrue(string >= 0);
     50
     51        assertTrue(ptd < tags);
     52        assertTrue(tags < string);
    4253    }
    4354
     
    4758    @Test
    4859    public void testIsDataFlavorSupported() {
    49         assertTrue(new PrimitiveTransferable(null).isDataFlavorSupported(PRIMITIVE_DATA));
    50         assertFalse(new PrimitiveTransferable(null).isDataFlavorSupported(null));
     60        assertTrue(new PrimitiveTransferable(null).isDataFlavorSupported(PrimitiveTransferData.DATA_FLAVOR));
     61        assertFalse(new PrimitiveTransferable(null).isDataFlavorSupported(DataFlavor.imageFlavor));
    5162    }
    5263
     
    5768    @Test
    5869    public void testGetTransferDataNominal() throws UnsupportedFlavorException {
    59         PrimitiveTransferable pt = new PrimitiveTransferable(Collections.singleton(new Node(1)));
    60         assertEquals("node 1 # incomplete\n", pt.getTransferData(DataFlavor.stringFlavor));
    61         Collection<PrimitiveData> td = ((PrimitiveTransferable.Data) pt.getTransferData(PRIMITIVE_DATA)).getPrimitiveData();
     70        PrimitiveTransferData data = PrimitiveTransferData.getData(Collections.singleton(new Node(1)));
     71        PrimitiveTransferable pt = new PrimitiveTransferable(data);
     72        assertEquals("node 1", pt.getTransferData(DataFlavor.stringFlavor));
     73        Collection<PrimitiveData> td = ((PrimitiveTransferData) pt.getTransferData(PrimitiveTransferData.DATA_FLAVOR)).getAll();
    6274        assertEquals(1, td.size());
    63         assertTrue(td.iterator().next() instanceof PrimitiveData);
     75        assertTrue(td.iterator().next() instanceof NodeData);
     76
     77
     78        data = PrimitiveTransferData.getData(Arrays.asList(new Node(1), new Node(2)));
     79        pt = new PrimitiveTransferable(data);
     80        assertEquals("node 1\nnode 2", pt.getTransferData(DataFlavor.stringFlavor));
    6481    }
    6582
     
    7087    @Test(expected = UnsupportedFlavorException.class)
    7188    public void testGetTransferDataError() throws UnsupportedFlavorException {
    72         new PrimitiveTransferable(Collections.singleton(new Node(1))).getTransferData(null);
     89        PrimitiveTransferData data = PrimitiveTransferData.getData(Collections.singleton(new Node(1)));
     90        new PrimitiveTransferable(data).getTransferData(DataFlavor.imageFlavor);
    7391    }
    7492}
  • trunk/test/unit/org/openstreetmap/josm/gui/datatransfer/RelationMemberTransferableTest.java

    r9717 r10604  
    3838    @Test
    3939    public void testGetTransferDataFlavors() {
    40         DataFlavor[] flavors = new RelationMemberTransferable(null).getTransferDataFlavors();
     40        DataFlavor[] flavors = new RelationMemberTransferable(Collections.<RelationMember>emptyList()).getTransferDataFlavors();
    4141        assertEquals(2, flavors.length);
    4242        assertEquals(RELATION_MEMBER_DATA, flavors[0]);
     
    4949    @Test
    5050    public void testIsDataFlavorSupported() {
    51         assertTrue(new RelationMemberTransferable(null).isDataFlavorSupported(RELATION_MEMBER_DATA));
    52         assertFalse(new RelationMemberTransferable(null).isDataFlavorSupported(null));
     51        RelationMemberTransferable transferable = new RelationMemberTransferable(Collections.<RelationMember>emptyList());
     52        assertTrue(transferable.isDataFlavorSupported(RELATION_MEMBER_DATA));
     53        assertFalse(transferable.isDataFlavorSupported(null));
    5354    }
    5455
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/actions/RelationEditorActionsTest.java

    r10113 r10604  
    22package org.openstreetmap.josm.gui.dialogs.relation.actions;
    33
    4 import org.junit.BeforeClass;
     4import org.junit.Rule;
    55import org.junit.Test;
    6 import org.openstreetmap.josm.JOSMFixture;
    76import org.openstreetmap.josm.data.osm.DataSet;
    87import org.openstreetmap.josm.data.osm.Relation;
     
    1514import org.openstreetmap.josm.gui.tagging.TagEditorModel;
    1615import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
     16import org.openstreetmap.josm.testutils.JOSMTestRules;
     17
     18import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
    1719
    1820/**
     
    2022 */
    2123public class RelationEditorActionsTest {
     24    /**
     25     * Plattform for tooltips.
     26     */
     27    @Rule
     28    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     29    public JOSMTestRules test = new JOSMTestRules().preferences().platform().commands();
    2230
    2331    /**
    24      * Setup test.
    25      */
    26     @BeforeClass
    27     public static void setUpBeforeClass() {
    28         JOSMFixture.createUnitTestFixture().init(true);
    29     }
    30 
    31     /**
    32      * Test all actions with minimal data.
     32     * Check that all actions do not crash.
    3333     */
    3434    @Test
     
    5858
    5959        new CopyMembersAction(memberTableModel, layer, editor).actionPerformed(null);
    60         new PasteMembersAction(memberTableModel, layer, editor).actionPerformed(null);
     60        new PasteMembersAction(memberTable, layer, editor).actionPerformed(null);
    6161
    6262        new DeleteCurrentRelationAction(layer, editor).actionPerformed(null);
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r10588 r10604  
    1212import org.junit.runners.model.InitializationError;
    1313import org.junit.runners.model.Statement;
     14import org.openstreetmap.josm.JOSMFixture;
    1415import org.openstreetmap.josm.Main;
    1516import org.openstreetmap.josm.data.projection.Projections;
     
    4041    private boolean platform;
    4142    private boolean useProjection;
     43    private boolean commands;
    4244    private boolean allowMemoryManagerLeaks;
    4345
     
    132134    public JOSMTestRules projection() {
    133135        useProjection = true;
     136        return this;
     137    }
     138
     139    /**
     140      * Allow the execution of commands using {@link Main#undoRedo}
     141      * @return this instance, for easy chaining
     142      */
     143    public JOSMTestRules commands() {
     144        commands = true;
    134145        return this;
    135146    }
     
    221232        if (platform) {
    222233            Main.determinePlatformHook();
     234        }
     235
     236        if (commands) {
     237            // TODO: Implement a more selective version of this once Main is restructured.
     238            JOSMFixture.createUnitTestFixture().init(true);
    223239        }
    224240    }
Note: See TracChangeset for help on using the changeset viewer.