Как написать модульный тест для моего класса интерактивного GPS? - PullRequest
1 голос
/ 22 октября 2019

Этот класс работает нормально, и теперь мне интересно, как написать модульный тест

У меня есть два независимых потока:

  • location
  • GPS-статус

Я использую опубликовать тему , чтобы сделать мой интерактив реагирующим образом. Оба потока смешиваются с функцией Observable.withLatestFrom , где я дополнительно добавляю фильтрацию и преобразование.

Правильно ли написан мой класс gps?

Может ли этот класс быть протестирован?

Мне нужно проверить initFullLocationObservable

class GpsInteractor @Inject constructor(
        private val locationManager: LocationManager,
        private val googleApiClient: GoogleApiClient,
        private val locationRequest: LocationRequest): GoogleApiClient.ConnectionCallbacks, LocationListener, GpsStatus.Listener {

        private var gpsStatus: GpsStatus? = null
        val locationSubject = PublishSubject.create<LocationModel>()
        val satellitesSubject = PublishSubject.create<List<SatelliteModel>>()
        lateinit var fullLocationObservable: Observable<FullLocation> private set

        @SuppressLint("MissingPermission")
        fun callLocationUpdates(updateInterval: Int, smallestDisplacement: Int, minAccuracy: Int, minSatellitesCount: Int, minSnr: Int) {
            locationRequest.interval = (updateInterval * 1000).toLong()
            locationRequest.fastestInterval = (updateInterval * 1000).toLong()
            locationRequest.smallestDisplacement = smallestDisplacement.toFloat()
            locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

            googleApiClient.registerConnectionCallbacks(this)
            locationManager.addGpsStatusListener(this)

            fullLocationObservable = initFullLocationObservable(minAccuracy, minSatellitesCount, minSnr)
            googleApiClient.connect()
        }

        fun removeLocationUpdates() {
            locationManager.removeGpsStatusListener(this)
            if (googleApiClient.isConnected) {
                LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this)
                googleApiClient.disconnect()
            }
        }

        @SuppressLint("MissingPermission")
        override fun onConnected(p0: Bundle?) {  LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this)
        }  

        override fun onLocationChanged(location: Location) {
            // TODO: make locationModel reusable
            locationSubject.onNext(LocationModel(latitude = location.latitude, longitude = location.longitude,
                time = location.time, speed = location.speed, accuracy = location.accuracy,
                altitude = location.altitude, bearing = location.bearing))
        }

        @SuppressLint("MissingPermission")
        override fun onGpsStatusChanged(event: Int) {
            if (event != GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                return
            }
            gpsStatus = locationManager.getGpsStatus(gpsStatus)
            if (gpsStatus != null) {
                // TODO: make satellitesModel reusable
                val satellites: List<SatelliteModel> = gpsStatus!!
                    .satellites.filter { it.usedInFix() }
                    .map { SatelliteModel(it.prn, it.elevation, it.azimuth, it.snr) }
                satellitesSubject.onNext(satellites)
            }
        }

        private fun initFullLocationObservable(minAccuracy: Int, minSatellitesCount: Int, minSnr: Int): Observable<FullLocation> {
            val locationObservable = locationSubject
                .filter { locationModel -> locationModel.accuracy <= minAccuracy }

            val satellitesObservable = satellitesSubject
                .map { satellites: List<SatelliteModel> ->
                    satellites.filter { it.snr  >= minSnr }
                }
                .filter { it.size >= minSatellitesCount }

            return locationObservable.withLatestFrom(satellitesObservable, BiFunction { locationModel: LocationModel, satellitesModel: List<SatelliteModel> ->
                val locationData = LocationData(locationModel)
                val satellites = satellitesModel.map { Satellite(it.snr, locationData) }
                FullLocation(locationData, satellites)
            })
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...