summaryrefslogtreecommitdiff
path: root/tests/location/location_gnss/src/android/location/cts/gnss/GnssStatusTest.java
blob: c23b272536d6e7400dafe3f26f2c4d50f8ceb84e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package android.location.cts.gnss;


import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;

import android.content.Context;
import android.location.GnssStatus;
import android.location.cts.common.GnssTestCase;
import android.location.cts.common.SoftAssert;
import android.location.cts.common.TestLocationListener;
import android.location.cts.common.TestLocationManager;
import android.location.cts.common.TestMeasurementUtil;
import android.location.cts.common.TestUtils;
import android.platform.test.annotations.AppModeFull;
import android.util.Log;

import java.util.List;

public class GnssStatusTest extends GnssTestCase  {

    private static final String TAG = "GnssStatusTest";
    private static final int LOCATION_TO_COLLECT_COUNT = 1;
    private static final int STATUS_TO_COLLECT_COUNT = 3;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    mTestLocationManager = new TestLocationManager(getContext());
  }

  /**
   * Tests that one can listen for {@link GnssStatus}.
   */
  @AppModeFull(reason = "Instant apps cannot access package manager to scan for permissions")
  public void testGnssStatusChanges() throws Exception {
    // Checks if GPS hardware feature is present, skips test (pass) if not
    if (!TestMeasurementUtil.canTestRunOnCurrentDevice(mTestLocationManager, TAG)) {
      return;
    }

    // Revoke location permissions from packages before running GnssStatusTest stops
    // active location requests, allowing this test to receive all necessary Gnss callbacks.
    List<String> courseLocationPackages = TestUtils.revokePermissions(ACCESS_COARSE_LOCATION);
    List<String> fineLocationPackages = TestUtils.revokePermissions(ACCESS_FINE_LOCATION);

    try {
        // Register Gps Status Listener.
        TestGnssStatusCallback testGnssStatusCallback =
            new TestGnssStatusCallback(TAG, STATUS_TO_COLLECT_COUNT);
        checkGnssChange(testGnssStatusCallback);
    } finally {
        // For each location package, re-grant the permission
        TestUtils.grantLocationPermissions(ACCESS_COARSE_LOCATION, courseLocationPackages);
        TestUtils.grantLocationPermissions(ACCESS_FINE_LOCATION, fineLocationPackages);
    }
  }

  private void checkGnssChange(TestGnssStatusCallback testGnssStatusCallback)
      throws InterruptedException {
    mTestLocationManager.registerGnssStatusCallback(testGnssStatusCallback);

    TestLocationListener locationListener = new TestLocationListener(LOCATION_TO_COLLECT_COUNT);
    mTestLocationManager.requestLocationUpdates(locationListener);

    boolean success = testGnssStatusCallback.awaitStart();
    success = success ? testGnssStatusCallback.awaitStatus() : false;
    success = success ? testGnssStatusCallback.awaitTtff() : false;
    mTestLocationManager.removeLocationUpdates(locationListener);
    success = success ? testGnssStatusCallback.awaitStop() : false;
    mTestLocationManager.unregisterGnssStatusCallback(testGnssStatusCallback);

    SoftAssert softAssert = new SoftAssert(TAG);
    softAssert.assertTrue(
        "Time elapsed without getting the right status changes."
            + " Possibly, the test has been run deep indoors."
            + " Consider retrying test outdoors.",
        success);
    softAssert.assertAll();
  }

  /**
   * Tests values of {@link GnssStatus}.
   */
  @AppModeFull(reason = "Instant apps cannot access package manager to scan for permissions")
  public void testGnssStatusValues() throws InterruptedException {
    // Checks if GPS hardware feature is present, skips test (pass) if not
    if (!TestMeasurementUtil.canTestRunOnCurrentDevice(mTestLocationManager, TAG)) {
      return;
    }

    // Revoke location permissions from packages before running GnssStatusTest stops
    // active location requests, allowing this test to receive all necessary Gnss callbacks.
    List<String> courseLocationPackages = TestUtils.revokePermissions(ACCESS_COARSE_LOCATION);
    List<String> fineLocationPackages = TestUtils.revokePermissions(ACCESS_FINE_LOCATION);

    try {
        SoftAssert softAssert = new SoftAssert(TAG);
        // Register Gps Status Listener.
        TestGnssStatusCallback testGnssStatusCallback =
            new TestGnssStatusCallback(TAG, STATUS_TO_COLLECT_COUNT);
        checkGnssChange(testGnssStatusCallback);
        validateGnssStatus(testGnssStatusCallback.getGnssStatus(), softAssert);
        softAssert.assertAll();
    } finally {
        // For each location package, re-grant the permission
        TestUtils.grantLocationPermissions(ACCESS_COARSE_LOCATION, courseLocationPackages);
        TestUtils.grantLocationPermissions(ACCESS_FINE_LOCATION, fineLocationPackages);
    }
  }

  /**
   * To validate the fields in GnssStatus class, the value is got from device
   * @param status, GnssStatus
   * @param softAssert, customized assert class.
   */
  private void validateGnssStatus(GnssStatus status, SoftAssert softAssert) {
    int sCount = status.getSatelliteCount();
    Log.i(TAG, "Total satellite:" + sCount);
    // total number of satellites for all constellation is less than 200
    softAssert.assertTrue("Satellite count test sCount : " + sCount , sCount < 200);
    for (int i = 0; i < sCount; ++i) {
      softAssert.assertTrue("azimuth_degrees: Azimuth in degrees: ",
          "0.0 <= X <= 360.0",
          String.valueOf(status.getAzimuthDegrees(i)),
          status.getAzimuthDegrees(i) >= 0.0 && status.getAzimuthDegrees(i) <= 360.0);
      TestMeasurementUtil.verifyGnssCarrierFrequency(softAssert, mTestLocationManager,
          status.hasCarrierFrequencyHz(i),
          status.hasCarrierFrequencyHz(i) ? status.getCarrierFrequencyHz(i) : 0F);

      softAssert.assertTrue("c_n0_dbhz: Carrier-to-noise density",
          "0.0 <= X <= 63",
          String.valueOf(status.getCn0DbHz(i)),
          status.getCn0DbHz(i) >= 0.0 &&
              status.getCn0DbHz(i) <= 63.0);

      Log.i(TAG, "hasBasebandCn0DbHz: " + status.hasBasebandCn0DbHz(i));
      if (status.hasBasebandCn0DbHz(i)) {
        softAssert.assertTrue("baseband_cn0_dbhz: Baseband carrier-to-noise density",
                "0.0 <= X <= 63",
                String.valueOf(status.getBasebandCn0DbHz(i)),
                status.getBasebandCn0DbHz(i) >= 0.0 &&
                        status.getBasebandCn0DbHz(i) <= 63.0);
      }

      softAssert.assertTrue("elevation_degrees: Elevation in Degrees :",
          "0.0 <= X <= 90.0",
          String.valueOf(status.getElevationDegrees(i)),
          status.getElevationDegrees(i) >= 0.0 && status.getElevationDegrees(i) <= 90.0);

      // in validateSvidSub, it will validate ConstellationType, svid
      // however, we don't have the event time in the current scope, pass in "-1" instead
      TestMeasurementUtil.validateSvidSub(softAssert, null,
          status.getConstellationType(i),status.getSvid(i));

      // For those function with boolean type return, just simply call the function
      // to make sure those function won't crash, also increase the test coverage.
      Log.i(TAG, "hasAlmanacData: " + status.hasAlmanacData(i));
      Log.i(TAG, "hasEphemerisData: " + status.hasEphemerisData(i));
      Log.i(TAG, "usedInFix: " + status.usedInFix(i));
    }
  }
}