Changeset 31418 in osm for applications


Ignore:
Timestamp:
2015-07-30T13:17:49+02:00 (9 years ago)
Author:
nokutu
Message:

Improved tests and code quality. Login is already working. Tests now do not requiere the JOSM window to be opened.

Location:
applications/editors/josm/plugins/mapillary
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryData.java

    r31415 r31418  
    6868   */
    6969  public synchronized void add(List<MapillaryAbstractImage> images) {
     70    add(images, true);
     71  }
     72
     73  /**
     74   * Adds an MapillaryImage to the object, and then repaints mapView.
     75   *
     76   * @param image
     77   *          The image to be added.
     78   */
     79  public synchronized void add(MapillaryAbstractImage image) {
     80    add(image, true);
     81  }
     82
     83  /**
     84   * Adds a new listener.
     85   *
     86   * @param lis
     87   *          Listener to be added.
     88   */
     89  public void addListener(MapillaryDataListener lis) {
     90    listeners.add(lis);
     91  }
     92
     93  /**
     94   * Removes a listener.
     95   *
     96   * @param lis
     97   *          Listener to be removed.
     98   */
     99  public void removeListener(MapillaryDataListener lis) {
     100    listeners.remove(lis);
     101  }
     102
     103  /**
     104   * Adds a set of MapillaryImages to the object, but doesn't repaint mapView.
     105   * This is needed for concurrency.
     106   *
     107   * @param images
     108   *          The set of images to be added.
     109   * @param update
     110   *          Whether the map must be updated or no.
     111   */
     112  public synchronized void add(List<MapillaryAbstractImage> images,
     113      boolean update) {
    70114    for (MapillaryAbstractImage image : images) {
    71       add(image);
    72     }
    73   }
    74 
    75   /**
    76    * Adds an MapillaryImage to the object, and then repaints mapView.
     115      add(image, update);
     116    }
     117  }
     118
     119  /**
     120   * Highlights the image under the cursor.
     121   *
     122   * @param image
     123   *          The image under the cursor.
     124   */
     125  public void setHighlightedImage(MapillaryAbstractImage image) {
     126    highlightedImage = image;
     127  }
     128
     129  /**
     130   * Returns the image under the mouse cursor.
     131   *
     132   * @return The image under the mouse cursor.
     133   */
     134  public MapillaryAbstractImage getHighlighted() {
     135    return highlightedImage;
     136  }
     137
     138  /**
     139   * Adds a MapillaryImage to the object, but doesn't repaint mapView. This is
     140   * needed for concurrency.
    77141   *
    78142   * @param image
    79143   *          The image to be added.
    80    */
    81   public synchronized void add(MapillaryAbstractImage image) {
     144   * @param update
     145   *          Whether the map must be updated or not.
     146   */
     147  public synchronized void add(MapillaryAbstractImage image, boolean update) {
    82148    if (!images.contains(image)) {
    83149      this.images.add(image);
    84150    }
    85     dataUpdated();
    86     fireImagesAdded();
    87   }
    88 
    89   /**
    90    * Adds a new listener.
    91    *
    92    * @param lis
    93    *          Listener to be added.
    94    */
    95   public void addListener(MapillaryDataListener lis) {
    96     listeners.add(lis);
    97   }
    98 
    99   /**
    100    * Removes a listener.
    101    *
    102    * @param lis
    103    *          Listener to be removed.
    104    */
    105   public void removeListener(MapillaryDataListener lis) {
    106     listeners.remove(lis);
    107   }
    108 
    109   /**
    110    * Adds a set of MapillaryImages to the object, but doesn't repaint mapView.
    111    * This is needed for concurrency.
    112    *
    113    * @param images
    114    *          The set of images to be added.
    115    */
    116   public synchronized void addWithoutUpdate(List<MapillaryAbstractImage> images) {
    117     for (MapillaryAbstractImage image : images) {
    118       addWithoutUpdate(image);
    119     }
    120   }
    121 
    122   /**
    123    * Highlights the image under the cursor.
    124    *
    125    * @param image
    126    *          The image under the cursor.
    127    */
    128   public void setHighlightedImage(MapillaryAbstractImage image) {
    129     highlightedImage = image;
    130   }
    131 
    132   /**
    133    * Returns the image under the mouse cursor.
    134    *
    135    * @return The image under the mouse cursor.
    136    */
    137   public MapillaryAbstractImage getHighlighted() {
    138     return highlightedImage;
    139   }
    140 
    141   /**
    142    * Adds a MapillaryImage to the object, but doesn't repaint mapView. This is
    143    * needed for concurrency.
    144    *
    145    * @param image
    146    *          The image to be added.
    147    */
    148   public synchronized void addWithoutUpdate(MapillaryAbstractImage image) {
    149     if (!images.contains(image)) {
    150       this.images.add(image);
    151     }
     151    if (update)
     152      dataUpdated();
    152153    fireImagesAdded();
    153154  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/MapillaryPlugin.java

    r31417 r31418  
    5252  private final MapillaryDownloadAction downloadAction;
    5353  private final MapillaryExportAction exportAction;
     54  /** Import action */
    5455  public static MapillaryImportAction importAction;
    5556  private final MapillaryZoomAction zoomAction;
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/actions/MapillaryImportAction.java

    r31417 r31418  
    5050   */
    5151  public MapillaryImportAction() {
    52     super(tr("Import pictures"), new ImageProvider(MapillaryPlugin.directory + "images/icon24.png"),
    53         tr("Import local pictures"), Shortcut.registerShortcut(
    54             "Import Mapillary", tr("Import pictures into Mapillary layer"),
     52    super(tr("Import pictures"), new ImageProvider(MapillaryPlugin.directory
     53        + "images/icon24.png"), tr("Import local pictures"), Shortcut
     54        .registerShortcut("Import Mapillary",
     55            tr("Import pictures into Mapillary layer"),
    5556            KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false, "mapillaryImport",
    5657        false);
     
    174175   */
    175176  public MapillaryImportedImage readNoTags(File file) {
     177    return readNoTags(
     178        file,
     179        Main.map.mapView.getProjection().eastNorth2latlon(
     180            Main.map.mapView.getCenter()));
     181  }
     182
     183  /**
     184   * Reads a image file that doesn't contain the needed GPS information. And
     185   * creates a new icon in the middle of the map.
     186   *
     187   * @param file
     188   * @param pos
     189   *          A {@link LatLon} object indicating the position in the map where
     190   *          the image must be set.
     191   * @return The imported image.
     192   */
     193  public MapillaryImportedImage readNoTags(File file, LatLon pos) {
    176194    double HORIZONTAL_DISTANCE = 0.0001;
    177195    double horDev;
     
    180198    else
    181199      horDev = -HORIZONTAL_DISTANCE * ((noTagsPics + 1) / 2);
    182     LatLon pos = Main.map.mapView.getProjection().eastNorth2latlon(
    183         Main.map.mapView.getCenter());
    184200    noTagsPics++;
    185201    return new MapillaryImportedImage(pos.lat(), pos.lon() + horDev, 0, file);
     
    246262    result += degMinSec[2].doubleValue() / 3600; // seconds
    247263
    248     while (result >= 180) {
    249       result -= 180;
    250     }
    251     while (result <= -180) {
    252       result += 180;
    253     }
    254 
    255264    if (GpsTagConstants.GPS_TAG_GPS_LATITUDE_REF_VALUE_SOUTH.equals(ref)
    256265        || GpsTagConstants.GPS_TAG_GPS_LONGITUDE_REF_VALUE_WEST.equals(ref)) {
     
    258267    }
    259268
     269    result = 360*((result+180)/360 - Math.floor((result+180)/360)) - 180;
    260270    return result;
    261271  }
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillaryDownloader.java

    r31409 r31418  
    3333  public final static String BASE_URL = "https://a.mapillary.com/v2/";
    3434  /** Client ID for the app */
    35   public final static String CLIENT_ID = "NzNRM2otQkR2SHJzaXJmNmdQWVQ0dzo1YTA2NmNlODhlNWMwOTBm";
     35  public final static String CLIENT_ID = "T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz";
    3636  /** Executor that will run the petitions. */
    3737  private static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(3, 5,
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/downloads/MapillarySequenceDownloadThread.java

    r31417 r31418  
    118118        }
    119119
    120         layer.getMapillaryData().addWithoutUpdate(
    121             new ArrayList<MapillaryAbstractImage>(finalImages));
     120        layer.getMapillaryData().add(
     121            new ArrayList<MapillaryAbstractImage>(finalImages), false);
    122122      }
    123123    } catch (IOException e) {
     
    125125          + " might be a Mapillary problem.");
    126126    }
     127    layer.getMapillaryData().dataUpdated();
    127128  }
    128129
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/gui/MapillaryPreferenceSetting.java

    r31406 r31418  
    8484      oauth.setText("Login");
    8585    else
    86       oauth.setText("Already loged in, click to relogin.");
     86      oauth.setText("Logged as: " + Main.pref.get("mapillary.username") + ". Click to relogin.");
    8787    panel.add(oauth);
    8888    gui.getDisplayPreference().addSubTab(this, "Mapillary", panel);
     
    130130      portListener.start();
    131131
    132       String url = "http://www.mapillary.io/connect?redirect_uri=http:%2F%2Flocalhost:8763%2F&client_id=MkJKbDA0bnZuZlcxeTJHTmFqN3g1dzplZTlkZjQyYjYyZTczOTdi&response_type=token&scope=user:email";
     132      String url = "http://www.mapillary.com/connect?redirect_uri=http:%2F%2Flocalhost:8763%2F&client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz&response_type=token&scope=user:read%20public:upload%20public:write";
    133133      Desktop desktop = Desktop.getDesktop();
    134134      if (desktop.isSupported(Desktop.Action.BROWSE)) {
  • applications/editors/josm/plugins/mapillary/src/org/openstreetmap/josm/plugins/mapillary/oauth/OAuthPortListener.java

    r31412 r31418  
    88import java.net.ServerSocket;
    99import java.net.Socket;
     10import java.net.URL;
    1011import java.util.Scanner;
    1112
     
    2526      ServerSocket serverSocket = new ServerSocket(8763);
    2627      Socket clientSocket = serverSocket.accept();
    27       PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"), true);
    28       Scanner in = new Scanner(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"));
     28      PrintWriter out = new PrintWriter(new OutputStreamWriter(
     29          clientSocket.getOutputStream(), "UTF-8"), true);
     30      Scanner in = new Scanner(new InputStreamReader(
     31          clientSocket.getInputStream(), "UTF-8"));
    2932      String s;
    3033      String accessToken = null;
     
    4144          }
    4245          break;
    43         }
     46        } else if (s.contains("keep-alive"))
     47          break;
    4448      }
    4549
    4650      writeContent(out);
    4751
    48       Main.info("Successful login with Mapillary, the access token is: "
    49           + accessToken);
    50       Main.pref.put("mapillary.access-token", accessToken);
    51 
    5252      out.close();
    5353      in.close();
    5454      serverSocket.close();
     55
     56      Main.info("Successful login with Mapillary, the access token is: "
     57          + accessToken);
     58      // Saves the access token in preferences.
     59      Main.pref.put("mapillary.access-token", accessToken);
     60      // Sets the logged username in preferences.
     61      Main.pref
     62          .put(
     63              "mapillary.username",
     64              OAuthUtils
     65                  .getWithHeader(
     66                      new URL(
     67                          "https://a.mapillary.com/v2/me?client_id=T1Fzd20xZjdtR0s1VDk5OFNIOXpYdzoxNDYyOGRkYzUyYTFiMzgz"))
     68                  .getString("username"));
     69
     70      Main.info("The username is: " + Main.pref.get("mapillary.username"));
     71
    5572    } catch (BindException e) {
    5673      return;
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/AbstractTest.java

    r31417 r31418  
    22
    33import org.junit.BeforeClass;
    4 import org.openstreetmap.josm.Main;
    5 import org.openstreetmap.josm.gui.MainApplication;
    64import org.openstreetmap.josm.plugins.mapillary.util.TestUtil;
    75
     6/**
     7 * Abstract class for tests that require JOSM's preferences running.
     8 *
     9 * @author nokutu
     10 *
     11 */
    812public abstract class AbstractTest {
    913
    1014  private static boolean started = false;
    1115
     16  /**
     17   * Initiates the basic parts of JOSM.
     18   */
    1219  @BeforeClass
    13   public static void setUpBeforeClass() throws Exception {
     20  public static void setUpBeforeClass() {
    1421    if (!started) {
    15       new Thread() {
    16         @Override
    17         public synchronized void run() {
    18           MainApplication
    19               .main(new String[] { "--download=http://www.openstreetmap.org/#map=18/40.42013/-3.68923" });
    20         }
    21       }.start();
     22      TestUtil.initPlugin();
    2223      started = true;
    23       while (Main.map == null || Main.map.mapView == null) {
    24         synchronized (Thread.currentThread()) {
    25           Thread.currentThread().wait(1000);
    26         }
    27       }
    2824    }
    2925  }
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/AllTests.java

    r31417 r31418  
    44import org.junit.runners.Suite;
    55import org.junit.runners.Suite.SuiteClasses;
    6 import org.openstreetmap.josm.plugins.mapillary.util.TestUtil;
    76
     7/**
     8 * Runs all tests.
     9 *
     10 * @author nokutu
     11 *
     12 */
    813@RunWith(Suite.class)
    9 @SuiteClasses({ ImportTest.class,
    10     MapillarySequenceDownloadThreadTest.class})
     14@SuiteClasses({ ImportTest.class, MapillarySequenceDownloadThreadTest.class })
    1115public class AllTests {
    1216}
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/ImportTest.java

    r31417 r31418  
    22
    33import static org.junit.Assert.*;
     4import org.junit.Test;
    45
    56import java.io.File;
     
    1011import org.apache.commons.imaging.common.RationalNumber;
    1112import org.apache.commons.imaging.formats.tiff.constants.GpsTagConstants;
    12 import org.junit.BeforeClass;
    13 import org.junit.Test;
    14 import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;
    1513import org.openstreetmap.josm.Main;
    1614import org.openstreetmap.josm.data.coor.LatLon;
    17 import org.openstreetmap.josm.gui.MainApplication;
    1815import org.openstreetmap.josm.plugins.mapillary.actions.MapillaryImportAction;
    1916
    20 public class ImportTest extends AbstractTest{
     17/**
     18 * Test the importation of images.
     19 *
     20 * @author nokutu
     21 *
     22 */
     23public class ImportTest extends AbstractTest {
    2124
     25  /**
     26   * Test the importation of images in PNG format.
     27   */
    2228  @Test
    23   public void importNoTagsTest() throws InterruptedException {
     29  public void importNoTagsTest() {
    2430    File image = new File(MapillaryPlugin.directory + "images/icon16.png");
    25     MapillaryImportedImage img = MapillaryPlugin.importAction.readNoTags(image);
     31    MapillaryImportedImage img = MapillaryPlugin.importAction.readNoTags(image,
     32        new LatLon(0, 0));
    2633    assertEquals(0, img.getCa(), 0.01);
    27     assert(Main.map.mapView.getRealBounds().getCenter().equalsEpsilon(img.getLatLon()));
     34    assert (Main.map.mapView.getRealBounds().getCenter().equalsEpsilon(img
     35        .getLatLon()));
    2836  }
    2937
    30   @Test(expected=IIOException.class)
     38  /**
     39   * Test if provided an invalid file, the proper exception is thrown.
     40   *
     41   * @throws IOException
     42   */
     43  @Test(expected = IIOException.class)
    3144  public void testInvalidFiles() throws IOException {
    32       MapillaryImportedImage img = new MapillaryImportedImage(0,0,0, null);
    33       assertEquals(null, img.getImage());
    34       assertEquals(null, img.getFile());
     45    MapillaryImportedImage img = new MapillaryImportedImage(0, 0, 0, null);
     46    assertEquals(null, img.getImage());
     47    assertEquals(null, img.getFile());
    3548
    36       img = new MapillaryImportedImage(0, 0, 0, new File(""));
    37       assertEquals(new File(""), img.getFile());
    38       img.getImage();
     49    img = new MapillaryImportedImage(0, 0, 0, new File(""));
     50    assertEquals(new File(""), img.getFile());
     51    img.getImage();
    3952  }
    4053
     54  /**
     55   * Test degMinSecToDouble method.
     56   */
    4157  @Test
    4258  public void degMinSecToDoubleTest() {
  • applications/editors/josm/plugins/mapillary/test/unit/org/openstreetmap/josm/plugins/mapillary/util/TestUtil.java

    r31417 r31418  
    1111import org.openstreetmap.josm.tools.I18n;
    1212
     13/**
     14 *
     15 * Utilities for tests.
     16 *
     17 * @author floscher
     18 */
    1319public class TestUtil {
    14     private static boolean isInitialized;
     20  private static boolean isInitialized;
    1521
    16     private TestUtil() {
    17         // Prevent instantiation
     22  private TestUtil() {
     23    // Prevent instantiation
     24  }
     25
     26  /**
     27   * Initializes the {@link Main} class of JOSM and the mapillary plugin with
     28   * the preferences from test/data/preferences.
     29   *
     30   * That is needed e.g. to use {@link MapillaryLayer#getInstance()}
     31   */
     32  public static final void initPlugin() {
     33    if (!isInitialized) {
     34      System.setProperty("josm.home", "test/data/preferences");
     35      Main.initApplicationPreferences();
     36      Main.pref.enableSaveOnPut(false);
     37      I18n.init();
     38      Main.determinePlatformHook();
     39      Main.platform.preStartupHook();
     40      Main.pref.init(false);
     41      I18n.set(Main.pref.get("language", "en"));
     42      Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
     43      try {
     44        new MapillaryPlugin(new PluginInformation(new File(
     45            "./build/libs/josm-mapillary-plugin.jar")));
     46      } catch (PluginException e) {
     47        e.printStackTrace();
     48      }
     49      isInitialized = true;
    1850    }
    19 
    20     /**
    21      * Initializes the {@link Main} class of JOSM and the mapillary plugin
    22      * with the preferences from test/data/preferences.
    23      *
    24      * That is needed e.g. to use {@link MapillaryLayer#getInstance()}
    25      */
    26     public static final void initPlugin() {
    27         if (!isInitialized) {
    28             System.setProperty("josm.home", "test/data/preferences");
    29             Main.initApplicationPreferences();
    30             Main.pref.enableSaveOnPut(false);
    31             I18n.init();
    32             Main.determinePlatformHook();
    33             Main.platform.preStartupHook();
    34             Main.pref.init(false);
    35             I18n.set(Main.pref.get("language", "en"));
    36             Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
    37             try {
    38               new MapillaryPlugin(new PluginInformation(new File("/home/nokutu/.josm/plugins/Mapillary.jar")));
    39             } catch (PluginException e) {
    40               // TODO Auto-generated catch block
    41               e.printStackTrace();
    42             }
    43             isInitialized = true;
    44         }
    45     }
     51  }
    4652
    4753}
Note: See TracChangeset for help on using the changeset viewer.