Testing GPS in Android

后端 未结 6 590
-上瘾入骨i
-上瘾入骨i 2020-12-07 23:38

How do you test GPS applications in Android? Can we test it using the Android emulator?

6条回答
  •  盖世英雄少女心
    2020-12-07 23:58

    I suggest that you write test which is sending location object to the listener class you want to test. With this in mind you will need some helper methods that will be able to produce location objects.

    E.g.:

    First you send a location with fixed coordinates (e.g. center of New York).

    // test if everything is OK

    Than you send a coordinate which is 100m appart from the first coordinate. You can do this with function I implemented recently: https://stackoverflow.com/a/17545955/322791

    // test if everything is OK

    It is common that GPS methods are related to time. I suggest that you implement method getNow() on listener class and call it whenever you need current time.

    long getNow() {
        if (isUnitTesting) {
             return unitTestTime;
        } else {
             System.getcurrenttimemillis();
        }
    }
    

    In unit test environment you simply set the obj.isUnitTesting=true and obj.unitTestTime to any time you want. After that you can send first location to the listener, then you change time (+x seconds) and send another location ...

    It will be much easier to test your application that way.

提交回复
热议问题