javaextensions/location/tsrc/vipertest/src/GetLocationTest.java
branchRCL_3
changeset 18 9ac0a0a7da70
parent 17 0fd27995241b
child 19 71c436fe3ce0
equal deleted inserted replaced
17:0fd27995241b 18:9ac0a0a7da70
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 import javax.microedition.location.*;
       
    18 
       
    19 class GetLocationTest extends ViperUnitTest
       
    20 {
       
    21     private Location iLoc;
       
    22 
       
    23     GetLocationTest()
       
    24     {
       
    25         super("GetLocationTest");
       
    26     }
       
    27 
       
    28     protected void runTest() throws Throwable
       
    29     {
       
    30         testBadArguments();
       
    31         testGetLocation();
       
    32         testAltitudeData();
       
    33         testSpeedData();
       
    34         testAddressInfo();
       
    35         testGetLocationTimeout();
       
    36         testReset();
       
    37         testGetLatest();
       
    38         testGetLocationOutOfService();
       
    39     }
       
    40 
       
    41     void testBadArguments() throws Exception
       
    42     {
       
    43         setCurrentTest("testBadArguments()");
       
    44         providerSetUp(null);
       
    45         assertTrue(iLP != null, "LocationProvider is null");
       
    46 
       
    47         assertBad(0);
       
    48         assertBad(-2);
       
    49         assertBad(-12345); // Sanity check
       
    50         assertBad(Integer.MIN_VALUE);
       
    51     }
       
    52 
       
    53     void testGetLocation() throws Exception
       
    54     {
       
    55         setCurrentTest("testGetLocation()");
       
    56         providerSetUp(null);
       
    57         assertTrue(iLP != null, "LocationProvider is null");
       
    58         simpleGetLocation();
       
    59     }
       
    60 
       
    61     void testAltitudeData() throws Exception
       
    62     {
       
    63         setCurrentTest("testAltitudeData()");
       
    64 
       
    65         Criteria criteria = new Criteria();
       
    66         criteria.setAltitudeRequired(true);
       
    67         criteria.setPreferredResponseTime(100); // 100 ms
       
    68 
       
    69         providerSetUp(criteria);
       
    70         assertTrue(iLP != null, "LocationProvider is null");
       
    71         simpleGetLocation();
       
    72 
       
    73         QualifiedCoordinates coords = iLoc.getQualifiedCoordinates();
       
    74         float altitude = coords.getAltitude();
       
    75         assertTrue(!Float.isNaN(altitude), "No altitude included");
       
    76         // echo("Altitude = " + altitude);
       
    77     }
       
    78 
       
    79     void testSpeedData() throws Exception
       
    80     {
       
    81         setCurrentTest("testSpeedData()");
       
    82 
       
    83         Criteria criteria = new Criteria();
       
    84         criteria.setPreferredResponseTime(100); // 100 ms
       
    85         criteria.setSpeedAndCourseRequired(true);
       
    86 
       
    87         providerSetUp(criteria);
       
    88         assertTrue(iLP != null, "LocationProvider is null");
       
    89         simpleGetLocation();
       
    90 
       
    91         float speed = iLoc.getSpeed();
       
    92         assertTrue(!Float.isNaN(speed), "No speed included");
       
    93         float course = iLoc.getCourse();
       
    94         assertTrue(!Float.isNaN(course), "No course included");
       
    95         // echo("Speed = " + speed + ", Course = " + course);
       
    96     }
       
    97 
       
    98     void testAddressInfo() throws Exception
       
    99     {
       
   100         setCurrentTest("testAddressInfo()");
       
   101 
       
   102         Criteria criteria = new Criteria();
       
   103         criteria.setPreferredResponseTime(100); // 100 ms
       
   104         criteria.setAddressInfoRequired(true);
       
   105 
       
   106         providerSetUp(criteria);
       
   107         if (iLP != null)
       
   108         {
       
   109             assertTrue(iLP != null, "LocationProvider is null");
       
   110             simpleGetLocation();
       
   111 
       
   112             AddressInfo addr = iLoc.getAddressInfo();
       
   113             if (addr != null)
       
   114             {
       
   115                 boolean hasNonNullField = false;
       
   116                 for (int i = 1; i <= 17; ++i)
       
   117                 {
       
   118                     String field = addr.getField(i);
       
   119                     if (field != null)
       
   120                     {
       
   121                         hasNonNullField = true;
       
   122                         // echo("Field #" + i + ": " + field);
       
   123                     }
       
   124                 }
       
   125                 assertTrue(hasNonNullField, "All AddressInfo fields are null");
       
   126             }
       
   127         }
       
   128     }
       
   129 
       
   130     void testGetLocationTimeout() throws Exception
       
   131     {
       
   132         setCurrentTest("testGetLocationTimeout()");
       
   133 
       
   134         Criteria criteria = new Criteria();
       
   135         criteria.setCostAllowed(false); // This will select the right provider
       
   136         providerSetUp(criteria);
       
   137         assertTrue(iLP != null, "LocationProvider is null");
       
   138         iLoc = null;
       
   139 
       
   140         try
       
   141         {
       
   142             int TIMEOUT = 1; // seconds
       
   143             iLoc = iLP.getLocation(TIMEOUT);
       
   144             assertTrue(false, "No timeout for getLocation");
       
   145         }
       
   146         catch (LocationException le)
       
   147         {
       
   148             // Timed out correctly
       
   149         }
       
   150 
       
   151         // Sanity check - test that normal request works ok
       
   152         simpleGetLocation();
       
   153 
       
   154         try
       
   155         {
       
   156             int TIMEOUT = 10; // seconds
       
   157             long startTime = System.currentTimeMillis();
       
   158             iLoc = iLP.getLocation(TIMEOUT);
       
   159             long duration = System.currentTimeMillis() - startTime;
       
   160             assertTrue(duration <= TIMEOUT * 1000, "GetLocation took too long");
       
   161             checkLocationData(iLoc);
       
   162         }
       
   163         catch (LocationException le)
       
   164         {
       
   165             assertTrue(false, "Illegal timeout");
       
   166         }
       
   167     }
       
   168 
       
   169     void testReset() throws Exception
       
   170     {
       
   171         setCurrentTest("testReset()");
       
   172 
       
   173         Criteria criteria = new Criteria();
       
   174         criteria.setCostAllowed(false); // This will select the right provider
       
   175         providerSetUp(criteria);
       
   176         assertTrue(iLP != null, "LocationProvider is null");
       
   177 
       
   178         LocThread t1 = new LocThread(iLP);
       
   179         LocThread t2 = new LocThread(iLP);
       
   180 
       
   181         t1.start();
       
   182         t2.start();
       
   183 
       
   184         try
       
   185         {
       
   186             // Sleep to let LocThreads get a chance to call getLocation
       
   187             Thread.sleep(1000);
       
   188             // Reset should cancel all getLocation calls
       
   189             iLP.reset();
       
   190             t1.join();
       
   191             t2.join();
       
   192         }
       
   193         finally
       
   194         {
       
   195             assertTrue(!t1.iGotPosition, "Thread1 got a position");
       
   196             assertTrue(!t2.iGotPosition, "Thread2 got a position");
       
   197             assertTrue(t1.iGotReset, "Thread1 was never reset");
       
   198             assertTrue(t2.iGotReset, "Thread2 was never reset");
       
   199         }
       
   200     }
       
   201 
       
   202     void testGetLatest()
       
   203     {
       
   204         setCurrentTest("testGetLatest()");
       
   205 
       
   206         iLP = null;
       
   207         iLoc = null;
       
   208 
       
   209         // Get the last known location
       
   210         iLoc = LocationProvider.getLastKnownLocation();
       
   211     }
       
   212 
       
   213     void testGetLocationOutOfService() throws Exception
       
   214     {
       
   215         setCurrentTest("testGetLocationOutOfService()");
       
   216 
       
   217         Criteria criteria = new Criteria();
       
   218         criteria.setCostAllowed(false); // This will select the right provider
       
   219         providerSetUp(criteria);
       
   220 
       
   221         assertTrue(iLP != null, "LocationProvider is null");
       
   222 
       
   223         simpleGetLocation();
       
   224 
       
   225         userMessage("Disable all PSYs");
       
   226 
       
   227         // now we get the last known Location:
       
   228         Location lastKnownLocation = LocationProvider.getLastKnownLocation();
       
   229         checkLocationData(lastKnownLocation);
       
   230 
       
   231         assertTrue(lastKnownLocation.getTimestamp() == iLoc.getTimestamp(),
       
   232                    "Not the last known location returned");
       
   233 
       
   234         long start = System.currentTimeMillis();
       
   235         long end = 0;
       
   236         try
       
   237         {
       
   238             iLoc = iLP.getLocation(-1);
       
   239             assertTrue(false, "No exception thrown for getLocation");
       
   240         }
       
   241         catch (LocationException le)
       
   242         {
       
   243             // Exception thrown correctly
       
   244             end = System.currentTimeMillis();
       
   245         }
       
   246         assertTrue(end - start < 500, "Expected LocationException immediately");
       
   247 
       
   248         userMessage("Test passed. Enable PSYs again");
       
   249     }
       
   250 
       
   251     // ------------------------ Helper methods -----------------------
       
   252 
       
   253     void simpleGetLocation() throws Exception
       
   254     {
       
   255         iLoc = null;
       
   256 
       
   257         try
       
   258         {
       
   259             iLoc = iLP.getLocation(-1);
       
   260             checkLocationData(iLoc);
       
   261         }
       
   262         catch (LocationException le)
       
   263         {
       
   264             assertTrue(false, "Could not get location: " + le);
       
   265         }
       
   266     }
       
   267 
       
   268     void assertBad(int aBadParam) throws Exception
       
   269     {
       
   270         try
       
   271         {
       
   272             iLoc = iLP.getLocation(aBadParam);
       
   273             assertTrue(false, "IllegalArgumentException was not thrown for: "
       
   274                        + aBadParam);
       
   275         }
       
   276         catch (IllegalArgumentException iae)
       
   277         {
       
   278             // Exception thrown correctly
       
   279         }
       
   280     }
       
   281 
       
   282     // ------------------------ Helper class -----------------------
       
   283 
       
   284     private class LocThread extends Thread
       
   285     {
       
   286         private LocationProvider iLP;
       
   287 
       
   288         boolean iGotReset = false;
       
   289 
       
   290         boolean iGotPosition = false;
       
   291 
       
   292         LocThread(LocationProvider aLP)
       
   293         {
       
   294             iLP = aLP;
       
   295         }
       
   296 
       
   297         public void run()
       
   298         {
       
   299             try
       
   300             {
       
   301                 int TIMEOUT = 10; // seconds
       
   302                 Location l = iLP.getLocation(TIMEOUT);
       
   303                 iGotPosition = true;
       
   304             }
       
   305             catch (InterruptedException ie)
       
   306             {
       
   307                 iGotReset = true;
       
   308             }
       
   309             catch (Exception e)
       
   310             {
       
   311                 echo("Unexpected exception thrown: " + e);
       
   312             }
       
   313         }
       
   314     }
       
   315 
       
   316 }