A.Sharma
A.Sharma

Reputation: 2799

Gluon PositionService for Desktop - Testing Purposes

I want to be able to test the Gluon PositionService from my laptop. Is this possible? Looking at the runtime dependencies, iOS and Android have this jar file included. The desktop dependencies don't have it though.

What is a workaround to be able to test this service from my laptop. I just want to be able to print out the location to the console right now. Is this a limitation of my laptop?

Desktop Discrepancy

Upvotes: 1

Views: 114

Answers (1)

José Pereda
José Pereda

Reputation: 45476

Given that the laptop/desktop machine doesn't include a GPS sensor, there is no sense on having a DesktopPositionService implementation.

But if you just want to test your code for mobile on your laptop, you can easily create a fake task that randomly provides a new position after a given period of time.

There are two simple ways to mock the PositionService on desktop.

One, by simply providing an alternative to the case where you don't actually have a PositionService implementation:

Services.get(PositionService.class)
        .map(s -> {
            // Mobile - real implementation
            s.positionProperty().addListener((obs, ov, nv) -> 
                System.out.println(String.format("Lat: %.6f, Lon: %.6f", nv.getLatitude(), nv.getLongitude())));
            return s.getPosition();
        }).orElseGet(() -> {
            if (Platform.isDesktop()) {
                // Desktop - Mock implementation
                PauseTransition pause = new PauseTransition(Duration.seconds(5));
                pause.setOnFinished(t -> {
                    System.out.println(String.format("Lat: %.6f, Lon: %.6f", new Random().nextFloat() * 100, new Random().nextFloat() * 100));
                    pause.playFromStart();
                });
                pause.play();
            }
            return null;
        }); 

And two, following the design of all the different plugins in Charm Down, by actually providing a PositionService implementation, creating the DesktopPositionService class in the Desktop/Java Package of your project under the package com.gluonhq.charm.down.plugins.desktop.

package com.gluonhq.charm.down.plugins.desktop;

import com.gluonhq.charm.down.plugins.Position;
import com.gluonhq.charm.down.plugins.PositionService;
import java.util.Random;
import javafx.animation.PauseTransition;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.util.Duration;

public class DesktopPositionService implements PositionService {

    private final ReadOnlyObjectWrapper<Position> positionProperty = new ReadOnlyObjectWrapper<>();

    public DesktopPositionService() {
        mockPosition();
    }

    @Override
    public ReadOnlyObjectProperty<Position> positionProperty() {
        return positionProperty.getReadOnlyProperty();
    }

    @Override
    public Position getPosition() {
        return positionProperty.get();
    }

    private void mockPosition() {
        PauseTransition pause = new PauseTransition(Duration.seconds(5));
        pause.setOnFinished(t -> {
            positionProperty.set(new Position(new Random().nextFloat() * 100, new Random().nextFloat() * 100));
            pause.playFromStart();
        });
        pause.play();
    }
}

So now this will work for both mobile (real sensor) and desktop (mock):

Services.get(PositionService.class)
        .ifPresent(s -> 
             s.positionProperty().addListener((obs, ov, nv) -> 
                System.out.println(String.format("Lat: %.6f, Lon: %.6f", nv.getLatitude(), nv.getLongitude()))));

Upvotes: 1

Related Questions