Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.hardware.cts;
     18 
     19 import android.hardware.GeomagneticField;
     20 import android.platform.test.annotations.Presubmit;
     21 import android.test.AndroidTestCase;
     22 
     23 import java.util.GregorianCalendar;
     24 
     25 public class GeomagneticFieldTest extends AndroidTestCase {
     26     private static final float DECLINATION_THRESHOLD = 0.1f;
     27     private static final float INCLINATION_THRESHOLD = 0.1f;
     28     private static final float FIELD_STRENGTH_THRESHOLD = 100;
     29 
     30     @Presubmit
     31     public void testGeomagneticField() {
     32         // Reference values calculated from NOAA online calculator for WMM 2015
     33         // https://www.ngdc.noaa.gov/geomag-web/#igrfwmm
     34         TestDataPoint testPoints[] = new TestDataPoint[] {
     35             // Mountain View, CA, USA on 2017/1/1
     36             new TestDataPoint(37.386f, -122.083f, 32, 2017, 1, 1,
     37                     13.4589f, 60.9542f, 48168.0f),
     38             // Chengdu, China on 2017/8/8
     39             new TestDataPoint(30.658f, 103.935f, 500f, 2017, 8, 8,
     40                     -1.9784f, 47.9723f, 50717.3f),
     41             // Sao Paulo, Brazil on 2018/12/25
     42             new TestDataPoint(-23.682f, -46.875f, 760f, 2018, 12, 25,
     43                     -21.3130f, -37.9940f, 22832.3f),
     44             // Boston, MA, USA on 2019/2/10
     45             new TestDataPoint(42.313f, -71.127f, 43f, 2019, 2, 10,
     46                     -14.5391f, 66.9693f, 51815.1f),
     47             // Cape Town, South Africa on 2019/5/1
     48             new TestDataPoint(-33.913f, 18.095f, 100f, 2019, 5, 1,
     49                     -25.2454f, -65.8887f, 25369.2f),
     50             // Sydney, Australia on 2020/1/1
     51             new TestDataPoint(-33.847f, 150.791f, 19f, 2020, 1, 1,
     52                     12.4469f, -64.3443f, 57087.9f)
     53         };
     54 
     55         for (TestDataPoint t : testPoints) {
     56             GeomagneticField field =
     57                     new GeomagneticField(t.latitude, t.longitude, t.altitude, t.epochTimeMillis);
     58             assertEquals(t.declinationDegree,  field.getDeclination(), DECLINATION_THRESHOLD);
     59             assertEquals(t.inclinationDegree,  field.getInclination(), INCLINATION_THRESHOLD);
     60             assertEquals(t.fieldStrengthNanoTesla, field.getFieldStrength(),
     61                     FIELD_STRENGTH_THRESHOLD);
     62 
     63             float horizontalFieldStrengthNanoTesla = (float)(
     64                     Math.cos(Math.toRadians(t.inclinationDegree)) * t.fieldStrengthNanoTesla);
     65             assertEquals(horizontalFieldStrengthNanoTesla, field.getHorizontalStrength(),
     66                     FIELD_STRENGTH_THRESHOLD);
     67 
     68             float verticalFieldStrengthNanoTesla = (float)(
     69                     Math.sin(Math.toRadians(t.inclinationDegree)) * t.fieldStrengthNanoTesla);
     70             assertEquals(verticalFieldStrengthNanoTesla, field.getZ(), FIELD_STRENGTH_THRESHOLD);
     71 
     72             float declinationDegree = (float)(
     73                     Math.toDegrees(Math.atan2(field.getY(), field.getX())));
     74             assertEquals(t.declinationDegree, declinationDegree, DECLINATION_THRESHOLD);
     75             assertEquals(horizontalFieldStrengthNanoTesla,
     76                     Math.sqrt(field.getX() * field.getX() + field.getY() * field.getY()),
     77                     FIELD_STRENGTH_THRESHOLD);
     78         }
     79     }
     80 
     81     private class TestDataPoint {
     82         public final float latitude;
     83         public final float longitude;
     84         public final float altitude;
     85         public final long epochTimeMillis;
     86         public final float declinationDegree;
     87         public final float inclinationDegree;
     88         public final float fieldStrengthNanoTesla;
     89 
     90         TestDataPoint(float lat, float lng, float alt, int year, int month, int day,
     91                 float dec, float inc, float strength) {
     92             latitude = lat;
     93             longitude = lng;
     94             altitude = alt;
     95             epochTimeMillis = new GregorianCalendar(year, month, day).getTimeInMillis();
     96             declinationDegree = dec;
     97             inclinationDegree = inc;
     98             fieldStrengthNanoTesla = strength;
     99         }
    100     }
    101 }
    102